Decouple Autocomplete from TagInput:

Allows to only have one autocompletion for all tags, rather than every
tag having its own
This commit is contained in:
Henrik Giesel 2021-06-25 15:44:44 +02:00
parent c5b10c1117
commit 1ba6909495
4 changed files with 72 additions and 73 deletions

View file

@ -16,23 +16,30 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
</script> </script>
<WithShortcut shortcut="Control+Shift+T" let:createShortcut let:shortcutLabel> <WithShortcut shortcut="Control+Shift+T" let:createShortcut let:shortcutLabel>
<div class="add-icon">
<Badge <Badge
class="add-icon d-flex me-1" class="me-1"
tooltip={appendInParentheses(tooltip, shortcutLabel)} tooltip={appendInParentheses(tooltip, shortcutLabel)}
on:click on:click
on:mouseenter={() => (theTagIcon = addTagIcon)} on:mouseenter={() => (theTagIcon = addTagIcon)}
on:mouseleave={() => (theTagIcon = tagIcon)} on:mouseleave={() => (theTagIcon = tagIcon)}
on:mount={withSpan(createShortcut)}>{@html theTagIcon}</Badge on:mount={withSpan(createShortcut)}>{@html theTagIcon}</Badge
> >
</div>
</WithShortcut> </WithShortcut>
<style lang="scss"> <style lang="scss">
:global(.add-icon > svg) { .add-icon {
line-height: 1;
:global(svg) {
cursor: pointer; cursor: pointer;
fill: currentColor;
opacity: 0.6; opacity: 0.6;
} }
:global(.add-icon > svg:hover) { :global(svg:hover) {
opacity: 1; opacity: 1;
} }
}
</style> </style>

View file

@ -8,8 +8,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export const suggestions = ["en::idioms", "anki::functionality", "math"]; export const suggestions = ["en::idioms", "anki::functionality", "math"];
const triggerId = "tagLabel" + String(Math.random()).slice(2); let className: string = "";
const triggerClass = "dropdown-toggle"; export { className as class };
let menu: HTMLDivElement; let menu: HTMLDivElement;
@ -33,10 +33,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
function updateActiveItem(even: FocusEvent): void {} function updateActiveItem(even: FocusEvent): void {}
</script> </script>
<div bind:this={menu} class="dropup dropdown-reverse"> <div bind:this={menu} class={`dropup dropdown-reverse ${className}`}>
<slot {triggerId} {triggerClass} /> <slot />
<DropdownMenu labelledby={triggerId}> <DropdownMenu>
{#each suggestions as tag} {#each suggestions as tag}
<DropdownItem on:focus={updateActiveItem} on:keydown={switchUpDown} <DropdownItem on:focus={updateActiveItem} on:keydown={switchUpDown}
>{tag}</DropdownItem >{tag}</DropdownItem

View file

@ -6,6 +6,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import StickyBottom from "components/StickyBottom.svelte"; import StickyBottom from "components/StickyBottom.svelte";
import AddTagBadge from "./AddTagBadge.svelte"; import AddTagBadge from "./AddTagBadge.svelte";
import Tag from "./Tag.svelte"; import Tag from "./Tag.svelte";
import TagAutocomplete from "./TagAutocomplete.svelte";
import TagInput from "./TagInput.svelte"; import TagInput from "./TagInput.svelte";
import { attachId, getName } from "./tags"; import { attachId, getName } from "./tags";
@ -86,7 +87,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
</script> </script>
<StickyBottom> <StickyBottom>
<div class="d-flex flex-wrap"> <div class="font-size-13 row-gap">
<TagAutocomplete class="d-flex flex-wrap align-items-center row-gap">
<AddTagBadge on:click={focusNewInput} /> <AddTagBadge on:click={focusNewInput} />
{#each tags as tag, index (tag.id)} {#each tags as tag, index (tag.id)}
@ -107,12 +109,16 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:tagadd={appendTag} on:tagadd={appendTag}
on:tagjoinprevious={joinWithLastTag} on:tagjoinprevious={joinWithLastTag}
/> />
</TagAutocomplete>
</div> </div>
</StickyBottom> </StickyBottom>
<style lang="scss"> <style lang="scss">
div { .font-size-13 {
font-size: 13px; font-size: 13px;
fill: currentColor; }
.row-gap > :global(.d-flex > *) {
margin-bottom: 2px;
} }
</style> </style>

View file

@ -96,28 +96,15 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
} }
function setTagname({ detail }: CustomEvent): void {
name = detail.name;
}
onMount(() => dispatch("mount", { input })); onMount(() => dispatch("mount", { input }));
</script> </script>
<TagAutocomplete <label
bind:name class="ps-2 pe-1"
let:triggerId
let:triggerClass
on:nameChosen={setTagname}
on:accept={onAccept}
>
<label
id={triggerId}
class={`ps-2 pe-1 ${triggerClass}`}
data-value={name} data-value={name}
data-bs-toggle="dropdown" data-bs-toggle="dropdown"
aria-expanded="false" aria-expanded="false"
data-bs-reference="parent" >
>
<input <input
bind:this={input} bind:this={input}
type="text" type="text"
@ -131,8 +118,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
on:paste={onPaste} on:paste={onPaste}
on:click on:click
/> />
</label> </label>
</TagAutocomplete>
<style lang="scss"> <style lang="scss">
label { label {