3 Commits
master ... cv2

Author SHA1 Message Date
6ceb3994e7 feat: cv2 2026-01-09 21:36:35 +01:00
156825a194 feat: cv2 2026-01-09 21:33:41 +01:00
4bc84b5399 feat: cv2 2026-01-09 14:42:33 +01:00
21 changed files with 1565 additions and 672 deletions

View File

@@ -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

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,203 @@
import { type KeyInfo } from "$lib/serial/keymap-codes";
import { syntaxTree } from "@codemirror/language";
import { linter, type Diagnostic } from "@codemirror/lint";
import { parsedChordsField } from "./parsed-chords-plugin";
import { actionMetaPlugin } from "./action-meta-plugin";
export function actionLinter(config?: Parameters<typeof linter>[1]) {
const finalConfig: Parameters<typeof linter>[1] = {
...config,
needsRefresh(update) {
console.log(
"test",
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 { ids, codes } = view.state.field(actionMetaPlugin.field);
const { meta, compoundInputs } = view.state.field(parsedChordsField);
syntaxTree(view.state)
.cursor()
.iterate((node) => {
let action: KeyInfo | undefined = undefined;
switch (node.name) {
case "SingleLetter": {
action = ids.get(view.state.doc.sliceString(node.from, node.to));
break;
}
case "ActionId": {
action = ids.get(view.state.doc.sliceString(node.from, node.to));
break;
}
case "HexNumber": {
const hexString = view.state.doc.sliceString(node.from, node.to);
const code = Number.parseInt(hexString, 16);
if (hexString.length === 10) {
if (compoundInputs.has(code)) {
diagnostics.push({
from: node.from,
to: node.to,
severity: "info",
message: "Compound hash literal can be expanded",
actions: [
{
name: "Expand",
apply(view, from, to) {
view.dispatch({
changes: {
from: from - 1,
to: to + 1,
insert: compoundInputs.get(code)! + "|",
},
});
},
},
],
});
}
return;
}
if (!(code >= 0 && code <= 1023)) {
diagnostics.push({
from: node.from,
to: node.to,
severity: "error",
message: "Hex code invalid (out of range)",
actions: [
{
name: "Remove",
apply(view, from, to) {
view.dispatch({ changes: { from, to } });
},
},
],
});
return;
}
action = codes.get(code);
break;
}
default:
return;
}
if (!action) {
const action = view.state.doc.sliceString(node.from, node.to);
diagnostics.push({
from: node.from,
to: node.to,
severity: node.name === "HexNumber" ? "warning" : "error",
message: `Unknown action: ${action}`,
actions: [
...(node.name === "SingleLetter"
? ([
{
name: "Generate Windows Hex Numpad Code",
apply(view, from, to) {
view.dispatch({
changes: {
from,
to,
insert:
"<PRESS_NEXT><LEFT_ALT><KP_PLUS>" +
action
.codePointAt(0)!
.toString(16)
.split("")
.map((c) =>
/^\d$/.test(c)
? `<KP_${c}>`
: c.toLowerCase(),
)
.join("") +
"<RELEASE_NEXT><LEFT_ALT>",
},
});
},
},
] satisfies Diagnostic["actions"])
: []),
],
});
}
});
for (const m of meta) {
if (m.invalidActions) {
diagnostics.push({
from: m.from,
to: m.to,
severity: "error",
markClass: "chord-invalid",
message: `Chord contains invalid actions`,
});
}
if (m.invalidInput) {
diagnostics.push({
from: m.from,
to: m.to,
severity: "error",
markClass: "chord-invalid",
message: `Chord input is invalid`,
});
}
if (m.emptyPhrase) {
diagnostics.push({
from: m.from,
to: m.from,
severity: "warning",
message: `Chord phrase is empty`,
});
}
if (m.overriddenBy) {
diagnostics.push({
from: m.from,
to: m.from,
severity: "warning",
message: `Chord overridden by previous chord`,
});
}
if (m.orphan) {
diagnostics.push({
from: m.from,
to: m.from,
severity: "warning",
message: `Orphan compound chord`,
});
}
if (m.disabled) {
diagnostics.push({
from: m.from,
to: m.to,
severity: "info",
markClass: "chord-ignored",
message: `Chord disabled`,
});
}
if ((m.overrides?.length ?? 0) > 0) {
diagnostics.push({
from: m.from,
to: m.from,
severity: "info",
message: `Chord overrides other chords`,
});
}
}
return diagnostics;
}, finalConfig);
}

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 {
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() {
@@ -56,18 +56,16 @@ function actionWidgets(view: EditorView) {
enter: (node) => {
if (node.name !== "ExplicitAction") return;
const value =
node.node.getChild("ActionId") ??
node.node.getChild("HexNumber") ??
node.node.getChild("DecimalNumber");
node.node.getChild("ActionId") ?? node.node.getChild("HexNumber");
if (!value) return;
if (!node.node.getChild("ExplicitDelimEnd")) {
return;
}
const id = view.state.doc.sliceString(value.from, value.to);
if (value.name === "HexNumber" && id.length === 10) return;
let deco = Decoration.replace({
widget: new ActionWidget(
value.name === "ActionId" ? id : parseInt(id),
value.name === "ActionId" ? id : Number.parseInt(id, 16),
),
});
widgets.push(deco.range(node.from, node.to));

View File

@@ -1,5 +1,21 @@
import { KEYMAP_CODES, type KeyInfo } from "$lib/serial/keymap-codes";
import {
KEYMAP_CODES,
KEYMAP_IDS,
type KeyInfo,
} from "$lib/serial/keymap-codes";
import type { CharaChordFile } from "$lib/share/chara-file";
import { syntaxTree } from "@codemirror/language";
import { StateEffect, ChangeDesc, type EditorState } from "@codemirror/state";
import type { Update } from "@codemirror/collab";
import { get } from "svelte/store";
import {
composeChordInput,
hasConcatenator,
hashChord,
splitCompound,
willBeValidChordInput,
} from "$lib/serial/chord";
import type { SyntaxNodeRef } from "@lezer/common";
export function canUseIdAsString(info: KeyInfo): boolean {
return !!info.id && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(info.id);
@@ -9,8 +25,286 @@ 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 interface ParseMeta {
from: number;
to: number;
hasConcatenator: boolean;
invalidActions?: true;
invalidInput?: true;
emptyPhrase?: true;
orphan?: true;
disabled?: true;
overrides?: number[];
overriddenBy?: number;
}
export interface ParseResult {
result: CharaChordFile["chords"];
meta: ParseMeta[];
compoundInputs: Map<number, string>;
}
export function parseCharaChords(
data: EditorState,
ids: Map<string, KeyInfo>,
codes: Map<number, KeyInfo>,
): ParseResult {
console.time("parseCharaChords");
const chords: CharaChordFile["chords"] = [];
const metas: ParseMeta[] = [];
const keys = new Map<string, number>();
const compoundInputs = new Map<number, string>();
const orphanCompounds = new Set<number>();
let currentChord: CharaChordFile["chords"][number] | undefined = undefined;
let compound: number | undefined = undefined;
let currentActions: number[] = [];
let invalidActions = false;
let invalidInput = false;
let chordFrom = 0;
const makeChordInput = (node: SyntaxNodeRef): number[] => {
invalidInput ||= !willBeValidChordInput(currentActions.length, !!compound);
const input = composeChordInput(currentActions, compound);
compound = hashChord(input);
if (!compoundInputs.has(compound)) {
compoundInputs.set(compound, data.doc.sliceString(chordFrom, node.from));
orphanCompounds.add(compound);
}
return input;
};
syntaxTree(data)
.cursor()
.iterate(
(node) => {
if (node.name === "Chord") {
currentChord = undefined;
compound = undefined;
invalidActions = false;
invalidInput = false;
chordFrom = node.from;
} else if (node.name === "ActionString") {
currentActions = [];
} else if (node.name === "HexNumber") {
const hexString = data.doc.sliceString(node.from, node.to);
const code = Number.parseInt(hexString, 16);
if (hexString.length === 10) {
if (compound !== undefined) {
invalidInput = true;
}
compound = code;
} else {
if (Number.isNaN(code) || code < 0 || code > 1023) {
invalidActions = true;
}
currentActions.push(code);
}
} else if (
node.name === "ActionId" ||
node.name === "SingleLetter" ||
node.name === "EscapedChar"
) {
const id = data.doc.sliceString(node.from, node.to);
const code = ids.get(id)?.code;
if (code === undefined) {
invalidActions = true;
const encoder = new TextEncoder();
const bytes = encoder.encode(id);
for (let byte of bytes) {
currentActions.push(-byte);
}
} else {
currentActions.push(code);
}
}
},
(node) => {
if (node.name === "Chord" && currentChord !== undefined) {
if (currentChord !== undefined) {
currentChord[1] = currentActions;
const index = chords.length;
chords.push(currentChord);
const meta: ParseMeta = {
from: node.from,
to: node.to,
hasConcatenator: hasConcatenator(currentChord[1], codes),
};
if (invalidActions) {
meta.invalidActions = true;
}
if (invalidInput) {
meta.invalidInput = true;
}
metas.push(meta);
if (currentChord[1].length === 0) {
meta.emptyPhrase = true;
}
const key = JSON.stringify(currentChord[0]);
if (!meta.invalidInput) {
if (keys.has(key)) {
const targetIndex = keys.get(key)!;
const targetMeta = metas[targetIndex]!;
if (!targetMeta.overrides) targetMeta.overrides = [];
targetMeta.overrides.push(index);
meta.overriddenBy = targetIndex;
} else {
keys.set(key, index);
}
}
if (
meta.emptyPhrase ||
meta.invalidInput ||
meta.invalidActions ||
meta.overriddenBy !== undefined
) {
meta.disabled = true;
}
}
} else if (node.name === "CompoundDelim") {
makeChordInput(node);
} else if (node.name === "PhraseDelim") {
const input = makeChordInput(node);
orphanCompounds.delete(hashChord(input));
currentChord = [input, []];
}
},
);
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;
}
}
console.timeEnd("parseCharaChords");
console.log(chords.length);
return { result: chords, meta: metas, compoundInputs };
}
class ChordRecord {
private chords = new Map<string, Set<string>>();
constructor(chords: CharaChordFile["chords"]) {
for (let chord of chords) {
const key = JSON.stringify(chord[0]);
if (!this.chords.has(key)) {
this.chords.set(key, new Set());
}
this.chords.get(key)!.add(JSON.stringify(chord));
}
}
static createDiff(
previous: CharaChordFile["chords"],
updated: CharaChordFile["chords"],
) {
const deleted = new ChordRecord(previous);
const added = new ChordRecord(updated);
const dupA = deleted.duplicates(added);
const dupB = added.duplicates(deleted);
for (let chord of dupA) {
deleted.remove(chord);
added.remove(chord);
}
for (let chord of dupB) {
deleted.remove(chord);
added.remove(chord);
}
return { deleted, added };
}
duplicates(
other: ChordRecord,
): IteratorObject<CharaChordFile["chords"][number]> {
const duplicates = new Set<string>();
for (let [key, chordSet] of this.chords) {
for (let chord of chordSet) {
if (other.hasInternal(key, chord)) {
duplicates.add(chord);
}
}
}
return duplicates
.values()
.map((it) => JSON.parse(it) as CharaChordFile["chords"][number]);
}
private hasInternal(key: string, chord: string): boolean {
return this.chords.get(key)?.has(chord) ?? false;
}
has(chord: CharaChordFile["chords"][number]): boolean {
return this.hasInternal(JSON.stringify(chord[0]), JSON.stringify(chord));
}
remove(chord: CharaChordFile["chords"][number]) {
const key = JSON.stringify(chord[0]);
const set = this.chords.get(key);
if (set) {
set.delete(JSON.stringify(chord));
if (set.size === 0) {
this.chords.delete(key);
}
}
}
}
export function syncChords(
previous: CharaChordFile["chords"],
updated: CharaChordFile["chords"],
state: EditorState,
) {
const deviceDiff = ChordRecord.createDiff(previous, updated);
const current = parseCharaChords(state, get(KEYMAP_IDS));
// 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
}
export function rebaseUpdates(
updates: readonly Update[],
over: readonly { changes: ChangeDesc; clientID: string }[],
) {
if (!over.length || !updates.length) return updates;
let changes: ChangeDesc | null = null,
skip = 0;
for (let update of over) {
let other = skip < updates.length ? updates[skip] : null;
if (other && other.clientID == update.clientID) {
if (changes) changes = changes.mapDesc(other.changes, true);
skip++;
} else {
changes = changes ? changes.composeDesc(update.changes) : update.changes;
}
}
if (skip) updates = updates.slice(skip);
return !changes
? updates
: updates.map((update) => {
let updateChanges = update.changes.map(changes!);
changes = changes!.mapDesc(update.changes, true);
return {
changes: updateChanges,
effects:
update.effects && StateEffect.mapEffects(update.effects, changes!),
clientID: update.clientID,
};
});
}

View File

@@ -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
);
}
},
);
}

View File

@@ -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 =
"&emsp;⇛" + (this.hasConcatenator ? "" : "&emsp;");
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;
}

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

@@ -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({})]);
}

View File

@@ -1,6 +1,6 @@
@top Program { Chord* }
ExplicitAction { ExplicitDelimStart (HexNumber | DecimalNumber | ActionId) ExplicitDelimEnd }
ExplicitAction { ExplicitDelimStart (HexNumber | ActionId) ExplicitDelimEnd }
EscapedSingleAction { Escape EscapedLetter }
Action { SingleLetter | ExplicitAction | EscapedSingleAction }
ActionString { Action* }
@@ -9,16 +9,15 @@ ChordPhrase { ActionString }
Chord { ChordInput PhraseDelim ChordPhrase ChordDelim }
@tokens {
@precedence {HexNumber, DecimalNumber}
@precedence {HexNumber}
@precedence {CompoundDelim, PhraseDelim, ExplicitDelimStart, ChordDelim, SingleLetter}
@precedence {EscapedLetter}
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 { ![] }

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,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>

View File

@@ -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 {

View File

@@ -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>

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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 {

View File

@@ -2,42 +2,20 @@
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 { 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);
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;
@@ -45,58 +23,131 @@
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),
],
state: loadPersistentState({
rawCode: $rawCode,
storeName: "chord-editor-state-storage",
autocomplete(query) {
queryFilter = query;
},
}),
});
return () => view.destroy();
});
function regenerate() {
const doc = $chords
.map((chord) => {
const [actions, compound] = splitCompound(chord.actions);
return (
(compound
? "<0x" + compound.toString(16).padStart(8, "0") + ">"
: "") +
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 },
});
}
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 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={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}
class:dense-spacing={$denseSpacing}
bind:this={editor}
></div>
<ActionList {queryFilter} ignoreIcon={$rawCode} />
</div>
<style lang="scss">
.split {
display: flex;
gap: 1rem;
width: calc(min(100%, 1400px));
height: 100%;
> :global(:first-child) {
max-width: 600px;
> :global(*) {
flex: 1;
}
}
.editor:not(.raw) :global(.cm-line) {
width: fit-content;
}
.editor :global(.cm-deletedChunk) {
opacity: 0.2;
}
.editor {
min-width: 600px;
height: 100%;
font-size: 16px;
@@ -122,6 +173,40 @@
}
}
&: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-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 +229,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 +258,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;
}