UNPKG

substance

Version:

Substance is a JavaScript library for web-based content editing. It provides building blocks for realizing custom text editors and web-based publishing system. It is developed to power our online editing platform [Substance](http://substance.io).

51 lines (43 loc) 1.06 kB
import { last } from '../util' export default class SimpleChangeHistory { constructor (editorSession) { this._editorSession = editorSession this._done = [] this._undone = [] } canUndo () { return this._done.length > 0 } canRedo () { return this._undone.length > 0 } getChanges () { return this._done.slice() } addChange (change) { this._done.push(change) // undone changes are cleared whenever a new change is recorded if (this._undone.length > 0) { this._undone.length = 0 } } undo () { const change = last(this._done) if (change) { const inverted = this._editorSession.getDocument().invert(change) this._editorSession.applyChange(inverted, { replay: true }) this._done.pop() this._undone.push(change) return inverted } } redo () { const change = last(this._undone) if (change) { this._editorSession.applyChange(change, { replay: true }) this._undone.pop() this._done.push(change) return change } } }