diff --git a/ts/editor/Tag.svelte b/ts/editor/Tag.svelte index 1ca4b1fba..d63cfbc54 100644 --- a/ts/editor/Tag.svelte +++ b/ts/editor/Tag.svelte @@ -44,6 +44,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html on:focusout={() => (active = false)} on:tagupdate={updateTag} on:tagadd + on:tagjoinprevious + on:tagjoinnext on:mount={(event) => event.detail.input.focus()} /> {:else} diff --git a/ts/editor/TagEditor.svelte b/ts/editor/TagEditor.svelte index 0623e5c1b..7cab4028a 100644 --- a/ts/editor/TagEditor.svelte +++ b/ts/editor/TagEditor.svelte @@ -45,6 +45,26 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html tags = tags; } + function joinWithPreviousTag(index: number): void { + if (index === 0) { + return; + } + + const spliced = tags.splice(index - 1, 1)[0]; + tags[index - 1].name = spliced.name + tags[index - 1].name; + tags = tags; + } + + function joinWithNextTag(index: number): void { + if (index === tags.length - 1) { + return; + } + + const spliced = tags.splice(index + 1, 1)[0]; + tags[index].name = tags[index].name + spliced.name; + tags = tags; + } + function appendTag(): void { const names = tags.map(getName); if (!names.includes(newName) && newName.length > 0) { @@ -54,6 +74,15 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html newName = ""; } + + function joinWithLastTag(): void { + const popped = tags.pop(); + tags = tags; + + if (popped) { + newName = popped.name + newName; + } + } @@ -66,6 +95,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html on:tagupdate={() => checkForDuplicateAt(index)} on:tagadd={() => insertTagAt(index)} on:tagdelete={() => deleteTagAt(index)} + on:tagjoinprevious={() => joinWithPreviousTag(index)} + on:tagjoinnext={() => joinWithNextTag(index)} /> {/each} @@ -74,6 +105,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html bind:name={newName} on:tagupdate={appendTag} on:tagadd={appendTag} + on:tagjoinprevious={joinWithLastTag} /> diff --git a/ts/editor/TagInput.svelte b/ts/editor/TagInput.svelte index c9e449489..3df9ab513 100644 --- a/ts/editor/TagInput.svelte +++ b/ts/editor/TagInput.svelte @@ -27,13 +27,37 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html /* dropdown.hide(); */ } + function onBackspace(event: KeyboardEvent) { + if (input.selectionStart === 0 && input.selectionEnd === 0) { + dispatch("tagjoinprevious"); + event.preventDefault(); + } else if (name.endsWith("::")) { + name = name.slice(0, -2); + event.preventDefault(); + } + } + + function onDelete(event: KeyboardEvent) { + if ( + input.selectionStart === input.value.length && + input.selectionEnd === input.value.length + ) { + dispatch("tagjoinnext"); + event.preventDefault(); + } else if (name.endsWith("::")) { + name = name.slice(0, -2); + event.preventDefault(); + } + } + function onKeydown(event: KeyboardEvent): void { if (event.code === "Space") { name += "::"; event.preventDefault(); - } else if (event.code === "Backspace" && name.endsWith("::")) { - name = name.slice(0, -2); - event.preventDefault(); + } else if (event.code === "Backspace") { + onBackspace(event); + } else if (event.code === "Delete") { + onDelete(event); } else if (event.code === "Enter") { onAccept(); event.preventDefault();