diff --git a/ts/editor/Tag.svelte b/ts/editor/Tag.svelte index fefcec644..010d18b11 100644 --- a/ts/editor/Tag.svelte +++ b/ts/editor/Tag.svelte @@ -9,13 +9,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import { deleteIcon } from "./icons"; export let name: string; + export let input: HTMLInputElement; export let active: boolean; export let blink: boolean = false; const dispatch = createEventDispatcher(); - let input: HTMLInputElement; - $: if (blink) { setTimeout(() => (blink = false), 300); } diff --git a/ts/editor/TagEditor.svelte b/ts/editor/TagEditor.svelte index 99a8631c3..a5558d007 100644 --- a/ts/editor/TagEditor.svelte +++ b/ts/editor/TagEditor.svelte @@ -69,18 +69,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } } - function insertTagAt(index: number): boolean { - const names = tags.map(getName); - const newName = names.splice(index, 1)[0]; - let added = false; + function insertTagAt(index: number): void { + const name = tags.map(getName).splice(index, 1)[0]; - if (!checkIfContainsNameAt(index) && newName.length > 0) { - tags.splice(index, 0, attachId(newName)); - added = true; + if (!checkIfContainsNameAt(index)) { + tags.splice(index, 0, attachId(name)); + tags = tags; } - - tags = tags; - return added; } function deleteTagAt(index: number): void { @@ -89,7 +84,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } function joinWithPreviousTag(index: number): void { - if (index === 0) { + if (isFirst(index)) { return; } @@ -99,7 +94,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } function joinWithNextTag(index: number): void { - if (index === tags.length - 1) { + if (isLast(index)) { return; } @@ -109,12 +104,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } function moveToPreviousTag(index: number): void { - if (index === 0 || tags.length === 1) { + if (isFirst(index)) { return; } - tags[index - 1].active = true; - tags[index].active = false; + const previousTag = tags[index - 1]; + previousTag.active = true; tags = tags; } @@ -124,11 +119,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html return; } - tags[index + 1].active = true; + const nextTag = tags[index + 1]; + nextTag.active = true; tags = tags; await tick(); - (document.activeElement as HTMLInputElement).setSelectionRange(0, 0); + nextTag.input?.setSelectionRange(0, 0); } @@ -146,6 +142,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html {#each tags as tag, index (tag.id)} addTagAt(index)} diff --git a/ts/editor/tags.ts b/ts/editor/tags.ts index 460471555..d38b30721 100644 --- a/ts/editor/tags.ts +++ b/ts/editor/tags.ts @@ -16,18 +16,19 @@ export function normalizeTagname(tagname: string): string { } interface Tag { + id: string; name: string; + input?: HTMLInputElement; active: boolean; blink: boolean; - id: string; } export function attachId(name: string, active = false): Tag { return { + id: Math.random().toString(36).substring(2), name, active, blink: false, - id: Math.random().toString(36).substring(2), }; }