mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
suggestions -> suggestionsPromise, so it works with external APIs
This commit is contained in:
parent
a515a9899b
commit
b2d2cb8715
2 changed files with 56 additions and 34 deletions
|
@ -39,9 +39,15 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
activeName = selected ?? activeTag.name;
|
activeName = selected ?? activeTag.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
let suggestions: string[] = [];
|
let suggestionsPromise: Promise<string[]> = Promise.resolve([]);
|
||||||
|
|
||||||
function updateSuggestions(): void {}
|
function updateSuggestions(): void {
|
||||||
|
suggestionsPromise = Promise.resolve([
|
||||||
|
"en::vocabulary",
|
||||||
|
"en::idioms",
|
||||||
|
Math.random().toString(36).substring(2),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
function updateWithTagName(tag: TagType): void {
|
function updateWithTagName(tag: TagType): void {
|
||||||
tag.name = activeName;
|
tag.name = activeName;
|
||||||
|
@ -244,6 +250,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
if (event.code.startsWith("Arrow")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
autocomplete.update();
|
autocomplete.update();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -259,7 +269,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
{#if index === active}
|
{#if index === active}
|
||||||
<WithAutocomplete
|
<WithAutocomplete
|
||||||
class="d-flex flex-column-reverse"
|
class="d-flex flex-column-reverse"
|
||||||
{suggestions}
|
{suggestionsPromise}
|
||||||
on:autocomplete={onAutocomplete}
|
on:autocomplete={onAutocomplete}
|
||||||
on:update={updateSuggestions}
|
on:update={updateSuggestions}
|
||||||
let:createAutocomplete
|
let:createAutocomplete
|
||||||
|
|
|
@ -14,7 +14,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
let className: string = "";
|
let className: string = "";
|
||||||
export { className as class };
|
export { className as class };
|
||||||
|
|
||||||
export let suggestions: string[];
|
export let suggestionsPromise: Promise<string[]>;
|
||||||
|
|
||||||
let target: HTMLElement;
|
let target: HTMLElement;
|
||||||
let dropdown: Dropdown;
|
let dropdown: Dropdown;
|
||||||
|
@ -27,6 +27,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
function selectNext() {
|
function selectNext() {
|
||||||
|
suggestionsPromise.then((suggestions) => {
|
||||||
if (selected === null) {
|
if (selected === null) {
|
||||||
selected = 0;
|
selected = 0;
|
||||||
} else if (selected >= suggestions.length - 1) {
|
} else if (selected >= suggestions.length - 1) {
|
||||||
|
@ -36,9 +37,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch("autocomplete", { selected: suggestions[selected ?? -1] });
|
dispatch("autocomplete", { selected: suggestions[selected ?? -1] });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectPrevious() {
|
function selectPrevious() {
|
||||||
|
suggestionsPromise.then((suggestions) => {
|
||||||
if (selected === null) {
|
if (selected === null) {
|
||||||
selected = suggestions.length - 1;
|
selected = suggestions.length - 1;
|
||||||
} else if (selected === 0) {
|
} else if (selected === 0) {
|
||||||
|
@ -48,6 +51,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch("autocomplete", { selected: suggestions[selected ?? -1] });
|
dispatch("autocomplete", { selected: suggestions[selected ?? -1] });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function chooseSelected() {
|
function chooseSelected() {
|
||||||
|
@ -61,6 +65,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
await tick();
|
await tick();
|
||||||
|
|
||||||
|
suggestionsPromise.then((suggestions) => {
|
||||||
if (suggestions.length > 0) {
|
if (suggestions.length > 0) {
|
||||||
dropdown.show();
|
dropdown.show();
|
||||||
// disabled class will tell Bootstrap not to show menu on clicking
|
// disabled class will tell Bootstrap not to show menu on clicking
|
||||||
|
@ -69,6 +74,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
dropdown.hide();
|
dropdown.hide();
|
||||||
target.classList.add("disabled");
|
target.classList.add("disabled");
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const createAutocomplete =
|
const createAutocomplete =
|
||||||
|
@ -97,6 +103,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
<slot createAutocomplete={createAutocomplete(createDropdown)} {autocomplete} />
|
<slot createAutocomplete={createAutocomplete(createDropdown)} {autocomplete} />
|
||||||
|
|
||||||
<DropdownMenu id={menuId} class={className}>
|
<DropdownMenu id={menuId} class={className}>
|
||||||
|
{#await suggestionsPromise}
|
||||||
|
<div class="suggestion-item">
|
||||||
|
<DropdownItem>...</DropdownItem>
|
||||||
|
</div>
|
||||||
|
{:then suggestions}
|
||||||
{#each suggestions as suggestion, i}
|
{#each suggestions as suggestion, i}
|
||||||
<div class="suggestion-item">
|
<div class="suggestion-item">
|
||||||
<DropdownItem
|
<DropdownItem
|
||||||
|
@ -105,6 +116,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
|
{/await}
|
||||||
</DropdownMenu>
|
</DropdownMenu>
|
||||||
</WithDropdownMenu>
|
</WithDropdownMenu>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue