mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-02-16 06:02:41 +00:00
Compare commits
5 Commits
v2.7.0
...
7d7c432bb2
| Author | SHA1 | Date | |
|---|---|---|---|
|
7d7c432bb2
|
|||
|
a3bf7ac1d5
|
|||
|
6ceb3994e7
|
|||
|
156825a194
|
|||
|
4bc84b5399
|
34
package.json
34
package.json
@@ -35,25 +35,27 @@
|
||||
},
|
||||
"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/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",
|
||||
@@ -76,29 +78,29 @@
|
||||
"matrix-js-sdk": "^37.12.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"
|
||||
}
|
||||
|
||||
808
pnpm-lock.yaml
generated
808
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
135
src/lib/chord-editor/action-linter.ts
Normal file
135
src/lib/chord-editor/action-linter.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
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`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
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,308 @@
|
||||
import { KEYMAP_CODES, type KeyInfo } from "$lib/serial/keymap-codes";
|
||||
import type { CharaChordFile } from "$lib/share/chara-file";
|
||||
import { syntaxTree } from "@codemirror/language";
|
||||
import type { EditorState } from "@codemirror/state";
|
||||
import { get } from "svelte/store";
|
||||
import {
|
||||
composeChordInput,
|
||||
hasConcatenator,
|
||||
hashChord,
|
||||
willBeValidChordInput,
|
||||
} from "$lib/serial/chord";
|
||||
import type {
|
||||
ActionMeta,
|
||||
ChordMeta,
|
||||
MetaRange,
|
||||
ParseResult,
|
||||
} from "./parse-meta";
|
||||
|
||||
export function canUseIdAsString(info: KeyInfo): boolean {
|
||||
return !!info.id && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(info.id);
|
||||
return !!info.id && /^[^>\n]+$/.test(info.id);
|
||||
}
|
||||
|
||||
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;
|
||||
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}>`;
|
||||
}
|
||||
|
||||
export function parseChordMeta(
|
||||
data: EditorState,
|
||||
ids: Map<string, KeyInfo>,
|
||||
codes: Map<number, KeyInfo>,
|
||||
): ChordMeta[] {
|
||||
console.time("parseChordTree");
|
||||
const result: ChordMeta[] = [];
|
||||
|
||||
let current: ChordMeta = { range: [0, 0], valid: false };
|
||||
let actions: ActionMeta[] = [];
|
||||
let actionRange: MetaRange | undefined = undefined;
|
||||
|
||||
syntaxTree(data)
|
||||
.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 = data.doc.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 = data.doc.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;
|
||||
}
|
||||
|
||||
function resolveChordOverrides(chords: 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");
|
||||
}
|
||||
|
||||
function resolveChordAliases(chords: 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 aliasList of aliases.values()) {
|
||||
if (aliasList.length > 1) {
|
||||
for (const info of aliasList) {
|
||||
info.aliases = aliasList.filter((i) => i !== info);
|
||||
}
|
||||
}
|
||||
}
|
||||
console.timeEnd("resolveAliases");
|
||||
}
|
||||
|
||||
function resolveCompoundParents(chords: 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");
|
||||
}
|
||||
|
||||
export function resolveChanges(
|
||||
chords: ChordMeta[],
|
||||
deviceChords: CharaChordFile["chords"],
|
||||
): CharaChordFile["chords"] {
|
||||
console.time("resolveChanges");
|
||||
const removed: CharaChordFile["chords"] = [];
|
||||
const info = new Map<string, ChordMeta>();
|
||||
for (const chord of chords) {
|
||||
if (chord.input && chord.phrase && !chord.disabled) {
|
||||
info.set(
|
||||
JSON.stringify([chord.input.value, chord.phrase?.value ?? []]),
|
||||
chord,
|
||||
);
|
||||
info.set(JSON.stringify(chord.input.value), chord);
|
||||
}
|
||||
}
|
||||
for (const deviceChord of deviceChords) {
|
||||
const exact = info.get(JSON.stringify(deviceChord));
|
||||
if (exact) {
|
||||
exact.phrase!.originalValue = exact.phrase!.value;
|
||||
continue;
|
||||
}
|
||||
const byInput = info.get(JSON.stringify(deviceChord[0]));
|
||||
if (byInput) {
|
||||
byInput.phrase!.originalValue = deviceChord[1];
|
||||
continue;
|
||||
}
|
||||
removed.push(deviceChord);
|
||||
}
|
||||
|
||||
console.timeEnd("resolveChanges");
|
||||
return removed;
|
||||
}
|
||||
|
||||
export function parseCharaChords(
|
||||
data: EditorState,
|
||||
ids: Map<string, KeyInfo>,
|
||||
codes: Map<number, KeyInfo>,
|
||||
deviceChords: CharaChordFile["chords"],
|
||||
): ParseResult {
|
||||
console.time("parseTotal");
|
||||
|
||||
const chords = parseChordMeta(data, ids, codes);
|
||||
resolveChordOverrides(chords);
|
||||
resolveChordAliases(chords);
|
||||
resolveCompoundParents(chords);
|
||||
const removed = resolveChanges(chords, deviceChords);
|
||||
|
||||
/*for (let i = 0; i < metas.length; i++) {
|
||||
const [, compound] = splitCompound(chords[i]![0]);
|
||||
if (
|
||||
compound !== undefined &&
|
||||
(!compoundInputs.has(compound) || orphanCompounds.has(compound))
|
||||
) {
|
||||
metas[i]!.orphan = true;
|
||||
}
|
||||
}
|
||||
|
||||
const removed: CharaChordFile["chords"] = [];
|
||||
for (let deviceChord of deviceChords) {
|
||||
const key = JSON.stringify(deviceChord[0]);
|
||||
if (!keys.has(key)) {
|
||||
removed.push(deviceChord);
|
||||
} else {
|
||||
const index = keys.get(key)!;
|
||||
const meta = metas[index]!;
|
||||
if (
|
||||
JSON.stringify(deviceChord[1]) !==
|
||||
JSON.stringify(chords[keys.get(key)!]![1])
|
||||
) {
|
||||
meta.originalPhrase = deviceChord[1];
|
||||
} else {
|
||||
meta.unchanged = true;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
console.timeEnd("parseTotal");
|
||||
|
||||
console.log(chords);
|
||||
return { chords, removed };
|
||||
}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
23
src/lib/chord-editor/changes-decorations-plugin.ts
Normal file
23
src/lib/chord-editor/changes-decorations-plugin.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { EditorView, Decoration, type DecorationSet } from "@codemirror/view";
|
||||
import { StateField } from "@codemirror/state";
|
||||
import { parsedChordsEffect } from "./parsed-chords-plugin";
|
||||
|
||||
const changedMark = Decoration.mark({ class: "cm-changed" });
|
||||
|
||||
const chordMetaMark = StateField.define<DecorationSet>({
|
||||
create() {
|
||||
return Decoration.none;
|
||||
},
|
||||
update(decorations, tr) {
|
||||
const newChords = tr.effects.findLast((e) => e.is(parsedChordsEffect));
|
||||
if (!newChords) {
|
||||
return decorations.map(tr.changes);
|
||||
}
|
||||
return newChords.value.meta.map(meta => {
|
||||
if (meta.originalPhrase) {
|
||||
return underlineMark.range(meta.from, meta.to);
|
||||
}
|
||||
});
|
||||
},
|
||||
provide: (f) => EditorView.decorations.from(f),
|
||||
});
|
||||
39
src/lib/chord-editor/changes-panel.ts
Normal file
39
src/lib/chord-editor/changes-panel.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { EditorState } from "@codemirror/state";
|
||||
import { EditorView, showPanel, type Panel } from "@codemirror/view";
|
||||
import { parsedChordsField } from "./parsed-chords-plugin";
|
||||
|
||||
function getChanges(state: EditorState): string {
|
||||
const parsed = state.field(parsedChordsField);
|
||||
const added = parsed.chords.reduce(
|
||||
(acc, chord) =>
|
||||
acc + (chord.phrase && chord.phrase.originalValue === undefined ? 1 : 0),
|
||||
0,
|
||||
);
|
||||
const changed = parsed.chords.reduce(
|
||||
(acc, chord) =>
|
||||
acc +
|
||||
(chord.phrase &&
|
||||
chord.phrase.originalValue &&
|
||||
chord.phrase.originalValue !== chord.phrase.value
|
||||
? 1
|
||||
: 0),
|
||||
0,
|
||||
);
|
||||
const removed = parsed.removed.length;
|
||||
return `+${added} ~${changed} -${removed} (${parsed.chords.length} total)`;
|
||||
}
|
||||
|
||||
function wordCountPanel(view: EditorView): Panel {
|
||||
let dom = document.createElement("div");
|
||||
dom.textContent = getChanges(view.state);
|
||||
return {
|
||||
dom,
|
||||
update(update) {
|
||||
dom.textContent = getChanges(update.state);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function changesPanel() {
|
||||
return showPanel.of(wordCountPanel);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
32
src/lib/chord-editor/chord-sync-plugin.ts
Normal file
32
src/lib/chord-editor/chord-sync-plugin.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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) {
|
||||
// save initial device chords
|
||||
// compare new device chords with initial device chords
|
||||
// take changed/new/removed chords
|
||||
// compare current editor chords with initial device chords
|
||||
// compare two change sets
|
||||
// apply removals if the chord didn't change on either end
|
||||
// apply
|
||||
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;
|
||||
},
|
||||
});
|
||||
@@ -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
|
||||
|
||||
172
src/lib/chord-editor/parse-meta.ts
Normal file
172
src/lib/chord-editor/parse-meta.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
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"];
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/lib/chord-editor/parsed-chords-plugin.ts
Normal file
30
src/lib/chord-editor/parsed-chords-plugin.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
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: [],
|
||||
};
|
||||
},
|
||||
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(transaction.state, ids, codes, deviceChords);
|
||||
}
|
||||
return mapParseResult(value, transaction.changes);
|
||||
},
|
||||
});
|
||||
133
src/lib/chord-editor/persistent-state-plugin.ts
Normal file
133
src/lib/chord-editor/persistent-state-plugin.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
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";
|
||||
import {
|
||||
parseCompressed,
|
||||
stringifyCompressed,
|
||||
} from "$lib/serial/serialization";
|
||||
|
||||
const serializedFields = {
|
||||
history: historyField,
|
||||
deviceChords: deviceChordField,
|
||||
};
|
||||
|
||||
export interface EditorConfig {
|
||||
rawCode?: boolean;
|
||||
storeName: string;
|
||||
autocomplete(query: string | undefined): void;
|
||||
}
|
||||
|
||||
export async function loadPersistentState(
|
||||
params: EditorConfig,
|
||||
): Promise<EditorState> {
|
||||
const stored = localStorage.getItem(params.storeName);
|
||||
const config = {
|
||||
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]),
|
||||
],
|
||||
} satisfies EditorStateConfig;
|
||||
|
||||
if (stored) {
|
||||
try {
|
||||
const parsed = await parseCompressed(new Blob([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),
|
||||
mergeMap(() =>
|
||||
stringifyCompressed(this.view.state.toJSON(serializedFields)),
|
||||
),
|
||||
mergeMap((blob) => blob.text()),
|
||||
)
|
||||
.subscribe((value) => {
|
||||
localStorage.setItem(storeName, value);
|
||||
});
|
||||
|
||||
constructor(readonly view: EditorView) {}
|
||||
|
||||
update(update: ViewUpdate) {
|
||||
if (update.state !== update.startState) {
|
||||
this.updateSubject.next();
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.subscription.unsubscribe();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
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,
|
||||
@@ -63,7 +66,7 @@
|
||||
|
||||
{#snippet kbdText()}
|
||||
{dynamicMapping ??
|
||||
info.icon ??
|
||||
icon ??
|
||||
info.display ??
|
||||
info.id ??
|
||||
`0x${info.code.toString(16)}`}
|
||||
@@ -71,7 +74,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}
|
||||
@@ -91,7 +94,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}
|
||||
@@ -106,7 +109,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}
|
||||
@@ -155,21 +158,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 {
|
||||
|
||||
@@ -19,13 +19,17 @@
|
||||
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 {
|
||||
|
||||
@@ -2,93 +2,224 @@
|
||||
import { chords } from "$lib/undo-redo";
|
||||
import { EditorView } from "codemirror";
|
||||
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 {
|
||||
drawSelection,
|
||||
dropCursor,
|
||||
highlightActiveLine,
|
||||
highlightSpecialChars,
|
||||
keymap,
|
||||
} from "@codemirror/view";
|
||||
import { history, standardKeymap } from "@codemirror/commands";
|
||||
import "$lib/chord-editor/chords.grammar";
|
||||
import {
|
||||
chordHighlightStyle,
|
||||
chordLanguageSupport,
|
||||
} from "$lib/chord-editor/chords-grammar-plugin";
|
||||
import { syntaxHighlighting } from "@codemirror/language";
|
||||
import { autocompletion } from "@codemirror/autocomplete";
|
||||
import { persistentWritable } from "$lib/storage";
|
||||
import ActionList from "$lib/components/layout/ActionList.svelte";
|
||||
import {
|
||||
composeChordInput,
|
||||
hashChord,
|
||||
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";
|
||||
import { chordSyncEffect } from "$lib/chord-editor/chord-sync-plugin";
|
||||
import { KEYMAP_IDS, type KeyInfo } from "$lib/serial/keymap-codes";
|
||||
|
||||
let queryFilter: string | undefined = $state(undefined);
|
||||
|
||||
const rawCode = persistentWritable("chord-editor-raw-code", false);
|
||||
const showEdits = persistentWritable("chord-editor-show-edits", true);
|
||||
let originalDoc = $derived(
|
||||
$chords
|
||||
.map((chord) => {
|
||||
return (
|
||||
chord.actions
|
||||
.filter((it) => it !== 0)
|
||||
.map((it) => actionToValue(it))
|
||||
.join("") +
|
||||
"=>" +
|
||||
chord.phrase.map((it) => actionToValue(it)).join("")
|
||||
);
|
||||
})
|
||||
.join("\n"),
|
||||
);
|
||||
const denseSpacing = persistentWritable("chord-editor-spacing", false);
|
||||
|
||||
let editor: HTMLDivElement | undefined = $state(undefined);
|
||||
let view: EditorView;
|
||||
|
||||
$effect(() => {
|
||||
if (!editor) return;
|
||||
view = new EditorView({
|
||||
parent: editor,
|
||||
doc: originalDoc,
|
||||
extensions: [
|
||||
...($rawCode ? [] : [delimPlugin, actionPlugin]),
|
||||
chordLanguageSupport(),
|
||||
autocompletion({ icons: false, selectOnOpen: true }),
|
||||
history(),
|
||||
dropCursor(),
|
||||
syntaxHighlighting(chordHighlightStyle),
|
||||
highlightActiveLine(),
|
||||
drawSelection(),
|
||||
highlightSpecialChars(),
|
||||
keymap.of(standardKeymap),
|
||||
],
|
||||
});
|
||||
return () => view.destroy();
|
||||
const viewPromise = loadPersistentState({
|
||||
rawCode: $rawCode,
|
||||
storeName: "chord-editor-state-storage",
|
||||
autocomplete(query) {
|
||||
queryFilter = query;
|
||||
},
|
||||
}).then(
|
||||
(state) =>
|
||||
new EditorView({
|
||||
parent: editor,
|
||||
state,
|
||||
}),
|
||||
);
|
||||
viewPromise.then((it) => (view = it));
|
||||
return () => viewPromise.then((it) => it.destroy());
|
||||
});
|
||||
|
||||
function regenerate() {
|
||||
const doc = $chords
|
||||
.map((chord) => {
|
||||
const [actions, compound] = splitCompound(chord.actions);
|
||||
return (
|
||||
(compound ? "|0x" + compound.toString(16) + "|" : "") +
|
||||
actions.map((it) => actionToValue(it)).join("") +
|
||||
"=>" +
|
||||
chord.phrase.map((it) => actionToValue(it)).join("")
|
||||
);
|
||||
})
|
||||
.join("\n");
|
||||
view.dispatch({
|
||||
changes: { from: 0, to: view.state.doc.length, insert: doc },
|
||||
effects: chordSyncEffect.of(
|
||||
$chords.map((chord) => [chord.actions, chord.phrase] as const),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
function largeFile() {
|
||||
const chordCount = 100000;
|
||||
const maxPhraseLength = 100;
|
||||
const maxInputLength = 8;
|
||||
const compoundChance = 0.05;
|
||||
|
||||
const actions = [...$KEYMAP_IDS.values()];
|
||||
function randomAction(): KeyInfo {
|
||||
return actions[Math.floor(actions.length * Math.random())]!;
|
||||
}
|
||||
|
||||
const backup: [KeyInfo[][], KeyInfo[]][] = Array.from(
|
||||
{ length: chordCount },
|
||||
() =>
|
||||
[
|
||||
[
|
||||
Array.from(
|
||||
{ length: Math.floor(Math.random() * maxInputLength) + 1 },
|
||||
randomAction,
|
||||
),
|
||||
],
|
||||
Array.from(
|
||||
{
|
||||
length: Math.floor(Math.log(Math.random() * maxPhraseLength)) + 1,
|
||||
},
|
||||
randomAction,
|
||||
),
|
||||
] as const,
|
||||
);
|
||||
for (const chord of backup) {
|
||||
if (Math.random() < compoundChance) {
|
||||
chord[0] = [
|
||||
...backup[Math.floor(backup.length * Math.random())]![0],
|
||||
...chord[0],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const doc = backup
|
||||
.map(([inputs, phrase]) => {
|
||||
return (
|
||||
inputs
|
||||
.map((input) => input.map((it) => actionToValue(it)).join(""))
|
||||
.join("|") +
|
||||
"=>" +
|
||||
phrase.map((it) => actionToValue(it)).join("")
|
||||
);
|
||||
})
|
||||
.join("\n");
|
||||
view.dispatch({
|
||||
changes: { from: 0, to: view.state.doc.length, insert: doc },
|
||||
effects: chordSyncEffect.of(
|
||||
$chords.map((chord) => [chord.actions, chord.phrase] as const),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
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("")
|
||||
);
|
||||
})
|
||||
.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);
|
||||
}
|
||||
|
||||
function downloadBackup() {
|
||||
const backup: CharaChordFile = {
|
||||
charaVersion: 1,
|
||||
type: "chords",
|
||||
chords: view.state.field(parsedChordsField).result,
|
||||
};
|
||||
console.log(JSON.stringify(backup));
|
||||
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 = "chord-backup.json";
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
</script>
|
||||
|
||||
<label><input type="checkbox" bind:checked={$rawCode} />View as code</label>
|
||||
<label><input type="checkbox" bind:checked={$showEdits} />Show edits</label>
|
||||
<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}>Regenerate from current chords</button>
|
||||
<!--<button onclick={largeFile}>Create Huge File</button>-->
|
||||
<button onclick={downloadBackup}>Download Backup</button>
|
||||
<input
|
||||
type="file"
|
||||
accept="application/json"
|
||||
onchange={loadBackup}
|
||||
style="margin-left: 1rem"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="split">
|
||||
<ActionList />
|
||||
<div
|
||||
class="editor"
|
||||
class:hide-edits={!$showEdits}
|
||||
class:raw={$rawCode}
|
||||
bind:this={editor}
|
||||
></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>
|
||||
</div>
|
||||
|
||||
<style lang="scss">
|
||||
.split {
|
||||
.vertical {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
height: 100%;
|
||||
|
||||
> :global(:first-child) {
|
||||
max-width: 600px;
|
||||
}
|
||||
}
|
||||
|
||||
.editor:not(.raw) :global(.cm-line) {
|
||||
width: fit-content;
|
||||
.split {
|
||||
display: flex;
|
||||
flex-grow: 1;
|
||||
flex-shrink: 1;
|
||||
width: calc(min(100%, 1400px));
|
||||
min-height: 0;
|
||||
|
||||
> :global(*) {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.editor :global(.cm-deletedChunk) {
|
||||
@@ -96,7 +227,6 @@
|
||||
}
|
||||
|
||||
.editor {
|
||||
min-width: 600px;
|
||||
height: 100%;
|
||||
font-size: 16px;
|
||||
|
||||
@@ -122,6 +252,45 @@
|
||||
}
|
||||
}
|
||||
|
||||
&:not(.raw) :global(.cm-line) {
|
||||
columns: 2;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
&.dense-spacing :global(.cm-line) {
|
||||
padding-block: 0;
|
||||
}
|
||||
|
||||
:global(.cm-line) {
|
||||
padding-block: 8px;
|
||||
width: 100%;
|
||||
text-wrap: wrap;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
|
||||
> :global(*) {
|
||||
break-before: avoid;
|
||||
break-after: avoid;
|
||||
break-inside: avoid;
|
||||
}
|
||||
}
|
||||
|
||||
: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;
|
||||
@@ -144,29 +313,20 @@
|
||||
}
|
||||
|
||||
:global(.cm-gutters) {
|
||||
border: none;
|
||||
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;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
:global(.cm-line) {
|
||||
border-bottom: 1px solid transparent;
|
||||
line-height: 3em;
|
||||
}
|
||||
|
||||
:global(.cm-scroller) {
|
||||
overflow: auto;
|
||||
font-family: inherit !important;
|
||||
}
|
||||
|
||||
:global(.cm-cursor) {
|
||||
border-color: var(--md-sys-color-on-surface);
|
||||
}
|
||||
|
||||
:global(.cm-changedLine) {
|
||||
background-color: color-mix(
|
||||
in srgb,
|
||||
@@ -182,17 +342,13 @@
|
||||
|
||||
:global(.cm-activeLine) {
|
||||
border-bottom: 1px solid var(--md-sys-color-surface-variant);
|
||||
/*background-color: color-mix(
|
||||
in srgb,
|
||||
var(--md-sys-color-surface-variant) 40%,
|
||||
transparent
|
||||
) !important;*/
|
||||
|
||||
&:not(.cm-changedLine) {
|
||||
background-color: transparent !important;
|
||||
}
|
||||
}
|
||||
|
||||
:global(::selection),
|
||||
:global(.cm-selectionBackground) {
|
||||
background-color: var(--md-sys-color-surface-variant) !important;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user