mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-02-16 06:02:41 +00:00
feat: cv2
This commit is contained in:
@@ -6,6 +6,7 @@ const config = {
|
||||
icons: [
|
||||
"rocket_launch",
|
||||
"deployed_code_update",
|
||||
"difference",
|
||||
"adjust",
|
||||
"add",
|
||||
"piano",
|
||||
|
||||
36
package.json
36
package.json
@@ -25,6 +25,7 @@
|
||||
"build:tauri": "tauri build",
|
||||
"tauri": "tauri",
|
||||
"test": "vitest run --coverage",
|
||||
"test:chord-sync": "vitest chord-sync",
|
||||
"preview": "vite preview",
|
||||
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
||||
"minify-icons": "node src/tools/minify-icon-font.js",
|
||||
@@ -35,25 +36,28 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@codemirror/autocomplete": "^6.20.0",
|
||||
"@codemirror/collab": "^6.1.1",
|
||||
"@codemirror/commands": "^6.10.1",
|
||||
"@codemirror/lang-javascript": "^6.2.4",
|
||||
"@codemirror/language": "^6.11.3",
|
||||
"@codemirror/language": "^6.12.1",
|
||||
"@codemirror/lint": "^6.9.2",
|
||||
"@codemirror/merge": "^6.11.2",
|
||||
"@codemirror/state": "^6.5.2",
|
||||
"@codemirror/view": "^6.39.4",
|
||||
"@codemirror/search": "^6.6.0",
|
||||
"@codemirror/state": "^6.5.3",
|
||||
"@codemirror/view": "^6.39.9",
|
||||
"@fontsource-variable/material-symbols-rounded": "^5.2.30",
|
||||
"@fontsource-variable/noto-sans-mono": "^5.2.10",
|
||||
"@lezer/common": "^1.4.0",
|
||||
"@lezer/common": "^1.5.0",
|
||||
"@lezer/generator": "^1.8.0",
|
||||
"@lezer/highlight": "^1.2.3",
|
||||
"@lezer/lr": "^1.4.5",
|
||||
"@lezer/lr": "^1.4.7",
|
||||
"@material/material-color-utilities": "^0.3.0",
|
||||
"@melt-ui/pp": "^0.3.2",
|
||||
"@melt-ui/svelte": "^0.86.6",
|
||||
"@modyfi/vite-plugin-yaml": "^1.1.1",
|
||||
"@sveltejs/adapter-static": "^3.0.10",
|
||||
"@sveltejs/kit": "^2.49.2",
|
||||
"@sveltejs/vite-plugin-svelte": "^6.2.1",
|
||||
"@sveltejs/kit": "^2.49.3",
|
||||
"@sveltejs/vite-plugin-svelte": "^6.2.3",
|
||||
"@tauri-apps/api": "^1.6.0",
|
||||
"@tauri-apps/cli": "^1.6.0",
|
||||
"@types/dom-view-transitions": "^1.0.6",
|
||||
@@ -75,29 +79,29 @@
|
||||
"jsdom": "^26.1.0",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^3.7.4",
|
||||
"prettier-plugin-css-order": "^2.1.2",
|
||||
"prettier-plugin-css-order": "^2.2.0",
|
||||
"prettier-plugin-svelte": "^3.4.1",
|
||||
"rxjs": "^7.8.2",
|
||||
"sass": "^1.97.0",
|
||||
"sass": "^1.97.2",
|
||||
"semver": "^7.7.3",
|
||||
"socket.io-client": "^4.8.1",
|
||||
"socket.io-client": "^4.8.3",
|
||||
"stylelint": "^16.26.1",
|
||||
"stylelint-config-html": "^1.1.0",
|
||||
"stylelint-config-prettier-scss": "^1.0.0",
|
||||
"stylelint-config-recommended-scss": "^16.0.2",
|
||||
"stylelint-config-standard-scss": "^16.0.0",
|
||||
"svelte": "5.37.1",
|
||||
"svelte-check": "^4.3.4",
|
||||
"svelte": "5.46.1",
|
||||
"svelte-check": "^4.3.5",
|
||||
"svelte-preprocess": "^6.0.3",
|
||||
"tippy.js": "^6.3.7",
|
||||
"typesafe-i18n": "^5.26.2",
|
||||
"typescript": "^5.8.3",
|
||||
"vite": "^7.0.6",
|
||||
"typescript": "^5.9.3",
|
||||
"vite": "^7.3.1",
|
||||
"vite-plugin-mkcert": "^1.17.9",
|
||||
"vite-plugin-pwa": "^1.0.2",
|
||||
"vite-plugin-pwa": "^1.2.0",
|
||||
"vitest": "^4.0.16",
|
||||
"web-serial-polyfill": "^1.0.15",
|
||||
"workbox-window": "^7.3.0"
|
||||
"workbox-window": "^7.4.0"
|
||||
},
|
||||
"type": "module"
|
||||
}
|
||||
|
||||
826
pnpm-lock.yaml
generated
826
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -6,15 +6,9 @@ import type {
|
||||
CharaSettingsFile,
|
||||
} from "$lib/share/chara-file.js";
|
||||
import type { Change } from "$lib/undo-redo.js";
|
||||
import {
|
||||
changes,
|
||||
ChangeType,
|
||||
chords,
|
||||
layout,
|
||||
settings,
|
||||
} from "$lib/undo-redo.js";
|
||||
import { changes, ChangeType, layout, settings } from "$lib/undo-redo.js";
|
||||
import { get } from "svelte/store";
|
||||
import { activeProfile, serialPort } from "../serial/connection";
|
||||
import { activeProfile, deviceChords, serialPort } from "../serial/connection";
|
||||
import { csvLayoutToJson, isCsvLayout } from "$lib/backup/compat/legacy-layout";
|
||||
import { isCsvChords, csvChordsToJson } from "./compat/legacy-chords";
|
||||
|
||||
@@ -60,7 +54,7 @@ export function createChordBackup(): CharaChordFile {
|
||||
return {
|
||||
charaVersion: 1,
|
||||
type: "chords",
|
||||
chords: get(chords).map((it) => [it.actions, it.phrase]),
|
||||
chords: get(deviceChords).map((it) => [it.actions, it.phrase]),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -168,7 +162,9 @@ export function restoreFromFile(
|
||||
export function getChangesFromChordFile(file: CharaChordFile) {
|
||||
const changes: Change[] = [];
|
||||
const existingChords = new Set(
|
||||
get(chords).map(({ phrase, actions }) => JSON.stringify([actions, phrase])),
|
||||
get(deviceChords).map(({ phrase, actions }) =>
|
||||
JSON.stringify([actions, phrase]),
|
||||
),
|
||||
);
|
||||
for (const [input, output] of file.chords) {
|
||||
if (existingChords.has(JSON.stringify([input, output]))) {
|
||||
|
||||
149
src/lib/chord-editor/ChangesPanel.svelte
Normal file
149
src/lib/chord-editor/ChangesPanel.svelte
Normal file
@@ -0,0 +1,149 @@
|
||||
<script lang="ts">
|
||||
import {
|
||||
serialPort,
|
||||
sync,
|
||||
syncProgress,
|
||||
syncStatus,
|
||||
} from "$lib/serial/connection";
|
||||
import type { ParseResult } from "./parse-meta";
|
||||
import { actionTooltip } from "$lib/title";
|
||||
import LL from "$i18n/i18n-svelte";
|
||||
import ProgressButton from "$lib/ProgressButton.svelte";
|
||||
import type { EditorView } from "codemirror";
|
||||
import { createSaveTask } from "./save-chords";
|
||||
import { goto } from "$app/navigation";
|
||||
|
||||
let { parsed, view }: { parsed: ParseResult; view: EditorView } = $props();
|
||||
|
||||
$inspect(parsed);
|
||||
|
||||
let added = $derived(
|
||||
parsed.chords.reduce(
|
||||
(acc, chord) =>
|
||||
acc +
|
||||
(chord.phrase && chord.phrase.originalValue === undefined ? 1 : 0),
|
||||
0,
|
||||
),
|
||||
);
|
||||
|
||||
let changed = $derived(
|
||||
parsed.chords.reduce(
|
||||
(acc, chord) =>
|
||||
acc +
|
||||
(chord.phrase?.originalValue !== undefined &&
|
||||
chord.phrase.originalValue !== chord.phrase.value
|
||||
? 1
|
||||
: 0),
|
||||
0,
|
||||
),
|
||||
);
|
||||
|
||||
let error: Error | undefined = $state(undefined);
|
||||
|
||||
async function save() {
|
||||
const port = $serialPort;
|
||||
if (!view || !port) return;
|
||||
error = undefined;
|
||||
const task = createSaveTask(view);
|
||||
const total = task.remove.length + task.set.length;
|
||||
$syncStatus = "uploading";
|
||||
$syncProgress = { current: 0, max: total };
|
||||
let progressCount = 0;
|
||||
for (const input of task.remove) {
|
||||
try {
|
||||
await port.deleteChord({ actions: input });
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
progressCount++;
|
||||
$syncProgress = { current: progressCount, max: total };
|
||||
}
|
||||
for (const [input, phrase] of task.set) {
|
||||
try {
|
||||
await port.setChord({ actions: input, phrase });
|
||||
} catch (e) {
|
||||
error = e as Error;
|
||||
}
|
||||
progressCount++;
|
||||
$syncProgress = { current: progressCount, max: total };
|
||||
}
|
||||
if (error !== undefined) {
|
||||
goto("/terminal");
|
||||
}
|
||||
await sync();
|
||||
}
|
||||
|
||||
let removed = $derived(parsed.removed.length);
|
||||
</script>
|
||||
|
||||
<div class="container">
|
||||
{#if added + changed + removed !== 0 || $syncStatus === "uploading" || $syncStatus === "error"}
|
||||
<div {@attach actionTooltip($LL.saveActions.SAVE())}>
|
||||
<ProgressButton
|
||||
disabled={$syncStatus !== "done"}
|
||||
working={$syncStatus === "uploading" || $syncStatus === "downloading"}
|
||||
progress={$syncProgress && $syncStatus === "uploading"
|
||||
? $syncProgress.current / $syncProgress.max
|
||||
: 0}
|
||||
style="--height: 36px"
|
||||
error={error !== undefined
|
||||
? (error.message ?? error.toString())
|
||||
: undefined}
|
||||
onclick={save}
|
||||
>
|
||||
<span class="icon">save</span>
|
||||
{$LL.saveActions.SAVE()}
|
||||
</ProgressButton>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div>
|
||||
{#if added}
|
||||
<span class="added">+{added}</span>
|
||||
{/if}
|
||||
{#if changed}
|
||||
<span class="changed">~{changed}</span>
|
||||
{/if}
|
||||
{#if removed}
|
||||
<span class="removed">-{removed}</span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if parsed.aliases.size > 0}
|
||||
<div class="section">
|
||||
<span class="icon">content_copy</span>
|
||||
<span>{parsed.aliases.size}</span>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style lang="scss">
|
||||
.icon {
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
justify-content: flex-start;
|
||||
align-items: center;
|
||||
gap: 32px;
|
||||
}
|
||||
|
||||
.section {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.added {
|
||||
color: var(--md-sys-color-success);
|
||||
}
|
||||
|
||||
.changed {
|
||||
color: var(--md-sys-color-warning);
|
||||
}
|
||||
|
||||
.removed {
|
||||
color: var(--md-sys-color-error);
|
||||
}
|
||||
</style>
|
||||
156
src/lib/chord-editor/action-linter.ts
Normal file
156
src/lib/chord-editor/action-linter.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import { linter, type Diagnostic } from "@codemirror/lint";
|
||||
import { parsedChordsField } from "./parsed-chords-plugin";
|
||||
|
||||
export function actionLinter(config?: Parameters<typeof linter>[1]) {
|
||||
const finalConfig: Parameters<typeof linter>[1] = {
|
||||
...config,
|
||||
needsRefresh(update) {
|
||||
return (
|
||||
update.startState.field(parsedChordsField) !==
|
||||
update.state.field(parsedChordsField)
|
||||
);
|
||||
},
|
||||
};
|
||||
return linter((view) => {
|
||||
console.log("lint");
|
||||
const diagnostics: Diagnostic[] = [];
|
||||
const parsed = view.state.field(parsedChordsField);
|
||||
|
||||
for (const chord of parsed.chords) {
|
||||
if (chord.disabled) {
|
||||
diagnostics.push({
|
||||
from: chord.range[0],
|
||||
to: chord.range[1],
|
||||
severity: "info",
|
||||
markClass: "chord-ignored",
|
||||
message: `Chord disabled`,
|
||||
});
|
||||
}
|
||||
if (chord.compounds) {
|
||||
for (const compound of chord.compounds) {
|
||||
if (compound.actions.length === 0 && compound.parent) {
|
||||
const replacement = view.state.doc.sliceString(
|
||||
compound.parent.range[0],
|
||||
compound.parent.input!.range[1],
|
||||
);
|
||||
diagnostics.push({
|
||||
from: compound.range[0],
|
||||
to: compound.range[1],
|
||||
severity: "warning",
|
||||
message: `Compound literal can be replaced with "${replacement}"`,
|
||||
actions: [
|
||||
{
|
||||
name: "Replace",
|
||||
apply(view, from, to) {
|
||||
view.dispatch({
|
||||
changes: {
|
||||
from,
|
||||
to,
|
||||
insert: replacement + "|",
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
const lastCompound = chord.compounds.at(-1);
|
||||
if (lastCompound) {
|
||||
const from = chord.range[0];
|
||||
const to = lastCompound.range[1];
|
||||
if (lastCompound.parent) {
|
||||
diagnostics.push({
|
||||
from,
|
||||
to,
|
||||
severity: "info",
|
||||
markClass: "chord-child",
|
||||
message: `Child of ${view.state.doc.sliceString(lastCompound.parent.range[0], lastCompound.parent.range[1])}`,
|
||||
actions: [
|
||||
{
|
||||
name: "Select Parent",
|
||||
apply(view) {
|
||||
view.dispatch({
|
||||
selection: {
|
||||
anchor: lastCompound.parent!.range[0],
|
||||
},
|
||||
scrollIntoView: true,
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
} else {
|
||||
diagnostics.push({
|
||||
from,
|
||||
to,
|
||||
severity: "warning",
|
||||
message: `Orphan compound`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (chord.children) {
|
||||
diagnostics.push({
|
||||
from: chord.range[0],
|
||||
to: chord.range[1],
|
||||
severity: "info",
|
||||
markClass: "chord-parent",
|
||||
message: `Parent of ${chord.children.length} compound(s)`,
|
||||
actions: chord.children.map((child) => ({
|
||||
name: `Go to ${view.state.doc.sliceString(child.range[0], child.range[1])}`,
|
||||
apply(view) {
|
||||
view.dispatch({
|
||||
selection: {
|
||||
anchor: child.range[0],
|
||||
},
|
||||
scrollIntoView: true,
|
||||
});
|
||||
},
|
||||
})),
|
||||
});
|
||||
}
|
||||
if (chord.phrase) {
|
||||
if (!chord.phrase.originalValue) {
|
||||
diagnostics.push({
|
||||
from: chord.range[0],
|
||||
to: chord.range[1],
|
||||
severity: "info",
|
||||
markClass: "chord-new",
|
||||
message: `New Chord`,
|
||||
});
|
||||
} else if (chord.phrase.originalValue !== chord.phrase.value) {
|
||||
diagnostics.push({
|
||||
from: chord.range[0],
|
||||
to: chord.range[1],
|
||||
severity: "info",
|
||||
markClass: "chord-unchanged",
|
||||
message: `Phrase changed`,
|
||||
});
|
||||
}
|
||||
|
||||
if (chord.aliases) {
|
||||
diagnostics.push({
|
||||
from: chord.phrase.range[0],
|
||||
to: chord.phrase.range[1],
|
||||
severity: "warning",
|
||||
markClass: "chord-alias",
|
||||
message: `Alias of ${chord.aliases.length} chord(s)`,
|
||||
actions: chord.aliases.map((alias) => ({
|
||||
name: `Go to ${view.state.doc.sliceString(alias.range[0], alias.input?.range[1] ?? alias.range[1])}`,
|
||||
apply(view) {
|
||||
view.dispatch({
|
||||
selection: {
|
||||
anchor: alias.range[0],
|
||||
},
|
||||
scrollIntoView: true,
|
||||
});
|
||||
},
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return diagnostics;
|
||||
}, finalConfig);
|
||||
}
|
||||
10
src/lib/chord-editor/action-meta-plugin.ts
Normal file
10
src/lib/chord-editor/action-meta-plugin.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { KEYMAP_CODES, KEYMAP_IDS } from "$lib/serial/keymap-codes";
|
||||
import { derived } from "svelte/store";
|
||||
import { reactiveStateField } from "./store-state-field";
|
||||
|
||||
const actionMeta = derived([KEYMAP_IDS, KEYMAP_CODES], ([ids, codes]) => ({
|
||||
ids,
|
||||
codes,
|
||||
}));
|
||||
|
||||
export const actionMetaPlugin = reactiveStateField(actionMeta);
|
||||
@@ -7,33 +7,34 @@ import {
|
||||
} from "@codemirror/view";
|
||||
import { mount, unmount } from "svelte";
|
||||
import Action from "$lib/components/Action.svelte";
|
||||
import { syntaxTree } from "@codemirror/language";
|
||||
import type { Range } from "@codemirror/state";
|
||||
import { parsedChordsField } from "./parsed-chords-plugin";
|
||||
import { iterActions } from "./parse-meta";
|
||||
|
||||
export class ActionWidget extends WidgetType {
|
||||
component?: {};
|
||||
element?: HTMLElement;
|
||||
|
||||
constructor(readonly id: string | number) {
|
||||
super();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
override eq(other: ActionWidget) {
|
||||
/*override eq(other: ActionWidget) {
|
||||
return this.id == other.id;
|
||||
}
|
||||
}*/
|
||||
|
||||
toDOM() {
|
||||
if (!this.element) {
|
||||
this.element = document.createElement("span");
|
||||
this.element.style.paddingInline = "2px";
|
||||
|
||||
this.component = mount(Action, {
|
||||
target: this.element,
|
||||
props: { action: this.id, display: "keys", inText: true },
|
||||
});
|
||||
if (this.component) {
|
||||
unmount(this.component);
|
||||
}
|
||||
return this.element;
|
||||
const element = document.createElement("span");
|
||||
element.style.paddingInline = "2px";
|
||||
|
||||
this.component = mount(Action, {
|
||||
target: element,
|
||||
props: { action: this.id, display: "keys", inText: true },
|
||||
});
|
||||
return element;
|
||||
}
|
||||
|
||||
override ignoreEvent() {
|
||||
@@ -50,29 +51,24 @@ export class ActionWidget extends WidgetType {
|
||||
function actionWidgets(view: EditorView) {
|
||||
const widgets: Range<Decoration>[] = [];
|
||||
for (const { from, to } of view.visibleRanges) {
|
||||
syntaxTree(view.state).iterate({
|
||||
from,
|
||||
to,
|
||||
enter: (node) => {
|
||||
if (node.name !== "ExplicitAction") return;
|
||||
const value =
|
||||
node.node.getChild("ActionId") ??
|
||||
node.node.getChild("HexNumber") ??
|
||||
node.node.getChild("DecimalNumber");
|
||||
if (!value) return;
|
||||
if (!node.node.getChild("ExplicitDelimEnd")) {
|
||||
for (const chord of view.state.field(parsedChordsField).chords) {
|
||||
if (chord.range[1] < from || chord.range[0] > to) continue;
|
||||
iterActions(chord, (action) => {
|
||||
if (
|
||||
view.state.selection.ranges.some(
|
||||
(r) => r.from <= action.range[1] && r.to > action.range[0],
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const id = view.state.doc.sliceString(value.from, value.to);
|
||||
let deco = Decoration.replace({
|
||||
widget: new ActionWidget(
|
||||
value.name === "ActionId" ? id : parseInt(id),
|
||||
),
|
||||
});
|
||||
widgets.push(deco.range(node.from, node.to));
|
||||
},
|
||||
});
|
||||
if (action.info && action.explicit) {
|
||||
const deco = Decoration.replace({
|
||||
widget: new ActionWidget(action.code),
|
||||
});
|
||||
widgets.push(deco.range(action.range[0], action.range[1]));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return Decoration.set(widgets);
|
||||
}
|
||||
@@ -89,7 +85,9 @@ export const actionPlugin = ViewPlugin.fromClass(
|
||||
if (
|
||||
update.docChanged ||
|
||||
update.viewportChanged ||
|
||||
syntaxTree(update.startState) != syntaxTree(update.state)
|
||||
update.selectionSet ||
|
||||
update.startState.field(parsedChordsField) !=
|
||||
update.state.field(parsedChordsField)
|
||||
)
|
||||
this.decorations = actionWidgets(update.view);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,263 @@
|
||||
import { KEYMAP_CODES, type KeyInfo } from "$lib/serial/keymap-codes";
|
||||
import { get } from "svelte/store";
|
||||
import type { KeyInfo } from "$lib/serial/keymap-codes";
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
import {
|
||||
composeChordInput,
|
||||
hasConcatenator,
|
||||
hashChord,
|
||||
willBeValidChordInput,
|
||||
} from "$lib/serial/chord";
|
||||
import type {
|
||||
ActionMeta,
|
||||
ChordMeta,
|
||||
MetaRange,
|
||||
ParseResult,
|
||||
} from "./parse-meta";
|
||||
import type { Tree } from "@lezer/common";
|
||||
|
||||
export function canUseIdAsString(info: KeyInfo): boolean {
|
||||
return !!info.id && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(info.id);
|
||||
function parseChordMeta(
|
||||
tree: Tree,
|
||||
ids: Map<string, KeyInfo>,
|
||||
codes: Map<number, KeyInfo>,
|
||||
sliceString: (from: number, to: number) => string,
|
||||
): ChordMeta[] {
|
||||
console.time("parseChordTree");
|
||||
const result: ChordMeta[] = [];
|
||||
|
||||
let current: ChordMeta = { range: [0, 0], valid: false };
|
||||
let actions: ActionMeta[] = [];
|
||||
let actionRange: MetaRange | undefined = undefined;
|
||||
|
||||
tree.cursor().iterate(
|
||||
(node) => {
|
||||
if (node.name === "Action") {
|
||||
actionRange = [node.from, node.to];
|
||||
} else if (node.name === "ChordPhrase") {
|
||||
current.phrase = {
|
||||
range: [node.from, node.to],
|
||||
value: [],
|
||||
valid: true,
|
||||
actions: [],
|
||||
hasConcatenator: false,
|
||||
};
|
||||
} else if (node.name === "Chord") {
|
||||
current = { range: [node.from, node.to], valid: false };
|
||||
} else if (node.name === "ActionString") {
|
||||
actions = [];
|
||||
} else if (node.name === "HexNumber") {
|
||||
const hexString = sliceString(node.from, node.to);
|
||||
const code = Number.parseInt(hexString, 16);
|
||||
const parentNode = node.node.parent;
|
||||
if (parentNode?.type.name === "CompoundLiteral") {
|
||||
current.compounds ??= [];
|
||||
current.compounds.push({
|
||||
range: [parentNode.from, parentNode.to],
|
||||
value: code,
|
||||
actions: [],
|
||||
valid: true, // TODO: validate compound literal
|
||||
});
|
||||
} else {
|
||||
const valid = !(Number.isNaN(code) || code < 0 || code > 1023);
|
||||
actions.push({
|
||||
code,
|
||||
info: codes.get(code),
|
||||
explicit: true,
|
||||
valid,
|
||||
range: actionRange!,
|
||||
});
|
||||
}
|
||||
} else if (
|
||||
node.name === "ActionId" ||
|
||||
node.name === "SingleLetter" ||
|
||||
node.name === "EscapedLetter"
|
||||
) {
|
||||
const id = sliceString(node.from, node.to);
|
||||
const info = ids.get(id);
|
||||
const value: ActionMeta = {
|
||||
code: info?.code ?? Number.NaN,
|
||||
info,
|
||||
valid: info !== undefined,
|
||||
range: actionRange!,
|
||||
};
|
||||
if (node.name === "ActionId") {
|
||||
value.explicit = true;
|
||||
}
|
||||
actions.push(value);
|
||||
}
|
||||
},
|
||||
(node) => {
|
||||
if (node.name === "Chord") {
|
||||
result.push(current);
|
||||
if (current.phrase) {
|
||||
current.phrase.actions = actions;
|
||||
current.phrase.value = actions.map(({ code }) => code);
|
||||
current.phrase.valid = actions.every(({ valid }) => valid);
|
||||
current.phrase.hasConcatenator = hasConcatenator(
|
||||
current.phrase.value,
|
||||
codes,
|
||||
);
|
||||
}
|
||||
current.valid =
|
||||
(current.phrase?.valid ?? false) && (current.input?.valid ?? false);
|
||||
if (!current.valid) {
|
||||
current.disabled = true;
|
||||
}
|
||||
} else if (node.name === "CompoundInput") {
|
||||
const lastCompound = current.compounds?.at(-1);
|
||||
current.compounds ??= [];
|
||||
current.compounds.push({
|
||||
range: [node.from, node.to],
|
||||
value: hashChord(
|
||||
composeChordInput(
|
||||
actions.map(({ code }) => code),
|
||||
lastCompound?.value,
|
||||
),
|
||||
),
|
||||
actions,
|
||||
valid:
|
||||
willBeValidChordInput(actions.length, lastCompound !== undefined) &&
|
||||
actions.every(({ valid }) => valid),
|
||||
});
|
||||
} else if (node.name === "ChordInput") {
|
||||
const lastCompound = current.compounds?.at(-1);
|
||||
current.input = {
|
||||
range: [node.from, node.to],
|
||||
value: composeChordInput(
|
||||
actions.map(({ code }) => code),
|
||||
lastCompound?.value,
|
||||
),
|
||||
valid:
|
||||
willBeValidChordInput(actions.length, lastCompound !== undefined) &&
|
||||
actions.every(({ valid }) => valid),
|
||||
actions,
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
console.timeEnd("parseChordTree");
|
||||
return result;
|
||||
}
|
||||
|
||||
export function actionToValue(action: number | KeyInfo) {
|
||||
const info =
|
||||
typeof action === "number" ? get(KEYMAP_CODES).get(action) : action;
|
||||
if (info && info.id?.length === 1)
|
||||
return /^[<>\\\s]$/.test(info.id) ? `\\${info.id}` : info.id;
|
||||
if (!info || !canUseIdAsString(info))
|
||||
return `<0x${(info?.code ?? action).toString(16).padStart(2, "0")}>`;
|
||||
return `<${info.id}>`;
|
||||
function resolveChordOverrides(chords: ChordMeta[]): Map<string, ChordMeta> {
|
||||
console.time("resolveOverrides");
|
||||
const seen = new Map<string, ChordMeta>();
|
||||
for (const info of chords) {
|
||||
if (!info.input || info.disabled) continue;
|
||||
const key = JSON.stringify(info.input.value);
|
||||
const override = seen.get(key);
|
||||
if (override) {
|
||||
override.overrides ??= [];
|
||||
override.overrides.push(info);
|
||||
info.overriddenBy = override;
|
||||
info.disabled = true;
|
||||
} else {
|
||||
seen.set(key, info);
|
||||
}
|
||||
}
|
||||
console.timeEnd("resolveOverrides");
|
||||
return seen;
|
||||
}
|
||||
|
||||
function resolveChordAliases(chords: ChordMeta[]): Map<string, ChordMeta[]> {
|
||||
console.time("resolveAliases");
|
||||
const aliases = new Map<string, ChordMeta[]>();
|
||||
for (const info of chords) {
|
||||
if (!info.phrase) continue;
|
||||
const key = JSON.stringify(info.phrase.value);
|
||||
const list = aliases.get(key) ?? [];
|
||||
list.push(info);
|
||||
aliases.set(key, list);
|
||||
}
|
||||
for (const [key, value] of aliases) {
|
||||
if (value.length <= 1) {
|
||||
aliases.delete(key);
|
||||
} else {
|
||||
for (const info of value) {
|
||||
info.aliases = value.filter((i) => i !== info);
|
||||
}
|
||||
}
|
||||
}
|
||||
console.timeEnd("resolveAliases");
|
||||
return aliases;
|
||||
}
|
||||
|
||||
function resolveCompoundParents(chords: ChordMeta[]): Map<number, ChordMeta> {
|
||||
console.time("resolveCompoundParents");
|
||||
const compounds = new Map<number, ChordMeta>();
|
||||
for (const chord of chords) {
|
||||
if (chord.input && !chord.disabled) {
|
||||
compounds.set(hashChord(chord.input.value), chord);
|
||||
}
|
||||
}
|
||||
for (const chord of chords) {
|
||||
if (chord.compounds) {
|
||||
for (const compound of chord.compounds) {
|
||||
const parent = compounds.get(compound.value);
|
||||
if (parent) {
|
||||
compound.parent = parent;
|
||||
}
|
||||
}
|
||||
const lastCompound = chord.compounds?.at(-1);
|
||||
if (lastCompound && lastCompound.parent) {
|
||||
lastCompound.parent.children ??= [];
|
||||
lastCompound.parent.children.push(chord);
|
||||
}
|
||||
}
|
||||
}
|
||||
console.timeEnd("resolveCompoundParents");
|
||||
return compounds;
|
||||
}
|
||||
|
||||
export function resolveChanges(
|
||||
chords: ChordMeta[],
|
||||
inputs: Map<string, ChordMeta>,
|
||||
deviceChords: CharaChordFile["chords"],
|
||||
): [CharaChordFile["chords"], Map<string, ChordMeta>] {
|
||||
console.time("resolveChanges");
|
||||
const removed: CharaChordFile["chords"] = [];
|
||||
const exact = new Map<string, ChordMeta>();
|
||||
for (const chord of chords) {
|
||||
if (chord.input && chord.phrase && !chord.disabled) {
|
||||
exact.set(
|
||||
JSON.stringify([chord.input.value, chord.phrase?.value ?? []]),
|
||||
chord,
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const deviceChord of deviceChords) {
|
||||
const exactMatch = exact.get(JSON.stringify(deviceChord));
|
||||
if (exactMatch) {
|
||||
exactMatch.phrase!.originalValue = exactMatch.phrase!.value;
|
||||
continue;
|
||||
}
|
||||
const byInput = inputs.get(JSON.stringify(deviceChord[0]));
|
||||
if (byInput) {
|
||||
byInput.phrase!.originalValue = deviceChord[1];
|
||||
continue;
|
||||
}
|
||||
removed.push(deviceChord);
|
||||
}
|
||||
|
||||
console.timeEnd("resolveChanges");
|
||||
return [removed, exact];
|
||||
}
|
||||
|
||||
export function parseCharaChords(
|
||||
tree: Tree,
|
||||
ids: Map<string, KeyInfo>,
|
||||
codes: Map<number, KeyInfo>,
|
||||
deviceChords: CharaChordFile["chords"],
|
||||
sliceString: (from: number, to: number) => string,
|
||||
): ParseResult {
|
||||
console.time("parseTotal");
|
||||
|
||||
const chords = parseChordMeta(tree, ids, codes, sliceString);
|
||||
const inputs = resolveChordOverrides(chords);
|
||||
const aliases = resolveChordAliases(chords);
|
||||
const compounds = resolveCompoundParents(chords);
|
||||
const [removed, exact] = resolveChanges(chords, inputs, deviceChords);
|
||||
|
||||
console.timeEnd("parseTotal");
|
||||
|
||||
return { chords, removed, aliases, compounds, inputs, exact };
|
||||
}
|
||||
|
||||
@@ -1,72 +1,39 @@
|
||||
import { KEYMAP_CATEGORIES, KEYMAP_CODES } from "$lib/serial/keymap-codes";
|
||||
import type {
|
||||
Completion,
|
||||
CompletionSection,
|
||||
CompletionSource,
|
||||
} from "@codemirror/autocomplete";
|
||||
import { derived, get } from "svelte/store";
|
||||
import { actionToValue, canUseIdAsString } from "./action-serializer";
|
||||
import {
|
||||
EditorView,
|
||||
ViewPlugin,
|
||||
ViewUpdate,
|
||||
type PluginValue,
|
||||
} from "@codemirror/view";
|
||||
import { syntaxTree } from "@codemirror/language";
|
||||
import type { EditorState } from "@codemirror/state";
|
||||
|
||||
const completionSections = derived(
|
||||
KEYMAP_CATEGORIES,
|
||||
(categories) =>
|
||||
new Map(
|
||||
categories.map(
|
||||
(category) =>
|
||||
[
|
||||
category,
|
||||
{
|
||||
name: category.name,
|
||||
} satisfies CompletionSection,
|
||||
] as const,
|
||||
),
|
||||
),
|
||||
);
|
||||
export function actionAutocompletePlugin(
|
||||
query: (query: string | undefined) => void,
|
||||
) {
|
||||
return ViewPlugin.fromClass(
|
||||
class implements PluginValue {
|
||||
constructor(readonly view: EditorView) {}
|
||||
|
||||
export const actionAutocompleteItems = derived(
|
||||
[KEYMAP_CODES, completionSections],
|
||||
([codes, sections]) =>
|
||||
codes
|
||||
.values()
|
||||
.map((info) => {
|
||||
const canUseId = canUseIdAsString(info);
|
||||
const completionValue =
|
||||
(canUseId && info.id) ||
|
||||
`0x${info.code.toString(16).padStart(2, "0")}`;
|
||||
return {
|
||||
label:
|
||||
[
|
||||
canUseId || !info.id ? undefined : `"${info.id}"`,
|
||||
info.title,
|
||||
info.variant?.replace(/^[a-z]/g, (c) => c.toUpperCase()),
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ") || completionValue,
|
||||
detail: actionToValue(info),
|
||||
section: info.category ? sections.get(info.category) : undefined,
|
||||
info: info.description,
|
||||
type: "keyword",
|
||||
apply: completionValue + ">",
|
||||
} satisfies Completion;
|
||||
})
|
||||
.filter(
|
||||
(item) => typeof item.label === "string" && item.apply !== undefined,
|
||||
)
|
||||
.toArray(),
|
||||
);
|
||||
update(update: ViewUpdate) {
|
||||
query(this.resolveAutocomplete(update.state));
|
||||
}
|
||||
|
||||
export const actionAutocomplete = ((context) => {
|
||||
let word = context.tokenBefore([
|
||||
"ExplicitDelimStart",
|
||||
"ActionId",
|
||||
"HexNumber",
|
||||
"DecimalNumber",
|
||||
]);
|
||||
if (!word) return null;
|
||||
console.log(get(actionAutocompleteItems));
|
||||
return {
|
||||
from: word.type.name === "ExplicitDelimStart" ? word.to : word.from,
|
||||
validFor: /^<?[a-zA-Z0-9_]*$/,
|
||||
options: get(actionAutocompleteItems),
|
||||
};
|
||||
}) satisfies CompletionSource;
|
||||
resolveAutocomplete(state: EditorState): string | undefined {
|
||||
if (state.selection.ranges.length !== 1) return;
|
||||
const from = state.selection.ranges[0]!.from;
|
||||
const to = state.selection.ranges[0]!.to;
|
||||
if (from !== to) return;
|
||||
const tree = syntaxTree(state);
|
||||
const node = tree.resolveInner(from, -1).parent;
|
||||
if (node?.name !== "ExplicitAction") return;
|
||||
if (node.getChild("ExplicitDelimEnd")) return;
|
||||
const queryNode = node.getChild("ExplicitDelimStart")?.nextSibling;
|
||||
return (
|
||||
(queryNode
|
||||
? state.doc.sliceString(queryNode.from, queryNode.to)
|
||||
: undefined) || undefined
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
44
src/lib/chord-editor/changes-panel.svelte.ts
Normal file
44
src/lib/chord-editor/changes-panel.svelte.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { EditorView, showPanel, type Panel } from "@codemirror/view";
|
||||
import { parsedChordsField } from "./parsed-chords-plugin";
|
||||
import { mount, unmount } from "svelte";
|
||||
import ChangesPanel from "./ChangesPanel.svelte";
|
||||
|
||||
function changesPanelFunc(view: EditorView): Panel {
|
||||
let dom = document.createElement("div");
|
||||
dom.style.display = "contents";
|
||||
let viewState = $state.raw(view);
|
||||
let parsed = $state.raw(view.state.field(parsedChordsField));
|
||||
let component: {};
|
||||
return {
|
||||
dom,
|
||||
mount() {
|
||||
component = mount(ChangesPanel, {
|
||||
target: dom,
|
||||
props: {
|
||||
get parsed() {
|
||||
return parsed;
|
||||
},
|
||||
get view() {
|
||||
return viewState;
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
update: (update) => {
|
||||
if (
|
||||
update.startState.field(parsedChordsField) !==
|
||||
update.state.field(parsedChordsField)
|
||||
) {
|
||||
console.log("update changes panel");
|
||||
parsed = update.state.field(parsedChordsField);
|
||||
}
|
||||
},
|
||||
destroy() {
|
||||
unmount(component);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function changesPanel() {
|
||||
return showPanel.of(changesPanelFunc);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import {
|
||||
EditorView,
|
||||
ViewPlugin,
|
||||
ViewUpdate,
|
||||
type PluginValue,
|
||||
} from "@codemirror/view";
|
||||
|
||||
export const changesPlugin = ViewPlugin.fromClass(
|
||||
class implements PluginValue {
|
||||
constructor(readonly view: EditorView) {}
|
||||
|
||||
update(update: ViewUpdate) {}
|
||||
},
|
||||
{
|
||||
eventHandlers: {},
|
||||
},
|
||||
);
|
||||
@@ -26,7 +26,7 @@ export class DelimWidget extends WidgetType {
|
||||
|
||||
toDOM() {
|
||||
if (!this.element) {
|
||||
this.element = document.createElement("span");
|
||||
/*this.element = document.createElement("span");
|
||||
this.element.innerHTML =
|
||||
" ⇛" + (this.hasConcatenator ? "" : " ");
|
||||
this.element.style.scale = "1.8";
|
||||
@@ -41,7 +41,9 @@ export class DelimWidget extends WidgetType {
|
||||
props: { action: 574, display: "keys", inText: true, ghost: true },
|
||||
});
|
||||
this.element.appendChild(button);
|
||||
}
|
||||
}*/
|
||||
this.element = document.createElement("div");
|
||||
this.element.style.breakAfter = "column";
|
||||
}
|
||||
return this.element;
|
||||
}
|
||||
|
||||
44
src/lib/chord-editor/chord-sync-plugin.ts
Normal file
44
src/lib/chord-editor/chord-sync-plugin.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
import { StateEffect, StateField } from "@codemirror/state";
|
||||
import { actionMetaPlugin } from "./action-meta-plugin";
|
||||
import { syncCharaChords } from "./chord-sync";
|
||||
import type { EditorView } from "@codemirror/view";
|
||||
|
||||
const chordSyncEffect = StateEffect.define<CharaChordFile["chords"]>();
|
||||
|
||||
export function editorSyncChords(
|
||||
view: EditorView,
|
||||
newDeviceChords: CharaChordFile["chords"],
|
||||
) {
|
||||
const { ids, codes } = view.state.field(actionMetaPlugin.field);
|
||||
const oldDeviceChords = view.state.field(deviceChordField);
|
||||
const changes = syncCharaChords(
|
||||
oldDeviceChords,
|
||||
newDeviceChords,
|
||||
ids,
|
||||
codes,
|
||||
view.state.doc.toString(),
|
||||
);
|
||||
view.dispatch({
|
||||
effects: chordSyncEffect.of(newDeviceChords),
|
||||
changes,
|
||||
});
|
||||
}
|
||||
|
||||
export const deviceChordField = StateField.define<CharaChordFile["chords"]>({
|
||||
create() {
|
||||
return [];
|
||||
},
|
||||
update(value, transaction) {
|
||||
return (
|
||||
transaction.effects.findLast((it) => it.is(chordSyncEffect))?.value ??
|
||||
value
|
||||
);
|
||||
},
|
||||
toJSON(value) {
|
||||
return value;
|
||||
},
|
||||
fromJSON(value) {
|
||||
return value;
|
||||
},
|
||||
});
|
||||
135
src/lib/chord-editor/chord-sync.spec.ts
Normal file
135
src/lib/chord-editor/chord-sync.spec.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import type { KeyInfo } from "$lib/serial/keymap-codes";
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { parseCharaChords } from "./action-serializer";
|
||||
import { parser } from "./chords.grammar";
|
||||
import { syncCharaChords } from "./chord-sync";
|
||||
import { Text } from "@codemirror/state";
|
||||
|
||||
const asciiInfo: KeyInfo[] = Array.from(
|
||||
{ length: 0x7f - 0x20 },
|
||||
(_, i) =>
|
||||
({
|
||||
code: i + 0x20,
|
||||
id: String.fromCharCode(i + 0x20),
|
||||
}) satisfies KeyInfo,
|
||||
);
|
||||
const asciiCodes = new Map<number, KeyInfo>(
|
||||
asciiInfo.map((info) => [info.code, info]),
|
||||
);
|
||||
const asciiIds = new Map<string, KeyInfo>(
|
||||
asciiInfo.map((info) => [info.id!, info]),
|
||||
);
|
||||
|
||||
function chords(...strings: string[]): string {
|
||||
return strings.join("\n");
|
||||
}
|
||||
|
||||
function backup(doc: string): CharaChordFile["chords"] {
|
||||
const tree = parser.parse(doc);
|
||||
const result = parseCharaChords(tree, asciiIds, asciiCodes, [], (from, to) =>
|
||||
doc.slice(from, to),
|
||||
);
|
||||
return result.chords
|
||||
.filter((chord) => !chord.disabled)
|
||||
.map((chord) => [chord.input?.value ?? [], chord.phrase?.value ?? []]);
|
||||
}
|
||||
|
||||
function expectSync(options: {
|
||||
org: string[];
|
||||
mod: string[];
|
||||
cur: string[];
|
||||
exp: string[];
|
||||
}) {
|
||||
expect(
|
||||
syncCharaChords(
|
||||
backup(chords(...options.org)),
|
||||
backup(chords(...options.mod)),
|
||||
asciiIds,
|
||||
asciiCodes,
|
||||
chords(...options.cur),
|
||||
)
|
||||
.apply(Text.of(options.cur))
|
||||
.toString()
|
||||
.replace(/\n$/, ""),
|
||||
).toEqual(chords(...options.exp));
|
||||
}
|
||||
|
||||
describe("chord sync", function () {
|
||||
it("should not do anything when no changes happened", function () {
|
||||
expectSync({
|
||||
org: ["abc=>def", "def=>ghi", "jkl=>mno"],
|
||||
mod: ["abc=>def", "def=>ghi", "jkl=>mno"],
|
||||
cur: ["abc=>def", "def=>ghi", "jkl=>mno"],
|
||||
exp: ["abc=>def", "def=>ghi", "jkl=>mno"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should not touch the doc if device chords are unchanged", function () {
|
||||
expectSync({
|
||||
org: ["abc=>def", "def=>ghi", "jkl=>mno"],
|
||||
mod: ["abc=>def", "def=>ghi", "jkl=>mno"],
|
||||
cur: ["ab=>def", "def=>gh"],
|
||||
exp: ["ab=>def", "def=>gh"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should apply removals to unchanged chords only", function () {
|
||||
expectSync({
|
||||
org: ["abc=>def", "def=>ghi", "jkl=>mno", "mno=>pqr"],
|
||||
mod: ["abc=>def"],
|
||||
cur: ["abc=>def", "def=>ghij", "jkl=>mno", "mno=>pqr"],
|
||||
exp: ["abc=>def", "def=>ghij"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should keep user modifications over device modifications", function () {
|
||||
expectSync({
|
||||
org: ["abc=>def", "def=>ghi", "jkl=>mno", "mno=>pqr"],
|
||||
mod: ["abc=>def", "def=>ghijk", "jkl=>mnop", "mno=>pqr"],
|
||||
cur: ["abc=>def", "def=>ghij", "jkl=>mno", "mno=>pqr"],
|
||||
exp: ["abc=>def", "def=>ghij", "jkl=>mnop", "mno=>pqr"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle complex changes", function () {
|
||||
expectSync({
|
||||
org: [
|
||||
"unchanged=>unchanged",
|
||||
"usermod=>usermod",
|
||||
"devmod=>devmod",
|
||||
"userremoval=>userremoval",
|
||||
"devremoval=>devremoval",
|
||||
"devremusermod=>devremusermod",
|
||||
],
|
||||
mod: [
|
||||
"unchanged=>unchanged",
|
||||
"devadd=>devadd",
|
||||
"usermod=>usermod",
|
||||
"userremoval=>userremoval",
|
||||
"devmod=>devmod1",
|
||||
"sameadd=>sameadd",
|
||||
],
|
||||
cur: [
|
||||
"useradd1=>useradd1",
|
||||
"unchanged=>unchanged",
|
||||
"usermod=>use",
|
||||
"devremusermod=>xyz",
|
||||
"devmod=>devmod",
|
||||
"sameadd=>sameadd",
|
||||
"devremoval=>devremoval",
|
||||
"useradd=>useradd",
|
||||
],
|
||||
exp: [
|
||||
"devadd=>devadd",
|
||||
"useradd1=>useradd1",
|
||||
"unchanged=>unchanged",
|
||||
"usermod=>use",
|
||||
"devremusermod=>xyz",
|
||||
"devmod=>devmod1",
|
||||
"sameadd=>sameadd",
|
||||
"useradd=>useradd",
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
130
src/lib/chord-editor/chord-sync.ts
Normal file
130
src/lib/chord-editor/chord-sync.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import type { KeyInfo } from "$lib/serial/keymap-codes";
|
||||
import { ChangeSet, type ChangeSpec } from "@codemirror/state";
|
||||
import { parseCharaChords } from "./action-serializer";
|
||||
import { parser } from "./chords.grammar";
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
import { splitCompound } from "$lib/serial/chord";
|
||||
|
||||
function canUseIdAsString(info: KeyInfo): boolean {
|
||||
return !!info.id && /^[^>\n]+$/.test(info.id);
|
||||
}
|
||||
|
||||
export function actionToValue(code: number, info?: KeyInfo) {
|
||||
if (info && info.id?.length === 1)
|
||||
return /^[<>|\\\s]$/.test(info.id) ? `\\${info.id}` : info.id;
|
||||
if (!info || !canUseIdAsString(info))
|
||||
return `<0x${code.toString(16).padStart(2, "0")}>`;
|
||||
return `<${info.id}>`;
|
||||
}
|
||||
|
||||
function canonicalInputSorting(input: number[], phrase: number[]): number[] {
|
||||
const tail = [...input];
|
||||
const prefix = phrase.filter((code) => {
|
||||
const index = tail.indexOf(code);
|
||||
if (index !== -1) {
|
||||
tail.splice(index, 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return [...prefix, ...tail];
|
||||
}
|
||||
|
||||
export interface ChangeType {
|
||||
from: number;
|
||||
to: number;
|
||||
insert: string;
|
||||
}
|
||||
|
||||
export function syncCharaChords(
|
||||
originalDeviceChords: CharaChordFile["chords"],
|
||||
newDeviceChords: CharaChordFile["chords"],
|
||||
ids: Map<string, KeyInfo>,
|
||||
codes: Map<number, KeyInfo>,
|
||||
doc: string,
|
||||
): ChangeSet {
|
||||
const tree = parser.parse(doc);
|
||||
const result = parseCharaChords(
|
||||
tree,
|
||||
ids,
|
||||
codes,
|
||||
originalDeviceChords,
|
||||
(from, to) => doc.slice(from, to),
|
||||
);
|
||||
|
||||
const exactChords = new Map<string, number>();
|
||||
for (const chord of originalDeviceChords) {
|
||||
const key = JSON.stringify(chord);
|
||||
const count = exactChords.get(key) ?? 0;
|
||||
exactChords.set(key, count + 1);
|
||||
}
|
||||
|
||||
const changes: ChangeType[] = [];
|
||||
|
||||
const inputModified = new Set<string>();
|
||||
for (const chord of newDeviceChords) {
|
||||
const key = JSON.stringify(chord);
|
||||
const count = exactChords.get(key) ?? 0;
|
||||
if (count > 0) {
|
||||
exactChords.set(key, count - 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
const inputKey = JSON.stringify(chord[0]);
|
||||
inputModified.add(inputKey);
|
||||
const byInput = result.inputs.get(inputKey);
|
||||
if (byInput) {
|
||||
if (
|
||||
byInput.phrase?.originalValue &&
|
||||
byInput.phrase.originalValue === byInput.phrase.value
|
||||
) {
|
||||
changes.push({
|
||||
from: byInput.phrase.range[0],
|
||||
to: byInput.phrase.range[1],
|
||||
insert: chord[1]
|
||||
.map((code) => actionToValue(code, codes.get(code)))
|
||||
.join(""),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const [inputs, compound] = splitCompound(chord[0]);
|
||||
const sortedInput = canonicalInputSorting(inputs, chord[1]);
|
||||
changes.push({
|
||||
from: 0,
|
||||
to: 0,
|
||||
insert:
|
||||
(compound ? `|0x${compound.toString(16)}|` : "") +
|
||||
sortedInput
|
||||
.map((code) => actionToValue(code, codes.get(code)))
|
||||
.join("") +
|
||||
"=>" +
|
||||
chord[1]
|
||||
.map((code) => actionToValue(code, codes.get(code)))
|
||||
.join("") +
|
||||
"\n",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
changes.push(
|
||||
...exactChords
|
||||
.entries()
|
||||
.filter(([, count]) => count > 0)
|
||||
.map(([key]) => result.exact.get(key))
|
||||
.filter((chord) => chord !== undefined)
|
||||
.filter(
|
||||
(chord) =>
|
||||
chord.input && !inputModified.has(JSON.stringify(chord.input.value)),
|
||||
)
|
||||
.map(
|
||||
(chord) =>
|
||||
({
|
||||
from: chord.range[0],
|
||||
to: chord.range[1],
|
||||
insert: "",
|
||||
}) satisfies ChangeSpec,
|
||||
),
|
||||
);
|
||||
|
||||
return ChangeSet.of(changes, doc.length);
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
HighlightStyle,
|
||||
} from "@codemirror/language";
|
||||
import { styleTags, tags } from "@lezer/highlight";
|
||||
import { actionAutocomplete } from "./autocomplete";
|
||||
|
||||
export const chordHighlightStyle = HighlightStyle.define([
|
||||
{
|
||||
@@ -51,7 +50,5 @@ export const chordLanguage = LRLanguage.define({
|
||||
});
|
||||
|
||||
export function chordLanguageSupport() {
|
||||
return new LanguageSupport(chordLanguage, [
|
||||
chordLanguage.data.of({ autocomplete: actionAutocomplete }),
|
||||
]);
|
||||
return new LanguageSupport(chordLanguage, [chordLanguage.data.of({})]);
|
||||
}
|
||||
|
||||
@@ -1,27 +1,43 @@
|
||||
@top Program { Chord* }
|
||||
|
||||
ExplicitAction { ExplicitDelimStart (HexNumber | DecimalNumber | ActionId) ExplicitDelimEnd }
|
||||
ExplicitAction { ExplicitDelimStart (HexNumber | ActionId) ExplicitDelimEnd }
|
||||
EscapedSingleAction { Escape EscapedLetter }
|
||||
Action { SingleLetter | ExplicitAction | EscapedSingleAction }
|
||||
ActionString { Action* }
|
||||
ChordInput { (ActionString CompoundDelim)* ActionString }
|
||||
|
||||
ActionString { Action+ }
|
||||
|
||||
CompoundLiteral { CompoundDelim HexNumber CompoundDelim }
|
||||
CompoundInput { ActionString CompoundDelim }
|
||||
|
||||
ChordInput { CompoundLiteral? CompoundInput* ActionString }
|
||||
ChordPhrase { ActionString }
|
||||
|
||||
Chord { ChordInput PhraseDelim ChordPhrase ChordDelim }
|
||||
|
||||
@skip {
|
||||
Space
|
||||
}
|
||||
|
||||
@tokens {
|
||||
@precedence {HexNumber, DecimalNumber}
|
||||
@precedence {CompoundDelim, PhraseDelim, ExplicitDelimStart, ChordDelim, SingleLetter}
|
||||
@precedence {EscapedLetter}
|
||||
@precedence { HexNumber, ActionId }
|
||||
@precedence { Space, Escape }
|
||||
@precedence { Space, SingleLetter }
|
||||
@precedence { Escape, SingleLetter }
|
||||
@precedence { CompoundDelim, SingleLetter }
|
||||
@precedence { ActionId, Space }
|
||||
@precedence { EscapedLetter, Space }
|
||||
|
||||
Space {" "}
|
||||
ExplicitDelimStart {"<"}
|
||||
ExplicitDelimEnd {">"}
|
||||
CompoundDelim {"+>"}
|
||||
CompoundDelim {"|"}
|
||||
PhraseDelim {"=>"}
|
||||
Escape { "\\" }
|
||||
HexNumber { "0x" $[a-fA-F0-9]+ }
|
||||
DecimalNumber { $[0-9]+ }
|
||||
ActionId { $[a-zA-Z_]$[a-zA-Z0-9_]* }
|
||||
SingleLetter { ![\\] }
|
||||
EscapedLetter { ![] }
|
||||
ChordDelim { ($[\n] | @eof) }
|
||||
ActionId { ![\n>]+ }
|
||||
SingleLetter { ![\n<] }
|
||||
EscapedLetter { ![\n] }
|
||||
ChordDelim { ("\n" | @eof) }
|
||||
}
|
||||
|
||||
@detectDelim
|
||||
|
||||
176
src/lib/chord-editor/parse-meta.ts
Normal file
176
src/lib/chord-editor/parse-meta.ts
Normal file
@@ -0,0 +1,176 @@
|
||||
import type { KeyInfo } from "$lib/serial/keymap-codes";
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
import type { ChangeDesc } from "@codemirror/state";
|
||||
|
||||
export type MetaRange = [from: number, to: number];
|
||||
|
||||
function mapMetaRange(range: MetaRange, change: ChangeDesc): MetaRange {
|
||||
const newFrom = change.mapPos(range[0]);
|
||||
const newTo = change.mapPos(range[1]);
|
||||
if (newFrom === range[0] && newTo === range[1]) {
|
||||
return range;
|
||||
}
|
||||
return [newFrom, newTo];
|
||||
}
|
||||
|
||||
export interface ActionMeta {
|
||||
code: number;
|
||||
info?: KeyInfo;
|
||||
explicit?: boolean;
|
||||
range: MetaRange;
|
||||
valid: boolean;
|
||||
}
|
||||
|
||||
function mapActionMeta(action: ActionMeta, change: ChangeDesc): ActionMeta {
|
||||
const newRange = mapMetaRange(action.range, change);
|
||||
if (newRange === action.range) {
|
||||
return action;
|
||||
}
|
||||
return {
|
||||
...action,
|
||||
range: newRange,
|
||||
};
|
||||
}
|
||||
|
||||
function mapArray<T>(
|
||||
array: T[],
|
||||
change: ChangeDesc,
|
||||
mapFn: (action: T, change: ChangeDesc) => T,
|
||||
): T[] {
|
||||
let changed = false;
|
||||
const newArray = array.map((value) => {
|
||||
const newValue = mapFn(value, change);
|
||||
if (newValue !== value) {
|
||||
changed = true;
|
||||
return newValue;
|
||||
}
|
||||
return value;
|
||||
});
|
||||
if (changed) {
|
||||
return newArray;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
export interface ActionStringMeta<T> {
|
||||
range: MetaRange;
|
||||
value: T;
|
||||
valid: boolean;
|
||||
actions: ActionMeta[];
|
||||
}
|
||||
|
||||
function mapActionStringMeta<T extends ActionStringMeta<unknown>>(
|
||||
actionString: T,
|
||||
change: ChangeDesc,
|
||||
) {
|
||||
const newRange = mapMetaRange(actionString.range, change);
|
||||
const newActions = mapArray(actionString.actions, change, mapActionMeta);
|
||||
if (newRange === actionString.range && newActions === actionString.actions) {
|
||||
return actionString;
|
||||
}
|
||||
return {
|
||||
...actionString,
|
||||
range: newRange,
|
||||
actions: newActions,
|
||||
};
|
||||
}
|
||||
|
||||
export interface PhraseMeta extends ActionStringMeta<number[]> {
|
||||
hasConcatenator: boolean;
|
||||
originalValue?: number[];
|
||||
}
|
||||
|
||||
export interface CompoundMeta extends ActionStringMeta<number> {
|
||||
parent?: ChordMeta;
|
||||
}
|
||||
|
||||
export interface InputMeta extends ActionStringMeta<number[]> {}
|
||||
|
||||
export interface ChordMeta {
|
||||
range: MetaRange;
|
||||
valid: boolean;
|
||||
disabled?: boolean;
|
||||
compounds?: CompoundMeta[];
|
||||
input?: InputMeta;
|
||||
phrase?: PhraseMeta;
|
||||
children?: ChordMeta[];
|
||||
overrides?: ChordMeta[];
|
||||
aliases?: ChordMeta[];
|
||||
overriddenBy?: ChordMeta;
|
||||
}
|
||||
|
||||
export function mapChordMeta(chord: ChordMeta, change: ChangeDesc): ChordMeta {
|
||||
const newRange = mapMetaRange(chord.range, change);
|
||||
const newCompounds = chord.compounds
|
||||
? mapArray(chord.compounds, change, mapActionStringMeta)
|
||||
: undefined;
|
||||
const newInput = chord.input
|
||||
? mapActionStringMeta(chord.input, change)
|
||||
: undefined;
|
||||
const newPhrase = chord.phrase
|
||||
? mapActionStringMeta(chord.phrase, change)
|
||||
: undefined;
|
||||
if (
|
||||
newRange === chord.range &&
|
||||
newCompounds === chord.compounds &&
|
||||
newInput === chord.input &&
|
||||
newPhrase === chord.phrase
|
||||
) {
|
||||
return chord;
|
||||
}
|
||||
|
||||
const newChord: ChordMeta = {
|
||||
...chord,
|
||||
range: newRange,
|
||||
};
|
||||
if (newCompounds) newChord.compounds = newCompounds;
|
||||
if (newInput) newChord.input = newInput;
|
||||
if (newPhrase) newChord.phrase = newPhrase;
|
||||
return newChord;
|
||||
}
|
||||
|
||||
export interface ParseResult {
|
||||
chords: ChordMeta[];
|
||||
removed: CharaChordFile["chords"];
|
||||
aliases: Map<string, ChordMeta[]>;
|
||||
compounds: Map<number, ChordMeta>;
|
||||
inputs: Map<string, ChordMeta>;
|
||||
exact: Map<string, ChordMeta>;
|
||||
}
|
||||
|
||||
export function mapParseResult(
|
||||
result: ParseResult,
|
||||
change: ChangeDesc,
|
||||
): ParseResult {
|
||||
const newChords = mapArray(result.chords, change, mapChordMeta);
|
||||
if (newChords === result.chords) {
|
||||
return result;
|
||||
}
|
||||
return {
|
||||
...result,
|
||||
chords: newChords,
|
||||
};
|
||||
}
|
||||
|
||||
export function iterActions(
|
||||
chord: ChordMeta,
|
||||
callback: (action: ActionMeta) => void,
|
||||
) {
|
||||
if (chord.input) {
|
||||
for (const action of chord.input.actions) {
|
||||
callback(action);
|
||||
}
|
||||
}
|
||||
if (chord.compounds) {
|
||||
for (const compound of chord.compounds) {
|
||||
for (const action of compound.actions) {
|
||||
callback(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (chord.phrase) {
|
||||
for (const action of chord.phrase.actions) {
|
||||
callback(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
src/lib/chord-editor/parsed-chords-plugin.ts
Normal file
40
src/lib/chord-editor/parsed-chords-plugin.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { StateField } from "@codemirror/state";
|
||||
import { parseCharaChords } from "./action-serializer";
|
||||
import { actionMetaPlugin } from "./action-meta-plugin";
|
||||
import { syntaxTree } from "@codemirror/language";
|
||||
import { deviceChordField } from "./chord-sync-plugin";
|
||||
import { mapParseResult, type ParseResult } from "./parse-meta";
|
||||
|
||||
export const parsedChordsField = StateField.define<ParseResult>({
|
||||
create() {
|
||||
return {
|
||||
chords: [],
|
||||
removed: [],
|
||||
aliases: new Map(),
|
||||
compounds: new Map(),
|
||||
inputs: new Map(),
|
||||
exact: new Map(),
|
||||
};
|
||||
},
|
||||
update(value, transaction) {
|
||||
const tree = syntaxTree(transaction.state);
|
||||
const ids = transaction.state.field(actionMetaPlugin.field).ids;
|
||||
const codes = transaction.state.field(actionMetaPlugin.field).codes;
|
||||
const deviceChords = transaction.state.field(deviceChordField);
|
||||
if (
|
||||
tree !== syntaxTree(transaction.startState) ||
|
||||
ids !== transaction.startState.field(actionMetaPlugin.field).ids ||
|
||||
codes !== transaction.startState.field(actionMetaPlugin.field).codes ||
|
||||
deviceChords !== transaction.startState.field(deviceChordField)
|
||||
) {
|
||||
return parseCharaChords(
|
||||
syntaxTree(transaction.state),
|
||||
ids,
|
||||
codes,
|
||||
deviceChords,
|
||||
(from, to) => transaction.state.doc.sliceString(from, to),
|
||||
);
|
||||
}
|
||||
return mapParseResult(value, transaction.changes);
|
||||
},
|
||||
});
|
||||
185
src/lib/chord-editor/persistent-state-plugin.ts
Normal file
185
src/lib/chord-editor/persistent-state-plugin.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import {
|
||||
EditorView,
|
||||
highlightActiveLine,
|
||||
keymap,
|
||||
lineNumbers,
|
||||
ViewPlugin,
|
||||
ViewUpdate,
|
||||
} from "@codemirror/view";
|
||||
import {
|
||||
history,
|
||||
historyField,
|
||||
historyKeymap,
|
||||
standardKeymap,
|
||||
} from "@codemirror/commands";
|
||||
import { debounceTime, mergeMap, Subject } from "rxjs";
|
||||
import { EditorState, type EditorStateConfig } from "@codemirror/state";
|
||||
import { lintGutter } from "@codemirror/lint";
|
||||
import {
|
||||
chordHighlightStyle,
|
||||
chordLanguageSupport,
|
||||
} from "./chords-grammar-plugin";
|
||||
import { actionLinter } from "./action-linter";
|
||||
import { actionAutocompletePlugin } from "./autocomplete";
|
||||
import { delimPlugin } from "./chord-delim-plugin";
|
||||
import { actionPlugin } from "./action-plugin";
|
||||
import { syntaxHighlighting } from "@codemirror/language";
|
||||
import { deviceChordField } from "./chord-sync-plugin";
|
||||
import { actionMetaPlugin } from "./action-meta-plugin";
|
||||
import { parsedChordsField } from "./parsed-chords-plugin";
|
||||
import { changesPanel } from "./changes-panel.svelte";
|
||||
import { searchKeymap } from "@codemirror/search";
|
||||
|
||||
const serializedFields = {
|
||||
history: historyField,
|
||||
deviceChords: deviceChordField,
|
||||
};
|
||||
|
||||
export interface EditorConfig {
|
||||
rawCode?: boolean;
|
||||
storeName: string;
|
||||
autocomplete(query: string | undefined): void;
|
||||
}
|
||||
|
||||
export function createConfig(params: EditorConfig) {
|
||||
return {
|
||||
extensions: [
|
||||
actionMetaPlugin.plugin,
|
||||
deviceChordField,
|
||||
parsedChordsField,
|
||||
changesPanel(),
|
||||
lintGutter(),
|
||||
params.rawCode ? [lineNumbers()] : [delimPlugin, actionPlugin],
|
||||
chordLanguageSupport(),
|
||||
actionLinter({
|
||||
delay: 100,
|
||||
markerFilter(diagnostics) {
|
||||
return diagnostics.filter((it) => it.from !== it.to);
|
||||
},
|
||||
}),
|
||||
actionAutocompletePlugin(params.autocomplete),
|
||||
persistentStatePlugin(params.storeName),
|
||||
history(),
|
||||
syntaxHighlighting(chordHighlightStyle),
|
||||
highlightActiveLine(),
|
||||
EditorView.theme({
|
||||
".cm-line": {
|
||||
borderBottom: "1px solid transparent",
|
||||
caretColor: "var(--md-sys-color-on-surface)",
|
||||
},
|
||||
".cm-scroller": {
|
||||
overflow: "auto",
|
||||
width: "100%",
|
||||
fontFamily: "inherit !important",
|
||||
gap: "8px",
|
||||
},
|
||||
".cm-content": {
|
||||
flex: 1,
|
||||
},
|
||||
".cm-cursor": {
|
||||
borderColor: "var(--md-sys-color-on-surface)",
|
||||
},
|
||||
}),
|
||||
keymap.of([...standardKeymap, ...historyKeymap, ...searchKeymap]),
|
||||
],
|
||||
} satisfies EditorStateConfig;
|
||||
}
|
||||
|
||||
export async function loadPersistentState(
|
||||
params: EditorConfig,
|
||||
): Promise<EditorState> {
|
||||
const stored = await getState(params.storeName);
|
||||
const config = createConfig(params);
|
||||
|
||||
if (stored) {
|
||||
try {
|
||||
return EditorState.fromJSON(stored, config, serializedFields);
|
||||
} catch (e) {
|
||||
console.error("Failed to parse persistent state:", e);
|
||||
}
|
||||
}
|
||||
return EditorState.create(config);
|
||||
}
|
||||
|
||||
export function persistentStatePlugin(storeName: string) {
|
||||
return ViewPlugin.fromClass(
|
||||
class {
|
||||
updateSubject = new Subject<void>();
|
||||
subscription = this.updateSubject
|
||||
.pipe(
|
||||
debounceTime(500),
|
||||
mergeMap(() =>
|
||||
storeState(storeName, this.view.state.toJSON(serializedFields)),
|
||||
),
|
||||
)
|
||||
.subscribe(() => {});
|
||||
|
||||
constructor(readonly view: EditorView) {}
|
||||
|
||||
update(update: ViewUpdate) {
|
||||
if (update.state !== update.startState) {
|
||||
this.updateSubject.next();
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.subscription.unsubscribe();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const dbName = "chord-state";
|
||||
const dbVersion = 1;
|
||||
const storeName = "state";
|
||||
|
||||
async function openDb(): Promise<IDBDatabase> {
|
||||
const dbRequest = indexedDB.open(dbName, dbVersion);
|
||||
return new Promise<IDBDatabase>((resolve, reject) => {
|
||||
dbRequest.onsuccess = () => resolve(dbRequest.result);
|
||||
dbRequest.onerror = () => reject(dbRequest.error);
|
||||
dbRequest.onupgradeneeded = () => {
|
||||
const db = dbRequest.result;
|
||||
if (!db.objectStoreNames.contains(storeName)) {
|
||||
db.createObjectStore(storeName);
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function getState<T>(name: string): Promise<T | undefined> {
|
||||
const db = await openDb();
|
||||
try {
|
||||
const readTransaction = db.transaction([storeName], "readonly");
|
||||
const store = readTransaction.objectStore(storeName);
|
||||
const itemRequest = store.get(name);
|
||||
const result = await new Promise<T | undefined>((resolve) => {
|
||||
itemRequest.onsuccess = () => resolve(itemRequest.result);
|
||||
itemRequest.onerror = () => resolve(undefined);
|
||||
});
|
||||
return result;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return undefined;
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function storeState<T>(name: string, state: T): Promise<void> {
|
||||
const db = await openDb();
|
||||
try {
|
||||
const putTransaction = db.transaction([storeName], "readwrite");
|
||||
const putStore = putTransaction.objectStore(storeName);
|
||||
const putRequest = putStore.put(state, name);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
putRequest.onsuccess = () => resolve();
|
||||
putRequest.onerror = () => reject(putRequest.error);
|
||||
});
|
||||
putTransaction.commit();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
58
src/lib/chord-editor/save-chords.ts
Normal file
58
src/lib/chord-editor/save-chords.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import type { EditorView } from "@codemirror/view";
|
||||
import { parser } from "./chords.grammar";
|
||||
import { parseCharaChords } from "./action-serializer";
|
||||
import { actionMetaPlugin } from "./action-meta-plugin";
|
||||
import { deviceChordField } from "./chord-sync-plugin";
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
|
||||
export interface SaveChordsTask {
|
||||
remove: number[][];
|
||||
set: [number[], number[]][];
|
||||
}
|
||||
|
||||
export function createSaveTask(view: EditorView): SaveChordsTask {
|
||||
const tree = parser.parse(view.state.doc.toString());
|
||||
const { ids, codes } = view.state.field(actionMetaPlugin.field);
|
||||
const deviceChords = view.state.field(deviceChordField);
|
||||
const result = parseCharaChords(tree, ids, codes, deviceChords, (from, to) =>
|
||||
view.state.doc.sliceString(from, to),
|
||||
);
|
||||
|
||||
return {
|
||||
remove: result.removed.map((chord) => chord[0]),
|
||||
set: result.chords
|
||||
.filter(
|
||||
(chord) =>
|
||||
!chord.disabled &&
|
||||
(!chord.phrase ||
|
||||
chord.phrase?.originalValue !== chord.phrase?.value),
|
||||
)
|
||||
.map((chord) => [chord.input?.value ?? [], chord.phrase?.value ?? []]),
|
||||
};
|
||||
}
|
||||
|
||||
export function applySaveTask(
|
||||
backup: CharaChordFile["chords"],
|
||||
task: SaveChordsTask,
|
||||
): CharaChordFile["chords"] {
|
||||
const newBackup = [...backup];
|
||||
for (const input of task.remove) {
|
||||
const index = newBackup.findIndex((chord) => {
|
||||
return JSON.stringify(chord[0]) === JSON.stringify(input);
|
||||
});
|
||||
if (index !== -1) {
|
||||
newBackup.splice(index, 1);
|
||||
}
|
||||
}
|
||||
for (const [input, phrase] of task.set) {
|
||||
const index = newBackup.findIndex((chord) => {
|
||||
return JSON.stringify(chord[0]) === JSON.stringify(input);
|
||||
});
|
||||
if (index !== -1) {
|
||||
newBackup[index] = [input, phrase];
|
||||
} else {
|
||||
newBackup.push([input, phrase]);
|
||||
}
|
||||
}
|
||||
return newBackup;
|
||||
}
|
||||
35
src/lib/chord-editor/store-state-field.ts
Normal file
35
src/lib/chord-editor/store-state-field.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { StateEffect, StateField } from "@codemirror/state";
|
||||
import { EditorView, ViewPlugin } from "@codemirror/view";
|
||||
import { get, type Readable } from "svelte/store";
|
||||
|
||||
export function reactiveStateField<T>(store: Readable<T>) {
|
||||
const effect = StateEffect.define<T>();
|
||||
const field = StateField.define<T>({
|
||||
create() {
|
||||
return get(store);
|
||||
},
|
||||
update(value, transaction) {
|
||||
return (
|
||||
transaction.effects.findLast((it) => it.is(effect))?.value ?? value
|
||||
);
|
||||
},
|
||||
});
|
||||
const plugin = ViewPlugin.fromClass(
|
||||
class {
|
||||
unsubscribe: () => void;
|
||||
|
||||
constructor(readonly view: EditorView) {
|
||||
this.unsubscribe = store.subscribe((value) => {
|
||||
setTimeout(() => {
|
||||
view.dispatch({ effects: effect.of(value) });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.unsubscribe();
|
||||
}
|
||||
},
|
||||
);
|
||||
return { field, plugin: [field, plugin] };
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
.=<LEFT_SHIFT> => =>
|
||||
;ims => <0x219><IMPULSE>
|
||||
-;<KSC_2C><LEFT_SHIFT> => <0x23e>_<0x23e>
|
||||
.;g => <0x23e>...<0x23e><LH_THUMB_3_3D>
|
||||
'dg => <0x23e>'<0x23e>
|
||||
'gl => <0x23e>'ll<0x23e>
|
||||
'ar => <0x23e>'re<0x23e>
|
||||
'gs => <0x23e>'s<0x23e>
|
||||
'ev => <0x23e>'ve<0x23e>
|
||||
<SPACE>-; => <0x23e><0x223>-<0x223><KSC_00>
|
||||
<SPACE>;<LEFT_SHIFT> => <0x23e><0x223><0x23d><0x223><KSC_00>
|
||||
<SPACE>;g => <0x23e><0x223><SPACE><0x223><KSC_00>
|
||||
deg => <0x23e>ed<0x23e>
|
||||
;gr => <0x23e>er<0x23e>
|
||||
;es => <0x23e>es<0x23e>
|
||||
;est => <0x23e>est<0x23e>
|
||||
a|.=<LEFT_SHIFT>=>t=t
|
||||
;ims=<0x219><IMPULSE>
|
||||
-;<KSC_2C><LEFT_SHIFT>=><0x23e>_<0x23e>
|
||||
.;g=><0x23e>...<0x23e><LH_THUMB_3_3D>
|
||||
'dg=><0x23e>'<0x23e>
|
||||
'gl=><0x23e>'ll<0x23e>
|
||||
'ar=><0x23e>'re<0x23e>
|
||||
'gs=><0x23e>'s<0x23e>
|
||||
'ev=><0x23e>'ve<0x23e>
|
||||
<SPACE>-;=><0x23e><0x223>-<0x223><KSC_00>
|
||||
<SPACE>;<LEFT_SHIFT>=><0x23e><0x223><0x23d><0x223><KSC_00>
|
||||
<SPACE>;g=><0x23e><0x223><SPACE><0x223><KSC_00>
|
||||
deg=><0x23e>ed<0x23e>
|
||||
;gr=><0x23e>er<0x23e>
|
||||
;es=><0x23e>es<0x23e>
|
||||
;est=><0x23e>est<0x23e>
|
||||
|
||||
@@ -8,10 +8,12 @@
|
||||
let {
|
||||
action,
|
||||
display,
|
||||
ignoreIcon = false,
|
||||
inText = false,
|
||||
}: {
|
||||
action: string | number | KeyInfo;
|
||||
display: "inline-keys" | "keys" | "verbose";
|
||||
ignoreIcon?: boolean;
|
||||
inText?: boolean;
|
||||
} = $props();
|
||||
|
||||
@@ -30,6 +32,7 @@
|
||||
? ({ code: 1024, id: action } satisfies KeyInfo)
|
||||
: action),
|
||||
);
|
||||
let icon = $derived(ignoreIcon ? undefined : info.icon);
|
||||
let dynamicMapping = $derived(info.keyCode && $osLayout.get(info.keyCode));
|
||||
let hasPopover = $derived(
|
||||
!retrievedInfo || !info.id || info.title || info.description,
|
||||
@@ -69,7 +72,7 @@
|
||||
|
||||
{#snippet kbdText()}
|
||||
{dynamicMapping ??
|
||||
info.icon ??
|
||||
icon ??
|
||||
info.display ??
|
||||
info.id ??
|
||||
`0x${info.code.toString(16)}`}
|
||||
@@ -77,7 +80,7 @@
|
||||
{#snippet kbdSnippet(withPopover = true)}
|
||||
<kbd
|
||||
class:in-text={inText}
|
||||
class:icon={!!info.icon}
|
||||
class:icon={!!icon}
|
||||
class:left={info.variant === "left"}
|
||||
class:right={info.variant === "right"}
|
||||
class:error={info.code > 1023}
|
||||
@@ -97,7 +100,7 @@
|
||||
class:left={info.variant === "left"}
|
||||
class:right={info.variant === "right"}>{dynamicMapping}</span
|
||||
>
|
||||
{:else if !info.icon && info.id?.length === 1}
|
||||
{:else if !icon && info.id?.length === 1}
|
||||
<span
|
||||
{@attach hasPopover ? actionTooltip(popover) : null}
|
||||
class:in-text={inText}
|
||||
@@ -112,7 +115,7 @@
|
||||
class:in-text={inText}
|
||||
class:left={info.variant === "left"}
|
||||
class:right={info.variant === "right"}
|
||||
class:icon={!!info.icon}
|
||||
class:icon={!!icon}
|
||||
class:warn={!retrievedInfo}
|
||||
class:error={info.code > 1023}
|
||||
{@attach hasPopover ? actionTooltip(popover) : null}
|
||||
@@ -161,21 +164,50 @@
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
$variant-offset: 12px;
|
||||
$variant-padding: calc(2px + $variant-offset);
|
||||
$variant-color: color-mix(
|
||||
in srgb,
|
||||
var(--md-sys-color-on-surface) 50%,
|
||||
transparent
|
||||
);
|
||||
|
||||
.left,
|
||||
.right {
|
||||
background-color: transparent;
|
||||
|
||||
&::before {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
outline: 2px dashed
|
||||
color-mix(in srgb, var(--bg-color), var(--md-sys-color-outline) 40%);
|
||||
outline-offset: -2px;
|
||||
border-radius: var(--border-radius);
|
||||
content: "";
|
||||
}
|
||||
}
|
||||
|
||||
$cutoff: 60%;
|
||||
|
||||
.left {
|
||||
padding-inline-end: $variant-padding;
|
||||
text-shadow: $variant-offset 0 2px $variant-color;
|
||||
background-image: linear-gradient(
|
||||
to right,
|
||||
var(--bg-color) $cutoff,
|
||||
transparent $cutoff
|
||||
);
|
||||
|
||||
&::before {
|
||||
clip-path: inset(0 0 0 $cutoff);
|
||||
}
|
||||
}
|
||||
.right {
|
||||
padding-inline-start: $variant-padding;
|
||||
text-shadow: -$variant-offset 0 2px $variant-color;
|
||||
background-image: linear-gradient(
|
||||
to left,
|
||||
var(--bg-color) $cutoff,
|
||||
transparent $cutoff
|
||||
);
|
||||
|
||||
&::before {
|
||||
clip-path: inset(0 $cutoff 0 0);
|
||||
}
|
||||
}
|
||||
|
||||
.inline-kbd {
|
||||
|
||||
@@ -14,18 +14,22 @@
|
||||
import type { KeymapCategory } from "$lib/meta/types/actions";
|
||||
import Action from "../Action.svelte";
|
||||
import { isVerbose } from "../verbose-action";
|
||||
import { actionToValue } from "$lib/chord-editor/action-serializer";
|
||||
import { actionToValue } from "$lib/chord-editor/chord-sync";
|
||||
|
||||
let {
|
||||
currentAction = undefined,
|
||||
nextAction = undefined,
|
||||
queryFilter = undefined,
|
||||
ignoreIcon,
|
||||
autofocus = false,
|
||||
onselect,
|
||||
onclose,
|
||||
}: {
|
||||
currentAction?: number;
|
||||
queryFilter?: string;
|
||||
nextAction?: number;
|
||||
autofocus?: boolean;
|
||||
ignoreIcon?: boolean;
|
||||
onselect?: (id: number) => void;
|
||||
onclose?: () => void;
|
||||
} = $props();
|
||||
@@ -43,6 +47,14 @@
|
||||
createIndex($KEYMAP_CODES);
|
||||
});
|
||||
|
||||
let didClear = true;
|
||||
$effect(() => {
|
||||
if (queryFilter !== undefined || !didClear) {
|
||||
searchBox.value = queryFilter ?? "";
|
||||
search();
|
||||
}
|
||||
});
|
||||
|
||||
async function createIndex(codes: Map<number, KeyInfo>) {
|
||||
for (const [, action] of codes) {
|
||||
await index?.addAsync(
|
||||
@@ -60,6 +72,7 @@
|
||||
(category) => [category, []] as [KeymapCategory, KeyInfo[]],
|
||||
),
|
||||
);
|
||||
didClear = searchBox.value === "";
|
||||
const result =
|
||||
searchBox.value === ""
|
||||
? Array.from($KEYMAP_CODES.keys())
|
||||
@@ -167,7 +180,7 @@
|
||||
<li>Action code is out of range</li>
|
||||
{/if}
|
||||
{/if}
|
||||
{#each results as [category, actions] (category)}
|
||||
{#each results as [category, actions] (actions)}
|
||||
{#if actions.length > 0}
|
||||
<div class="category">
|
||||
<h3>{category.name}</h3>
|
||||
@@ -191,7 +204,7 @@
|
||||
}
|
||||
: undefined}
|
||||
>
|
||||
<Action {action} display="verbose"></Action>
|
||||
<Action {action} display="verbose" {ignoreIcon}></Action>
|
||||
</button>
|
||||
{/each}
|
||||
</ul>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { compressActions, decompressActions } from "../serialization/actions";
|
||||
import type { KeyInfo } from "./keymap-codes";
|
||||
|
||||
export interface Chord {
|
||||
actions: number[];
|
||||
@@ -56,6 +57,103 @@ export function deserializeActions(native: bigint): number[] {
|
||||
return actions;
|
||||
}
|
||||
|
||||
const compoundHashItems = 3;
|
||||
const maxChordInputItems = 12;
|
||||
const actionBits = 10;
|
||||
const actionMask = (1 << actionBits) - 1;
|
||||
|
||||
/**
|
||||
* Applies the compound value to a **valid** chord input
|
||||
*/
|
||||
export function applyCompound(actions: number[], compound: number): number[] {
|
||||
const result = [...actions];
|
||||
for (let i = 0; i < compoundHashItems; i++) {
|
||||
result[i] = (compound >>> (i * actionBits)) & actionMask;
|
||||
}
|
||||
result[compoundHashItems] = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the compound value from a chord input, if present
|
||||
*/
|
||||
export function splitCompound(
|
||||
actions: number[],
|
||||
): [inputs: number[], compound: number | undefined] {
|
||||
if (actions[compoundHashItems] != 0) {
|
||||
return [
|
||||
actions.slice(
|
||||
Math.max(
|
||||
0,
|
||||
actions.findIndex((it) => it !== 0),
|
||||
),
|
||||
),
|
||||
undefined,
|
||||
];
|
||||
}
|
||||
|
||||
let compound = 0;
|
||||
for (let i = 0; i < compoundHashItems; i++) {
|
||||
compound |= (actions[i] ?? 0) << (i * actionBits);
|
||||
}
|
||||
|
||||
return [
|
||||
actions.slice(
|
||||
actions.findIndex((it, i) => i > compoundHashItems && it !== 0),
|
||||
),
|
||||
compound === 0 ? undefined : compound,
|
||||
];
|
||||
}
|
||||
|
||||
export function willBeValidChordInput(
|
||||
inputCount: number,
|
||||
hasCompound: boolean,
|
||||
): boolean {
|
||||
return (
|
||||
inputCount > 0 &&
|
||||
inputCount <= maxChordInputItems - (hasCompound ? compoundHashItems + 1 : 0)
|
||||
);
|
||||
}
|
||||
|
||||
const ACTION_JOIN = 574;
|
||||
const ACTION_KSC_00 = 256;
|
||||
|
||||
export function hasConcatenator(
|
||||
actions: number[],
|
||||
ids: Map<number, KeyInfo>,
|
||||
): boolean {
|
||||
const lastAction = actions.at(-1);
|
||||
for (const action of actions) {
|
||||
if (!ids.get(action)?.printable) {
|
||||
if (actions.length == 0) {
|
||||
return false;
|
||||
}
|
||||
return lastAction == ACTION_JOIN;
|
||||
}
|
||||
}
|
||||
return lastAction != ACTION_KSC_00;
|
||||
}
|
||||
|
||||
/**
|
||||
* Composes a chord input from a list of actions and an optional compound value
|
||||
* to a valid chord input
|
||||
*/
|
||||
export function composeChordInput(
|
||||
actions: number[],
|
||||
compound?: number,
|
||||
): number[] {
|
||||
const result = [
|
||||
...Array.from(
|
||||
{
|
||||
length: Math.max(0, maxChordInputItems - actions.length),
|
||||
},
|
||||
() => 0,
|
||||
),
|
||||
...actions.slice(0, maxChordInputItems).sort((a, b) => a - b),
|
||||
];
|
||||
return compound !== undefined ? applyCompound(result, compound) : result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes a chord input the same way as CCOS
|
||||
*/
|
||||
@@ -72,5 +170,6 @@ export function hashChord(actions: number[]) {
|
||||
if ((hash & 0xff) === 0xff) {
|
||||
hash ^= 0xff;
|
||||
}
|
||||
return hash & 0x3fff_ffff;
|
||||
hash &= 0x3fff_ffff;
|
||||
return hash === 0 ? 1 : hash;
|
||||
}
|
||||
|
||||
@@ -55,7 +55,10 @@ export const syncStatus: Writable<
|
||||
"done" | "error" | "downloading" | "uploading"
|
||||
> = writable("done");
|
||||
|
||||
export const deviceMeta = writable<VersionMeta | undefined>(undefined);
|
||||
export const deviceMeta = persistentWritable<VersionMeta | undefined>(
|
||||
"current-meta",
|
||||
undefined,
|
||||
);
|
||||
|
||||
export interface ProgressInfo {
|
||||
max: number;
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
kbd {
|
||||
display: inline-flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
margin-block: 6px;
|
||||
border-radius: 4px;
|
||||
|
||||
//border: 1px solid currentcolor;
|
||||
background: color-mix(
|
||||
--bg-color: color-mix(
|
||||
in srgb,
|
||||
var(--md-sys-color-surface-variant) 50%,
|
||||
transparent
|
||||
);
|
||||
padding: 4px;
|
||||
--border-radius: 4px;
|
||||
|
||||
display: inline-flex;
|
||||
position: relative;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
margin-block: 6px;
|
||||
border-radius: var(--border-radius);
|
||||
background: var(--bg-color);
|
||||
padding: 4px;
|
||||
height: 20px;
|
||||
color: currentcolor;
|
||||
font-weight: normal;
|
||||
|
||||
font-size: 14px;
|
||||
|
||||
&.icon {
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
import { persistentWritable } from "$lib/storage";
|
||||
import { derived } from "svelte/store";
|
||||
import { hashChord, type Chord } from "$lib/serial/chord";
|
||||
import {
|
||||
deviceChords,
|
||||
deviceLayout,
|
||||
deviceSettings,
|
||||
} from "$lib/serial/connection";
|
||||
import { KEYMAP_CODES } from "$lib/serial/keymap-codes";
|
||||
import { deviceLayout, deviceSettings } from "$lib/serial/connection";
|
||||
|
||||
export enum ChangeType {
|
||||
Layout,
|
||||
Chord,
|
||||
Setting,
|
||||
}
|
||||
|
||||
@@ -22,14 +15,6 @@ export interface LayoutChange {
|
||||
profile?: number;
|
||||
}
|
||||
|
||||
export interface ChordChange {
|
||||
type: ChangeType.Chord;
|
||||
deleted?: true;
|
||||
id: number[];
|
||||
actions: number[];
|
||||
phrase: number[];
|
||||
}
|
||||
|
||||
export interface SettingChange {
|
||||
type: ChangeType.Setting;
|
||||
id: number;
|
||||
@@ -42,20 +27,18 @@ export interface ChangeInfo {
|
||||
isCommitted?: boolean;
|
||||
}
|
||||
|
||||
export type Change = LayoutChange | ChordChange | SettingChange;
|
||||
export type Change = LayoutChange | SettingChange;
|
||||
|
||||
export const changes = persistentWritable<Change[][]>("changes", []);
|
||||
|
||||
export interface Overlay {
|
||||
layout: Array<Array<Map<number, number> | undefined> | undefined>;
|
||||
chords: Map<string, Chord & { deleted: boolean }>;
|
||||
settings: Array<Map<number, number> | undefined>;
|
||||
}
|
||||
|
||||
export const overlay = derived(changes, (changes) => {
|
||||
const overlay: Overlay = {
|
||||
layout: [],
|
||||
chords: new Map(),
|
||||
settings: [],
|
||||
};
|
||||
|
||||
@@ -71,13 +54,6 @@ export const overlay = derived(changes, (changes) => {
|
||||
change.action,
|
||||
);
|
||||
break;
|
||||
case ChangeType.Chord:
|
||||
overlay.chords.set(JSON.stringify(change.id), {
|
||||
actions: change.actions,
|
||||
phrase: change.phrase,
|
||||
deleted: change.deleted ?? false,
|
||||
});
|
||||
break;
|
||||
case ChangeType.Setting:
|
||||
change.profile ??= 0;
|
||||
overlay.settings[change.profile] ??= new Map();
|
||||
@@ -113,90 +89,3 @@ export const layout = derived([overlay, deviceLayout], ([overlay, profiles]) =>
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
export type ChordInfo = Chord &
|
||||
ChangeInfo & {
|
||||
phraseChanged: boolean;
|
||||
actionsChanged: boolean;
|
||||
sortBy: string;
|
||||
} & {
|
||||
id: number[];
|
||||
deleted: boolean;
|
||||
};
|
||||
export const chords = derived(
|
||||
[overlay, deviceChords, KEYMAP_CODES],
|
||||
([overlay, chords, codes]) => {
|
||||
const newChords = new Set(overlay.chords.keys());
|
||||
|
||||
const changedChords = chords.map<ChordInfo>((chord) => {
|
||||
const id = JSON.stringify(chord.actions);
|
||||
if (overlay.chords.has(id)) {
|
||||
newChords.delete(id);
|
||||
const changedChord = overlay.chords.get(id)!;
|
||||
return {
|
||||
id: chord.actions,
|
||||
// use the old phrase for stable editing
|
||||
sortBy: chord.phrase.map((it) => codes.get(it)?.id ?? it).join(),
|
||||
actions: changedChord.actions,
|
||||
phrase: changedChord.phrase,
|
||||
actionsChanged: id !== JSON.stringify(changedChord.actions),
|
||||
phraseChanged:
|
||||
JSON.stringify(chord.phrase) !==
|
||||
JSON.stringify(changedChord.phrase),
|
||||
isApplied: false,
|
||||
deleted: changedChord.deleted,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
id: chord.actions,
|
||||
sortBy: chord.phrase.map((it) => codes.get(it)?.id ?? it).join(),
|
||||
actions: chord.actions,
|
||||
phrase: chord.phrase,
|
||||
phraseChanged: false,
|
||||
actionsChanged: false,
|
||||
isApplied: true,
|
||||
deleted: false,
|
||||
};
|
||||
}
|
||||
});
|
||||
for (const id of newChords) {
|
||||
const chord = overlay.chords.get(id)!;
|
||||
changedChords.push({
|
||||
sortBy: "",
|
||||
isApplied: false,
|
||||
actionsChanged: true,
|
||||
phraseChanged: false,
|
||||
deleted: chord.deleted,
|
||||
id: JSON.parse(id),
|
||||
phrase: chord.phrase,
|
||||
actions: chord.actions,
|
||||
});
|
||||
}
|
||||
|
||||
return changedChords.sort(({ sortBy: a }, { sortBy: b }) =>
|
||||
a.localeCompare(b),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
export const duplicateChords = derived(chords, (chords) => {
|
||||
const duplicates = new Set<string>();
|
||||
const seen = new Set<string>();
|
||||
|
||||
for (const chord of chords) {
|
||||
const key = JSON.stringify(chord.actions);
|
||||
if (seen.has(key)) {
|
||||
duplicates.add(key);
|
||||
} else {
|
||||
seen.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
return duplicates;
|
||||
});
|
||||
|
||||
export const chordHashes = derived(
|
||||
chords,
|
||||
(chords) =>
|
||||
new Map(chords.map((chord) => [hashChord(chord.actions), chord] as const)),
|
||||
);
|
||||
|
||||
@@ -13,14 +13,7 @@
|
||||
|
||||
let isNavigating = $state(false);
|
||||
|
||||
const routeOrder = [
|
||||
"/config",
|
||||
"/learn",
|
||||
"/docs",
|
||||
"/editor",
|
||||
"/chat",
|
||||
"/plugin",
|
||||
];
|
||||
const routeOrder = ["/config", "/docs", "/editor", "/chat", "/plugin"];
|
||||
|
||||
function routeIndex(route: string | undefined): number {
|
||||
return routeOrder.findIndex((it) => route?.startsWith(it));
|
||||
|
||||
@@ -6,18 +6,15 @@
|
||||
layout,
|
||||
overlay,
|
||||
settings,
|
||||
duplicateChords,
|
||||
} from "$lib/undo-redo";
|
||||
import type { Change, ChordChange } from "$lib/undo-redo";
|
||||
import type { Change } from "$lib/undo-redo";
|
||||
import { fly } from "svelte/transition";
|
||||
import { actionTooltip } from "$lib/title";
|
||||
import {
|
||||
deviceChords,
|
||||
deviceLayout,
|
||||
deviceSettings,
|
||||
serialLog,
|
||||
serialPort,
|
||||
sync,
|
||||
syncProgress,
|
||||
syncStatus,
|
||||
} from "$lib/serial/connection";
|
||||
@@ -106,115 +103,7 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
async function safeDeleteChord(actions: number[]): Promise<boolean> {
|
||||
const port = $serialPort;
|
||||
if (!port) return false;
|
||||
try {
|
||||
await port.deleteChord({ actions });
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
try {
|
||||
if ((await port.getChordPhrase(actions)) === undefined) {
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async function saveChords(progress: () => void): Promise<boolean> {
|
||||
const port = $serialPort;
|
||||
if (!port) return false;
|
||||
let ok = true;
|
||||
|
||||
const empty = new Set<string>();
|
||||
for (const [id, chord] of $overlay.chords) {
|
||||
if (chord.actions.length === 0 || chord.phrase.length === 0) {
|
||||
empty.add(id);
|
||||
}
|
||||
}
|
||||
changes.update((changes) => {
|
||||
changes.push([
|
||||
...empty.keys().map(
|
||||
(id) =>
|
||||
({
|
||||
type: ChangeType.Chord,
|
||||
id: JSON.parse(id),
|
||||
deleted: true,
|
||||
actions: [],
|
||||
phrase: [],
|
||||
}) satisfies ChordChange,
|
||||
),
|
||||
]);
|
||||
return changes;
|
||||
});
|
||||
await tick();
|
||||
|
||||
const deleted = new Set<string>();
|
||||
const changed = new Map<string, number[]>();
|
||||
for (const [id, chord] of $overlay.chords) {
|
||||
if (!chord.deleted) continue;
|
||||
if (await safeDeleteChord(JSON.parse(id))) {
|
||||
deleted.add(id);
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
progress();
|
||||
}
|
||||
deviceChords.update((chords) =>
|
||||
chords.filter((chord) => !deleted.has(JSON.stringify(chord.actions))),
|
||||
);
|
||||
deleted.clear();
|
||||
await tick();
|
||||
|
||||
for (const [id, chord] of $overlay.chords) {
|
||||
if (chord.deleted) continue;
|
||||
if ($duplicateChords.has(JSON.stringify(chord.actions))) {
|
||||
ok = false;
|
||||
} else {
|
||||
let skip = false;
|
||||
if (id !== JSON.stringify(chord.actions)) {
|
||||
if (await safeDeleteChord(JSON.parse(id))) {
|
||||
deleted.add(id);
|
||||
} else {
|
||||
skip = true;
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
if (!skip) {
|
||||
try {
|
||||
await port.setChord({
|
||||
actions: chord.actions,
|
||||
phrase: chord.phrase,
|
||||
});
|
||||
deleted.add(JSON.stringify(chord.actions));
|
||||
changed.set(JSON.stringify(chord.actions), chord.phrase);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
ok = false;
|
||||
}
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
progress();
|
||||
}
|
||||
deviceChords.update((chords) => {
|
||||
chords.filter((chord) => !deleted.has(JSON.stringify(chord.actions)));
|
||||
for (const [id, phrase] of changed) {
|
||||
chords.push({ actions: JSON.parse(id), phrase });
|
||||
}
|
||||
return chords;
|
||||
});
|
||||
await tick();
|
||||
return ok;
|
||||
}
|
||||
|
||||
async function save() {
|
||||
let needsSync = false;
|
||||
try {
|
||||
const port = $serialPort;
|
||||
if (!port) {
|
||||
@@ -235,10 +124,8 @@
|
||||
(acc, profile) => acc + (profile?.size ?? 0),
|
||||
0,
|
||||
);
|
||||
const chordChanges = $overlay.chords.size;
|
||||
needsSync = chordChanges > 0;
|
||||
const needsCommit = settingChanges > 0 || layoutChanges > 0;
|
||||
const progressMax = layoutChanges + settingChanges + chordChanges;
|
||||
const progressMax = layoutChanges + settingChanges;
|
||||
|
||||
let progressCurrent = 0;
|
||||
|
||||
@@ -261,11 +148,9 @@
|
||||
layoutSuccess = false;
|
||||
}
|
||||
}
|
||||
let chordsSuccess = await saveChords(updateProgress);
|
||||
|
||||
if (layoutSuccess && settingsSuccess && chordsSuccess) {
|
||||
if (layoutSuccess && settingsSuccess) {
|
||||
changes.set([]);
|
||||
needsSync = true;
|
||||
} else {
|
||||
throw new Error("Some changes could not be saved.");
|
||||
}
|
||||
@@ -280,10 +165,6 @@
|
||||
} finally {
|
||||
$syncStatus = "done";
|
||||
}
|
||||
|
||||
if (needsSync) {
|
||||
await sync();
|
||||
}
|
||||
}
|
||||
|
||||
let progressPopover: HTMLElement | undefined = $state();
|
||||
|
||||
@@ -1,468 +1,280 @@
|
||||
<script lang="ts">
|
||||
import { KEYMAP_CODES, type KeyInfo } from "$lib/serial/keymap-codes";
|
||||
import FlexSearch, { type Index } from "flexsearch";
|
||||
import LL from "$i18n/i18n-svelte";
|
||||
import { actionTooltip } from "$lib/title";
|
||||
import { onDestroy, onMount, setContext, tick } from "svelte";
|
||||
import { changes, ChangeType, chords } from "$lib/undo-redo";
|
||||
import type { ChordChange, ChordInfo } from "$lib/undo-redo";
|
||||
import { derived, writable } from "svelte/store";
|
||||
import ChordEdit from "./ChordEdit.svelte";
|
||||
import { crossfade, fly } from "svelte/transition";
|
||||
import ChordActionEdit from "./ChordActionEdit.svelte";
|
||||
import { browser } from "$app/environment";
|
||||
import { expoOut } from "svelte/easing";
|
||||
import { osLayout } from "$lib/os-layout";
|
||||
import randomTips from "$lib/assets/random-tips/en.json";
|
||||
import { deviceMeta } from "$lib/serial/connection";
|
||||
import { restoreFromFile } from "$lib/backup/backup";
|
||||
import { EditorView } from "codemirror";
|
||||
import "$lib/chord-editor/chords.grammar";
|
||||
import { persistentWritable } from "$lib/storage";
|
||||
import ActionList from "$lib/components/layout/ActionList.svelte";
|
||||
import {
|
||||
createConfig,
|
||||
loadPersistentState,
|
||||
} from "$lib/chord-editor/persistent-state-plugin";
|
||||
import { parsedChordsField } from "$lib/chord-editor/parsed-chords-plugin";
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { deviceChords } from "$lib/serial/connection";
|
||||
import { editorSyncChords } from "$lib/chord-editor/chord-sync-plugin";
|
||||
|
||||
const resultSize = 38;
|
||||
let results: HTMLElement;
|
||||
const pageSize = writable(0);
|
||||
let resizeObserver: ResizeObserver;
|
||||
let queryFilter: string | undefined = $state(undefined);
|
||||
|
||||
let abortIndexing: (() => void) | undefined;
|
||||
let progress = $state(0);
|
||||
const rawCode = persistentWritable("chord-editor-raw-code", false);
|
||||
const showEdits = persistentWritable("chord-editor-show-edits", true);
|
||||
const denseSpacing = persistentWritable("chord-editor-spacing", false);
|
||||
|
||||
onMount(() => {
|
||||
resizeObserver = new ResizeObserver(() => {
|
||||
pageSize.set(Math.floor(results.clientHeight / resultSize));
|
||||
});
|
||||
pageSize.set(Math.floor(results.clientHeight / resultSize));
|
||||
resizeObserver.observe(results);
|
||||
});
|
||||
let editor: HTMLDivElement | undefined = $state(undefined);
|
||||
let view: EditorView | undefined = $state(undefined);
|
||||
|
||||
onDestroy(() => {
|
||||
resizeObserver?.disconnect();
|
||||
});
|
||||
|
||||
let index = new FlexSearch.Index();
|
||||
let searchIndex = writable<Index | undefined>(undefined);
|
||||
$effect(() => {
|
||||
abortIndexing?.();
|
||||
progress = 0;
|
||||
buildIndex($chords, $osLayout, $KEYMAP_CODES).then(searchIndex.set);
|
||||
});
|
||||
|
||||
function encodeChord(
|
||||
chord: ChordInfo,
|
||||
osLayout: Map<string, string>,
|
||||
codes: Map<number, KeyInfo>,
|
||||
onlyPhrase: boolean = false,
|
||||
) {
|
||||
const plainPhrase: string[] = [""];
|
||||
const tags = new Set<string>();
|
||||
const extraActions = new Set<string>();
|
||||
const extraCodes = new Set<string>();
|
||||
|
||||
for (const actionCode of chord.phrase ?? []) {
|
||||
const action = codes.get(actionCode);
|
||||
if (!action) {
|
||||
extraCodes.add(`0x${actionCode.toString(16)}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const osCode = action.keyCode && osLayout.get(action.keyCode);
|
||||
const token = osCode?.length === 1 ? osCode : action.display || action.id;
|
||||
if (!token) {
|
||||
extraCodes.add(`0x${action.code.toString(16)}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
(token === "SPACE" || /^\s$/.test(token)) &&
|
||||
plainPhrase.at(-1) !== ""
|
||||
) {
|
||||
plainPhrase.push("");
|
||||
} else if (token.length === 1) {
|
||||
plainPhrase[plainPhrase.length - 1] =
|
||||
plainPhrase[plainPhrase.length - 1] + token;
|
||||
} else {
|
||||
extraActions.add(token);
|
||||
}
|
||||
}
|
||||
|
||||
if (chord.phrase?.[0] === 298) {
|
||||
tags.add("suffix");
|
||||
}
|
||||
if (
|
||||
["ARROW_LT", "ARROW_RT", "ARROW_UP", "ARROW_DN"].some((it) =>
|
||||
extraActions.has(it),
|
||||
)
|
||||
) {
|
||||
tags.add("cursor warp");
|
||||
}
|
||||
if (
|
||||
["CTRL", "ALT", "GUI", "ENTER", "TAB"].some((it) => extraActions.has(it))
|
||||
) {
|
||||
tags.add("macro");
|
||||
}
|
||||
if (chord.actions[0] !== 0) {
|
||||
tags.add("compound");
|
||||
}
|
||||
|
||||
const input = chord.actions
|
||||
.slice(chord.actions.lastIndexOf(0) + 1)
|
||||
.map((it) => {
|
||||
const info = codes.get(it);
|
||||
if (!info) return `0x${it.toString(16)}`;
|
||||
const osCode = info.keyCode && osLayout.get(info.keyCode);
|
||||
const result = osCode?.length === 1 ? osCode : info.id;
|
||||
return result ?? `0x${it.toString(16)}`;
|
||||
});
|
||||
|
||||
if (onlyPhrase) {
|
||||
return plainPhrase.join(" ");
|
||||
}
|
||||
|
||||
return [
|
||||
...plainPhrase,
|
||||
`+${input.join("+")}`,
|
||||
...tags,
|
||||
...extraActions,
|
||||
...extraCodes,
|
||||
].join(" ");
|
||||
}
|
||||
|
||||
async function buildIndex(
|
||||
chords: ChordInfo[],
|
||||
osLayout: Map<string, string>,
|
||||
codes: Map<number, KeyInfo>,
|
||||
): Promise<Index> {
|
||||
if (chords.length === 0 || !browser) return index;
|
||||
|
||||
index = new FlexSearch.Index({
|
||||
tokenize: "full",
|
||||
encode(phrase: string) {
|
||||
return phrase.split(/\s+/).flatMap((it) => {
|
||||
if (/^[A-Z_]+$/.test(it)) {
|
||||
return it;
|
||||
}
|
||||
if (it.startsWith("+")) {
|
||||
return it
|
||||
.slice(1)
|
||||
.split("+")
|
||||
.map((it) => `+${it}`);
|
||||
}
|
||||
return it.toLowerCase();
|
||||
});
|
||||
if (!editor) return;
|
||||
const viewPromise = loadPersistentState({
|
||||
rawCode: $rawCode,
|
||||
storeName: "chord-editor-state-storage",
|
||||
autocomplete(query) {
|
||||
queryFilter = query;
|
||||
},
|
||||
});
|
||||
|
||||
let abort = false;
|
||||
abortIndexing = () => {
|
||||
abort = true;
|
||||
};
|
||||
|
||||
const batchSize = 200;
|
||||
const batches = Math.ceil(chords.length / batchSize);
|
||||
|
||||
for (let b = 0; b < batches; b++) {
|
||||
if (abort) return index;
|
||||
|
||||
const start = b * batchSize;
|
||||
const end = Math.min((b + 1) * batchSize, chords.length);
|
||||
const batch = chords.slice(start, end);
|
||||
|
||||
const promises = batch.map((chord, i) => {
|
||||
const chordIndex = start + i;
|
||||
progress = chordIndex + 1;
|
||||
|
||||
if ("phrase" in chord) {
|
||||
const encodedChord = encodeChord(chord, osLayout, codes);
|
||||
return index.addAsync(chordIndex, encodedChord);
|
||||
}
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
const searchFilter = writable<number[] | undefined>(undefined);
|
||||
let currentSearchQuery = $state("");
|
||||
|
||||
async function search(index: Index, event: Event) {
|
||||
const query = (event.target as HTMLInputElement).value;
|
||||
currentSearchQuery = query;
|
||||
searchFilter.set(
|
||||
query && searchIndex
|
||||
? ((await index.searchAsync(query)) as number[])
|
||||
: undefined,
|
||||
}).then(
|
||||
(state) =>
|
||||
new EditorView({
|
||||
parent: editor,
|
||||
state,
|
||||
}),
|
||||
);
|
||||
page = 0;
|
||||
}
|
||||
viewPromise.then((it) => (view = it));
|
||||
return () => viewPromise.then((it) => it.destroy());
|
||||
});
|
||||
|
||||
// Re-run search when chords change to fix stale indices
|
||||
$effect(() => {
|
||||
if (currentSearchQuery && $searchIndex) {
|
||||
search($searchIndex, { target: { value: currentSearchQuery } } as any);
|
||||
console.log("Syncing chords to editor");
|
||||
if (view) {
|
||||
editorSyncChords(
|
||||
view,
|
||||
$deviceChords.map((chord) => [chord.actions, chord.phrase] as const),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
function insertChord(actions: number[]) {
|
||||
const id = JSON.stringify(actions);
|
||||
if ($chords.some((it) => JSON.stringify(it.actions) === id)) {
|
||||
alert($LL.configure.chords.DUPLICATE());
|
||||
return;
|
||||
}
|
||||
changes.update((changes) => {
|
||||
changes.push([
|
||||
{
|
||||
type: ChangeType.Chord,
|
||||
id: actions,
|
||||
actions,
|
||||
phrase: [],
|
||||
},
|
||||
]);
|
||||
return changes;
|
||||
});
|
||||
}
|
||||
|
||||
function downloadVocabulary() {
|
||||
const vocabulary = new Set(
|
||||
$chords.map((it) =>
|
||||
"phrase" in it
|
||||
? encodeChord(it, $osLayout, $KEYMAP_CODES, true).trim()
|
||||
: "",
|
||||
function regenerate() {
|
||||
if (!view) return;
|
||||
view.setState(
|
||||
EditorState.create(
|
||||
createConfig({
|
||||
rawCode: $rawCode,
|
||||
storeName: "chord-editor-state-storage",
|
||||
autocomplete(query) {
|
||||
queryFilter = query;
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
vocabulary.delete("");
|
||||
const blob = new Blob([Array.from(vocabulary).join("|")], {
|
||||
type: "text/plain",
|
||||
editorSyncChords(
|
||||
view,
|
||||
$deviceChords.map((chord) => [chord.actions, chord.phrase] as const),
|
||||
);
|
||||
}
|
||||
|
||||
function downloadBackup() {
|
||||
if (!view) return;
|
||||
const backup: CharaChordFile = {
|
||||
charaVersion: 1,
|
||||
type: "chords",
|
||||
chords: view.state
|
||||
.field(parsedChordsField)
|
||||
.chords.map((chord) => [
|
||||
chord.input?.value ?? [],
|
||||
chord.phrase?.value ?? [],
|
||||
]),
|
||||
};
|
||||
const blob = new Blob([JSON.stringify(backup)], {
|
||||
type: "application/json",
|
||||
});
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = "vocabulary.txt";
|
||||
a.download = "chord-backup.json";
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
function clearChords() {
|
||||
changes.update((changes) => {
|
||||
changes.push(
|
||||
$chords.map<ChordChange>((it) => ({
|
||||
type: ChangeType.Chord,
|
||||
id: it.id,
|
||||
actions: it.actions,
|
||||
phrase: it.phrase,
|
||||
deleted: true,
|
||||
})),
|
||||
);
|
||||
return changes;
|
||||
});
|
||||
}
|
||||
|
||||
const items = derived(
|
||||
[searchFilter, chords],
|
||||
([filter, chords]) =>
|
||||
filter?.map((it) => [chords[it], it] as const) ??
|
||||
chords.map((it, i) => [it, i] as const),
|
||||
);
|
||||
const lastPage = derived(
|
||||
[items, pageSize],
|
||||
([items, pageSize]) => Math.ceil((items.length + 1) / pageSize) - 1,
|
||||
);
|
||||
|
||||
setContext("cursor-crossfade", crossfade({}));
|
||||
|
||||
let page = $state(0);
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Chord Manager - CharaChorder Device Manager</title>
|
||||
<meta name="description" content="Manage your chords" />
|
||||
</svelte:head>
|
||||
|
||||
<div class="search-container">
|
||||
<input
|
||||
type="search"
|
||||
placeholder={$LL.configure.chords.search.PLACEHOLDER(progress)}
|
||||
value={currentSearchQuery}
|
||||
oninput={(event) => $searchIndex && search($searchIndex, event)}
|
||||
class:loading={progress !== $chords.length}
|
||||
/>
|
||||
<div class="paginator">
|
||||
{#if $lastPage !== -1}
|
||||
{page + 1} / {$lastPage + 1}
|
||||
{:else}
|
||||
- / -
|
||||
{/if}
|
||||
<div class="vertical">
|
||||
<div style:display="flex">
|
||||
<label><input type="checkbox" bind:checked={$rawCode} />Edit as code</label>
|
||||
<!--<label><input type="checkbox" bind:checked={$showEdits} />Show edits</label>-->
|
||||
<label
|
||||
><input type="checkbox" bind:checked={$denseSpacing} />Dense Spacing</label
|
||||
>
|
||||
<button onclick={regenerate}>Reset</button>
|
||||
<!--<button onclick={largeFile}>Create Huge File</button>-->
|
||||
<button onclick={downloadBackup}>Download Backup</button>
|
||||
</div>
|
||||
|
||||
<div class="split">
|
||||
<div
|
||||
class="editor"
|
||||
class:hide-edits={!$showEdits}
|
||||
class:raw={$rawCode}
|
||||
class:dense-spacing={$denseSpacing}
|
||||
bind:this={editor}
|
||||
></div>
|
||||
<ActionList {queryFilter} ignoreIcon={$rawCode} />
|
||||
</div>
|
||||
<button
|
||||
class="icon"
|
||||
onclick={() => (page = Math.max(page - 1, 0))}
|
||||
{@attach actionTooltip("", "ctrl+left")}>navigate_before</button
|
||||
>
|
||||
<button
|
||||
class="icon"
|
||||
onclick={() => (page = Math.min(page + 1, $lastPage))}
|
||||
{@attach actionTooltip("", "ctrl+right")}>navigate_next</button
|
||||
>
|
||||
</div>
|
||||
|
||||
<section bind:this={results}>
|
||||
<!-- fixes some unresponsiveness -->
|
||||
{#await tick() then}
|
||||
<div class="results">
|
||||
<table transition:fly={{ y: 48, easing: expoOut }}>
|
||||
{#if $lastPage !== -1}
|
||||
<tbody>
|
||||
{#if page === 0}
|
||||
<tr
|
||||
><th class="new-chord"
|
||||
><ChordActionEdit
|
||||
onsubmit={(action) => insertChord(action)}
|
||||
/></th
|
||||
><td></td><td></td></tr
|
||||
>
|
||||
{/if}
|
||||
{#each $items.slice(page * $pageSize - (page === 0 ? 0 : 1), (page + 1) * $pageSize - 1) as [chord]}
|
||||
{#if chord}
|
||||
<ChordEdit {chord} onduplicate={() => (page = 0)} />
|
||||
{/if}
|
||||
{/each}</tbody
|
||||
>
|
||||
{:else}
|
||||
<caption>{$LL.configure.chords.search.NO_RESULTS()}</caption>
|
||||
{/if}
|
||||
</table>
|
||||
</div>
|
||||
<div class="sidebar">
|
||||
<textarea
|
||||
placeholder={$LL.configure.chords.TRY_TYPING() +
|
||||
"\n\nDid you know? " +
|
||||
randomTips[Math.floor(randomTips.length * Math.random())]}
|
||||
></textarea>
|
||||
<button onclick={clearChords}
|
||||
><span class="icon">delete_sweep</span>
|
||||
Clear Chords</button
|
||||
>
|
||||
<div>
|
||||
{#each Object.entries($deviceMeta?.factoryDefaults?.chords ?? {}) as [title, library]}
|
||||
<button onclick={() => restoreFromFile(library)}
|
||||
><span class="icon">library_add</span>{title}</button
|
||||
>
|
||||
{/each}
|
||||
</div>
|
||||
<button onclick={downloadVocabulary}
|
||||
><span class="icon">download</span>
|
||||
{$LL.configure.chords.VOCABULARY()}</button
|
||||
>
|
||||
</div>
|
||||
{/await}
|
||||
</section>
|
||||
|
||||
<style lang="scss">
|
||||
.search-container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.paginator {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
min-width: 8ch;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
.vertical {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
> button {
|
||||
padding-inline-start: 0;
|
||||
.split {
|
||||
display: flex;
|
||||
flex-grow: 1;
|
||||
flex-shrink: 1;
|
||||
width: calc(min(100%, 1400px));
|
||||
min-height: 0;
|
||||
|
||||
> :global(*) {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
|
||||
textarea {
|
||||
flex: 1;
|
||||
transition: outline-color 250ms ease;
|
||||
margin: 2px;
|
||||
outline: 2px solid transparent;
|
||||
outline-offset: -1px;
|
||||
border: 1px dashed var(--md-sys-color-outline);
|
||||
border-radius: 4px;
|
||||
background: none;
|
||||
padding: 8px;
|
||||
color: inherit;
|
||||
|
||||
&:focus {
|
||||
outline-color: var(--md-sys-color-primary);
|
||||
}
|
||||
.editor :global(.cm-deletedChunk) {
|
||||
opacity: 0.2;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0% {
|
||||
opacity: 0.4;
|
||||
}
|
||||
50% {
|
||||
opacity: 1;
|
||||
}
|
||||
100% {
|
||||
opacity: 0.4;
|
||||
}
|
||||
}
|
||||
|
||||
input[type="search"] {
|
||||
transition: all 250ms ease;
|
||||
margin-block-start: 16px;
|
||||
border: 0 solid var(--md-sys-color-surface-variant);
|
||||
border-bottom-width: 1px;
|
||||
|
||||
background: none;
|
||||
padding-inline: 16px;
|
||||
padding-block: 8px;
|
||||
width: 512px;
|
||||
color: inherit;
|
||||
|
||||
.editor {
|
||||
height: 100%;
|
||||
font-size: 16px;
|
||||
|
||||
@media (prefers-contrast: more) {
|
||||
border-style: dashed;
|
||||
border-color: var(--md-sys-color-outline);
|
||||
}
|
||||
|
||||
&::placeholder {
|
||||
opacity: 0.8;
|
||||
:global(.cm-tooltip) {
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
background-color: var(--md-sys-color-surface-variant);
|
||||
color: var(--md-sys-color-on-surface-variant);
|
||||
|
||||
:global(ul) {
|
||||
font-family: inherit !important;
|
||||
}
|
||||
|
||||
:global(li[role="option"][aria-selected="true"]) {
|
||||
border-radius: 4px;
|
||||
background-color: var(--md-sys-color-primary);
|
||||
color: var(--md-sys-color-on-primary);
|
||||
}
|
||||
|
||||
:global(completion-section) {
|
||||
margin-block: 8px;
|
||||
border-bottom: none !important;
|
||||
}
|
||||
}
|
||||
|
||||
&:focus {
|
||||
&:not(.raw) :global(.cm-line) {
|
||||
vertical-align: middle;
|
||||
columns: 2;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
&.dense-spacing :global(.cm-line) {
|
||||
padding-block: 0;
|
||||
}
|
||||
|
||||
:global(.cm-line) {
|
||||
padding-block: 8px;
|
||||
width: 100%;
|
||||
text-wrap: wrap;
|
||||
text-wrap-style: stable;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
|
||||
> :global(*) {
|
||||
break-before: avoid;
|
||||
break-after: avoid;
|
||||
break-inside: avoid;
|
||||
}
|
||||
}
|
||||
|
||||
:global(.cm-panels) {
|
||||
border-top: none;
|
||||
background-color: var(--md-sys-color-surface);
|
||||
color: var(--md-sys-color-on-surface);
|
||||
}
|
||||
|
||||
:global(.chord-ignored) {
|
||||
opacity: 0.5;
|
||||
background-image: none;
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
:global(.chord-child) {
|
||||
background-image: none;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
:global(.chord-invalid) {
|
||||
color: var(--md-sys-color-error);
|
||||
text-decoration-color: var(--md-sys-color-error);
|
||||
}
|
||||
|
||||
:global(.change-button) {
|
||||
height: 24px;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
:global(.cm-deletedLineGutter) {
|
||||
background-color: var(--md-sys-color-error);
|
||||
}
|
||||
|
||||
:global(.cm-changedLineGutter) {
|
||||
background-color: var(--md-sys-color-success);
|
||||
}
|
||||
|
||||
:global(.cm-changedText) {
|
||||
background: linear-gradient(
|
||||
var(--md-sys-color-primary),
|
||||
var(--md-sys-color-primary)
|
||||
)
|
||||
bottom / 100% 1px no-repeat;
|
||||
}
|
||||
|
||||
:global(.cm-gutters) {
|
||||
border-color: transparent;
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
&.raw :global(.cm-gutters) {
|
||||
border-color: var(--md-sys-color-surface-variant);
|
||||
background-color: var(--md-sys-color-surface);
|
||||
}
|
||||
|
||||
:global(.cm-editor) {
|
||||
outline: none;
|
||||
border-style: solid;
|
||||
border-color: var(--md-sys-color-primary);
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
&.loading {
|
||||
opacity: 0.4;
|
||||
:global(.cm-changedLine) {
|
||||
background-color: color-mix(
|
||||
in srgb,
|
||||
var(--md-sys-color-primary) 5%,
|
||||
transparent
|
||||
) !important;
|
||||
}
|
||||
}
|
||||
|
||||
section {
|
||||
display: flex;
|
||||
position: relative;
|
||||
:global(.cm-activeLine),
|
||||
:global(.cm-line:hover) {
|
||||
--auto-space-show: 1;
|
||||
}
|
||||
|
||||
border-radius: 16px;
|
||||
padding-inline: 8px;
|
||||
:global(.cm-activeLine) {
|
||||
border-bottom: 1px solid var(--md-sys-color-surface-variant);
|
||||
|
||||
height: 100%;
|
||||
&:not(.cm-changedLine) {
|
||||
background-color: transparent !important;
|
||||
}
|
||||
}
|
||||
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.results {
|
||||
min-width: min(90vw, 20cm);
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
table {
|
||||
transition: all 1s ease;
|
||||
height: fit-content;
|
||||
overflow-y: hidden;
|
||||
:global(::selection),
|
||||
:global(.cm-selectionBackground) {
|
||||
background-color: var(--md-sys-color-surface-variant) !important;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,240 +0,0 @@
|
||||
<script lang="ts">
|
||||
import type { ChordInfo } from "$lib/undo-redo";
|
||||
import { SvelteSet } from "svelte/reactivity";
|
||||
import { changes, chordHashes, ChangeType } from "$lib/undo-redo";
|
||||
import LL from "$i18n/i18n-svelte";
|
||||
import ActionString from "$lib/components/ActionString.svelte";
|
||||
import { selectAction } from "./action-selector";
|
||||
import { serialPort } from "$lib/serial/connection";
|
||||
import { get } from "svelte/store";
|
||||
import { inputToAction } from "./input-converter";
|
||||
import { hashChord, type Chord } from "$lib/serial/chord";
|
||||
|
||||
let {
|
||||
chord = undefined,
|
||||
onsubmit,
|
||||
interactive = true,
|
||||
}: {
|
||||
chord?: ChordInfo;
|
||||
interactive?: boolean;
|
||||
onsubmit: (actions: number[]) => void;
|
||||
} = $props();
|
||||
|
||||
let pressedKeys = new SvelteSet<number>();
|
||||
let editing = $state(false);
|
||||
|
||||
function compare(a: number, b: number) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
function makeChordInput(...actions: number[]) {
|
||||
const compound = compoundInputs[0]
|
||||
? hashChord(compoundInputs[0].actions)
|
||||
: 0;
|
||||
return [
|
||||
...Array.from(
|
||||
{
|
||||
length: 12 - actions.length,
|
||||
},
|
||||
(_, i) => (compound >> (i * 10)) & 0x3ff,
|
||||
),
|
||||
...actions.toSorted(compare),
|
||||
];
|
||||
}
|
||||
|
||||
function edit() {
|
||||
pressedKeys.clear();
|
||||
editing = true;
|
||||
}
|
||||
|
||||
function keydown(event: KeyboardEvent) {
|
||||
// This is obviously a tradeoff
|
||||
if (event.key === "Tab" || event.key === "Escape") return;
|
||||
if (!editing) return;
|
||||
event.preventDefault();
|
||||
const input = inputToAction(event, get(serialPort)?.device === "X");
|
||||
if (input == undefined) {
|
||||
alert("Invalid key");
|
||||
return;
|
||||
}
|
||||
pressedKeys.add(input);
|
||||
}
|
||||
|
||||
function keyup() {
|
||||
if (!editing) return;
|
||||
editing = false;
|
||||
if (pressedKeys.size < 1) return;
|
||||
if (!chord) return onsubmit(makeChordInput(...pressedKeys));
|
||||
changes.update((changes) => {
|
||||
changes.push([
|
||||
{
|
||||
type: ChangeType.Chord,
|
||||
id: chord!.id,
|
||||
actions: makeChordInput(...pressedKeys),
|
||||
phrase: chord!.phrase,
|
||||
},
|
||||
]);
|
||||
return changes;
|
||||
});
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function addSpecial(event: MouseEvent) {
|
||||
event.stopPropagation();
|
||||
selectAction(event, (action) => {
|
||||
if (!chord) return onsubmit([action]);
|
||||
changes.update((changes) => {
|
||||
changes.push([
|
||||
{
|
||||
type: ChangeType.Chord,
|
||||
id: chord!.id,
|
||||
actions: makeChordInput(...chordActions!, action),
|
||||
phrase: chord!.phrase,
|
||||
},
|
||||
]);
|
||||
return changes;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function* resolveCompound(chord?: ChordInfo) {
|
||||
if (!chord) return;
|
||||
let current: Chord = chord;
|
||||
for (let i = 0; i < 10; i++) {
|
||||
if (current.actions[3] !== 0) return;
|
||||
const compound = current.actions
|
||||
.slice(0, 3)
|
||||
.reduce((a, b, i) => a | (b << (i * 10)));
|
||||
if (compound === 0) return;
|
||||
const next = $chordHashes.get(compound);
|
||||
if (!next) {
|
||||
return null;
|
||||
}
|
||||
|
||||
current = next;
|
||||
yield next;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let chordActions = $derived(
|
||||
chord?.actions.slice(chord.actions.lastIndexOf(0) + 1).toSorted(compare),
|
||||
);
|
||||
let compoundInputs = $derived([...resolveCompound(chord)].reverse());
|
||||
</script>
|
||||
|
||||
<button
|
||||
class:deleted={chord && chord.deleted}
|
||||
class:edited={chord && chord.actionsChanged}
|
||||
class:invalid={chord &&
|
||||
chordActions &&
|
||||
(chordActions.length < 2 ||
|
||||
chordActions.some((it, i) => chordActions[i] !== it))}
|
||||
class="chord"
|
||||
onclick={edit}
|
||||
onkeydown={keydown}
|
||||
onkeyup={keyup}
|
||||
onblur={keyup}
|
||||
disabled={!interactive}
|
||||
>
|
||||
{#if editing && pressedKeys.size === 0}
|
||||
<span>{$LL.configure.chords.HOLD_KEYS()}</span>
|
||||
{:else if !editing && !chord}
|
||||
<span>{$LL.configure.chords.NEW_CHORD()}</span>
|
||||
{/if}
|
||||
{#if !editing}
|
||||
{#each compoundInputs as compound}
|
||||
<sub
|
||||
><ActionString
|
||||
display="keys"
|
||||
actions={compound.actions.slice(compound.actions.lastIndexOf(0) + 1)}
|
||||
></ActionString>
|
||||
</sub>
|
||||
<span>→</span>
|
||||
{/each}
|
||||
{/if}
|
||||
<ActionString
|
||||
display="keys"
|
||||
actions={editing ? [...pressedKeys].sort(compare) : (chordActions ?? [])}
|
||||
/>
|
||||
<sup>•</sup>
|
||||
<div role="button" class="icon add" onclick={addSpecial}>add_circle</div>
|
||||
</button>
|
||||
|
||||
<style lang="scss">
|
||||
span {
|
||||
opacity: 0.5;
|
||||
|
||||
@media (prefers-contrast: more) {
|
||||
opacity: 0.8;
|
||||
}
|
||||
}
|
||||
|
||||
sup {
|
||||
translate: 0 -60%;
|
||||
opacity: 0;
|
||||
transition: opacity 250ms ease;
|
||||
}
|
||||
|
||||
.add {
|
||||
opacity: 0;
|
||||
height: 20px;
|
||||
font-size: 18px;
|
||||
--icon-fill: 1;
|
||||
}
|
||||
|
||||
.chord:hover .add {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.chord {
|
||||
display: inline-flex;
|
||||
position: relative;
|
||||
gap: 4px;
|
||||
margin-inline: 4px;
|
||||
|
||||
height: 32px;
|
||||
|
||||
&:focus-within {
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
|
||||
.chord::after {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
transform-origin: center left;
|
||||
translate: -20px 0;
|
||||
scale: 0 1;
|
||||
|
||||
transition:
|
||||
scale 250ms ease,
|
||||
color 250ms ease;
|
||||
|
||||
background: currentcolor;
|
||||
|
||||
width: calc(100% - 60px);
|
||||
height: 1px;
|
||||
content: "";
|
||||
}
|
||||
|
||||
.edited {
|
||||
color: var(--md-sys-color-primary);
|
||||
|
||||
& > sup {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.invalid {
|
||||
color: var(--md-sys-color-error);
|
||||
}
|
||||
|
||||
.deleted {
|
||||
color: var(--md-sys-color-error);
|
||||
|
||||
&::after {
|
||||
scale: 1;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -1,172 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { changes, ChangeType, chords } from "$lib/undo-redo.js";
|
||||
import type { ChordInfo } from "$lib/undo-redo.js";
|
||||
import ChordPhraseEdit from "./ChordPhraseEdit.svelte";
|
||||
import ChordActionEdit from "./ChordActionEdit.svelte";
|
||||
import type { Chord } from "$lib/serial/chord";
|
||||
import { slide } from "svelte/transition";
|
||||
import { charaFileToUriComponent } from "$lib/share/share-url";
|
||||
import SharePopup from "../SharePopup.svelte";
|
||||
import tippy from "tippy.js";
|
||||
import { mount, unmount } from "svelte";
|
||||
|
||||
let { chord, onduplicate }: { chord: ChordInfo; onduplicate: () => void } =
|
||||
$props();
|
||||
|
||||
function remove() {
|
||||
changes.update((changes) => {
|
||||
changes.push([
|
||||
{
|
||||
type: ChangeType.Chord,
|
||||
id: chord.id,
|
||||
actions: chord.actions,
|
||||
phrase: chord.phrase,
|
||||
deleted: true,
|
||||
},
|
||||
]);
|
||||
return changes;
|
||||
});
|
||||
}
|
||||
|
||||
function isSameChord(a: Chord, b: Chord) {
|
||||
return (
|
||||
a.actions.length === b.actions.length &&
|
||||
a.actions.every((it, i) => it === b.actions[i])
|
||||
);
|
||||
}
|
||||
|
||||
function restore() {
|
||||
changes.update((changes) =>
|
||||
changes
|
||||
.map((it) =>
|
||||
it.filter(
|
||||
(it) => !(it.type === ChangeType.Chord && isSameChord(it, chord)),
|
||||
),
|
||||
)
|
||||
.filter((it) => it.length > 0),
|
||||
);
|
||||
}
|
||||
|
||||
function duplicate() {
|
||||
const id = [...chord.id];
|
||||
id.splice(id.indexOf(0), 1);
|
||||
id.push(0);
|
||||
while ($chords.some((it) => JSON.stringify(it.id) === JSON.stringify(id))) {
|
||||
id[id.length - 1] = id[id.length - 1]! + 1;
|
||||
}
|
||||
|
||||
changes.update((changes) => {
|
||||
changes.push([
|
||||
{
|
||||
type: ChangeType.Chord,
|
||||
id,
|
||||
actions: [...chord.actions],
|
||||
phrase: [...chord.phrase],
|
||||
},
|
||||
]);
|
||||
return changes;
|
||||
});
|
||||
|
||||
onduplicate();
|
||||
}
|
||||
|
||||
async function share(event: Event) {
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.set(
|
||||
"import",
|
||||
await charaFileToUriComponent({
|
||||
charaVersion: 1,
|
||||
type: "chords",
|
||||
chords: [[chord.actions, chord.phrase]],
|
||||
}),
|
||||
);
|
||||
await navigator.clipboard.writeText(url.toString());
|
||||
let shareComponent = {};
|
||||
tippy(event.target as HTMLElement, {
|
||||
onCreate(instance) {
|
||||
const target = instance.popper.querySelector(".tippy-content")!;
|
||||
shareComponent = mount(SharePopup, { target });
|
||||
},
|
||||
onHidden(instance) {
|
||||
instance.destroy();
|
||||
},
|
||||
onDestroy(_instance) {
|
||||
unmount(shareComponent);
|
||||
},
|
||||
}).show();
|
||||
}
|
||||
</script>
|
||||
|
||||
<tr>
|
||||
<th>
|
||||
<ChordActionEdit {chord} onsubmit={() => {}} />
|
||||
</th>
|
||||
<td class="phrase-edit">
|
||||
<ChordPhraseEdit {chord} />
|
||||
</td>
|
||||
<td>
|
||||
<div class="table-buttons">
|
||||
{#if !chord.deleted}
|
||||
<button transition:slide class="icon compact" onclick={remove}
|
||||
>delete</button
|
||||
>
|
||||
{:else}
|
||||
<button transition:slide class="icon compact" onclick={restore}
|
||||
>restore_from_trash</button
|
||||
>
|
||||
{/if}
|
||||
<button disabled={chord.deleted} class="icon compact" onclick={duplicate}
|
||||
>content_copy</button
|
||||
>
|
||||
<button
|
||||
class="icon compact"
|
||||
class:disabled={chord.isApplied}
|
||||
onclick={restore}>undo</button
|
||||
>
|
||||
<div class="separator"></div>
|
||||
<button class="icon compact" onclick={share}>share</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<style lang="scss">
|
||||
.separator {
|
||||
display: inline-flex;
|
||||
|
||||
opacity: 0.2;
|
||||
background: currentcolor;
|
||||
|
||||
width: 1px;
|
||||
height: 24px;
|
||||
}
|
||||
|
||||
button {
|
||||
transition: opacity 75ms ease;
|
||||
}
|
||||
|
||||
.phrase-edit {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
tr {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.table-buttons {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 0;
|
||||
transform: translate(100%, -50%);
|
||||
opacity: 0;
|
||||
transition: opacity 75ms ease;
|
||||
background: var(--md-sys-color-surface-variant);
|
||||
}
|
||||
|
||||
.icon {
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
tr:hover .table-buttons {
|
||||
opacity: 1;
|
||||
}
|
||||
</style>
|
||||
@@ -1,399 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { KEYMAP_CODES, KEYMAP_IDS } from "$lib/serial/keymap-codes";
|
||||
import { onMount, tick } from "svelte";
|
||||
import { changes, ChangeType } from "$lib/undo-redo";
|
||||
import type { ChordInfo } from "$lib/undo-redo";
|
||||
import { scale } from "svelte/transition";
|
||||
import { selectAction } from "./action-selector";
|
||||
import { inputToAction } from "./input-converter";
|
||||
import { deviceMeta, serialPort } from "$lib/serial/connection";
|
||||
import { get } from "svelte/store";
|
||||
import semverGte from "semver/functions/gte";
|
||||
import Action from "$lib/components/Action.svelte";
|
||||
import AutospaceSelector from "$lib/chord-editor/AutospaceSelector.svelte";
|
||||
|
||||
let { chord }: { chord: ChordInfo } = $props();
|
||||
|
||||
const JOIN_ACTION = 574;
|
||||
const NO_CONCATENATOR_ACTION = 256;
|
||||
|
||||
onMount(() => {
|
||||
if (chord.phrase.length === 0) {
|
||||
box?.focus();
|
||||
}
|
||||
});
|
||||
|
||||
function keypress(event: KeyboardEvent) {
|
||||
console.log(event);
|
||||
if (!event.shiftKey && event.key === "ArrowUp") {
|
||||
addSpecial(event);
|
||||
} else if (!event.shiftKey && event.key === "ArrowLeft") {
|
||||
moveCursor(cursorPosition - 1, true);
|
||||
} else if (!event.shiftKey && event.key === "ArrowRight") {
|
||||
moveCursor(cursorPosition + 1, true);
|
||||
} else if (event.key === " " && $KEYMAP_IDS.has("HYPERSPACE")) {
|
||||
insertAction(cursorPosition, $KEYMAP_IDS.get("HYPERSPACE")!.code);
|
||||
tick().then(() => moveCursor(cursorPosition + 1));
|
||||
} else if (event.key === "Backspace") {
|
||||
deleteAction(cursorPosition - 1, 1, true);
|
||||
moveCursor(cursorPosition - 1, true);
|
||||
} else if (event.key === "Delete") {
|
||||
deleteAction(cursorPosition, 1, true);
|
||||
} else {
|
||||
if (event.key === "Shift" || event.key === "Meta") return;
|
||||
const action = inputToAction(event, get(serialPort)?.device === "X");
|
||||
if (action !== undefined) {
|
||||
insertAction(cursorPosition, action);
|
||||
tick().then(() => moveCursor(cursorPosition + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function moveCursor(to: number, user = false) {
|
||||
if (!box) return;
|
||||
cursorPosition = Math.max(
|
||||
user ? chord.phrase.findIndex((it, i, arr) => !isHidden(it, i, arr)) : 0,
|
||||
Math.min(
|
||||
to,
|
||||
user
|
||||
? chord.phrase.findLastIndex((it, i, arr) => !isHidden(it, i, arr)) +
|
||||
1 || chord.phrase.length
|
||||
: chord.phrase.length,
|
||||
),
|
||||
);
|
||||
const item = box.children.item(cursorPosition) as HTMLElement;
|
||||
cursorOffset = item.offsetLeft + item.offsetWidth;
|
||||
}
|
||||
|
||||
function deleteAction(at: number, count = 1, user = false) {
|
||||
if (user && isHidden(chord.phrase[at]!, at, chord.phrase)) return;
|
||||
if (!(at in chord.phrase)) return;
|
||||
changes.update((changes) => {
|
||||
changes.push([
|
||||
{
|
||||
type: ChangeType.Chord,
|
||||
id: chord.id,
|
||||
actions: chord.actions,
|
||||
phrase: chord.phrase.toSpliced(at, count),
|
||||
},
|
||||
]);
|
||||
return changes;
|
||||
});
|
||||
}
|
||||
|
||||
function insertAction(at: number, action: number) {
|
||||
changes.update((changes) => {
|
||||
changes.push([
|
||||
{
|
||||
type: ChangeType.Chord,
|
||||
id: chord.id,
|
||||
actions: chord.actions,
|
||||
phrase: chord.phrase.toSpliced(at, 0, action),
|
||||
},
|
||||
]);
|
||||
return changes;
|
||||
});
|
||||
}
|
||||
|
||||
function clickCursor(event: MouseEvent) {
|
||||
if (box === undefined || event.target === button) return;
|
||||
const distance = (event as unknown as { layerX: number }).layerX;
|
||||
|
||||
let i = 0;
|
||||
for (const child of box.children) {
|
||||
const { offsetLeft, offsetWidth } = child as HTMLElement;
|
||||
if (distance < offsetLeft + offsetWidth / 2) {
|
||||
moveCursor(i - 1, true);
|
||||
return;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
moveCursor(i - 1, true);
|
||||
}
|
||||
|
||||
function addSpecial(event: MouseEvent | KeyboardEvent) {
|
||||
selectAction(
|
||||
event,
|
||||
(action) => {
|
||||
insertAction(cursorPosition, action);
|
||||
tick().then(() => moveCursor(cursorPosition + 1));
|
||||
},
|
||||
() => box?.focus(),
|
||||
);
|
||||
}
|
||||
|
||||
function resolveAutospace(autospace: boolean) {
|
||||
if (autospace) {
|
||||
if (chord.phrase.at(-1) === JOIN_ACTION) {
|
||||
if (
|
||||
chord.phrase.every(
|
||||
(action, i, arr) =>
|
||||
$KEYMAP_CODES.get(action)?.printable || i === arr.length - 1,
|
||||
)
|
||||
) {
|
||||
deleteAction(chord.phrase.length - 1);
|
||||
moveCursor(cursorPosition, true);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (isPrintable) {
|
||||
return;
|
||||
} else if (chord.phrase.at(-1) === NO_CONCATENATOR_ACTION) {
|
||||
deleteAction(chord.phrase.length - 1);
|
||||
moveCursor(cursorPosition, true);
|
||||
} else {
|
||||
insertAction(chord.phrase.length, JOIN_ACTION);
|
||||
moveCursor(cursorPosition, true);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (chord.phrase.at(-1) === JOIN_ACTION) {
|
||||
deleteAction(chord.phrase.length - 1);
|
||||
moveCursor(cursorPosition, true);
|
||||
} else {
|
||||
if (chord.phrase.at(-1) === NO_CONCATENATOR_ACTION) {
|
||||
if (
|
||||
chord.phrase.every(
|
||||
(action, i, arr) =>
|
||||
$KEYMAP_CODES.get(action)?.printable || i === arr.length - 1,
|
||||
)
|
||||
) {
|
||||
return;
|
||||
} else {
|
||||
deleteAction(chord.phrase.length - 1);
|
||||
moveCursor(cursorPosition, true);
|
||||
}
|
||||
} else {
|
||||
insertAction(chord.phrase.length, NO_CONCATENATOR_ACTION);
|
||||
moveCursor(cursorPosition, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let button: HTMLButtonElement | undefined = $state();
|
||||
let box: HTMLDivElement | undefined = $state();
|
||||
let cursorPosition = 0;
|
||||
let cursorOffset = $state(0);
|
||||
|
||||
let hasFocus = $state(false);
|
||||
|
||||
let isPrintable = $derived(
|
||||
chord.phrase.every(
|
||||
(action) => $KEYMAP_CODES.get(action)?.printable === true,
|
||||
),
|
||||
);
|
||||
let supportsAutospace = $derived(
|
||||
semverGte($deviceMeta?.version ?? "0.0.0", "2.1.0"),
|
||||
);
|
||||
let supportsAutospaceV2 = $derived(
|
||||
semverGte($deviceMeta?.version ?? "0.0.0", "3.0.0-gamma.5"),
|
||||
);
|
||||
let hasAutospace = $derived(
|
||||
supportsAutospaceV2
|
||||
? chord.phrase.at(-1) !== NO_CONCATENATOR_ACTION
|
||||
: isPrintable || chord.phrase.at(-1) === JOIN_ACTION,
|
||||
);
|
||||
|
||||
function isHidden(action: number, index: number, array: number[]) {
|
||||
return (
|
||||
(index === 0 && action === JOIN_ACTION) ||
|
||||
(index === array.length - 1 &&
|
||||
(action === JOIN_ACTION || action === NO_CONCATENATOR_ACTION))
|
||||
);
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- svelte-ignore a11y_interactive_supports_focus -->
|
||||
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
||||
<div
|
||||
role="textbox"
|
||||
class="wrapper"
|
||||
class:edited={!chord.deleted && chord.phraseChanged}
|
||||
onclick={() => {
|
||||
box?.focus();
|
||||
}}
|
||||
>
|
||||
{#if supportsAutospace}
|
||||
<AutospaceSelector
|
||||
variant="start"
|
||||
value={chord.phrase[0] === JOIN_ACTION}
|
||||
onchange={async (event) => {
|
||||
const autospace = hasAutospace;
|
||||
if ((event.target as HTMLInputElement).checked) {
|
||||
if (chord.phrase[0] === JOIN_ACTION) {
|
||||
deleteAction(0, 1);
|
||||
await tick();
|
||||
moveCursor(cursorPosition - 1, true);
|
||||
}
|
||||
} else {
|
||||
if (chord.phrase[0] !== JOIN_ACTION) {
|
||||
insertAction(0, JOIN_ACTION);
|
||||
moveCursor(cursorPosition + 1, true);
|
||||
}
|
||||
}
|
||||
if (!supportsAutospaceV2) {
|
||||
await tick();
|
||||
resolveAutospace(autospace);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{/if}
|
||||
<div
|
||||
onkeydown={keypress}
|
||||
onmousedown={clickCursor}
|
||||
role="textbox"
|
||||
tabindex="0"
|
||||
bind:this={box}
|
||||
onfocusin={() => (hasFocus = true)}
|
||||
onfocusout={(event) => {
|
||||
if (event.relatedTarget !== button) hasFocus = false;
|
||||
}}
|
||||
>
|
||||
{#if hasFocus}
|
||||
<div transition:scale class="cursor" style:translate="{cursorOffset}px 0">
|
||||
<button class="icon" bind:this={button} onclick={addSpecial}>add</button
|
||||
>
|
||||
</div>
|
||||
{:else}
|
||||
<div></div>
|
||||
<!-- placeholder for cursor placement -->
|
||||
{/if}
|
||||
{#each chord.phrase as action, i}
|
||||
{#if isHidden(action, i, chord.phrase)}
|
||||
<span style:display="none"></span>
|
||||
{:else}
|
||||
<Action display="inline-keys" {action} />
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
{#if supportsAutospace}
|
||||
<AutospaceSelector
|
||||
variant="end"
|
||||
value={!hasAutospace}
|
||||
onchange={async (event) => {
|
||||
if (supportsAutospaceV2) {
|
||||
if ((event.target as HTMLInputElement).checked) {
|
||||
if (chord.phrase.at(-1) === NO_CONCATENATOR_ACTION) {
|
||||
deleteAction(chord.phrase.length - 1);
|
||||
await tick();
|
||||
moveCursor(cursorPosition, true);
|
||||
}
|
||||
} else {
|
||||
if (chord.phrase.at(-1) !== NO_CONCATENATOR_ACTION) {
|
||||
insertAction(chord.phrase.length, NO_CONCATENATOR_ACTION);
|
||||
moveCursor(cursorPosition, true);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resolveAutospace((event.target as HTMLInputElement).checked);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{/if}
|
||||
<sup>•</sup>
|
||||
</div>
|
||||
|
||||
<style lang="scss">
|
||||
sup {
|
||||
translate: 0 -40%;
|
||||
opacity: 0;
|
||||
transition: opacity 250ms ease;
|
||||
}
|
||||
|
||||
.cursor {
|
||||
position: absolute;
|
||||
transform: translateX(-50%);
|
||||
translate: 0 0;
|
||||
|
||||
transition: translate 50ms ease;
|
||||
|
||||
background: var(--md-sys-color-on-secondary-container);
|
||||
|
||||
width: 2px;
|
||||
height: 100%;
|
||||
|
||||
button {
|
||||
position: absolute;
|
||||
top: -24px;
|
||||
left: 0;
|
||||
border: 2px solid currentcolor;
|
||||
border-radius: 12px 12px 12px 0;
|
||||
|
||||
background: var(--md-sys-color-secondary-container);
|
||||
padding: 0;
|
||||
|
||||
height: 24px;
|
||||
|
||||
color: var(--md-sys-color-on-secondary-container);
|
||||
}
|
||||
}
|
||||
|
||||
.edited {
|
||||
color: var(--md-sys-color-primary);
|
||||
|
||||
sup {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.wrapper {
|
||||
display: flex;
|
||||
|
||||
position: relative;
|
||||
align-items: center;
|
||||
|
||||
padding-block: 4px;
|
||||
|
||||
height: 1em;
|
||||
|
||||
&::after,
|
||||
&::before {
|
||||
position: absolute;
|
||||
bottom: -4px;
|
||||
|
||||
opacity: 0;
|
||||
|
||||
transition:
|
||||
opacity 150ms ease,
|
||||
scale 250ms ease;
|
||||
background: currentcolor;
|
||||
|
||||
width: calc(100% - 8px);
|
||||
height: 1px;
|
||||
content: "";
|
||||
}
|
||||
|
||||
&::after {
|
||||
scale: 0 1;
|
||||
transition-duration: 250ms;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
--auto-space-show: 1;
|
||||
|
||||
&::before {
|
||||
opacity: 0.3;
|
||||
}
|
||||
}
|
||||
|
||||
&:has(> :focus-within)::after {
|
||||
scale: 1;
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
[role="textbox"] {
|
||||
display: flex;
|
||||
|
||||
position: relative;
|
||||
align-items: center;
|
||||
cursor: text;
|
||||
white-space: pre;
|
||||
|
||||
&:focus-within {
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -1,56 +0,0 @@
|
||||
import ActionSelector from "$lib/components/layout/ActionSelector.svelte";
|
||||
import { mount, unmount, tick } from "svelte";
|
||||
|
||||
export function selectAction(
|
||||
event: MouseEvent | KeyboardEvent,
|
||||
select: (action: number) => void,
|
||||
dismissed?: () => void,
|
||||
) {
|
||||
const component = mount(ActionSelector, {
|
||||
target: document.body,
|
||||
props: {
|
||||
onclose: () => closed(),
|
||||
onselect: (action: number) => {
|
||||
select(action);
|
||||
closed();
|
||||
},
|
||||
},
|
||||
});
|
||||
const dialog = document.querySelector("dialog > div") as HTMLDivElement;
|
||||
const backdrop = document.querySelector("dialog") as HTMLDialogElement;
|
||||
const dialogRect = dialog.getBoundingClientRect();
|
||||
const groupRect = (event.target as HTMLElement).getBoundingClientRect();
|
||||
|
||||
const scale = 0.5;
|
||||
const dialogScale = `${
|
||||
1 - scale * (1 - groupRect.width / dialogRect.width)
|
||||
} ${1 - scale * (1 - groupRect.height / dialogRect.height)}`;
|
||||
const dialogTranslate = `${scale * (groupRect.x - dialogRect.x)}px ${
|
||||
scale * (groupRect.y - dialogRect.y)
|
||||
}px`;
|
||||
|
||||
const duration = 150;
|
||||
const options = { duration, easing: "ease" };
|
||||
const dialogAnimation = dialog.animate(
|
||||
[
|
||||
{ scale: dialogScale, translate: dialogTranslate },
|
||||
{ translate: "0 0", scale: "1" },
|
||||
],
|
||||
options,
|
||||
);
|
||||
const backdropAnimation = backdrop.animate(
|
||||
[{ opacity: 0 }, { opacity: 1 }],
|
||||
options,
|
||||
);
|
||||
|
||||
async function closed() {
|
||||
dialogAnimation.reverse();
|
||||
backdropAnimation.reverse();
|
||||
|
||||
await dialogAnimation.finished;
|
||||
|
||||
unmount(component);
|
||||
await tick();
|
||||
dismissed?.();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { KEYMAP_IDS, KEYMAP_KEYCODES } from "$lib/serial/keymap-codes";
|
||||
import { get } from "svelte/store";
|
||||
|
||||
export function inputToAction(
|
||||
event: KeyboardEvent,
|
||||
useKeycodes?: boolean,
|
||||
): number | undefined {
|
||||
if (useKeycodes) {
|
||||
return get(KEYMAP_KEYCODES).get(event.code);
|
||||
} else {
|
||||
return (
|
||||
get(KEYMAP_IDS).get(event.key)?.code ??
|
||||
get(KEYMAP_KEYCODES).get(event.code)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { serializeActions } from "$lib/serial/chord";
|
||||
import { chords } from "$lib/undo-redo";
|
||||
import ChordEdit from "../ChordEdit.svelte";
|
||||
|
||||
export function hashChord(actions: number[]) {
|
||||
const chord = new Uint8Array(16);
|
||||
@@ -17,7 +15,8 @@
|
||||
}
|
||||
|
||||
const broken = $derived(
|
||||
$chords.filter((it) => (hashChord(it.actions) & 0xff) === 0xff),
|
||||
[],
|
||||
// $chords.filter((it) => (hashChord(it.actions) & 0xff) === 0xff),
|
||||
);
|
||||
</script>
|
||||
|
||||
@@ -35,7 +34,7 @@
|
||||
>, your library might have been corrupted.
|
||||
</p>
|
||||
{#each broken as chord}
|
||||
<ChordEdit {chord} onduplicate={() => {}} />
|
||||
<!--<ChordEdit {chord} onduplicate={() => {}} />-->
|
||||
{/each}
|
||||
{:else}
|
||||
<p>No problematic chords found</p>
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { defineConfig } from "vitest/config";
|
||||
import { svelte } from "@sveltejs/vite-plugin-svelte";
|
||||
import { sveltekit } from "@sveltejs/kit/vite";
|
||||
import { lezerGrammarPlugin } from "./vite-plugin-lezer";
|
||||
import { layoutPlugin } from "./vite-plugin-layout";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [svelte({ hot: !process.env.VITEST })],
|
||||
plugins: [layoutPlugin(), sveltekit(), lezerGrammarPlugin()],
|
||||
test: {
|
||||
globals: true,
|
||||
environment: "jsdom",
|
||||
|
||||
Reference in New Issue
Block a user