Files
Beyond-KLE/mock/src/domain/history.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

77 lines
2.4 KiB
TypeScript

import type { ProjectFile } from "./project";
/** Snapshot history for whole-project undo/redo. T-17. */
export class ProjectHistory {
private items: ProjectFile[];
private cursor = 0;
private pending = false;
constructor(
initial: ProjectFile,
private readonly limit = 100,
) {
this.items = [structuredClone(initial)];
}
/** Adds one undoable snapshot unless a grouped edit is active. */
push(value: ProjectFile): void {
if (this.pending) {
this.replaceTop(value);
return;
}
this.add(value);
}
/** Starts grouping subsequent pushes into a single operation. T-17. */
begin(): void {
if (this.pending) throw new Error("History group already active");
this.items = this.items.slice(0, this.cursor + 1);
this.items.push(structuredClone(this.items[this.cursor]!));
this.cursor = this.items.length - 1;
if (this.items.length > this.limit) {
this.items.shift();
this.cursor--;
}
this.pending = true;
}
/** Commits the final grouped snapshot as one undo step. T-17. */
commit(value?: ProjectFile): void {
if (!this.pending) throw new Error("No history group active");
if (value) this.items[this.cursor] = structuredClone(value);
if (JSON.stringify(this.items[this.cursor]) === JSON.stringify(this.items[this.cursor - 1])) {
this.items.pop();
this.cursor--;
}
this.pending = false;
}
/** Replaces the current snapshot during a grouped drag. T-17. */
replaceTop(value: ProjectFile): void {
this.items[this.cursor] = structuredClone(value);
}
/** Moves to the previous snapshot. */
undo(): ProjectFile {
if (!this.canUndo) throw new Error("Nothing to undo");
return structuredClone(this.items[--this.cursor]!);
}
/** Moves to the next snapshot. */
redo(): ProjectFile {
if (!this.canRedo) throw new Error("Nothing to redo");
return structuredClone(this.items[++this.cursor]!);
}
get canUndo(): boolean {
return this.cursor > 0;
}
get canRedo(): boolean {
return this.cursor < this.items.length - 1;
}
/** Returns a copy of the current snapshot. */
current(): ProjectFile {
return structuredClone(this.items[this.cursor]!);
}
private add(value: ProjectFile) {
this.items = this.items.slice(0, this.cursor + 1);
this.items.push(structuredClone(value));
this.cursor = this.items.length - 1;
if (this.items.length > this.limit) {
this.items.shift();
this.cursor--;
}
}
}