@ckeditor/ckeditor5-editor-multi-root 0.0.0-internal-20241017.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,1079 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ import { Editor, secureSourceElement } from '@ckeditor/ckeditor5-core/dist/index.js';
6
+ import { CKEditorError, logWarning, setDataInElement, getDataFromElement } from '@ckeditor/ckeditor5-utils/dist/index.js';
7
+ import { EditorUI, EditorUIView, ToolbarView, MenuBarView, InlineEditableUIView } from '@ckeditor/ckeditor5-ui/dist/index.js';
8
+ import { enablePlaceholder } from '@ckeditor/ckeditor5-engine/dist/index.js';
9
+ import { isElement as isElement$1 } from 'lodash-es';
10
+
11
+ /**
12
+ * The multi-root editor UI class.
13
+ */ class MultiRootEditorUI extends EditorUI {
14
+ /**
15
+ * The main (top–most) view of the editor UI.
16
+ */ view;
17
+ /**
18
+ * The editable element that was focused the last time when any of the editables had focus.
19
+ */ _lastFocusedEditableElement;
20
+ /**
21
+ * Creates an instance of the multi-root editor UI class.
22
+ *
23
+ * @param editor The editor instance.
24
+ * @param view The view of the UI.
25
+ */ constructor(editor, view){
26
+ super(editor);
27
+ this.view = view;
28
+ this._lastFocusedEditableElement = null;
29
+ }
30
+ /**
31
+ * Initializes the UI.
32
+ */ init() {
33
+ const view = this.view;
34
+ view.render();
35
+ // Keep track of the last focused editable element. Knowing which one was focused
36
+ // is useful when the focus moves from editable to other UI components like balloons
37
+ // (especially inputs) but the editable remains the "focus context" (e.g. link balloon
38
+ // attached to a link in an editable). In this case, the editable should preserve visual
39
+ // focus styles.
40
+ this.focusTracker.on('change:focusedElement', (evt, name, focusedElement)=>{
41
+ for (const editable of Object.values(this.view.editables)){
42
+ if (focusedElement === editable.element) {
43
+ this._lastFocusedEditableElement = editable.element;
44
+ }
45
+ }
46
+ });
47
+ // If the focus tracker loses focus, stop tracking the last focused editable element.
48
+ // Wherever the focus is restored, it will no longer be in the context of that editable
49
+ // because the focus "came from the outside", as opposed to the focus moving from one element
50
+ // to another within the editor UI.
51
+ this.focusTracker.on('change:isFocused', (evt, name, isFocused)=>{
52
+ if (!isFocused) {
53
+ this._lastFocusedEditableElement = null;
54
+ }
55
+ });
56
+ for (const editable of Object.values(this.view.editables)){
57
+ this.addEditable(editable);
58
+ }
59
+ this._initToolbar();
60
+ this._initMenuBar(this.view.menuBarView);
61
+ this.fire('ready');
62
+ }
63
+ /**
64
+ * Adds the editable to the editor UI.
65
+ *
66
+ * After the editable is added to the editor UI it can be considered "active".
67
+ *
68
+ * The editable is attached to the editor editing pipeline, which means that it will be updated as the editor model updates and
69
+ * changing its content will be reflected in the editor model. Keystrokes, focus handling and placeholder are initialized.
70
+ *
71
+ * @param editable The editable instance to add.
72
+ * @param placeholder Placeholder for the editable element. If not set, placeholder value from the
73
+ * {@link module:core/editor/editorconfig~EditorConfig#placeholder editor configuration} will be used (if it was provided).
74
+ */ addEditable(editable, placeholder) {
75
+ // The editable UI element in DOM is available for sure only after the editor UI view has been rendered.
76
+ // But it can be available earlier if a DOM element has been passed to `MultiRootEditor.create()`.
77
+ const editableElement = editable.element;
78
+ // Bind the editable UI element to the editing view, making it an end– and entry–point
79
+ // of the editor's engine. This is where the engine meets the UI.
80
+ this.editor.editing.view.attachDomRoot(editableElement, editable.name);
81
+ // Register each editable UI view in the editor.
82
+ this.setEditableElement(editable.name, editableElement);
83
+ // Let the editable UI element respond to the changes in the global editor focus
84
+ // tracker. It has been added to the same tracker a few lines above but, in reality, there are
85
+ // many focusable areas in the editor, like balloons, toolbars or dropdowns and as long
86
+ // as they have focus, the editable should act like it is focused too (although technically
87
+ // it isn't), e.g. by setting the proper CSS class, visually announcing focus to the user.
88
+ // Doing otherwise will result in editable focus styles disappearing, once e.g. the
89
+ // toolbar gets focused.
90
+ editable.bind('isFocused').to(this.focusTracker, 'isFocused', this.focusTracker, 'focusedElement', (isFocused, focusedElement)=>{
91
+ // When the focus tracker is blurred, it means the focus moved out of the editor UI.
92
+ // No editable will maintain focus then.
93
+ if (!isFocused) {
94
+ return false;
95
+ }
96
+ // If the focus tracker says the editor UI is focused and currently focused element
97
+ // is the editable, then the editable should be visually marked as focused too.
98
+ if (focusedElement === editableElement) {
99
+ return true;
100
+ } else {
101
+ return this._lastFocusedEditableElement === editableElement;
102
+ }
103
+ });
104
+ this._initPlaceholder(editable, placeholder);
105
+ }
106
+ /**
107
+ * Removes the editable instance from the editor UI.
108
+ *
109
+ * Removed editable can be considered "deactivated".
110
+ *
111
+ * The editable is detached from the editing pipeline, so model changes are no longer reflected in it. All handling added in
112
+ * {@link #addEditable} is removed.
113
+ *
114
+ * @param editable Editable to remove from the editor UI.
115
+ */ removeEditable(editable) {
116
+ this.editor.editing.view.detachDomRoot(editable.name);
117
+ editable.unbind('isFocused');
118
+ this.removeEditableElement(editable.name);
119
+ }
120
+ /**
121
+ * @inheritDoc
122
+ */ destroy() {
123
+ super.destroy();
124
+ for (const editable of Object.values(this.view.editables)){
125
+ this.removeEditable(editable);
126
+ }
127
+ this.view.destroy();
128
+ }
129
+ /**
130
+ * Initializes the editor main toolbar and its panel.
131
+ */ _initToolbar() {
132
+ const editor = this.editor;
133
+ const view = this.view;
134
+ const toolbar = view.toolbar;
135
+ toolbar.fillFromConfig(editor.config.get('toolbar'), this.componentFactory);
136
+ // Register the toolbar, so it becomes available for Alt+F10 and Esc navigation.
137
+ this.addToolbar(view.toolbar);
138
+ }
139
+ /**
140
+ * Enables the placeholder text on a given editable.
141
+ *
142
+ * @param editable Editable on which the placeholder should be set.
143
+ * @param placeholder Placeholder for the editable element. If not set, placeholder value from the
144
+ * {@link module:core/editor/editorconfig~EditorConfig#placeholder editor configuration} will be used (if it was provided).
145
+ */ _initPlaceholder(editable, placeholder) {
146
+ if (!placeholder) {
147
+ const configPlaceholder = this.editor.config.get('placeholder');
148
+ if (configPlaceholder) {
149
+ placeholder = typeof configPlaceholder === 'string' ? configPlaceholder : configPlaceholder[editable.name];
150
+ }
151
+ }
152
+ const editingView = this.editor.editing.view;
153
+ const editingRoot = editingView.document.getRoot(editable.name);
154
+ if (placeholder) {
155
+ editingRoot.placeholder = placeholder;
156
+ }
157
+ enablePlaceholder({
158
+ view: editingView,
159
+ element: editingRoot,
160
+ isDirectHost: false,
161
+ keepOnFocus: true
162
+ });
163
+ }
164
+ }
165
+
166
+ /**
167
+ * The multi-root editor UI view. It is a virtual view providing an inline
168
+ * {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#editable} and a
169
+ * {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#toolbar}, but without any
170
+ * specific arrangement of the components in the DOM.
171
+ *
172
+ * See {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`}
173
+ * to learn more about this view.
174
+ */ class MultiRootEditorUIView extends EditorUIView {
175
+ /**
176
+ * The main toolbar of the multi-root editor UI.
177
+ */ toolbar;
178
+ /**
179
+ * Editable elements used by the multi-root editor UI.
180
+ */ editables;
181
+ editable;
182
+ /**
183
+ * Menu bar view instance.
184
+ */ menuBarView;
185
+ /**
186
+ * The editing view instance this view is related to.
187
+ */ _editingView;
188
+ /**
189
+ * Creates an instance of the multi-root editor UI view.
190
+ *
191
+ * @param locale The {@link module:core/editor/editor~Editor#locale} instance.
192
+ * @param editingView The editing view instance this view is related to.
193
+ * @param editableNames Names for all editable views. For each name, one
194
+ * {@link module:ui/editableui/inline/inlineeditableuiview~InlineEditableUIView `InlineEditableUIView`} instance will be initialized.
195
+ * @param options Configuration options for the view instance.
196
+ * @param options.editableElements The editable elements to be used, assigned to their names. If not specified, they will be
197
+ * automatically created by {@link module:ui/editableui/inline/inlineeditableuiview~InlineEditableUIView `InlineEditableUIView`}
198
+ * instances.
199
+ * @param options.shouldToolbarGroupWhenFull When set to `true` enables automatic items grouping
200
+ * in the main {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#toolbar toolbar}.
201
+ * See {@link module:ui/toolbar/toolbarview~ToolbarOptions#shouldGroupWhenFull} to learn more.
202
+ * @param options.label When set, this value will be used as an accessible `aria-label` of the
203
+ * {@link module:ui/editableui/editableuiview~EditableUIView editable view} elements.
204
+ */ constructor(locale, editingView, editableNames, options = {}){
205
+ super(locale);
206
+ this._editingView = editingView;
207
+ this.toolbar = new ToolbarView(locale, {
208
+ shouldGroupWhenFull: options.shouldToolbarGroupWhenFull
209
+ });
210
+ this.menuBarView = new MenuBarView(locale);
211
+ this.editables = {};
212
+ // Create `InlineEditableUIView` instance for each editable.
213
+ for (const editableName of editableNames){
214
+ const editableElement = options.editableElements ? options.editableElements[editableName] : undefined;
215
+ let { label } = options;
216
+ if (typeof label === 'object') {
217
+ label = label[editableName];
218
+ }
219
+ this.createEditable(editableName, editableElement, label);
220
+ }
221
+ this.editable = Object.values(this.editables)[0];
222
+ // This toolbar may be placed anywhere in the page so things like font size need to be reset in it.
223
+ // Because of the above, make sure the toolbar supports rounded corners.
224
+ // Also, make sure the toolbar has the proper dir attribute because its ancestor may not have one
225
+ // and some toolbar item styles depend on this attribute.
226
+ this.toolbar.extendTemplate({
227
+ attributes: {
228
+ class: [
229
+ 'ck-reset_all',
230
+ 'ck-rounded-corners'
231
+ ],
232
+ dir: locale.uiLanguageDirection
233
+ }
234
+ });
235
+ this.menuBarView.extendTemplate({
236
+ attributes: {
237
+ class: [
238
+ 'ck-reset_all',
239
+ 'ck-rounded-corners'
240
+ ],
241
+ dir: locale.uiLanguageDirection
242
+ }
243
+ });
244
+ }
245
+ /**
246
+ * Creates an editable instance with given name and registers it in the editor UI view.
247
+ *
248
+ * If `editableElement` is provided, the editable instance will be created on top of it. Otherwise, the editor will create a new
249
+ * DOM element and use it instead.
250
+ *
251
+ * @param editableName The name for the editable.
252
+ * @param editableElement DOM element for which the editable should be created.
253
+ * @param label The accessible editable label used by assistive technologies.
254
+ * @returns The created editable instance.
255
+ */ createEditable(editableName, editableElement, label) {
256
+ const editable = new InlineEditableUIView(this.locale, this._editingView, editableElement, {
257
+ label
258
+ });
259
+ this.editables[editableName] = editable;
260
+ editable.name = editableName;
261
+ if (this.isRendered) {
262
+ this.registerChild(editable);
263
+ }
264
+ return editable;
265
+ }
266
+ /**
267
+ * Destroys and removes the editable from the editor UI view.
268
+ *
269
+ * @param editableName The name of the editable that should be removed.
270
+ */ removeEditable(editableName) {
271
+ const editable = this.editables[editableName];
272
+ if (this.isRendered) {
273
+ this.deregisterChild(editable);
274
+ }
275
+ delete this.editables[editableName];
276
+ editable.destroy();
277
+ }
278
+ /**
279
+ * @inheritDoc
280
+ */ render() {
281
+ super.render();
282
+ this.registerChild(Object.values(this.editables));
283
+ this.registerChild(this.toolbar);
284
+ this.registerChild(this.menuBarView);
285
+ }
286
+ }
287
+
288
+ /**
289
+ * The multi-root editor implementation.
290
+ *
291
+ * The multi-root editor provides multiple inline editable elements and a toolbar. All editable areas are controlled by one editor
292
+ * instance, which means that they share common configuration, document ID, or undo stack.
293
+ *
294
+ * This type of editor is dedicated to integrations which require a customized UI with an open structure, featuring multiple editable areas,
295
+ * allowing developers to have a control over the exact location of these editable areas.
296
+ *
297
+ * In order to create a multi-root editor instance, use the static
298
+ * {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`} method.
299
+ *
300
+ * Note that you will need to attach the editor toolbar to your web page manually, in a desired place, after the editor is initialized.
301
+ */ class MultiRootEditor extends Editor {
302
+ /**
303
+ * @inheritdoc
304
+ */ static get editorName() {
305
+ return 'MultiRootEditor';
306
+ }
307
+ /**
308
+ * @inheritDoc
309
+ */ ui;
310
+ /**
311
+ * The elements on which the editor has been initialized.
312
+ */ sourceElements;
313
+ /**
314
+ * Holds attributes keys that were passed in {@link module:core/editor/editorconfig~EditorConfig#rootsAttributes `rootsAttributes`}
315
+ * config property and should be returned by {@link #getRootsAttributes}.
316
+ */ _registeredRootsAttributesKeys = new Set();
317
+ /**
318
+ * A set of lock IDs for enabling or disabling particular root.
319
+ */ _readOnlyRootLocks = new Map();
320
+ /**
321
+ * Creates an instance of the multi-root editor.
322
+ *
323
+ * **Note:** Do not use the constructor to create editor instances. Use the static
324
+ * {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`} method instead.
325
+ *
326
+ * @param sourceElementsOrData The DOM elements that will be the source for the created editor
327
+ * or the editor's initial data. The editor will initialize multiple roots with names according to the keys in the passed object.
328
+ * For more information see {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`}.
329
+ * @param config The editor configuration.
330
+ */ constructor(sourceElementsOrData, config = {}){
331
+ const rootNames = Object.keys(sourceElementsOrData);
332
+ const sourceIsData = rootNames.length === 0 || typeof sourceElementsOrData[rootNames[0]] === 'string';
333
+ if (sourceIsData && config.initialData !== undefined && Object.keys(config.initialData).length > 0) {
334
+ // Documented in core/editor/editorconfig.jsdoc.
335
+ // eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
336
+ throw new CKEditorError('editor-create-initial-data', null);
337
+ }
338
+ super(config);
339
+ if (!sourceIsData) {
340
+ this.sourceElements = sourceElementsOrData;
341
+ } else {
342
+ this.sourceElements = {};
343
+ }
344
+ if (this.config.get('initialData') === undefined) {
345
+ // Create initial data object containing data from all roots.
346
+ const initialData = {};
347
+ for (const rootName of rootNames){
348
+ initialData[rootName] = getInitialData(sourceElementsOrData[rootName]);
349
+ }
350
+ this.config.set('initialData', initialData);
351
+ }
352
+ if (!sourceIsData) {
353
+ for (const rootName of rootNames){
354
+ secureSourceElement(this, sourceElementsOrData[rootName]);
355
+ }
356
+ }
357
+ this.editing.view.document.roots.on('add', (evt, viewRoot)=>{
358
+ // Here we change the standard binding of readOnly flag by adding
359
+ // additional constraint that multi-root has (enabling / disabling particular root).
360
+ viewRoot.unbind('isReadOnly');
361
+ viewRoot.bind('isReadOnly').to(this.editing.view.document, 'isReadOnly', (isReadOnly)=>{
362
+ return isReadOnly || this._readOnlyRootLocks.has(viewRoot.rootName);
363
+ });
364
+ // Hacky solution to nested editables.
365
+ // Nested editables should be managed each separately and do not base on view document or view root.
366
+ viewRoot.on('change:isReadOnly', (evt, prop, value)=>{
367
+ const viewRange = this.editing.view.createRangeIn(viewRoot);
368
+ for (const viewItem of viewRange.getItems()){
369
+ if (viewItem.is('editableElement')) {
370
+ viewItem.unbind('isReadOnly');
371
+ viewItem.isReadOnly = value;
372
+ }
373
+ }
374
+ });
375
+ });
376
+ for (const rootName of rootNames){
377
+ // Create root and `UIView` element for each editable container.
378
+ this.model.document.createRoot('$root', rootName);
379
+ }
380
+ if (this.config.get('lazyRoots')) {
381
+ for (const rootName of this.config.get('lazyRoots')){
382
+ const root = this.model.document.createRoot('$root', rootName);
383
+ root._isLoaded = false;
384
+ }
385
+ }
386
+ if (this.config.get('rootsAttributes')) {
387
+ const rootsAttributes = this.config.get('rootsAttributes');
388
+ for (const [rootName, attributes] of Object.entries(rootsAttributes)){
389
+ if (!this.model.document.getRoot(rootName)) {
390
+ /**
391
+ * Trying to set attributes on a non-existing root.
392
+ *
393
+ * Roots specified in {@link module:core/editor/editorconfig~EditorConfig#rootsAttributes} do not match initial
394
+ * editor roots.
395
+ *
396
+ * @error multi-root-editor-root-attributes-no-root
397
+ */ throw new CKEditorError('multi-root-editor-root-attributes-no-root', null);
398
+ }
399
+ for (const key of Object.keys(attributes)){
400
+ this.registerRootAttribute(key);
401
+ }
402
+ }
403
+ this.data.on('init', ()=>{
404
+ this.model.enqueueChange({
405
+ isUndoable: false
406
+ }, (writer)=>{
407
+ for (const [name, attributes] of Object.entries(rootsAttributes)){
408
+ const root = this.model.document.getRoot(name);
409
+ for (const [key, value] of Object.entries(attributes)){
410
+ if (value !== null) {
411
+ writer.setAttribute(key, value, root);
412
+ }
413
+ }
414
+ }
415
+ });
416
+ });
417
+ }
418
+ const options = {
419
+ shouldToolbarGroupWhenFull: !this.config.get('toolbar.shouldNotGroupWhenFull'),
420
+ editableElements: sourceIsData ? undefined : sourceElementsOrData,
421
+ label: this.config.get('label')
422
+ };
423
+ const view = new MultiRootEditorUIView(this.locale, this.editing.view, rootNames, options);
424
+ this.ui = new MultiRootEditorUI(this, view);
425
+ this.model.document.on('change:data', ()=>{
426
+ const changedRoots = this.model.document.differ.getChangedRoots();
427
+ // Fire detaches first. If there are multiple roots removed and added in one batch, it should be easier to handle if
428
+ // changes aren't mixed. Detaching will usually lead to just removing DOM elements. Detaching first will lead to a clean DOM
429
+ // when new editables are added in `addRoot` event.
430
+ for (const changes of changedRoots){
431
+ const root = this.model.document.getRoot(changes.name);
432
+ if (changes.state == 'detached') {
433
+ this.fire('detachRoot', root);
434
+ }
435
+ }
436
+ for (const changes of changedRoots){
437
+ const root = this.model.document.getRoot(changes.name);
438
+ if (changes.state == 'attached') {
439
+ this.fire('addRoot', root);
440
+ }
441
+ }
442
+ });
443
+ // Overwrite `Model#canEditAt()` decorated method.
444
+ // Check if the provided selection is inside a read-only root. If so, return `false`.
445
+ this.listenTo(this.model, 'canEditAt', (evt, [selection])=>{
446
+ // Skip empty selections.
447
+ if (!selection) {
448
+ return;
449
+ }
450
+ let selectionInReadOnlyRoot = false;
451
+ for (const range of selection.getRanges()){
452
+ const root = range.root;
453
+ if (this._readOnlyRootLocks.has(root.rootName)) {
454
+ selectionInReadOnlyRoot = true;
455
+ break;
456
+ }
457
+ }
458
+ // If selection is in read-only root, return `false` and prevent further processing.
459
+ // Otherwise, allow for other callbacks (or default callback) to evaluate.
460
+ if (selectionInReadOnlyRoot) {
461
+ evt.return = false;
462
+ evt.stop();
463
+ }
464
+ }, {
465
+ priority: 'high'
466
+ });
467
+ this.decorate('loadRoot');
468
+ this.on('loadRoot', (evt, [rootName])=>{
469
+ const root = this.model.document.getRoot(rootName);
470
+ if (!root) {
471
+ /**
472
+ * The root to load does not exist.
473
+ *
474
+ * @error multi-root-editor-load-root-no-root
475
+ */ throw new CKEditorError('multi-root-editor-load-root-no-root', this, {
476
+ rootName
477
+ });
478
+ }
479
+ if (root._isLoaded) {
480
+ /**
481
+ * The root to load was already loaded before. The `loadRoot()` call has no effect.
482
+ *
483
+ * @error multi-root-editor-load-root-already-loaded
484
+ */ logWarning('multi-root-editor-load-root-already-loaded');
485
+ evt.stop();
486
+ }
487
+ }, {
488
+ priority: 'highest'
489
+ });
490
+ }
491
+ /**
492
+ * Destroys the editor instance, releasing all resources used by it.
493
+ *
494
+ * Updates the original editor element with the data if the
495
+ * {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy `updateSourceElementOnDestroy`}
496
+ * configuration option is set to `true`.
497
+ *
498
+ * **Note**: The multi-root editor does not remove the toolbar and editable when destroyed. You can
499
+ * do that yourself in the destruction chain, if you need to:
500
+ *
501
+ * ```ts
502
+ * editor.destroy().then( () => {
503
+ * // Remove the toolbar from DOM.
504
+ * editor.ui.view.toolbar.element.remove();
505
+ *
506
+ * // Remove editable elements from DOM.
507
+ * for ( const editable of Object.values( editor.ui.view.editables ) ) {
508
+ * editable.element.remove();
509
+ * }
510
+ *
511
+ * console.log( 'Editor was destroyed' );
512
+ * } );
513
+ * ```
514
+ */ destroy() {
515
+ const shouldUpdateSourceElement = this.config.get('updateSourceElementOnDestroy');
516
+ // Cache the data and editable DOM elements, then destroy.
517
+ // It's safe to assume that the model->view conversion will not work after `super.destroy()`,
518
+ // same as `ui.getEditableElement()` method will not return editables.
519
+ const data = {};
520
+ for (const rootName of Object.keys(this.sourceElements)){
521
+ data[rootName] = shouldUpdateSourceElement ? this.getData({
522
+ rootName
523
+ }) : '';
524
+ }
525
+ this.ui.destroy();
526
+ return super.destroy().then(()=>{
527
+ for (const rootName of Object.keys(this.sourceElements)){
528
+ setDataInElement(this.sourceElements[rootName], data[rootName]);
529
+ }
530
+ });
531
+ }
532
+ /**
533
+ * Adds a new root to the editor.
534
+ *
535
+ * ```ts
536
+ * editor.addRoot( 'myRoot', { data: '<p>Initial root data.</p>' } );
537
+ * ```
538
+ *
539
+ * After a root is added, you will be able to modify and retrieve its data.
540
+ *
541
+ * All root names must be unique. An error will be thrown if you will try to create a root with the name same as
542
+ * an already existing, attached root. However, you can call this method for a detached root. See also {@link #detachRoot}.
543
+ *
544
+ * Whenever a root is added, the editor instance will fire {@link #event:addRoot `addRoot` event}. The event is also called when
545
+ * the root is added indirectly, e.g. by the undo feature or on a remote client during real-time collaboration.
546
+ *
547
+ * Note, that this method only adds a root to the editor model. It **does not** create a DOM editable element for the new root.
548
+ * Until such element is created (and attached to the root), the root is "virtual": it is not displayed anywhere and its data can
549
+ * be changed only using the editor API.
550
+ *
551
+ * To create a DOM editable element for the root, listen to {@link #event:addRoot `addRoot` event} and call {@link #createEditable}.
552
+ * Then, insert the DOM element in a desired place, that will depend on the integration with your application and your requirements.
553
+ *
554
+ * ```ts
555
+ * editor.on( 'addRoot', ( evt, root ) => {
556
+ * const editableElement = editor.createEditable( root );
557
+ *
558
+ * // You may want to create a more complex DOM structure here.
559
+ * //
560
+ * // Alternatively, you may want to create a DOM structure before
561
+ * // calling `editor.addRoot()` and only append `editableElement` at
562
+ * // a proper place.
563
+ *
564
+ * document.querySelector( '#editors' ).appendChild( editableElement );
565
+ * } );
566
+ *
567
+ * // ...
568
+ *
569
+ * editor.addRoot( 'myRoot' ); // Will create a root, a DOM editable element and append it to `#editors` container element.
570
+ * ```
571
+ *
572
+ * You can set root attributes on the new root while you add it:
573
+ *
574
+ * ```ts
575
+ * // Add a collapsed root at fourth position from top.
576
+ * // Keep in mind that these are just examples of attributes. You need to provide your own features that will handle the attributes.
577
+ * editor.addRoot( 'myRoot', { attributes: { isCollapsed: true, index: 4 } } );
578
+ * ```
579
+ *
580
+ * Note that attributes added together with a root are automatically registered.
581
+ *
582
+ * See also {@link ~MultiRootEditor#registerRootAttribute `MultiRootEditor#registerRootAttribute()`} and
583
+ * {@link module:core/editor/editorconfig~EditorConfig#rootsAttributes `config.rootsAttributes` configuration option}.
584
+ *
585
+ * By setting `isUndoable` flag to `true`, you can allow for detaching the root using the undo feature.
586
+ *
587
+ * Additionally, you can group adding multiple roots in one undo step. This can be useful if you add multiple roots that are
588
+ * combined into one, bigger UI element, and want them all to be undone together.
589
+ *
590
+ * ```ts
591
+ * let rowId = 0;
592
+ *
593
+ * editor.model.change( () => {
594
+ * editor.addRoot( 'left-row-' + rowId, { isUndoable: true } );
595
+ * editor.addRoot( 'center-row-' + rowId, { isUndoable: true } );
596
+ * editor.addRoot( 'right-row-' + rowId, { isUndoable: true } );
597
+ *
598
+ * rowId++;
599
+ * } );
600
+ * ```
601
+ *
602
+ * @param rootName Name of the root to add.
603
+ * @param options Additional options for the added root.
604
+ */ addRoot(rootName, { data = '', attributes = {}, elementName = '$root', isUndoable = false } = {}) {
605
+ const _addRoot = (writer)=>{
606
+ const root = writer.addRoot(rootName, elementName);
607
+ if (data) {
608
+ writer.insert(this.data.parse(data, root), root, 0);
609
+ }
610
+ for (const key of Object.keys(attributes)){
611
+ this.registerRootAttribute(key);
612
+ writer.setAttribute(key, attributes[key], root);
613
+ }
614
+ };
615
+ if (isUndoable) {
616
+ this.model.change(_addRoot);
617
+ } else {
618
+ this.model.enqueueChange({
619
+ isUndoable: false
620
+ }, _addRoot);
621
+ }
622
+ }
623
+ /**
624
+ * Detaches a root from the editor.
625
+ *
626
+ * ```ts
627
+ * editor.detachRoot( 'myRoot' );
628
+ * ```
629
+ *
630
+ * A detached root is not entirely removed from the editor model, however it can be considered removed.
631
+ *
632
+ * After a root is detached all its children are removed, all markers inside it are removed, and whenever something is inserted to it,
633
+ * it is automatically removed as well. Finally, a detached root is not returned by
634
+ * {@link module:engine/model/document~Document#getRootNames} by default.
635
+ *
636
+ * It is possible to re-add a previously detached root calling {@link #addRoot}.
637
+ *
638
+ * Whenever a root is detached, the editor instance will fire {@link #event:detachRoot `detachRoot` event}. The event is also
639
+ * called when the root is detached indirectly, e.g. by the undo feature or on a remote client during real-time collaboration.
640
+ *
641
+ * Note, that this method only detached a root in the editor model. It **does not** destroy the DOM editable element linked with
642
+ * the root and it **does not** remove the DOM element from the DOM structure of your application.
643
+ *
644
+ * To properly remove a DOM editable element after a root was detached, listen to {@link #event:detachRoot `detachRoot` event}
645
+ * and call {@link #detachEditable}. Then, remove the DOM element from your application.
646
+ *
647
+ * ```ts
648
+ * editor.on( 'detachRoot', ( evt, root ) => {
649
+ * const editableElement = editor.detachEditable( root );
650
+ *
651
+ * // You may want to do an additional DOM clean-up here.
652
+ *
653
+ * editableElement.remove();
654
+ * } );
655
+ *
656
+ * // ...
657
+ *
658
+ * editor.detachRoot( 'myRoot' ); // Will detach the root, and remove the DOM editable element.
659
+ * ```
660
+ *
661
+ * By setting `isUndoable` flag to `true`, you can allow for re-adding the root using the undo feature.
662
+ *
663
+ * Additionally, you can group detaching multiple roots in one undo step. This can be useful if the roots are combined into one,
664
+ * bigger UI element, and you want them all to be re-added together.
665
+ *
666
+ * ```ts
667
+ * editor.model.change( () => {
668
+ * editor.detachRoot( 'left-row-3', true );
669
+ * editor.detachRoot( 'center-row-3', true );
670
+ * editor.detachRoot( 'right-row-3', true );
671
+ * } );
672
+ * ```
673
+ *
674
+ * @param rootName Name of the root to detach.
675
+ * @param isUndoable Whether detaching the root can be undone (using the undo feature) or not.
676
+ */ detachRoot(rootName, isUndoable = false) {
677
+ if (isUndoable) {
678
+ this.model.change((writer)=>writer.detachRoot(rootName));
679
+ } else {
680
+ this.model.enqueueChange({
681
+ isUndoable: false
682
+ }, (writer)=>writer.detachRoot(rootName));
683
+ }
684
+ }
685
+ /**
686
+ * Creates and returns a new DOM editable element for the given root element.
687
+ *
688
+ * The new DOM editable is attached to the model root and can be used to modify the root content.
689
+ *
690
+ * @param root Root for which the editable element should be created.
691
+ * @param placeholder Placeholder for the editable element. If not set, placeholder value from the
692
+ * {@link module:core/editor/editorconfig~EditorConfig#placeholder editor configuration} will be used (if it was provided).
693
+ * @param label The accessible label text describing the editable to the assistive technologies.
694
+ * @returns The created DOM element. Append it in a desired place in your application.
695
+ */ createEditable(root, placeholder, label) {
696
+ const editable = this.ui.view.createEditable(root.rootName, undefined, label);
697
+ this.ui.addEditable(editable, placeholder);
698
+ this.editing.view.forceRender();
699
+ return editable.element;
700
+ }
701
+ /**
702
+ * Detaches the DOM editable element that was attached to the given root.
703
+ *
704
+ * @param root Root for which the editable element should be detached.
705
+ * @returns The DOM element that was detached. You may want to remove it from your application DOM structure.
706
+ */ detachEditable(root) {
707
+ const rootName = root.rootName;
708
+ const editable = this.ui.view.editables[rootName];
709
+ this.ui.removeEditable(editable);
710
+ this.ui.view.removeEditable(rootName);
711
+ return editable.element;
712
+ }
713
+ /**
714
+ * Loads a root that has previously been declared in {@link module:core/editor/editorconfig~EditorConfig#lazyRoots `lazyRoots`}
715
+ * configuration option.
716
+ *
717
+ * Only roots specified in the editor config can be loaded. A root cannot be loaded multiple times. A root cannot be unloaded and
718
+ * loading a root cannot be reverted using the undo feature.
719
+ *
720
+ * When a root becomes loaded, it will be treated by the editor as though it was just added. This, among others, means that all
721
+ * related events and mechanisms will be fired, including {@link ~MultiRootEditor#event:addRoot `addRoot` event},
722
+ * {@link module:engine/model/document~Document#event:change `model.Document` `change` event}, model post-fixers and conversion.
723
+ *
724
+ * Until the root becomes loaded, all above mechanisms are suppressed.
725
+ *
726
+ * This method is {@link module:utils/observablemixin~Observable#decorate decorated}.
727
+ *
728
+ * Note that attributes loaded together with a root are automatically registered.
729
+ *
730
+ * See also {@link ~MultiRootEditor#registerRootAttribute `MultiRootEditor#registerRootAttribute()`} and
731
+ * {@link module:core/editor/editorconfig~EditorConfig#rootsAttributes `config.rootsAttributes` configuration option}.
732
+ *
733
+ * When this method is used in real-time collaboration environment, its effects become asynchronous as the editor will first synchronize
734
+ * with the remote editing session, before the root is added to the editor.
735
+ *
736
+ * If the root has been already loaded by any other client, the additional data passed in `loadRoot()` parameters will be ignored.
737
+ *
738
+ * @param rootName Name of the root to load.
739
+ * @param options Additional options for the loaded root.
740
+ * @fires loadRoot
741
+ */ loadRoot(rootName, { data = '', attributes = {} } = {}) {
742
+ // `root` will be defined as it is guaranteed by a check in a higher priority callback.
743
+ const root = this.model.document.getRoot(rootName);
744
+ this.model.enqueueChange({
745
+ isUndoable: false
746
+ }, (writer)=>{
747
+ if (data) {
748
+ writer.insert(this.data.parse(data, root), root, 0);
749
+ }
750
+ for (const key of Object.keys(attributes)){
751
+ this.registerRootAttribute(key);
752
+ writer.setAttribute(key, attributes[key], root);
753
+ }
754
+ root._isLoaded = true;
755
+ this.model.document.differ._bufferRootLoad(root);
756
+ });
757
+ }
758
+ /**
759
+ * Returns the document data for all attached roots.
760
+ *
761
+ * @param options Additional configuration for the retrieved data.
762
+ * Editor features may introduce more configuration options that can be set through this parameter.
763
+ * @param options.trim Whether returned data should be trimmed. This option is set to `'empty'` by default,
764
+ * which means that whenever editor content is considered empty, an empty string is returned. To turn off trimming
765
+ * use `'none'`. In such cases exact content will be returned (for example `'<p>&nbsp;</p>'` for an empty editor).
766
+ * @returns The full document data.
767
+ */ getFullData(options) {
768
+ const data = {};
769
+ for (const rootName of this.model.document.getRootNames()){
770
+ data[rootName] = this.data.get({
771
+ ...options,
772
+ rootName
773
+ });
774
+ }
775
+ return data;
776
+ }
777
+ /**
778
+ * Returns attributes for all attached roots.
779
+ *
780
+ * Note: all and only {@link ~MultiRootEditor#registerRootAttribute registered} roots attributes will be returned.
781
+ * If a registered root attribute is not set for a given root, `null` will be returned.
782
+ *
783
+ * @returns Object with roots attributes. Keys are roots names, while values are attributes set on given root.
784
+ */ getRootsAttributes() {
785
+ const rootsAttributes = {};
786
+ for (const rootName of this.model.document.getRootNames()){
787
+ rootsAttributes[rootName] = this.getRootAttributes(rootName);
788
+ }
789
+ return rootsAttributes;
790
+ }
791
+ /**
792
+ * Returns attributes for the specified root.
793
+ *
794
+ * Note: all and only {@link ~MultiRootEditor#registerRootAttribute registered} roots attributes will be returned.
795
+ * If a registered root attribute is not set for a given root, `null` will be returned.
796
+ */ getRootAttributes(rootName) {
797
+ const rootAttributes = {};
798
+ const root = this.model.document.getRoot(rootName);
799
+ for (const key of this._registeredRootsAttributesKeys){
800
+ rootAttributes[key] = root.hasAttribute(key) ? root.getAttribute(key) : null;
801
+ }
802
+ return rootAttributes;
803
+ }
804
+ /**
805
+ * Registers given string as a root attribute key. Registered root attributes are added to
806
+ * {@link module:engine/model/schema~Schema schema}, and also returned by
807
+ * {@link ~MultiRootEditor#getRootAttributes `getRootAttributes()`} and
808
+ * {@link ~MultiRootEditor#getRootsAttributes `getRootsAttributes()`}.
809
+ *
810
+ * Note: attributes passed in {@link module:core/editor/editorconfig~EditorConfig#rootsAttributes `config.rootsAttributes`} are
811
+ * automatically registered as the editor is initialized. However, registering the same attribute twice does not have any negative
812
+ * impact, so it is recommended to use this method in any feature that uses roots attributes.
813
+ */ registerRootAttribute(key) {
814
+ if (this._registeredRootsAttributesKeys.has(key)) {
815
+ return;
816
+ }
817
+ this._registeredRootsAttributesKeys.add(key);
818
+ this.editing.model.schema.extend('$root', {
819
+ allowAttributes: key
820
+ });
821
+ }
822
+ /**
823
+ * Switches given editor root to the read-only mode.
824
+ *
825
+ * In contrary to {@link module:core/editor/editor~Editor#enableReadOnlyMode `enableReadOnlyMode()`}, which switches the whole editor
826
+ * to the read-only mode, this method turns only a particular root to the read-only mode. This can be useful when you want to prevent
827
+ * editing only a part of the editor content.
828
+ *
829
+ * When you switch a root to the read-only mode, you need provide a unique identifier (`lockId`) that will identify this request. You
830
+ * will need to provide the same `lockId` when you will want to
831
+ * {@link module:editor-multi-root/multirooteditor~MultiRootEditor#enableRoot re-enable} the root.
832
+ *
833
+ * ```ts
834
+ * const model = editor.model;
835
+ * const myRoot = model.document.getRoot( 'myRoot' );
836
+ *
837
+ * editor.disableRoot( 'myRoot', 'my-lock' );
838
+ * model.canEditAt( myRoot ); // `false`
839
+ *
840
+ * editor.disableRoot( 'myRoot', 'other-lock' );
841
+ * editor.disableRoot( 'myRoot', 'other-lock' ); // Multiple locks with the same ID have no effect.
842
+ * model.canEditAt( myRoot ); // `false`
843
+ *
844
+ * editor.enableRoot( 'myRoot', 'my-lock' );
845
+ * model.canEditAt( myRoot ); // `false`
846
+ *
847
+ * editor.enableRoot( 'myRoot', 'other-lock' );
848
+ * model.canEditAt( myRoot ); // `true`
849
+ * ```
850
+ *
851
+ * See also {@link module:core/editor/editor~Editor#enableReadOnlyMode `Editor#enableReadOnlyMode()`} and
852
+ * {@link module:editor-multi-root/multirooteditor~MultiRootEditor#enableRoot `MultiRootEditor#enableRoot()`}.
853
+ *
854
+ * @param rootName Name of the root to switch to read-only mode.
855
+ * @param lockId A unique ID for setting the editor to the read-only state.
856
+ */ disableRoot(rootName, lockId) {
857
+ if (rootName == '$graveyard') {
858
+ /**
859
+ * You cannot disable the `$graveyard` root.
860
+ *
861
+ * @error multi-root-editor-cannot-disable-graveyard-root
862
+ */ throw new CKEditorError('multi-root-editor-cannot-disable-graveyard-root', this);
863
+ }
864
+ const locksForGivenRoot = this._readOnlyRootLocks.get(rootName);
865
+ if (locksForGivenRoot) {
866
+ locksForGivenRoot.add(lockId);
867
+ } else {
868
+ this._readOnlyRootLocks.set(rootName, new Set([
869
+ lockId
870
+ ]));
871
+ const editableRootElement = this.editing.view.document.getRoot(rootName);
872
+ editableRootElement.isReadOnly = true;
873
+ // Since one of the roots has changed read-only state, we need to refresh all commands that affect data.
874
+ Array.from(this.commands.commands()).forEach((command)=>command.affectsData && command.refresh());
875
+ }
876
+ }
877
+ /**
878
+ * Removes given read-only lock from the given root.
879
+ *
880
+ * See {@link module:editor-multi-root/multirooteditor~MultiRootEditor#disableRoot `disableRoot()`}.
881
+ *
882
+ * @param rootName Name of the root to switch back from the read-only mode.
883
+ * @param lockId A unique ID for setting the editor to the read-only state.
884
+ */ enableRoot(rootName, lockId) {
885
+ const locksForGivenRoot = this._readOnlyRootLocks.get(rootName);
886
+ if (!locksForGivenRoot || !locksForGivenRoot.has(lockId)) {
887
+ return;
888
+ }
889
+ if (locksForGivenRoot.size === 1) {
890
+ this._readOnlyRootLocks.delete(rootName);
891
+ const editableRootElement = this.editing.view.document.getRoot(rootName);
892
+ editableRootElement.isReadOnly = this.isReadOnly;
893
+ // Since one of the roots has changed read-only state, we need to refresh all commands that affect data.
894
+ Array.from(this.commands.commands()).forEach((command)=>command.affectsData && command.refresh());
895
+ } else {
896
+ locksForGivenRoot.delete(lockId);
897
+ }
898
+ }
899
+ /**
900
+ * Creates a new multi-root editor instance.
901
+ *
902
+ * **Note:** remember that `MultiRootEditor` does not append the toolbar element to your web page, so you have to do it manually
903
+ * after the editor has been initialized.
904
+ *
905
+ * There are a few different ways to initialize the multi-root editor.
906
+ *
907
+ * # Using existing DOM elements:
908
+ *
909
+ * ```ts
910
+ * MultiRootEditor.create( {
911
+ * intro: document.querySelector( '#editor-intro' ),
912
+ * content: document.querySelector( '#editor-content' ),
913
+ * sidePanelLeft: document.querySelector( '#editor-side-left' ),
914
+ * sidePanelRight: document.querySelector( '#editor-side-right' ),
915
+ * outro: document.querySelector( '#editor-outro' )
916
+ * } )
917
+ * .then( editor => {
918
+ * console.log( 'Editor was initialized', editor );
919
+ *
920
+ * // Append the toolbar inside a provided DOM element.
921
+ * document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
922
+ * } )
923
+ * .catch( err => {
924
+ * console.error( err.stack );
925
+ * } );
926
+ * ```
927
+ *
928
+ * The elements' content will be used as the editor data and elements will become editable elements.
929
+ *
930
+ * # Creating a detached editor
931
+ *
932
+ * Alternatively, you can initialize the editor by passing the initial data directly as strings.
933
+ * In this case, you will have to manually append both the toolbar element and the editable elements to your web page.
934
+ *
935
+ * ```ts
936
+ * MultiRootEditor.create( {
937
+ * intro: '<p><strong>Exciting</strong> intro text to an article.</p>',
938
+ * content: '<p>Lorem ipsum dolor sit amet.</p>',
939
+ * sidePanelLeft: '<blockquote>Strong quotation from article.</blockquote>',
940
+ * sidePanelRight: '<p>List of similar articles...</p>',
941
+ * outro: '<p>Closing text.</p>'
942
+ * } )
943
+ * .then( editor => {
944
+ * console.log( 'Editor was initialized', editor );
945
+ *
946
+ * // Append the toolbar inside a provided DOM element.
947
+ * document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
948
+ *
949
+ * // Append DOM editable elements created by the editor.
950
+ * const editables = editor.ui.view.editables;
951
+ * const container = document.querySelector( '#editable-container' );
952
+ *
953
+ * container.appendChild( editables.intro.element );
954
+ * container.appendChild( editables.content.element );
955
+ * container.appendChild( editables.outro.element );
956
+ * } )
957
+ * .catch( err => {
958
+ * console.error( err.stack );
959
+ * } );
960
+ * ```
961
+ *
962
+ * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
963
+ * web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
964
+ *
965
+ * # Using an existing DOM element (and data provided in `config.initialData`)
966
+ *
967
+ * You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
968
+ *
969
+ * ```ts
970
+ * MultiRootEditor.create( {
971
+ * intro: document.querySelector( '#editor-intro' ),
972
+ * content: document.querySelector( '#editor-content' ),
973
+ * sidePanelLeft: document.querySelector( '#editor-side-left' ),
974
+ * sidePanelRight: document.querySelector( '#editor-side-right' ),
975
+ * outro: document.querySelector( '#editor-outro' )
976
+ * }, {
977
+ * initialData: {
978
+ * intro: '<p><strong>Exciting</strong> intro text to an article.</p>',
979
+ * content: '<p>Lorem ipsum dolor sit amet.</p>',
980
+ * sidePanelLeft '<blockquote>Strong quotation from article.</blockquote>':
981
+ * sidePanelRight '<p>List of similar articles...</p>':
982
+ * outro: '<p>Closing text.</p>'
983
+ * }
984
+ * } )
985
+ * .then( editor => {
986
+ * console.log( 'Editor was initialized', editor );
987
+ *
988
+ * // Append the toolbar inside a provided DOM element.
989
+ * document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
990
+ * } )
991
+ * .catch( err => {
992
+ * console.error( err.stack );
993
+ * } );
994
+ * ```
995
+ *
996
+ * This method can be used to initialize the editor on an existing element with the specified content in case if your integration
997
+ * makes it difficult to set the content of the source element.
998
+ *
999
+ * Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
1000
+ *
1001
+ * # Configuring the editor
1002
+ *
1003
+ * See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
1004
+ * customizing plugins, toolbar and more.
1005
+ *
1006
+ * @param sourceElementsOrData The DOM elements that will be the source for the created editor
1007
+ * or the editor's initial data. The editor will initialize multiple roots with names according to the keys in the passed object.
1008
+ *
1009
+ * If DOM elements are passed, their content will be automatically loaded to the editor upon initialization and the elements will be
1010
+ * used as the editor's editable areas. The editor data will be set back to the original element once the editor is destroyed if the
1011
+ * {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy updateSourceElementOnDestroy} option
1012
+ * is set to `true`.
1013
+ *
1014
+ * If the initial data is passed, a detached editor will be created. For each entry in the passed object, one editor root and one
1015
+ * editable DOM element will be created. You will need to attach the editable elements into the DOM manually. The elements are available
1016
+ * through the {@link module:editor-multi-root/multirooteditorui~MultiRootEditorUI#getEditableElement `editor.ui.getEditableElement()`}
1017
+ * method.
1018
+ * @param config The editor configuration.
1019
+ * @returns A promise resolved once the editor is ready. The promise resolves with the created editor instance.
1020
+ */ static create(sourceElementsOrData, config = {}) {
1021
+ return new Promise((resolve)=>{
1022
+ for (const sourceItem of Object.values(sourceElementsOrData)){
1023
+ if (isElement(sourceItem) && sourceItem.tagName === 'TEXTAREA') {
1024
+ // Documented in core/editor/editor.js
1025
+ // eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
1026
+ throw new CKEditorError('editor-wrong-element', null);
1027
+ }
1028
+ }
1029
+ const editor = new this(sourceElementsOrData, config);
1030
+ resolve(editor.initPlugins().then(()=>editor.ui.init()).then(()=>{
1031
+ // This is checked directly before setting the initial data,
1032
+ // as plugins may change `EditorConfig#initialData` value.
1033
+ editor._verifyRootsWithInitialData();
1034
+ return editor.data.init(editor.config.get('initialData'));
1035
+ }).then(()=>editor.fire('ready')).then(()=>editor));
1036
+ });
1037
+ }
1038
+ /**
1039
+ * @internal
1040
+ */ _verifyRootsWithInitialData() {
1041
+ const initialData = this.config.get('initialData');
1042
+ // Roots that are not in the initial data.
1043
+ for (const rootName of this.model.document.getRootNames()){
1044
+ if (!(rootName in initialData)) {
1045
+ /**
1046
+ * Editor roots do not match the
1047
+ * {@link module:core/editor/editorconfig~EditorConfig#initialData `initialData` configuration}.
1048
+ *
1049
+ * This may happen for one of the two reasons:
1050
+ *
1051
+ * * Configuration error. The `sourceElementsOrData` parameter in
1052
+ * {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`} contains different
1053
+ * roots than {@link module:core/editor/editorconfig~EditorConfig#initialData `initialData` configuration}.
1054
+ * * As the editor was initialized, the {@link module:core/editor/editorconfig~EditorConfig#initialData `initialData`}
1055
+ * configuration value or the state of the editor roots has been changed.
1056
+ *
1057
+ * @error multi-root-editor-root-initial-data-mismatch
1058
+ */ throw new CKEditorError('multi-root-editor-root-initial-data-mismatch', null);
1059
+ }
1060
+ }
1061
+ // Roots that are not in the editor.
1062
+ for (const rootName of Object.keys(initialData)){
1063
+ const root = this.model.document.getRoot(rootName);
1064
+ if (!root || !root.isAttached()) {
1065
+ // eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
1066
+ throw new CKEditorError('multi-root-editor-root-initial-data-mismatch', null);
1067
+ }
1068
+ }
1069
+ }
1070
+ }
1071
+ function getInitialData(sourceElementOrData) {
1072
+ return isElement(sourceElementOrData) ? getDataFromElement(sourceElementOrData) : sourceElementOrData;
1073
+ }
1074
+ function isElement(value) {
1075
+ return isElement$1(value);
1076
+ }
1077
+
1078
+ export { MultiRootEditor };
1079
+ //# sourceMappingURL=index.js.map