diff --git a/src/lib/action-autocomplete.ts b/src/lib/action-autocomplete.ts new file mode 100644 index 00000000..fbfb64e9 --- /dev/null +++ b/src/lib/action-autocomplete.ts @@ -0,0 +1,59 @@ +import type {Action} from "svelte/action" +import Index from "flexsearch" +import {KEYMAP_CODES} from "$lib/serial/keymap-codes" +import tippy from "tippy.js" +import ActionAutocomplete from "$lib/components/ActionAutocomplete.svelte" +import {browser} from "$app/environment" + +const index = browser ? new Index({tokenize: "full"}) : undefined +for (const action of Object.values(KEYMAP_CODES)) { + index?.add( + action.code, + `${action.title || ""} ${action.variant || ""} ${action.category} ${action.id || ""} ${ + action.description || "" + }`, + ) +} +const exact = Object.fromEntries( + Object.values(KEYMAP_CODES) + .filter(it => !!it.id) + .map(it => [it.id, it] as const), +) + +export const actionAutocomplete: Action = node => { + if (!browser) return + + let completionComponent: ActionAutocomplete + const completionDialog = tippy(node, { + interactive: true, + placement: "bottom-start", + hideOnClick: false, + theme: "surface-variant search-completion", + arrow: false, + trigger: "focus", + offset: [0, 0], + onCreate(instance) { + const target = instance.popper.querySelector(".tippy-content")! + completionComponent = new ActionAutocomplete({target, props: {width: node.clientWidth}}) + }, + onDestroy() { + completionComponent.$destroy() + }, + }) + + function input(event: Event) { + completionComponent.$set({ + results: index!.search(node.value), + exact: exact[node.value], + code: Number(node.value), + }) + } + + node.addEventListener("input", input) + + return { + destroy() { + node.removeEventListener("input", input) + }, + } +} diff --git a/src/lib/assets/keymaps/action-codes.yml b/src/lib/assets/keymaps/action-codes.yml deleted file mode 100644 index 265ff1ea..00000000 --- a/src/lib/assets/keymaps/action-codes.yml +++ /dev/null @@ -1,67 +0,0 @@ -name: Action Codes -description: Invalid action codes -actions: - 0x00: - id: "0x00" - 0x01: - id: "0x01" - 0x02: - id: "0x02" - 0x03: - id: "0x03" - 0x04: - id: "0x04" - 0x05: - id: "0x05" - 0x06: - id: "0x06" - 0x07: - id: "0x07" - 0x08: - id: "0x08" - 0x09: - id: "0x09" - 0x0A: - id: "0x0A" - 0x0B: - id: "0x0B" - 0x0C: - id: "0x0C" - 0x0D: - id: "0x0D" - 0x0E: - id: "0x0E" - 0x0F: - id: "0x0F" - 0x10: - id: "0x10" - 0x11: - id: "0x11" - 0x12: - id: "0x12" - 0x13: - id: "0x13" - 0x14: - id: "0x14" - 0x15: - id: "0x15" - 0x16: - id: "0x16" - 0x17: - id: "0x17" - 0x18: - id: "0x18" - 0x19: - id: "0x19" - 0x1A: - id: "0x1A" - 0x1B: - id: "0x1B" - 0x1C: - id: "0x1C" - 0x1D: - id: "0x1D" - 0x1E: - id: "0x1E" - 0x1F: - id: "0x1F" diff --git a/src/lib/components/ActionAutocomplete.svelte b/src/lib/components/ActionAutocomplete.svelte new file mode 100644 index 00000000..cab8cc4c --- /dev/null +++ b/src/lib/components/ActionAutocomplete.svelte @@ -0,0 +1,66 @@ + + +
+ {#if exact !== undefined} +
+ Exact match + +
+ {/if} + {#if !exact && code} + {#if code >= 2 ** 5 && code < 2 ** 13} + + {:else} +
Action code is out of range
+ {/if} + {/if} + {#each results as id (id)} + + {/each} +
+ + diff --git a/src/lib/components/ActionListItem.svelte b/src/lib/components/ActionListItem.svelte new file mode 100644 index 00000000..096d07f0 --- /dev/null +++ b/src/lib/components/ActionListItem.svelte @@ -0,0 +1,71 @@ + + + + + diff --git a/src/lib/components/layout/InputEdit.svelte b/src/lib/components/layout/InputEdit.svelte index 5563ac3b..a204e05e 100644 --- a/src/lib/components/layout/InputEdit.svelte +++ b/src/lib/components/layout/InputEdit.svelte @@ -3,52 +3,16 @@ import {KEYMAP_CODES} from "$lib/serial/keymap-codes" import ActionSelector from "$lib/components/layout/ActionSelector.svelte" import {popup} from "$lib/popup" + import ActionListItem from "$lib/components/ActionListItem.svelte" export let id: number = 0 {#each $layout as layer, i} - {@const action = KEYMAP_CODES[layer[id]]} - + {/each}
counter_{i + 1}
- - diff --git a/src/lib/components/layout/Layout.svelte b/src/lib/components/layout/Layout.svelte index 74e39297..145aa573 100644 --- a/src/lib/components/layout/Layout.svelte +++ b/src/lib/components/layout/Layout.svelte @@ -1,9 +1,15 @@ -
@@ -13,7 +19,7 @@
- {#each [["Numeric Layer", "123", 1], ["Primary Layer", "abc", 0], ["Function Layer", "function", 2]] as [title, icon, value]} + {#each layers as [title, icon, value]}