diff --git a/src/lib/components/utils/TagList.svelte b/src/lib/components/utils/TagList.svelte index 07de1cba9..fca3f6bd2 100644 --- a/src/lib/components/utils/TagList.svelte +++ b/src/lib/components/utils/TagList.svelte @@ -14,96 +14,44 @@ export let tags: Tag[] = []; export let editable = false; - let inputA: InputComponentDev; + let inputElement: InputComponentDev; const shake = false; - let allTags: Tag[] = []; - let filteredTagsMatched: Tag[] = []; - let filteredTagsUnmatched: Tag[] = []; + let tagSearch = ''; - let newTagText = ''; + $: allTags = $getAllTags.data?.getTags ?? []; + $: availableTags = allTags.filter((tag) => !tags.some((t) => t.id == tag.id)); + $: filteredTagsMatched = availableTags.filter((tag) => tag.name.toLowerCase().includes(tagSearch)); + $: filteredTagsUnmatched = availableTags.filter((tag) => !filteredTagsMatched.some((t) => t.id == tag.id)); - let newTag: HTMLInputElement; - let newTagContainer: HTMLElement = null; let surface: MenuSurfaceComponentDev; - let focused = false; - - $: newTag = inputA?.getElement(); - - function filterAvailableTags(tagList: Tag[], currentTags: Tag[], filterText: string): [Tag[], Tag[]] { - if (!tagList || !currentTags || !filterText) { - return [tagList, []]; - } - console.log(filterText); - filterText = filterText.toLowerCase(); - let unfiltered = tagList.filter((tag) => !currentTags.find((t) => t.id == tag.id)); - const filtered = unfiltered.filter((tag) => !newTag || tag.name.toLowerCase().includes(filterText)); - unfiltered = unfiltered.filter((tag) => filtered.findIndex((t) => t.id === tag.id) === -1); - return [filtered, unfiltered]; - } + let tagBox: HTMLDivElement; - function updateTags() { - tags = tags; - [filteredTagsMatched, filteredTagsUnmatched] = filterAvailableTags(allTags, tags, newTagText); - } + let focused = false; if (editable) { query(getAllTags); - getAllTags.subscribe(() => { - if (!getAllTags.fetching && !getAllTags.error) { - allTags = getAllTags.data.getTags; - updateTags(); - } - }); } - function addTag(newTagObj: string | Tag) { - if (!allTags) { - return false; + function addTag(newTag: Tag) { + if (!tags.find((tag) => tag.id == newTag.id)) { + tags = [...tags, newTag]; } - const tagToAdd = allTags.find((tag) => { - if (typeof newTagObj == 'string') { - return newTagObj == tag.name || newTagObj == tag.id; - } else { - return newTagObj.id == tag.id; - } - }) as Tag; - if (tagToAdd && !tags.find((tag) => tag.id == tagToAdd.id)) { - tags.push(tagToAdd); - updateTags(); - return true; - } - updateTags(); - return false; } function deleteTag(tag: Tag) { tags = tags.filter((v) => v != tag); - updateTags(); } function onFocusLost() { setTimeout(() => { - if (newTagContainer && !newTagContainer.contains(document.activeElement)) { + if (!tagBox.contains(document.activeElement)) { surface.setOpen(false); } }, 200); } - - function onInput(e) { - //on:input is thrown before the inner text of the input is updated. - // The following if-else ensure the text we search with is up-to-date - if (e.data) { - newTagText += e.data; - } else { - newTagText = newTagText.substring(0, newTagText.length - 1); - } - updateTags(); - e.preventDefault(); - } - updateTags();
(focused = true)} on:focusout={() => (focused = false)}> @@ -118,66 +66,62 @@
{/if} {:else} - - 0) || focused || tags.length > 0}> - Tags - -
- {#each tags as tag} -
- # - - deleteTag(tag)} - >cancel - -

{tag.name}

-
- {/each} -
-
surface.setOpen(true)} - on:focusout={onFocusLost}> - -
-
- -
-

Available Tags

- {#if filteredTagsMatched.length > 0} -
- tag.name}> - addTag(chip.name)}> - {chip.name} - - -
- {/if} - {#if filteredTagsUnmatched.length > 0} -
- tag.name}> - addTag(chip.name)}> - {chip.name} - - -
- {/if} -
-
+
surface.setOpen(true)} on:focusout={onFocusLost}> + + 0}> + Tags + +
+ {#each tags as tag} +
+ # + + deleteTag(tag)} + >cancel + +

{tag.name}

+
+ {/each} +
+
+ +
+
+ inputElement.focus()}> +
+

Available Tags

+ {#if filteredTagsMatched.length > 0} +
+ tag.name}> + addTag(chip)}> + {chip.name} + + +
+ {/if} + {#if filteredTagsUnmatched.length > 0} +
+ tag.name}> + addTag(chip)}> + {chip.name} + + +
+ {/if} +
+
+
{/if}