UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

148 lines 4.7 kB
import { EventObject } from "@aurigma/design-atoms-model/EventObject"; export class History { constructor(handler, historySize = 10) { this._snapshots = []; this._current = -1; this._enabled = true; this._locked = false; this._pauseCount = 0; this._addSnapshotOnResume = false; this._overflowMaxUndoStepCount = false; this._changed = new EventObject(); this._undoPerformed = new EventObject(); this._redoPerformed = new EventObject(); this._snapshotHandler = handler; this.historySize = historySize; } set historySize(historySize) { if (historySize >= 0) this._maxUndoStepCount = historySize; else { console.warn("History: historySize value isn't correct, reset to 10"); this._maxUndoStepCount = 10; } } get canRedo() { return (this._current < this._snapshots.length - 1); } get canUndo() { return (this._current >= 1); } get overflowMaxUndoStepCount() { return this._overflowMaxUndoStepCount; } enable() { if (!this._enabled) this._enabled = true; } disable() { if (this._enabled) { this._enabled = false; this.clear(); } } isPaused() { return this._pauseCount > 0; } pause() { this._pauseCount++; if (this._pauseCount === 1) this._addSnapshotOnResume = false; } resume(addSnapshot = true, forceAddSnapshot = false, replaceLastSnapshot = false) { if (this._pauseCount <= 0) return; this._pauseCount--; if (this._pauseCount === 0) { if (addSnapshot && this._addSnapshotOnResume || forceAddSnapshot) this.addSnapshot(false, replaceLastSnapshot); } } raiseChanged() { this._changed.notify(); } _clearRedo() { this._snapshots.splice(this._current + 1); } _clearUndo() { this._snapshots.splice(0, this._current + 1); this._current = -1; this._overflowMaxUndoStepCount = false; } clear(suppressOnChanged = false) { this._snapshots = []; this._current = -1; this.addSnapshot(true); if (!suppressOnChanged) this.raiseChanged(); } addSnapshot(suppressOnChanged = false, replaceLastSnapshot = false) { if (!this._enabled || this._locked) return; if (this._pauseCount > 0) { this._addSnapshotOnResume = true; return; } const snapshot = this._snapshotHandler.createSnapshot(); if (snapshot == null) throw "Snapshot can not be null"; this._locked = true; if (this.canRedo) this._clearRedo(); if (replaceLastSnapshot && this._snapshots.length > 0) { this._snapshots.pop(); this._current--; } this._snapshots.push(snapshot); this._current++; this._locked = false; if (this._current > this._maxUndoStepCount) { this._snapshots.splice(0, 1); this._current--; this._overflowMaxUndoStepCount = true; } if (!suppressOnChanged) this.raiseChanged(); } redo() { if (!this._enabled || !this.canRedo) return; this._locked = true; this._current++; const snapshot = this._snapshots[this._current]; this._snapshotHandler.loadSnapshot(snapshot); this._locked = false; this.raiseChanged(); this._redoPerformed.notify(); } undo() { if (!this._enabled || !this.canUndo) return; this._locked = true; this._current--; const snapshot = this._snapshots[this._current]; this._snapshotHandler.loadSnapshot(snapshot); this._locked = false; this.raiseChanged(); this._undoPerformed.notify(); } addChanged(listener) { this._changed.add(listener); } removeChanged(listener) { this._changed.remove(listener); } addUndoPerformed(listener) { this._undoPerformed.add(listener); } removeUndoPerformed(listener) { this._undoPerformed.remove(listener); } addRedoPerformed(listener) { this._redoPerformed.add(listener); } removeRedoPerformed(listener) { this._redoPerformed.remove(listener); } } //# sourceMappingURL=History.js.map