From b93646209a2f789e56876e5adddf153a79f53aae Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 29 Jun 2021 01:37:23 +0200 Subject: [PATCH] Introduce AutocompleteItem * needs too much custom styling / behavior to be done with DropdownItem --- ts/components/DropdownItem.svelte | 17 ++--- ts/editor/AutocompleteItem.svelte | 81 ++++++++++++++++++++++ ts/editor/TagEditor.svelte | 1 + ts/editor/WithAutocomplete.svelte | 109 +++++++++++++++--------------- ts/sass/button-mixins.scss | 6 +- 5 files changed, 147 insertions(+), 67 deletions(-) create mode 100644 ts/editor/AutocompleteItem.svelte diff --git a/ts/components/DropdownItem.svelte b/ts/components/DropdownItem.svelte index ddb603353..de49d1d9c 100644 --- a/ts/components/DropdownItem.svelte +++ b/ts/components/DropdownItem.svelte @@ -50,10 +50,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html background: none; box-shadow: none !important; border: none; - } - - .btn-day { - color: black; &:active, &.active { @@ -62,19 +58,16 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } } + .btn-day { + color: black; + } + .btn-night { color: white; &:hover, - &:focus, - &.focus { + &:focus { @include button.btn-night-base; } - - &:active, - &.active { - background-color: button.$focus-color; - color: white; - } } diff --git a/ts/editor/AutocompleteItem.svelte b/ts/editor/AutocompleteItem.svelte new file mode 100644 index 000000000..edf7b02e4 --- /dev/null +++ b/ts/editor/AutocompleteItem.svelte @@ -0,0 +1,81 @@ + + + + + + diff --git a/ts/editor/TagEditor.svelte b/ts/editor/TagEditor.svelte index 4ae3d7cd2..3e8f62478 100644 --- a/ts/editor/TagEditor.svelte +++ b/ts/editor/TagEditor.svelte @@ -245,6 +245,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html break; case "Enter": + console.log("choose"); autocomplete.chooseSelected(); event.preventDefault(); break; diff --git a/ts/editor/WithAutocomplete.svelte b/ts/editor/WithAutocomplete.svelte index f1f5b626c..80180f074 100644 --- a/ts/editor/WithAutocomplete.svelte +++ b/ts/editor/WithAutocomplete.svelte @@ -9,7 +9,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import WithDropdownMenu from "components/WithDropdownMenu.svelte"; import DropdownMenu from "components/DropdownMenu.svelte"; - import DropdownItem from "components/DropdownItem.svelte"; + import AutocompleteItem from "./AutocompleteItem.svelte"; let className: string = ""; export { className as class }; @@ -26,32 +26,32 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html const dispatch = createEventDispatcher(); - function selectNext() { - suggestionsPromise.then((suggestions) => { - if (selected === null) { - selected = 0; - } else if (selected >= suggestions.length - 1) { - selected = null; - } else { - selected++; - } + async function selectNext() { + const suggestions = await suggestionsPromise; - dispatch("autocomplete", { selected: suggestions[selected ?? -1] }); - }); + if (selected === null) { + selected = 0; + } else if (selected >= suggestions.length - 1) { + selected = null; + } else { + selected++; + } + + dispatch("autocomplete", { selected: suggestions[selected ?? -1] }); } - function selectPrevious() { - suggestionsPromise.then((suggestions) => { - if (selected === null) { - selected = suggestions.length - 1; - } else if (selected === 0) { - selected = null; - } else { - selected--; - } + async function selectPrevious() { + const suggestions = await suggestionsPromise; - dispatch("autocomplete", { selected: suggestions[selected ?? -1] }); - }); + if (selected === null) { + selected = suggestions.length - 1; + } else if (selected === 0) { + selected = null; + } else { + selected--; + } + + dispatch("autocomplete", { selected: suggestions[selected ?? -1] }); } function chooseSelected() { @@ -63,18 +63,16 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html dropdown.update(); dispatch("update"); - await tick(); + const [, suggestions] = await Promise.all([tick(), suggestionsPromise]); - suggestionsPromise.then((suggestions) => { - if (suggestions.length > 0) { - dropdown.show(); - // disabled class will tell Bootstrap not to show menu on clicking - target.classList.remove("disabled"); - } else { - dropdown.hide(); - target.classList.add("disabled"); - } - }); + if (suggestions.length > 0) { + dropdown.show(); + // disabled class will tell Bootstrap not to show menu on clicking + target.classList.remove("disabled"); + } else { + dropdown.hide(); + target.classList.add("disabled"); + } } const createAutocomplete = @@ -97,6 +95,21 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html }; onDestroy(() => dropdown?.dispose()); + + function setSelected(index: number): void { + selected = index; + } + + async function chooseIndex(index: number): Promise { + const suggestions = await suggestionsPromise; + dispatch("autocomplete", { selected: suggestions[index] }); + } + + function selectIfMousedown(event: MouseEvent, index: number): void { + if (event.buttons === 1) { + setSelected(index); + } + } @@ -104,27 +117,17 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html {#await suggestionsPromise} -
- ... -
+ ... {:then suggestions} - {#each suggestions as suggestion, i} -
- {suggestion} -
+ {#each suggestions as suggestion, index} + setSelected(index)} + on:mouseenter={(event) => selectIfMousedown(event, index)} + on:mouseup={() => chooseIndex(index)}>{suggestion} {/each} {/await}
- - diff --git a/ts/sass/button-mixins.scss b/ts/sass/button-mixins.scss index bf47a36b9..c46c1e451 100644 --- a/ts/sass/button-mixins.scss +++ b/ts/sass/button-mixins.scss @@ -28,7 +28,8 @@ $btn-base-color-day: white; @content ($btn-base-color-day); @if ($with-hover) { - &:hover { + &:hover, + &.hover { background-color: darken($btn-base-color-day, 8%); } } @@ -76,7 +77,8 @@ $btn-base-color-night: #666; @content ($btn-base-color-night); @if ($with-hover) { - &:hover { + &:hover, + &.hover { background-color: lighten($btn-base-color-night, 8%); border-color: lighten($btn-base-color-night, 8%); }