This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
/
decouplededitorui.js
314 lines (249 loc) · 8.51 KB
/
decouplededitorui.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals document, Event */
import View from '@ckeditor/ckeditor5-ui/src/view';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import DecoupledEditorUI from '../src/decouplededitorui';
import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import DecoupledEditorUIView from '../src/decouplededitoruiview';
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import { assertBinding } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
import { isElement } from 'lodash-es';
describe( 'DecoupledEditorUI', () => {
let editor, view, ui, viewElement;
testUtils.createSinonSandbox();
beforeEach( () => {
return VirtualDecoupledTestEditor
.create( '', {
toolbar: [ 'foo', 'bar' ],
} )
.then( newEditor => {
editor = newEditor;
ui = editor.ui;
view = ui.view;
viewElement = view.element;
} );
} );
afterEach( () => {
editor.destroy();
} );
describe( 'constructor()', () => {
it( 'extends EditorUI', () => {
expect( ui ).to.instanceof( EditorUI );
} );
} );
describe( 'init()', () => {
it( 'renders the #view', () => {
expect( view.isRendered ).to.be.true;
} );
describe( 'editable', () => {
it( 'registers view.editable#element in editor focus tracker', () => {
ui.focusTracker.isFocused = false;
view.editable.element.dispatchEvent( new Event( 'focus' ) );
expect( ui.focusTracker.isFocused ).to.true;
} );
it( 'sets view.editable#name', () => {
const editable = editor.editing.view.document.getRoot();
expect( view.editable.name ).to.equal( editable.rootName );
} );
it( 'binds view.editable#isFocused', () => {
assertBinding(
view.editable,
{ isFocused: false },
[
[ ui.focusTracker, { isFocused: true } ]
],
{ isFocused: true }
);
} );
it( 'attaches editable UI as view\'s DOM root', () => {
expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
} );
} );
describe( 'placeholder', () => {
it( 'sets placeholder from editor.config.placeholder', () => {
return VirtualDecoupledTestEditor
.create( 'foo', {
extraPlugins: [ Paragraph ],
placeholder: 'placeholder-text',
} )
.then( newEditor => {
const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
return newEditor.destroy();
} );
} );
it( 'sets placeholder from the "placeholder" attribute of a passed <textarea>', () => {
const element = document.createElement( 'textarea' );
element.setAttribute( 'placeholder', 'placeholder-text' );
return VirtualDecoupledTestEditor
.create( element, {
extraPlugins: [ Paragraph ]
} )
.then( newEditor => {
const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'placeholder-text' );
return newEditor.destroy();
} );
} );
it( 'uses editor.config.placeholder rather than the "placeholder" attribute of a passed <textarea>', () => {
const element = document.createElement( 'textarea' );
element.setAttribute( 'placeholder', 'placeholder-text' );
return VirtualDecoupledTestEditor
.create( element, {
placeholder: 'config takes precedence',
extraPlugins: [ Paragraph ]
} )
.then( newEditor => {
const firstChild = newEditor.editing.view.document.getRoot().getChild( 0 );
expect( firstChild.getAttribute( 'data-placeholder' ) ).to.equal( 'config takes precedence' );
return newEditor.destroy();
} );
} );
} );
describe( 'view.toolbar', () => {
describe( '#items', () => {
it( 'are filled with the config.toolbar (specified as an Array)', () => {
return VirtualDecoupledTestEditor
.create( '', {
toolbar: [ 'foo', 'bar' ]
} )
.then( editor => {
const items = editor.ui.view.toolbar.items;
expect( items.get( 0 ).name ).to.equal( 'foo' );
expect( items.get( 1 ).name ).to.equal( 'bar' );
return editor.destroy();
} );
} );
it( 'are filled with the config.toolbar (specified as an Object)', () => {
return VirtualDecoupledTestEditor
.create( '', {
toolbar: {
items: [ 'foo', 'bar' ],
viewportTopOffset: 100
}
} )
.then( editor => {
const items = editor.ui.view.toolbar.items;
expect( items.get( 0 ).name ).to.equal( 'foo' );
expect( items.get( 1 ).name ).to.equal( 'bar' );
return editor.destroy();
} );
} );
} );
} );
it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
return VirtualDecoupledTestEditor.create( '' )
.then( editor => {
const ui = editor.ui;
const view = ui.view;
const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
ui.focusTracker.isFocused = true;
ui.view.toolbar.focusTracker.isFocused = false;
editor.keystrokes.press( {
keyCode: keyCodes.f10,
altKey: true,
preventDefault: sinon.spy(),
stopPropagation: sinon.spy()
} );
sinon.assert.calledOnce( spy );
return editor.destroy();
} );
} );
} );
describe( 'destroy()', () => {
it( 'detaches the DOM root then destroys the UI view', () => {
return VirtualDecoupledTestEditor.create( '' )
.then( newEditor => {
const destroySpy = sinon.spy( newEditor.ui.view, 'destroy' );
const detachSpy = sinon.spy( newEditor.editing.view, 'detachDomRoot' );
return newEditor.destroy()
.then( () => {
sinon.assert.callOrder( detachSpy, destroySpy );
} );
} );
} );
it( 'restores the editor element back to its original state', () => {
const domElement = document.createElement( 'div' );
domElement.setAttribute( 'foo', 'bar' );
domElement.setAttribute( 'data-baz', 'qux' );
domElement.classList.add( 'foo-class' );
return VirtualDecoupledTestEditor.create( domElement )
.then( newEditor => {
return newEditor.destroy()
.then( () => {
const attributes = {};
for ( const attribute of Array.from( domElement.attributes ) ) {
attributes[ attribute.name ] = attribute.value;
}
expect( attributes ).to.deep.equal( {
foo: 'bar',
'data-baz': 'qux',
class: 'foo-class'
} );
} );
} );
} );
} );
describe( 'element()', () => {
it( 'returns correct element instance', () => {
expect( ui.element ).to.equal( viewElement );
} );
} );
describe( 'getEditableElement()', () => {
it( 'returns editable element (default)', () => {
expect( ui.getEditableElement() ).to.equal( view.editable.element );
} );
it( 'returns editable element (root name passed)', () => {
expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
} );
it( 'returns undefined if editable with the given name is absent', () => {
expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
} );
} );
} );
function viewCreator( name ) {
return locale => {
const view = new View( locale );
view.name = name;
view.element = document.createElement( 'a' );
return view;
};
}
class VirtualDecoupledTestEditor extends VirtualTestEditor {
constructor( sourceElementOrData, config ) {
super( config );
if ( isElement( sourceElementOrData ) ) {
this.sourceElement = sourceElementOrData;
}
const view = new DecoupledEditorUIView( this.locale, this.editing.view );
this.ui = new DecoupledEditorUI( this, view );
this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
}
destroy() {
this.ui.destroy();
return super.destroy();
}
static create( sourceElementOrData, config ) {
return new Promise( resolve => {
const editor = new this( sourceElementOrData, config );
resolve(
editor.initPlugins()
.then( () => {
editor.ui.init();
const initialData = isElement( sourceElementOrData ) ?
sourceElementOrData.innerHTML :
sourceElementOrData;
editor.data.init( initialData );
editor.fire( 'ready' );
} )
.then( () => editor )
);
} );
}
}