Start implemention tagmove

This commit is contained in:
Henrik Giesel 2021-06-26 00:51:41 +02:00
parent 4420a24363
commit 180ef140f8
3 changed files with 45 additions and 5 deletions

View file

@ -49,6 +49,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:tagadd on:tagadd
on:tagjoinprevious on:tagjoinprevious
on:tagjoinnext on:tagjoinnext
on:tagmoveprevious
on:tagmovenext
on:mount={(event) => event.detail.input.focus()} on:mount={(event) => event.detail.input.focus()}
/> />
{:else} {:else}

View file

@ -88,6 +88,23 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
setPosition(length); setPosition(length);
} }
function moveToPreviousTag(index: number): void {
if (index === 0 || tags.length === 1) {
return;
}
const before = tags.splice(index - 1, 1)[0];
tags.splice(index, 0, before);
tags = tags;
}
function moveToNextTag(index: number): void {
if (index === tags.length - 1 || tags.length === 1) {
return;
}
// TODO
}
function appendTag(): void { function appendTag(): void {
const names = tags.map(getName); const names = tags.map(getName);
if (!names.includes(newName) && newName.length > 0) { if (!names.includes(newName) && newName.length > 0) {
@ -107,6 +124,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
setPosition(popped.name.length); setPosition(popped.name.length);
} }
} }
function moveToLastTag(): void {
appendTag();
}
</script> </script>
<StickyBottom> <StickyBottom>
@ -132,6 +153,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
joinWithPreviousTag(index, detail.setPosition)} joinWithPreviousTag(index, detail.setPosition)}
on:tagjoinnext={({ detail }) => on:tagjoinnext={({ detail }) =>
joinWithNextTag(index, detail.setPosition)} joinWithNextTag(index, detail.setPosition)}
on:tagmoveprevious={() => moveToPreviousTag(index)}
on:tagmovenext={() => moveToNextTag(index)}
/> />
{/each} {/each}
@ -144,6 +167,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:tagadd={appendTag} on:tagadd={appendTag}
on:tagjoinprevious={({ detail }) => on:tagjoinprevious={({ detail }) =>
joinWithLastTag(detail.setPosition)} joinWithLastTag(detail.setPosition)}
on:tagmoveprevious={moveToLastTag}
/> />
</TagAutocomplete> </TagAutocomplete>
</ButtonToolbar> </ButtonToolbar>

View file

@ -11,6 +11,17 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
function caretAtStart(): boolean {
return input.selectionStart === 0 && input.selectionEnd === 0;
}
function caretAtEnd(): boolean {
return (
input.selectionStart === input.value.length &&
input.selectionEnd === input.value.length
);
}
function setPosition(position: number): void { function setPosition(position: number): void {
setTimeout(() => input.setSelectionRange(position, position)); setTimeout(() => input.setSelectionRange(position, position));
} }
@ -21,7 +32,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
function onBackspace(event: KeyboardEvent) { function onBackspace(event: KeyboardEvent) {
if (input.selectionStart === 0 && input.selectionEnd === 0) { if (caretAtStart()) {
dispatch("tagjoinprevious", { setPosition }); dispatch("tagjoinprevious", { setPosition });
event.preventDefault(); event.preventDefault();
} else if (name.endsWith("::")) { } else if (name.endsWith("::")) {
@ -31,10 +42,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
function onDelete(event: KeyboardEvent) { function onDelete(event: KeyboardEvent) {
if ( if (caretAtEnd()) {
input.selectionStart === input.value.length &&
input.selectionEnd === input.value.length
) {
dispatch("tagjoinnext", { setPosition }); dispatch("tagjoinnext", { setPosition });
event.preventDefault(); event.preventDefault();
} else if (name.endsWith("::")) { } else if (name.endsWith("::")) {
@ -47,6 +55,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
if (event.code === "Space") { if (event.code === "Space") {
name += "::"; name += "::";
event.preventDefault(); event.preventDefault();
} else if (event.code === "ArrowLeft" && caretAtStart()) {
dispatch("tagmoveprevious");
event.preventDefault();
} else if (event.code === "ArrowRight" && caretAtEnd()) {
dispatch("tagmovenext");
event.preventDefault();
} else if (event.code === "Backspace") { } else if (event.code === "Backspace") {
onBackspace(event); onBackspace(event);
} else if (event.code === "Delete") { } else if (event.code === "Delete") {