Files
Beyond-KLE/mock/src/domain/idx.ts
T
ewdernaandClaude Opus 5.5 fccbc28bee mock: 仮決定に沿ってモックを実装する(PHY Editor・IDX・ELC・MAP・Generator)
provisional/decisions.md の仮決定(T-1〜T-28)に従い、mock/ に Vue 3 + Vite + TypeScript のモックを置いた。
T-3 の1〜3(回転したクラスター内のドラッグと保存の往復、IDX・MAPとの連携、ELCと出力の対応表)まで。疑似デバイスとの往復(T-24)は未着手。

- KLE JSONの入出力は、KLE本家の serial.js の deserialize の結果を期待値とする fixture(tests/fixtures/kle-oracle.json)で照合した。本家のコードは取り込んでいない(T-7)
- 基本キーコードの表は QMK の data/constants/keycodes/keycodes_0.0.1_basic.hjson(1e340cd)から抽出した
- 実装は Codex(Luna)、レビューは Codex(Sol)。検証: Vitest 53件、Playwright 20件、vue-tsc、vite build
- TypeScript は vue-tsc が 7 系に未対応のため 6 系に固定した
- 操作性の改善(ELCの提供者の表示、一括配線とGeneratorのピン設定の引き継ぎ、キー内の文字のはみ出し)は別途行う

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AATqjRyp6rrnwTNdYw8XBT
2026-09-25 22:42:30 +09:00

58 lines
2.2 KiB
TypeScript

import type { PhyFile, Node, KeyNode } from "./phy";
export type PhyReference = { source: string; checksum: string; [key: string]: unknown };
export type IdxFile = {
version: string;
phyReference: PhyReference;
order: string[];
[key: string]: unknown;
};
/** Collects keys in document order, including ghost keys and excluding decals. T-18. */
export function eligibleKeyIds(phy: PhyFile): string[] {
const ids: string[] = [];
const visit = (nodes: Node[]) =>
nodes.forEach((n) => {
if (n.type === "key") {
if (!n.decal) ids.push(n.id);
} else if (n.type === "cluster") visit(n.children);
else if (n.type === "variantGroup") n.children.forEach((v) => visit(v.children));
});
visit(phy.root);
return ids;
}
/** Makes initial index order from KLE occurrence order. T-18. */
export function createIdx(phy: PhyFile, keyOrder: string[], phyReference: PhyReference): IdxFile {
const valid = new Set(eligibleKeyIds(phy));
return {
version: "0.1.0",
phyReference: structuredClone(phyReference),
order: keyOrder.filter((id) => valid.has(id)),
};
}
/** Lists eligible PHY keys omitted from the saved order. T-18. */
export function unregisteredKeys(phy: PhyFile, idx: IdxFile): string[] {
const registered = new Set(idx.order);
return eligibleKeyIds(phy).filter((id) => !registered.has(id));
}
/** Registers new keys at the end without rebuilding existing order. T-18. */
export function appendKeys(idx: IdxFile, ids: string[]): IdxFile {
const out = structuredClone(idx);
for (const id of ids) if (!out.order.includes(id)) out.order.push(id);
return out;
}
/** Removes one key from index order. */
export function excludeKey(idx: IdxFile, keyId: string): IdxFile {
const o = structuredClone(idx);
o.order = o.order.filter((x) => x !== keyId);
return o;
}
/** Reorders the saved key list. */
export function reorderKeys(idx: IdxFile, order: string[]): IdxFile {
if (
order.length !== idx.order.length ||
new Set(order).size !== order.length ||
order.some((x) => !idx.order.includes(x))
)
throw new Error("Order must contain every registered key exactly once");
return { ...structuredClone(idx), order: [...order] };
}