|
| 1 | +import { Transactor, TransactedCallback, Commit, CommitOptions } from '@openscd/oscd-api/dist/Transactor.js'; |
| 2 | +import { EditV2 } from '@openscd/oscd-api/dist/editv2.js'; |
| 3 | + |
| 4 | +import { Subject } from './subject.js'; |
| 5 | +import { handleEditV2 } from '../../foundation.js'; |
| 6 | + |
| 7 | +export interface OscdCommit<C> extends Commit<C> { |
| 8 | + time: number; |
| 9 | +} |
| 10 | + |
| 11 | +export class XMLEditor implements Transactor<EditV2> { |
| 12 | + public past: OscdCommit<EditV2>[] = []; |
| 13 | + public future: OscdCommit<EditV2>[] = []; |
| 14 | + |
| 15 | + private commitSubject = new Subject<OscdCommit<EditV2>>(); |
| 16 | + private undoSubject = new Subject<OscdCommit<EditV2>>(); |
| 17 | + private redoSubject = new Subject<OscdCommit<EditV2>>(); |
| 18 | + |
| 19 | + get canUndo(): boolean { |
| 20 | + return this.past.length > 0; |
| 21 | + } |
| 22 | + |
| 23 | + get canRedo(): boolean { |
| 24 | + return this.future.length > 0; |
| 25 | + } |
| 26 | + |
| 27 | + reset(): void { |
| 28 | + this.past = []; |
| 29 | + this.future = []; |
| 30 | + } |
| 31 | + |
| 32 | + commit(change: EditV2, { title, squash }: CommitOptions = {}): OscdCommit<EditV2> { |
| 33 | + const commit: OscdCommit<EditV2> = |
| 34 | + squash && this.past.length |
| 35 | + ? this.past[this.past.length - 1] |
| 36 | + : { undo: [], redo: [], time: Date.now() }; |
| 37 | + // TODO: Fix type once issue is fixed https://github.com/openscd/oscd-api/issues/57 |
| 38 | + const undo = handleEditV2(change as any); |
| 39 | + // typed as per https://github.com/microsoft/TypeScript/issues/49280#issuecomment-1144181818 recommendation: |
| 40 | + commit.undo.unshift(...[undo].flat(Infinity as 1)); |
| 41 | + commit.redo.push(...[change].flat(Infinity as 1)); |
| 42 | + if (title) commit.title = title; |
| 43 | + if (squash && this.past.length) this.past.pop(); |
| 44 | + this.past.push(commit); |
| 45 | + this.future = []; |
| 46 | + this.commitSubject.next(commit); |
| 47 | + return commit; |
| 48 | + }; |
| 49 | + |
| 50 | + undo(): OscdCommit<EditV2> | undefined { |
| 51 | + const commit = this.past.pop(); |
| 52 | + if (!commit) return; |
| 53 | + // TODO: Fix type once issue is fixed https://github.com/openscd/oscd-api/issues/57 |
| 54 | + handleEditV2(commit.undo as any); |
| 55 | + this.future.unshift(commit); |
| 56 | + this.undoSubject.next(commit); |
| 57 | + return commit; |
| 58 | + }; |
| 59 | + |
| 60 | + redo(): OscdCommit<EditV2> | undefined { |
| 61 | + const commit = this.future.shift(); |
| 62 | + if (!commit) return; |
| 63 | + // TODO: Fix type once issue is fixed https://github.com/openscd/oscd-api/issues/57 |
| 64 | + handleEditV2(commit.redo as any); |
| 65 | + this.past.push(commit); |
| 66 | + this.redoSubject.next(commit); |
| 67 | + return commit; |
| 68 | + }; |
| 69 | + |
| 70 | + subscribe(txCallback: TransactedCallback<EditV2>): () => TransactedCallback<EditV2> { |
| 71 | + return this.commitSubject.subscribe(txCallback) as () => TransactedCallback<EditV2>; |
| 72 | + }; |
| 73 | + |
| 74 | + subscribeUndo(txCallback: TransactedCallback<EditV2>): () => TransactedCallback<EditV2> { |
| 75 | + return this.undoSubject.subscribe(txCallback) as () => TransactedCallback<EditV2>; |
| 76 | + } |
| 77 | + |
| 78 | + subscribeRedo(txCallback: TransactedCallback<EditV2>): () => TransactedCallback<EditV2> { |
| 79 | + return this.redoSubject.subscribe(txCallback) as () => TransactedCallback<EditV2>; |
| 80 | + } |
| 81 | + |
| 82 | + subscribeUndoRedo(txCallback: TransactedCallback<EditV2>): () => TransactedCallback<EditV2> { |
| 83 | + const unsubscribeUndo = this.subscribeUndo(txCallback); |
| 84 | + const unsubscribeRedo = this.subscribeRedo(txCallback); |
| 85 | + |
| 86 | + return () => { |
| 87 | + unsubscribeUndo(); |
| 88 | + unsubscribeRedo(); |
| 89 | + return txCallback; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments