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