diff --git a/manual-testing-sandbox/next-edit/next-edit-test-1.ts b/manual-testing-sandbox/next-edit/next-edit-test-1.ts new file mode 100644 index 000000000..7de9bf338 --- /dev/null +++ b/manual-testing-sandbox/next-edit/next-edit-test-1.ts @@ -0,0 +1,3 @@ +function foo(a: string, b: string, c: string) { + return `${a} ${b.length} ${c.length}`; +} diff --git a/manual-testing-sandbox/next-edit/next-edit-test-2.ts b/manual-testing-sandbox/next-edit/next-edit-test-2.ts new file mode 100644 index 000000000..795ffacd9 --- /dev/null +++ b/manual-testing-sandbox/next-edit/next-edit-test-2.ts @@ -0,0 +1,91 @@ +type DocumentNode = ParagraphNode | ListNode; + +abstract class Node { + abstract render(): string; +} + +class ParagraphNode extends Node { + constructor(public text: string) { + super(); + } + + render(): string { + return `

${this.text}

`; + } +} + +class ListNode extends Node { + constructor(public items: string[]) { + super(); + } + + render(): string { + return ``; + } +} + +interface Command { + execute(): void; + undo(): void; +} + +interface Observer { + update(doc: DocumentEditor): void; +} + +class DocumentEditor { + private nodes: DocumentNode[] = []; + private undoStack: Command[] = []; + private redoStack: Command[] = []; + private observers: Observer[] = []; + + addNode(node: DocumentNode) { + this.nodes.push(node); + this.notify(); + } + + executeCommand(command: Command) { + command.execute(); + this.undoStack.push(command); + this.redoStack = []; + this.notify(); + } + + undo() { + const cmd = this.undoStack.pop(); + if (cmd) { + cmd.undo(); + this.redoStack.push(cmd); + this.notify(); + } + } + + redo() { + const cmd = this.redoStack.pop(); + if (cmd) { + cmd.execute(); + this.undoStack.push(cmd); + this.notify(); + } + } + + getContent(): string { + return this.nodes.map((n) => n.render()).join("\n"); + } + + getNodes(): DocumentNode[] { + return this.nodes; + } + + setNodes(nodes: DocumentNode[]) { + this.nodes = nodes; + } + + subscribe(observer: Observer) { + this.observers.push(observer); + } + + private notify() { + this.observers.forEach((o) => o.update(this)); + } +}