From 473517c3b30fe4ed44b5036383085511c91a4883 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Fri, 25 Jun 2021 03:03:31 +0200 Subject: [PATCH] Enable keyed blocks in TagEditor for tags --- ts/editor/TagEditor.svelte | 30 +++++++++++++++++------------- ts/editor/tags.ts | 13 +++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/ts/editor/TagEditor.svelte b/ts/editor/TagEditor.svelte index f3d509078..26c84ca48 100644 --- a/ts/editor/TagEditor.svelte +++ b/ts/editor/TagEditor.svelte @@ -7,25 +7,27 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import AddTagBadge from "./AddTagBadge.svelte"; import Tag from "./Tag.svelte"; import TagInput from "./TagInput.svelte"; + import { attachId, getName } from "./tags"; - export let tags = ["en::foobar", "test", "def"]; + export let initialNames = ["en::foobar", "test", "def"]; - let tagInputNew: HTMLInputElement; + let tags = initialNames.map(attachId); + let newInput: HTMLInputElement; let newName: string = ""; function focusInputNew(): void { - tagInputNew.focus(); + newInput.focus(); } const insertTagAt = (index: number) => () => { - const copy = tags.slice(0); - copy.splice(index, 1); + const names = tags.map(getName); + const nameToInsert = names.splice(index, 1)[0]; - if (copy.includes(tags[index])) { + if (names.includes(nameToInsert)) { return; } - tags.splice(index - 1, 0, tags[index]); + tags.splice(index, 0, attachId(nameToInsert)); tags = tags; }; @@ -35,11 +37,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html }; function appendTag(): void { - if (!tags.includes(newName) && newName.length > 0) { - tags.push(newName); + const names = tags.map(getName); + if (!names.includes(newName) && newName.length > 0) { + tags.push(attachId(newName)); + tags = tags; } + newName = ""; - tags = tags; } @@ -47,16 +51,16 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
- {#each tags as tag, index} + {#each tags as tag, index (tag.id)} {/each}