mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
Implement on:autocomplete
This commit is contained in:
parent
146a901d91
commit
b4e84e8043
3 changed files with 35 additions and 7 deletions
|
@ -3,6 +3,8 @@ Copyright: Ankitects Pty Ltd and contributors
|
||||||
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
-->
|
-->
|
||||||
<script lang="typescript">
|
<script lang="typescript">
|
||||||
|
import { createEventDispatcher } from "svelte";
|
||||||
|
|
||||||
import type Dropdown from "bootstrap/js/dist/dropdown";
|
import type Dropdown from "bootstrap/js/dist/dropdown";
|
||||||
|
|
||||||
import WithDropdownMenu from "components/WithDropdownMenu.svelte";
|
import WithDropdownMenu from "components/WithDropdownMenu.svelte";
|
||||||
|
@ -20,12 +22,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
let displayed: string[] = [];
|
let displayed: string[] = [];
|
||||||
let selected: number | null = null;
|
let selected: number | null = null;
|
||||||
|
|
||||||
export let choice: string | undefined;
|
|
||||||
$: choice = displayed[selected ?? -1];
|
|
||||||
|
|
||||||
// blue highlight
|
// blue highlight
|
||||||
let active: boolean = false;
|
let active: boolean = false;
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
function select(index: number) {
|
function select(index: number) {
|
||||||
selected = index;
|
selected = index;
|
||||||
}
|
}
|
||||||
|
@ -51,6 +52,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
} else {
|
} else {
|
||||||
selected--;
|
selected--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const choice = displayed[selected ?? -1];
|
||||||
|
dispatch("autocomplete", { choice });
|
||||||
} else if (event.code === "ArrowUp" || event.code === "Tab") {
|
} else if (event.code === "ArrowUp" || event.code === "Tab") {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (selected === null) {
|
if (selected === null) {
|
||||||
|
@ -60,6 +64,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
} else {
|
} else {
|
||||||
selected++;
|
selected++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const choice = displayed[selected ?? -1];
|
||||||
|
dispatch("autocomplete", { choice });
|
||||||
} else if (event.code === "Enter") {
|
} else if (event.code === "Enter") {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
active = true;
|
active = true;
|
||||||
|
|
|
@ -34,7 +34,24 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
let activeAfterBlur: number | null = null;
|
let activeAfterBlur: number | null = null;
|
||||||
let activeName = "";
|
let activeName = "";
|
||||||
|
|
||||||
let autocompletionChoice: string | undefined;
|
function onAutocomplete({ detail }) {
|
||||||
|
const activeTag = tags[active!];
|
||||||
|
const autocompletionChoice = detail.choice;
|
||||||
|
|
||||||
|
console.log(autocompletionChoice, activeTag);
|
||||||
|
if (autocompletionChoice) {
|
||||||
|
activeName = autocompletionChoice;
|
||||||
|
} else {
|
||||||
|
activeName = activeTag.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateTagName() {
|
||||||
|
console.log("updatetagname");
|
||||||
|
const activeTag = tags[active!];
|
||||||
|
activeTag.name = activeName;
|
||||||
|
tags = tags;
|
||||||
|
}
|
||||||
|
|
||||||
function setActiveAfterBlur(value: number): void {
|
function setActiveAfterBlur(value: number): void {
|
||||||
if (activeAfterBlur === null) {
|
if (activeAfterBlur === null) {
|
||||||
|
@ -206,7 +223,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
class="d-flex flex-column-reverse"
|
class="d-flex flex-column-reverse"
|
||||||
{suggestions}
|
{suggestions}
|
||||||
search={tags[active ?? -1]?.name ?? ""}
|
search={tags[active ?? -1]?.name ?? ""}
|
||||||
bind:choice={autocompletionChoice}
|
on:autocomplete={onAutocomplete}
|
||||||
let:updateAutocomplete
|
let:updateAutocomplete
|
||||||
let:destroyAutocomplete
|
let:destroyAutocomplete
|
||||||
>
|
>
|
||||||
|
@ -218,6 +235,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
bind:input
|
bind:input
|
||||||
on:focus={() => (activeName = tag.name)}
|
on:focus={() => (activeName = tag.name)}
|
||||||
on:keydown={updateAutocomplete}
|
on:keydown={updateAutocomplete}
|
||||||
|
on:input={updateTagName}
|
||||||
on:tagsplit={({ detail }) =>
|
on:tagsplit={({ detail }) =>
|
||||||
splitTag(index, detail.start, detail.end)}
|
splitTag(index, detail.start, detail.end)}
|
||||||
on:tagadd={() => insertTag(index)}
|
on:tagadd={() => insertTag(index)}
|
||||||
|
@ -251,8 +269,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
</TagAutocomplete>
|
</TagAutocomplete>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
a, aab, an: {active}
|
a, aab, an, ac: {active}
|
||||||
{activeAfterBlur} "{activeName}";<br />{JSON.stringify(tags)}
|
{activeAfterBlur} "{activeName}";
|
||||||
|
<br />
|
||||||
|
{JSON.stringify(tags)}
|
||||||
</div>
|
</div>
|
||||||
</ButtonToolbar>
|
</ButtonToolbar>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -181,6 +181,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
on:blur={onBlur}
|
on:blur={onBlur}
|
||||||
on:keydown={onKeydown}
|
on:keydown={onKeydown}
|
||||||
on:keydown
|
on:keydown
|
||||||
|
on:input
|
||||||
on:paste={onPaste}
|
on:paste={onPaste}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
|
Loading…
Reference in a new issue