All files / roosterjs-content-model-core/lib/corePlugin/undo SnapshotsManagerImpl.ts

98.31% Statements 58/59
95.38% Branches 62/65
100% Functions 12/12
98.31% Lines 58/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150        1x   1x   155x     155x                 1x 108x       264x     1x 524x 524x     1x 10x   10x 4x 4x 4x     10x   10x     1x 241x   241x           241x   241x 185x 185x 185x 185x   185x 185x       2x     2x     185x 2x 2x   2x         185x 1x   56x   2x     241x     1x 244x 4x 4x 4x       4x     4x 4x 4x   4x       1x 5x           1x 191x   1x             1x 155x       119x                          
import type { Snapshot, Snapshots, SnapshotsManager } from 'roosterjs-content-model-types';
 
// Max stack size that cannot be exceeded. When exceeded, old undo history will be dropped
// to keep size under limit. This is kept at 10MB
const MAX_SIZE_LIMIT = 1e7;
 
class SnapshotsManagerImpl implements SnapshotsManager {
    private snapshots: Snapshots;
    private hasNewContentValue: boolean = false;
 
    constructor(snapshots?: Snapshots) {
        this.snapshots = snapshots ?? {
            snapshots: [],
            totalSize: 0,
            currentIndex: -1,
            autoCompleteIndex: -1,
            maxSize: MAX_SIZE_LIMIT,
        };
    }
 
    get hasNewContent(): boolean {
        return this.hasNewContentValue;
    }
 
    set hasNewContent(value: boolean) {
        this.hasNewContentValue = value;
    }
 
    canMove(step: number): boolean {
        const newIndex = this.snapshots.currentIndex + step;
        return newIndex >= 0 && newIndex < this.snapshots.snapshots.length;
    }
 
    move(step: number): Snapshot | null {
        let result: Snapshot | null = null;
 
        if (this.canMove(step)) {
            this.snapshots.currentIndex += step;
            this.snapshots.autoCompleteIndex = -1;
            result = this.snapshots.snapshots[this.snapshots.currentIndex];
        }
 
        this.snapshots.onChanged?.('move');
 
        return result;
    }
 
    addSnapshot(snapshot: Snapshot, isAutoCompleteSnapshot: boolean): void {
        const currentSnapshot = this.snapshots.snapshots[this.snapshots.currentIndex];
        const isSameSnapshot =
            currentSnapshot &&
            currentSnapshot.html == snapshot.html &&
            !currentSnapshot.additionalState &&
            !snapshot.additionalState &&
            !currentSnapshot.entityStates &&
            !snapshot.entityStates;
        const addSnapshot = !currentSnapshot || shouldAddSnapshot(currentSnapshot, snapshot);
 
        if (this.snapshots.currentIndex < 0 || addSnapshot) {
            this.clearRedo();
            this.snapshots.snapshots.push(snapshot);
            this.snapshots.currentIndex++;
            this.snapshots.totalSize += this.getSnapshotLength(snapshot);
 
            let removeCount = 0;
            while (
                removeCount < this.snapshots.snapshots.length &&
                this.snapshots.totalSize > this.snapshots.maxSize
            ) {
                this.snapshots.totalSize -= this.getSnapshotLength(
                    this.snapshots.snapshots[removeCount]
                );
                removeCount++;
            }
 
            if (removeCount > 0) {
                this.snapshots.snapshots.splice(0, removeCount);
                this.snapshots.currentIndex -= removeCount;
 
                Iif (this.snapshots.autoCompleteIndex >= 0) {
                    this.snapshots.autoCompleteIndex -= removeCount;
                }
            }
 
            if (isAutoCompleteSnapshot) {
                this.snapshots.autoCompleteIndex = this.snapshots.currentIndex;
            }
        } else if (isSameSnapshot) {
            // replace the currentSnapshot's metadata so the selection is updated
            this.snapshots.snapshots.splice(this.snapshots.currentIndex, 1, snapshot);
        }
 
        this.snapshots.onChanged?.('add');
    }
 
    clearRedo(): void {
        if (this.canMove(1)) {
            let removedSize = 0;
            for (
                let i = this.snapshots.currentIndex + 1;
                i < this.snapshots.snapshots.length;
                i++
            ) {
                removedSize += this.getSnapshotLength(this.snapshots.snapshots[i]);
            }
 
            this.snapshots.snapshots.splice(this.snapshots.currentIndex + 1);
            this.snapshots.totalSize -= removedSize;
            this.snapshots.autoCompleteIndex = -1;
 
            this.snapshots.onChanged?.('clear');
        }
    }
 
    canUndoAutoComplete(): boolean {
        return (
            this.snapshots.autoCompleteIndex >= 0 &&
            this.snapshots.currentIndex - this.snapshots.autoCompleteIndex == 1
        );
    }
 
    private getSnapshotLength(snapshot: Snapshot) {
        return snapshot.html?.length ?? 0;
    }
}
 
/**
 * @internal
 * Create a new instance of Undo Snapshots Manager
 * @param snapshots @optional Snapshots object for storing undo snapshots. If not passed, default implementation will be used
 */
export function createSnapshotsManager(snapshots?: Snapshots): SnapshotsManager {
    return new SnapshotsManagerImpl(snapshots);
}
 
function shouldAddSnapshot(currentSnapshot: Snapshot, snapshot: Snapshot) {
    return (
        currentSnapshot.html !== snapshot.html ||
        (currentSnapshot.additionalState &&
            snapshot.additionalState &&
            JSON.stringify(currentSnapshot.additionalState) !==
                JSON.stringify(snapshot.additionalState)) ||
        (!currentSnapshot.additionalState && snapshot.additionalState) ||
        (currentSnapshot.entityStates &&
            snapshot.entityStates &&
            currentSnapshot.entityStates !== snapshot.entityStates) ||
        (!currentSnapshot.entityStates && snapshot.entityStates)
    );
}