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).

91 lines (86 loc) 3.49 kB
import { test } from 'substance-test' import { AbstractEditorSession } from 'substance' import createTestArticle from './shared/createTestArticle' import simple from './fixture/simple' test('ChangeHistory: undoing and redoing changes by a single user', t => { const { doc, editorSession } = _setup() const p1 = doc.get('p1') const originalContent = p1.getText() editorSession.setSelection({ type: 'property', path: p1.getPath(), startOffset: 0 }) editorSession.transaction(tx => { tx.insertText('X') }) editorSession.transaction(tx => { tx.insertText('Y') }) editorSession.transaction(tx => { tx.insertText('Z') }) t.comment('applying three changes') t.equal(p1.getText(), 'XYZ' + originalContent, 'the text should have been changed') t.ok(editorSession.canUndo(), 'undo should be possible') t.notOk(editorSession.canRedo(), 'redo should not be possible') t.comment('undo') editorSession.undo() t.equal(p1.getText(), 'XY' + originalContent, 'third change should have been undone') t.ok(editorSession.canUndo(), 'undo should be possible') t.ok(editorSession.canRedo(), 'redo should be possible') t.comment('undo') editorSession.undo() t.equal(p1.getText(), 'X' + originalContent, 'second change should have been undone') t.ok(editorSession.canUndo(), 'undo should be possible') t.ok(editorSession.canRedo(), 'redo should be possible') t.comment('undo') editorSession.undo() t.equal(p1.getText(), originalContent, 'first change should have been undone') t.notOk(editorSession.canUndo(), 'undo should not be possible') t.ok(editorSession.canRedo(), 'redo should be possible') t.comment('redo') editorSession.redo() t.equal(p1.getText(), 'X' + originalContent, 'first change should have been redone') t.ok(editorSession.canUndo(), 'undo should be possible') t.ok(editorSession.canRedo(), 'redo should be possible') t.comment('redo') editorSession.redo() t.equal(p1.getText(), 'XY' + originalContent, 'second change should have been redone') t.ok(editorSession.canUndo(), 'undo should be possible') t.ok(editorSession.canRedo(), 'redo should be possible') t.comment('redo') editorSession.redo() t.equal(p1.getText(), 'XYZ' + originalContent, 'third change should have been redone') t.ok(editorSession.canUndo(), 'undo should be possible') t.notOk(editorSession.canRedo(), 'redo should not be possible') t.comment('undo') editorSession.undo() t.equal(p1.getText(), 'XY' + originalContent, 'third change should have been undone') t.ok(editorSession.canUndo(), 'undo should be possible') t.ok(editorSession.canRedo(), 'redo should be possible') t.comment('undo') editorSession.undo() t.equal(p1.getText(), 'X' + originalContent, 'second change should have been undone') t.ok(editorSession.canUndo(), 'undo should be possible') t.ok(editorSession.canRedo(), 'redo should be possible') t.comment('undo') editorSession.undo() t.equal(p1.getText(), originalContent, 'first change should have been undone') t.notOk(editorSession.canUndo(), 'undo should not be possible') t.ok(editorSession.canRedo(), 'redo should be possible') t.end() }) function _setup () { const doc = createTestArticle(simple) const editorSession = new TestEditorSession('test', doc) return { doc, editorSession } } class TestEditorSession extends AbstractEditorSession { _getSelection () { return this._selection } _setSelection (sel) { this._selection = sel } }