| 34 | } |
| 35 | |
| 36 | class DocumentEditor { |
| 37 | private nodes: DocumentNode[] = []; |
| 38 | private undoStack: Command[] = []; |
| 39 | private redoStack: Command[] = []; |
| 40 | private observers: Observer[] = []; |
| 41 | |
| 42 | addNode(node: DocumentNode) { |
| 43 | this.nodes.push(node); |
| 44 | this.notify(); |
| 45 | } |
| 46 | |
| 47 | executeCommand(command: Command) { |
| 48 | command.execute(); |
| 49 | this.undoStack.push(command); |
| 50 | this.redoStack = []; |
| 51 | this.notify(); |
| 52 | } |
| 53 | |
| 54 | undo() { |
| 55 | const cmd = this.undoStack.pop(); |
| 56 | if (cmd) { |
| 57 | cmd.undo(); |
| 58 | this.redoStack.push(cmd); |
| 59 | this.notify(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | redo() { |
| 64 | const cmd = this.redoStack.pop(); |
| 65 | if (cmd) { |
| 66 | cmd.execute(); |
| 67 | this.undoStack.push(cmd); |
| 68 | this.notify(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | getContent(): string { |
| 73 | return this.nodes.map((n) => n.render()).join("\n"); |
| 74 | } |
| 75 | |
| 76 | getNodes(): DocumentNode[] { |
| 77 | return this.nodes; |
| 78 | } |
| 79 | |
| 80 | setNodes(nodes: DocumentNode[]) { |
| 81 | this.nodes = nodes; |
| 82 | } |
| 83 | |
| 84 | subscribe(observer: Observer) { |
| 85 | this.observers.push(observer); |
| 86 | } |
| 87 | |
| 88 | private notify() { |
| 89 | this.observers.forEach((o) => o.update(this)); |
| 90 | } |
| 91 | } |
nothing calls this directly
no outgoing calls
no test coverage detected