This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
undoediting.js
185 lines (138 loc) · 5.17 KB
/
undoediting.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import UndoEditing from '../src/undoediting';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
describe( 'UndoEditing', () => {
let editor, undo, model, root;
beforeEach( () => {
editor = new ModelTestEditor();
model = editor.model;
root = model.document.getRoot();
undo = new UndoEditing( editor );
undo.init();
} );
afterEach( () => {
undo.destroy();
} );
it( 'should register undo command and redo command', () => {
expect( editor.commands.get( 'undo' ) ).to.equal( undo._undoCommand );
expect( editor.commands.get( 'redo' ) ).to.equal( undo._redoCommand );
} );
it( 'should add a batch to undo command and clear redo stack, if it\'s type is "default"', () => {
sinon.spy( undo._undoCommand, 'addBatch' );
sinon.spy( undo._redoCommand, 'clearStack' );
expect( undo._undoCommand.addBatch.called ).to.be.false;
expect( undo._redoCommand.clearStack.called ).to.be.false;
model.change( writer => {
writer.insertText( 'foobar', root );
} );
expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
expect( undo._redoCommand.clearStack.calledOnce ).to.be.true;
} );
it( 'should add each batch only once', () => {
sinon.spy( undo._undoCommand, 'addBatch' );
model.change( writer => {
writer.insertText( 'foobar', root );
writer.insertText( 'foobar', root );
} );
expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
} );
it( 'should add a batch to undo command, if it\'s type is undo and it comes from redo command', () => {
sinon.spy( undo._undoCommand, 'addBatch' );
sinon.spy( undo._redoCommand, 'clearStack' );
const batch = model.createBatch();
undo._redoCommand._createdBatches.add( batch );
model.enqueueChange( batch, writer => {
writer.insertText( 'foobar', root );
} );
expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
expect( undo._redoCommand.clearStack.called ).to.be.false;
} );
it( 'should add a batch to redo command on undo revert event', () => {
sinon.spy( undo._redoCommand, 'addBatch' );
sinon.spy( undo._redoCommand, 'clearStack' );
undo._undoCommand.fire( 'revert', null, model.createBatch() );
expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
expect( undo._redoCommand.clearStack.called ).to.be.false;
} );
it( 'should add redo batch to undo', () => {
sinon.spy( undo._undoCommand, 'addBatch' );
model.change( writer => {
writer.insertText( 'foobar', root );
} );
model.change( writer => {
writer.insertText( 'baz', root );
} );
editor.execute( 'undo' );
editor.execute( 'undo' );
editor.execute( 'redo' );
sinon.assert.calledThrice( undo._undoCommand.addBatch );
editor.execute( 'redo' );
sinon.assert.callCount( undo._undoCommand.addBatch, 4 );
} );
it( 'should not add a batch that has only non-document operations', () => {
sinon.spy( undo._undoCommand, 'addBatch' );
model.change( writer => {
const docFrag = writer.createDocumentFragment();
const element = writer.createElement( 'paragraph' );
writer.insert( element, docFrag, 0 );
writer.insertText( 'foo', null, element, 0 );
} );
expect( undo._undoCommand.addBatch.called ).to.be.false;
} );
it( 'should not add a transparent batch', () => {
sinon.spy( undo._undoCommand, 'addBatch' );
model.enqueueChange( 'transparent', writer => {
writer.insertText( 'foobar', root );
} );
expect( undo._undoCommand.addBatch.called ).to.be.false;
} );
it( 'should add a batch that has both document and non-document operations', () => {
sinon.spy( undo._undoCommand, 'addBatch' );
model.change( writer => {
const element = writer.createElement( 'paragraph' );
writer.insertText( 'foo', null, element, 0 );
writer.insert( element, root, 0 );
} );
expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
} );
it( 'should set CTRL+Z keystroke', () => {
const spy = sinon.stub( editor, 'execute' );
const wasHandled = editor.keystrokes.press( {
keyCode: keyCodes.z,
ctrlKey: true,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
} );
expect( wasHandled ).to.be.true;
expect( spy.calledWithExactly( 'undo' ) ).to.be.true;
} );
it( 'should set CTRL+Y keystroke', () => {
const spy = sinon.stub( editor, 'execute' );
const wasHandled = editor.keystrokes.press( {
keyCode: keyCodes.y,
ctrlKey: true,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
} );
expect( wasHandled ).to.be.true;
expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
} );
it( 'should set CTRL+SHIFT+Z keystroke', () => {
const spy = sinon.stub( editor, 'execute' );
const keyEventData = {
keyCode: keyCodes.z,
ctrlKey: true,
shiftKey: true,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
};
const wasHandled = editor.keystrokes.press( keyEventData );
expect( wasHandled ).to.be.true;
expect( spy.calledWithExactly( 'redo' ) ).to.be.true;
expect( keyEventData.preventDefault.calledOnce ).to.be.true;
} );
} );