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 19
/
decouplededitor.js
390 lines (360 loc) · 14.4 KB
/
decouplededitor.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module editor-decoupled/decouplededitor
*/
import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import DecoupledEditorUI from './decouplededitorui';
import DecoupledEditorUIView from './decouplededitoruiview';
import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement';
import log from '@ckeditor/ckeditor5-utils/src/log';
import { isElement } from 'lodash-es';
/**
* The {@glink builds/guides/overview#decoupled-editor decoupled editor} implementation.
* It provides an inline editables and a toolbar. However, unlike other editors,
* it does not render these components anywhere in the DOM unless configured.
*
* This type of an editor is dedicated to integrations which require a customized UI with an open
* structure, allowing developers to specify the exact location of the interface.
*
* See the {@glink examples/builds/document-editor document editor demo} to learn about possible use cases for the decoupled editor.
*
* In order to create a decoupled editor instance, use the static
* {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`} method.
*
* # Decoupled editor and document editor build
*
* The decoupled editor can be used directly from source (if you installed the
* [`@ckeditor/ckeditor5-editor-decoupled`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-decoupled) package)
* but it is also available in the {@glink builds/guides/overview#document-editor document editor build}.
*
* {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
* source you need to take care of loading all plugins by yourself
* (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
* Using the editor from source gives much better flexibility and allows for easier customization.
*
* Read more about initializing the editor from source or as a build in
* {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}.
*
* @implements module:core/editor/editorwithui~EditorWithUI
* @extends module:core/editor/editor~Editor
*/
export default class DecoupledEditor extends Editor {
/**
* Creates an instance of the decoupled editor.
*
* **Note:** Do not use the constructor to create editor instances. Use the static
* {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`} method instead.
*
* @protected
* @param {Object.<String,String>|Object.<String,HTMLElement>} sourceElementsOrData The object where each key
* is the name of the root and corresponding value is the DOM element that will be the source for the created editor
* (on which the editor will be initialized) or initial data for the editor. For more information see
* {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}.
* @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
*/
constructor( sourceElementsOrData, config ) {
super( config );
/**
* The array of all sourceElements (which becomes editable areas) if any were passed in `sourceElementsOrData`.
*
* @protected
* @member {Array.<HTMLElement>}
*/
this._sourceElements = [];
/**
* The array of all root names.
*
* @protected
* @member {Array.<String>}
*/
this._rootNames = sourceElementsOrData ? Object.keys( sourceElementsOrData ) : [];
this.data.processor = new HtmlDataProcessor();
const editables = [];
for ( const name of this._rootNames ) {
const sourceElementOrData = sourceElementsOrData[ name ];
this.model.document.createRoot( '$root', name );
let sourceElement = null;
if ( isElement( sourceElementOrData ) ) {
sourceElement = sourceElementOrData;
this._sourceElements.push( sourceElement );
}
editables.push( { name, sourceElement } );
}
this.ui = new DecoupledEditorUI( this, new DecoupledEditorUIView( this.locale, editables ) );
}
/**
* @inheritDoc
*/
get element() {
// This editor has no single "main UI element". Its editable and toolbar are exposed separately and need
// to be added to the DOM manually by the consumer.
return null;
}
/**
* Destroys the editor instance, releasing all resources used by it.
*
* **Note**: The decoupled editor does not remove the toolbar and editables when destroyed. You can
* do that yourself in the destruction chain:
*
* editor.destroy()
* .then( () => {
* // Remove the toolbar from DOM.
* editor.ui.view.toolbar.element.remove();
*
* // Remove editables from DOM.
* editor.ui.view.editables.forEach( editable => editable.element.remove() );
*
* console.log( 'Editor was destroyed' );
* } );
*
* @returns {Promise}
*/
destroy() {
// Cache the data, then destroy.
// It's safe to assume that the model->view conversion will not work after super.destroy().
const data = [];
for ( const rootName of this._rootNames ) {
data.push( this.getData( rootName ) );
}
this.ui.destroy();
return super.destroy()
.then( () => {
if ( this._sourceElements.length ) {
for ( let i = 0; i < this._rootNames.length; i++ ) {
setDataInElement( this._sourceElements[ i ], data[ i ] );
}
}
} );
}
/**
* Gets the data from the editor.
*
* editor.getData(); // -> '<p>This is editor!</p>'
* editor.getData( 'rootName' ); // -> '<h1>CKEditor 5</h1>'
*
* By default the editor outputs HTML. This can be controlled by injecting a different data processor.
* See the {@glink features/markdown Markdown output} guide for more details.
*
* Note: Not only is the format of the data configurable, but the type of the `getData()`'s return value does not
* have to be a string either. You can e.g. return an object or a DOM `DocumentFragment` if you consider this
* the right format for you.
*
* @method #getData
* @param {String} [rootName='main'] The root name from which to get data.
* @returns {String} Output data.
*/
getData( rootName = 'main' ) {
if ( !this._rootNames.includes( rootName ) ) {
/**
* Thrown when there is an attempt to get data on a non-existing root.
*
* @error trying-to-get-data-on-non-existing-root
*/
throw new CKEditorError( 'trying-to-get-data-on-non-existing-root: ' +
`Attempting to get data from non-existing "${ rootName }" root.`
);
}
return this.data.get( rootName );
}
/**
* Sets the data in the editor editable areas.
*
* There are couple of ways `setData` can be used. When only data is passed it is set on the default root (`main`):
*
* editor.setData( '<h1>CKEditor 5</h1>' ); // Sets data on the `main` root.
*
* Along with the data, object specifying root name can be passed:
*
* editor.setData( '<p>This is editor!</p>', { rootName: 'content' } ); // Sets data on the `content` root.
*
* Finally, object specifying `rootName` - `data` pairs could be used:
*
* editor.setData( { main: '<h1>CKEditor 5</h1>', content: '<p>This is editor!</p>' } ); // Sets data on `main` and `content` root.
*
* By default the editor accepts HTML. This can be controlled by injecting a different data processor.
* See the {@glink features/markdown Markdown output} guide for more details.
*
* Note: Not only is the format of the data configurable, but the type of the `setData()`'s data values does not
* have to be a string either. The data can be e.g. an object or a DOM `DocumentFragment` if you consider this
* the right format for you.
*
* @method #setData
* @param {String|Object.<String,String>} data Data as a sting or an object containing input data where each key
* is the name of the root and corresponding value is a data to set.
* @param {Object} [options] The options object specifying on which root to set data. Has effect only if first
* parameter is passed as a string.
*/
setData( data, options ) {
let roots = {};
if ( typeof data === 'string' ) {
const rootName = options && options.rootName ? options.rootName : 'main';
roots[ rootName ] = data;
} else {
roots = data;
}
for ( const [ rootName, content ] of Object.entries( roots ) ) {
if ( !this._rootNames.includes( rootName ) ) {
/**
* Thrown when there is an attempt to set data on a non-existing root.
*
* @error trying-to-set-data-on-non-existing-root
*/
log.warn( `trying-to-set-data-on-non-existing-root: Attempting to set data on non-existing "${ rootName }" root.` );
} else {
this.data.set( content, rootName );
}
}
}
/**
* Creates a decoupled editor instance.
*
* Creating an instance when using a {@glink builds/index CKEditor 5 build}:
*
* DecoupledEditor
* .create( document.querySelector( '#editor' ) )
* .then( editor => {
* // Append the toolbar to the <body> element.
* document.body.appendChild( editor.ui.view.toolbar.element );
*
* console.log( 'Editor was initialized', editor );
* } )
* .catch( err => {
* console.error( err.stack );
* } );
*
* Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
*
* import DecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor';
* import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
* import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
* import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
* import ...
*
* DecoupledEditor
* .create( document.querySelector( '#editor' ), {
* plugins: [ Essentials, Bold, Italic, ... ],
* toolbar: [ 'bold', 'italic', ... ]
* } )
* .then( editor => {
* // Append the toolbar to the <body> element.
* document.body.appendChild( editor.ui.view.toolbar.element );
*
* console.log( 'Editor was initialized', editor );
* } )
* .catch( err => {
* console.error( err.stack );
* } );
*
* It is possible to create the editor out of a pure data string. The editor will then render
* an editable element that must be inserted into the DOM for the editor to work properly:
*
* DecoupledEditor
* .create( '<p>Editor data</p>' )
* .then( editor => {
* // Append the toolbar to the <body> element.
* document.body.appendChild( editor.ui.view.toolbar.element );
*
* // Append the editable to the <body> element.
* document.body.appendChild( editor.ui.view.editables[ 0 ].element );
*
* console.log( 'Editor was initialized', editor );
* } )
* .catch( err => {
* console.error( err.stack );
* } );
*
* Since decoupled editor supports multiple roots, it can be created by passing object containing `rootName` - `DOM
* element` pairs:
*
* DecoupledEditor
* .create( {
* title: document.querySelector( '#editor-title' ),
* content: document.querySelector( '#editor-content' )
* } )
* .then( editor => {
* // Append the toolbar to the <body> element.
* document.body.appendChild( editor.ui.view.toolbar.element );
*
* console.log( 'Editor was initialized', editor );
* } )
* .catch( err => {
* console.error( err.stack );
* } );
*
* or a `rootName` - `initial data` pairs:
*
* DecoupledEditor
* .create( {
* title: '<h1>Multiple root editor</h1>',
* content: '<p>Second editor root.</p>'
* } )
* .then( editor => {
* // Append the toolbar to the <body> element.
* document.body.appendChild( editor.ui.view.toolbar.element );
*
* // Append the editables to the <body> element.
* editor.ui.view.editables.forEach( editable => document.body.appendChild( editable.element ) );
*
* console.log( 'Editor was initialized', editor );
* } )
* .catch( err => {
* console.error( err.stack );
* } );
*
* @param {HTMLElement|String|Object.<String,String>|Object.<String,HTMLElement>} sourceElementOrData The DOM element
* that will be the source for the created editor (on which the editor will be initialized), initial data for the editor
* or an object containing `rootName` - `DOM element` or `rootName` - `initial data` pairs for creating multiple root editor.
*
* If a source elements are passed (directly or in the object parameter), then its contents will be automatically
* {@link module:editor-decoupled/decouplededitor~DecoupledEditor#setData loaded} to the editor on startup and the elements
* itself will be used as the editor's editable elements.
*
* If data is provided, then the `editor.ui.view.editables` array will be created automatically and each root
* (e.g. `editor.ui.view.editables[ 0 ].element`) needs to be added to the DOM manually.
* @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
* @returns {Promise} A promise resolved once the editor is ready.
* The promise returns the created {@link module:editor-decoupled/decouplededitor~DecoupledEditor} instance.
*/
static create( sourceElementsOrData, config ) {
const newSourceElementsOrData = handleSingleInput( sourceElementsOrData );
return new Promise( resolve => {
const editor = new this( newSourceElementsOrData, config );
resolve(
editor.initPlugins()
.then( () => {
editor.ui.init();
editor.fire( 'uiReady' );
} )
.then( () => {
const data = {};
const names = Object.keys( newSourceElementsOrData );
for ( const name of names ) {
const sourceElementOrData = newSourceElementsOrData[ name ];
const initialData = isElement( sourceElementOrData ) ?
getDataFromElement( sourceElementOrData ) :
sourceElementOrData;
data[ name ] = initialData;
}
return editor.data.init( data );
} )
.then( () => {
editor.fire( 'dataReady' );
editor.fire( 'ready' );
} )
.then( () => editor )
);
} );
}
}
function handleSingleInput( sourceElementOrData ) {
if ( typeof sourceElementOrData === 'string' || isElement( sourceElementOrData ) ) {
return { main: sourceElementOrData };
}
return sourceElementOrData;
}