feat: cv2

This commit is contained in:
2026-01-09 21:33:41 +01:00
parent 4bc84b5399
commit 156825a194
10 changed files with 474 additions and 116 deletions

View File

@@ -1,25 +1,33 @@
import { import { type KeyInfo } from "$lib/serial/keymap-codes";
KEYMAP_CODES,
KEYMAP_IDS,
type KeyInfo,
} from "$lib/serial/keymap-codes";
import { syntaxTree } from "@codemirror/language"; import { syntaxTree } from "@codemirror/language";
import { linter, type Diagnostic } from "@codemirror/lint"; import { linter, type Diagnostic } from "@codemirror/lint";
import { derived, get } from "svelte/store"; import { parsedChordsField } from "./parsed-chords-plugin";
import { parseCharaChords } from "./action-serializer"; import { actionMetaPlugin } from "./action-meta-plugin";
import { deviceChords } from "$lib/serial/connection";
export const actionLinterDependencies = derived( export function actionLinter(config?: Parameters<typeof linter>[1]) {
[KEYMAP_IDS, KEYMAP_CODES, deviceChords], const finalConfig: Parameters<typeof linter>[1] = {
(it) => it, ...config,
); needsRefresh(update) {
console.log(
export const actionLinter = linter( "test",
(view) => { update.startState.field(actionMetaPlugin.field) !==
update.state.field(actionMetaPlugin.field),
update.startState.field(parsedChordsField) !==
update.state.field(parsedChordsField),
);
return (
update.startState.field(actionMetaPlugin.field) !==
update.state.field(actionMetaPlugin.field) ||
update.startState.field(parsedChordsField) !==
update.state.field(parsedChordsField)
);
},
};
return linter((view) => {
console.log("lint");
const diagnostics: Diagnostic[] = []; const diagnostics: Diagnostic[] = [];
const [ids, codes, deviceChords] = get(actionLinterDependencies); const { ids, codes } = view.state.field(actionMetaPlugin.field);
const { meta, compoundInputs } = view.state.field(parsedChordsField);
const { meta, compoundInputs } = parseCharaChords(view.state, ids);
syntaxTree(view.state) syntaxTree(view.state)
.cursor() .cursor()
@@ -150,7 +158,7 @@ export const actionLinter = linter(
if (m.emptyPhrase) { if (m.emptyPhrase) {
diagnostics.push({ diagnostics.push({
from: m.from, from: m.from,
to: m.to, to: m.from,
severity: "warning", severity: "warning",
message: `Chord phrase is empty`, message: `Chord phrase is empty`,
}); });
@@ -158,7 +166,7 @@ export const actionLinter = linter(
if (m.overriddenBy) { if (m.overriddenBy) {
diagnostics.push({ diagnostics.push({
from: m.from, from: m.from,
to: m.to, to: m.from,
severity: "warning", severity: "warning",
message: `Chord overridden by previous chord`, message: `Chord overridden by previous chord`,
}); });
@@ -166,7 +174,7 @@ export const actionLinter = linter(
if (m.orphan) { if (m.orphan) {
diagnostics.push({ diagnostics.push({
from: m.from, from: m.from,
to: m.to, to: m.from,
severity: "warning", severity: "warning",
message: `Orphan compound chord`, message: `Orphan compound chord`,
}); });
@@ -183,7 +191,7 @@ export const actionLinter = linter(
if ((m.overrides?.length ?? 0) > 0) { if ((m.overrides?.length ?? 0) > 0) {
diagnostics.push({ diagnostics.push({
from: m.from, from: m.from,
to: m.to, to: m.from,
severity: "info", severity: "info",
message: `Chord overrides other chords`, message: `Chord overrides other chords`,
}); });
@@ -191,6 +199,5 @@ export const actionLinter = linter(
} }
return diagnostics; return diagnostics;
}, }, finalConfig);
{ delay: 100 }, }
);

View 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);

View File

@@ -12,28 +12,28 @@ import type { Range } from "@codemirror/state";
export class ActionWidget extends WidgetType { export class ActionWidget extends WidgetType {
component?: {}; component?: {};
element?: HTMLElement;
constructor(readonly id: string | number) { constructor(readonly id: string | number) {
super(); super();
this.id = id; this.id = id;
} }
override eq(other: ActionWidget) { /*override eq(other: ActionWidget) {
return this.id == other.id; return this.id == other.id;
} }*/
toDOM() { toDOM() {
if (!this.element) { if (this.component) {
this.element = document.createElement("span"); unmount(this.component);
this.element.style.paddingInline = "2px"; }
const element = document.createElement("span");
element.style.paddingInline = "2px";
this.component = mount(Action, { this.component = mount(Action, {
target: this.element, target: element,
props: { action: this.id, display: "keys", inText: true }, props: { action: this.id, display: "keys", inText: true },
}); });
} return element;
return this.element;
} }
override ignoreEvent() { override ignoreEvent() {

View File

@@ -10,6 +10,7 @@ import type { Update } from "@codemirror/collab";
import { get } from "svelte/store"; import { get } from "svelte/store";
import { import {
composeChordInput, composeChordInput,
hasConcatenator,
hashChord, hashChord,
splitCompound, splitCompound,
willBeValidChordInput, willBeValidChordInput,
@@ -33,6 +34,7 @@ export function actionToValue(action: number | KeyInfo) {
export interface ParseMeta { export interface ParseMeta {
from: number; from: number;
to: number; to: number;
hasConcatenator: boolean;
invalidActions?: true; invalidActions?: true;
invalidInput?: true; invalidInput?: true;
emptyPhrase?: true; emptyPhrase?: true;
@@ -51,11 +53,14 @@ export interface ParseResult {
export function parseCharaChords( export function parseCharaChords(
data: EditorState, data: EditorState,
ids: Map<string, KeyInfo>, ids: Map<string, KeyInfo>,
codes: Map<number, KeyInfo>,
): ParseResult { ): ParseResult {
console.time("parseCharaChords");
const chords: CharaChordFile["chords"] = []; const chords: CharaChordFile["chords"] = [];
const metas: ParseMeta[] = []; const metas: ParseMeta[] = [];
const keys = new Map<string, number>(); const keys = new Map<string, number>();
const compoundInputs = new Map<number, string>(); const compoundInputs = new Map<number, string>();
const orphanCompounds = new Set<number>();
let currentChord: CharaChordFile["chords"][number] | undefined = undefined; let currentChord: CharaChordFile["chords"][number] | undefined = undefined;
let compound: number | undefined = undefined; let compound: number | undefined = undefined;
@@ -64,15 +69,16 @@ export function parseCharaChords(
let invalidInput = false; let invalidInput = false;
let chordFrom = 0; let chordFrom = 0;
function makeChordInput(node: SyntaxNodeRef): number[] { const makeChordInput = (node: SyntaxNodeRef): number[] => {
invalidInput ||= !willBeValidChordInput(currentActions.length, !!compound); invalidInput ||= !willBeValidChordInput(currentActions.length, !!compound);
const input = composeChordInput(currentActions, compound); const input = composeChordInput(currentActions, compound);
compound = hashChord(input); compound = hashChord(input);
if (!compoundInputs.has(compound)) { if (!compoundInputs.has(compound)) {
compoundInputs.set(compound, data.doc.sliceString(chordFrom, node.from)); compoundInputs.set(compound, data.doc.sliceString(chordFrom, node.from));
orphanCompounds.add(compound);
} }
return input; return input;
} };
syntaxTree(data) syntaxTree(data)
.cursor() .cursor()
@@ -125,7 +131,11 @@ export function parseCharaChords(
currentChord[1] = currentActions; currentChord[1] = currentActions;
const index = chords.length; const index = chords.length;
chords.push(currentChord); chords.push(currentChord);
const meta: ParseMeta = { from: node.from, to: node.to }; const meta: ParseMeta = {
from: node.from,
to: node.to,
hasConcatenator: hasConcatenator(currentChord[1], codes),
};
if (invalidActions) { if (invalidActions) {
meta.invalidActions = true; meta.invalidActions = true;
} }
@@ -161,18 +171,25 @@ export function parseCharaChords(
makeChordInput(node); makeChordInput(node);
} else if (node.name === "PhraseDelim") { } else if (node.name === "PhraseDelim") {
const input = makeChordInput(node); const input = makeChordInput(node);
currentChord = [composeChordInput(input, compound), []]; orphanCompounds.delete(hashChord(input));
currentChord = [input, []];
} }
}, },
); );
for (let i = 0; i < metas.length; i++) { for (let i = 0; i < metas.length; i++) {
const [, compound] = splitCompound(chords[i]![0]); const [, compound] = splitCompound(chords[i]![0]);
if (compound !== undefined && !compoundInputs.has(compound)) { if (
compound !== undefined &&
(!compoundInputs.has(compound) || orphanCompounds.has(compound))
) {
metas[i]!.orphan = true; metas[i]!.orphan = true;
} }
} }
console.timeEnd("parseCharaChords");
console.log(chords.length);
return { result: chords, meta: metas, compoundInputs }; return { result: chords, meta: metas, compoundInputs };
} }

View File

@@ -0,0 +1,25 @@
import type { CharaChordFile } from "$lib/share/chara-file";
import { StateEffect, StateField } from "@codemirror/state";
export const chordSyncEffect = StateEffect.define<CharaChordFile["chords"]>();
export const deviceChordField = StateField.define<CharaChordFile["chords"]>({
create() {
return [];
},
update(value, transaction) {
return (
transaction.effects.findLast((it) => it.is(chordSyncEffect))?.value ??
value
);
},
compare(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
},
toJSON(value) {
return value;
},
fromJSON(value) {
return value;
},
});

View File

@@ -0,0 +1,102 @@
import {
ChangeDesc,
StateEffect,
StateField,
type Extension,
} from "@codemirror/state";
import { parseCharaChords, type ParseResult } from "./action-serializer";
import { type KeyInfo } from "$lib/serial/keymap-codes";
import { actionMetaPlugin } from "./action-meta-plugin";
import { EditorView, ViewPlugin, ViewUpdate } from "@codemirror/view";
import type { Tree } from "@lezer/common";
import { syntaxParserRunning, syntaxTree } from "@codemirror/language";
import { debounceTime, Subject } from "rxjs";
import { forceLinting } from "@codemirror/lint";
function mapParseResult(value: ParseResult, change: ChangeDesc): ParseResult {
if (change.empty) return value;
if (
value.meta.every(
(it) =>
change.mapPos(it.to) === it.to && change.mapPos(it.from) === it.from,
)
)
return value;
return {
result: value.result,
meta: value.meta.map((it) => ({
...it,
from: change.mapPos(it.from),
to: change.mapPos(it.to),
})),
compoundInputs: value.compoundInputs,
};
}
export const parsedChordsEffect = StateEffect.define<ParseResult>({
map: mapParseResult,
});
export const parsedChordsField = StateField.define<ParseResult>({
create() {
return { compoundInputs: new Map(), meta: [], result: [] };
},
update(value, transaction) {
return (
transaction.effects.findLast((it) => it.is(parsedChordsEffect))?.value ??
mapParseResult(value, transaction.changes)
);
},
});
export function parsedChordsPlugin(debounce = 200): Extension {
const plugin = ViewPlugin.fromClass(
class {
tree: Tree;
ids: Map<string, KeyInfo>;
codes: Map<number, KeyInfo>;
needsUpdate = new Subject<void>();
subscription = this.needsUpdate
.pipe(debounceTime(debounce))
.subscribe(() => {
if (syntaxParserRunning(this.view)) {
this.needsUpdate.next();
return;
}
requestIdleCallback(() => {
this.view.dispatch({
effects: parsedChordsEffect.of(
parseCharaChords(this.view.state, this.ids, this.codes),
),
});
forceLinting(this.view);
});
});
constructor(readonly view: EditorView) {
this.tree = syntaxTree(view.state);
this.ids = view.state.field(actionMetaPlugin.field).ids;
this.codes = view.state.field(actionMetaPlugin.field).codes;
this.needsUpdate.next();
}
update(update: ViewUpdate) {
const tree = syntaxTree(update.state);
const ids = update.state.field(actionMetaPlugin.field).ids;
const codes = update.state.field(actionMetaPlugin.field).codes;
if (tree !== this.tree || ids !== this.ids || codes !== this.codes) {
this.tree = tree;
this.ids = ids;
this.codes = codes;
this.needsUpdate.next();
}
}
destroy() {
this.subscription.unsubscribe();
}
},
);
return [parsedChordsField, plugin];
}

View File

@@ -0,0 +1,122 @@
import {
EditorView,
highlightActiveLine,
keymap,
lineNumbers,
ViewPlugin,
ViewUpdate,
} from "@codemirror/view";
import {
history,
historyField,
historyKeymap,
standardKeymap,
} from "@codemirror/commands";
import { debounceTime, 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 { parsedChordsPlugin } from "./parsed-chords-plugin";
const serializedFields = {
history: historyField,
deviceChords: deviceChordField,
};
export interface EditorConfig {
rawCode?: boolean;
storeName: string;
autocomplete(query: string | undefined): void;
}
export function loadPersistentState(params: EditorConfig): EditorState {
const stored = localStorage.getItem(params.storeName);
const config = {
extensions: [
actionMetaPlugin.plugin,
deviceChordField,
parsedChordsPlugin(),
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]),
],
} satisfies EditorStateConfig;
if (stored) {
try {
const parsed = JSON.parse(stored);
return EditorState.fromJSON(parsed, 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))
.subscribe(() => {
localStorage.setItem(
storeName,
JSON.stringify(this.view.state.toJSON(serializedFields)),
);
});
constructor(readonly view: EditorView) {}
update(update: ViewUpdate) {
if (update.state !== update.startState) {
this.updateSubject.next();
}
}
destroy() {
this.subscription.unsubscribe();
}
},
);
}

View 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] };
}

View File

@@ -1,4 +1,5 @@
import { compressActions, decompressActions } from "../serialization/actions"; import { compressActions, decompressActions } from "../serialization/actions";
import type { KeyInfo } from "./keymap-codes";
export interface Chord { export interface Chord {
actions: number[]; actions: number[];
@@ -114,6 +115,25 @@ export function willBeValidChordInput(
); );
} }
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 * Composes a chord input from a list of actions and an optional compound value
* to a valid chord input * to a valid chord input

View File

@@ -2,26 +2,13 @@
import { chords } from "$lib/undo-redo"; import { chords } from "$lib/undo-redo";
import { EditorView } from "codemirror"; import { EditorView } from "codemirror";
import { actionToValue } from "$lib/chord-editor/action-serializer"; import { actionToValue } from "$lib/chord-editor/action-serializer";
import { actionPlugin } from "$lib/chord-editor/action-plugin";
import { delimPlugin } from "$lib/chord-editor/chord-delim-plugin";
import { highlightActiveLine, keymap } from "@codemirror/view";
import { history, standardKeymap } from "@codemirror/commands";
import "$lib/chord-editor/chords.grammar"; import "$lib/chord-editor/chords.grammar";
import {
chordHighlightStyle,
chordLanguageSupport,
} from "$lib/chord-editor/chords-grammar-plugin";
import { syntaxHighlighting } from "@codemirror/language";
import { persistentWritable } from "$lib/storage"; import { persistentWritable } from "$lib/storage";
import ActionList from "$lib/components/layout/ActionList.svelte"; import ActionList from "$lib/components/layout/ActionList.svelte";
import { actionAutocompletePlugin } from "$lib/chord-editor/autocomplete";
import {
actionLinter,
actionLinterDependencies,
} from "$lib/chord-editor/action-linter";
import { forceLinting } from "@codemirror/lint";
import { untrack } from "svelte";
import { splitCompound } from "$lib/serial/chord"; import { splitCompound } from "$lib/serial/chord";
import { 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";
let queryFilter: string | undefined = $state(undefined); let queryFilter: string | undefined = $state(undefined);
@@ -29,8 +16,26 @@
const showEdits = persistentWritable("chord-editor-show-edits", true); const showEdits = persistentWritable("chord-editor-show-edits", true);
const denseSpacing = persistentWritable("chord-editor-spacing", false); const denseSpacing = persistentWritable("chord-editor-spacing", false);
let originalDoc = $derived( let editor: HTMLDivElement | undefined = $state(undefined);
$chords let view: EditorView;
$effect(() => {
if (!editor) return;
view = new EditorView({
parent: editor,
state: loadPersistentState({
rawCode: $rawCode,
storeName: "chord-editor-state-storage",
autocomplete(query) {
queryFilter = query;
},
}),
});
return () => view.destroy();
});
function regenerate() {
const doc = $chords
.map((chord) => { .map((chord) => {
const [actions, compound] = splitCompound(chord.actions); const [actions, compound] = splitCompound(chord.actions);
return ( return (
@@ -42,63 +47,78 @@
chord.phrase.map((it) => actionToValue(it)).join("") chord.phrase.map((it) => actionToValue(it)).join("")
); );
}) })
.join("\n"), .join("\n");
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: doc },
});
}
function loadBackup(event: Event) {
const input = event.target as HTMLInputElement;
if (!input.files || input.files.length === 0) return;
const file = input.files[0];
const reader = new FileReader();
reader.onload = (e) => {
try {
const content = e.target?.result as string;
const backup: CharaChordFile = JSON.parse(content);
const doc = backup.chords
.map((chord) => {
const [actions, compound] = splitCompound(chord[0]);
return (
(compound
? "<0x" + compound.toString(16).padStart(8, "0") + ">"
: "") +
actions.map((it) => actionToValue(it)).join("") +
"=>" +
chord[1].map((it) => actionToValue(it)).join("")
); );
let editor: HTMLDivElement | undefined = $state(undefined); })
let view: EditorView; .join("\n");
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: doc },
});
} catch (err) {
alert("Failed to load backup: " + err);
}
};
reader.readAsText(file);
}
$effect(() => { function downloadBackup() {
if (!editor) return; const backup: CharaChordFile = {
view = new EditorView({ charaVersion: 1,
parent: editor, type: "chords",
doc: originalDoc, chords: view.state.field(parsedChordsField).result,
extensions: [ };
...($rawCode ? [] : [delimPlugin, actionPlugin]), console.log(JSON.stringify(backup));
chordLanguageSupport(), const blob = new Blob([JSON.stringify(backup)], {
actionLinter, type: "application/json",
// lineNumbers(),
actionAutocompletePlugin((query) => {
queryFilter = query;
}),
history(),
syntaxHighlighting(chordHighlightStyle),
highlightActiveLine(),
// drawSelection(),
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": {
width: "100%",
},
".cm-cursor": {
borderColor: "var(--md-sys-color-on-surface)",
},
}),
keymap.of(standardKeymap),
],
});
return () => view.destroy();
});
$effect(() => {
$actionLinterDependencies;
untrack(() => view && forceLinting(view));
}); });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "chord-backup.json";
a.click();
URL.revokeObjectURL(url);
}
</script> </script>
<label><input type="checkbox" bind:checked={$rawCode} />Edit as code</label> <div style:display="flex">
<!--<label><input type="checkbox" bind:checked={$showEdits} />Show edits</label>--> <label><input type="checkbox" bind:checked={$rawCode} />Edit as code</label>
<label <!--<label><input type="checkbox" bind:checked={$showEdits} />Show edits</label>-->
<label
><input type="checkbox" bind:checked={$denseSpacing} />Dense Spacing</label ><input type="checkbox" bind:checked={$denseSpacing} />Dense Spacing</label
> >
<button onclick={regenerate}>Regenerate from current chords</button>
<button onclick={downloadBackup}>Download Backup</button>
<input
type="file"
accept="application/json"
onchange={loadBackup}
style="margin-left: 1rem"
/>
</div>
<div class="split"> <div class="split">
<div <div
@@ -115,10 +135,11 @@
.split { .split {
display: flex; display: flex;
gap: 1rem; gap: 1rem;
width: calc(min(100%, 1400px));
height: 100%; height: 100%;
> :global(:last-child) { > :global(*) {
width: min(600px, 30vw); flex: 1;
} }
} }
@@ -127,7 +148,6 @@
} }
.editor { .editor {
width: min(600px, 30vw);
height: 100%; height: 100%;
font-size: 16px; font-size: 16px;