@ckeditor/ckeditor5-editor-multi-root 0.0.1 → 37.0.0-alpha.2
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/CHANGELOG.md +4 -0
- package/LICENSE.md +1 -0
- package/README.md +19 -2
- package/build/editor-multi-root.js +4 -0
- package/package.json +47 -4
- package/src/index.d.ts +8 -0
- package/src/index.js +8 -0
- package/src/multirooteditor.d.ts +223 -0
- package/src/multirooteditor.js +293 -0
- package/src/multirooteditorui.d.ts +42 -0
- package/src/multirooteditorui.js +143 -0
- package/src/multirooteditoruiview.d.ts +53 -0
- package/src/multirooteditoruiview.js +73 -0
package/CHANGELOG.md
ADDED
package/LICENSE.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
Software License Agreement
|
2
2
|
==========================
|
3
3
|
|
4
|
+
**Multi-root editor implementation** – https://github.com/ckeditor/ckeditor5-editor-multi-root <br>
|
4
5
|
Copyright (c) 2003-2023, [CKSource Holding sp. z o.o.](https://cksource.com) All rights reserved.
|
5
6
|
|
6
7
|
Licensed under the terms of [GNU General Public License Version 2 or later](http://www.gnu.org/licenses/gpl.html).
|
package/README.md
CHANGED
@@ -1,4 +1,21 @@
|
|
1
1
|
CKEditor 5 multi-root editor implementation
|
2
|
-
|
2
|
+
========================================
|
3
3
|
|
4
|
-
|
4
|
+
[](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-multi-root)
|
5
|
+
[](https://coveralls.io/github/ckeditor/ckeditor5?branch=master)
|
6
|
+
[](https://app.travis-ci.com/github/ckeditor/ckeditor5)
|
7
|
+

|
8
|
+
|
9
|
+
The multi-root editor implementation for CKEditor 5. Read more about the [multi-root editor build](https://ckeditor.com/docs/ckeditor5/latest/installation/getting-started/predefined-builds.html#multi-root-editor) and see the [demo](https://ckeditor.com/docs/ckeditor5/latest/examples/builds/multi-root-editor.html).
|
10
|
+
|
11
|
+
This package contains the [`MultiRootEditor`](https://ckeditor.com/docs/ckeditor5/latest/api/module_editor-multi-root_multirooteditor-MultiRootEditor.html) class. Follow there to learn more about this type of editor and how to initialize it.
|
12
|
+
|
13
|
+
This package contains the source version of the multi-root editor. Read more about [CKEditor 5 predefined builds](https://ckeditor.com/docs/ckeditor5/latest/installation/advanced/alternative-setups/predefined-builds.html) in the CKEditor 5 documentation.
|
14
|
+
|
15
|
+
## Documentation
|
16
|
+
|
17
|
+
See the [`@ckeditor/ckeditor5-editor-multi-root` package](https://ckeditor.com/docs/ckeditor5/latest/api/editor-multi-root.html) page in [CKEditor 5 documentation](https://ckeditor.com/docs/ckeditor5/latest/).
|
18
|
+
|
19
|
+
## License
|
20
|
+
|
21
|
+
Licensed under the terms of [GNU General Public License Version 2 or later](http://www.gnu.org/licenses/gpl.html). For full details about the license, please check the `LICENSE.md` file or [https://ckeditor.com/legal/ckeditor-oss-license](https://ckeditor.com/legal/ckeditor-oss-license).
|
@@ -0,0 +1,4 @@
|
|
1
|
+
/*!
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md.
|
4
|
+
*/(()=>{var t={704:(t,e,o)=>{t.exports=o(79)("./src/core.js")},492:(t,e,o)=>{t.exports=o(79)("./src/engine.js")},273:(t,e,o)=>{t.exports=o(79)("./src/ui.js")},209:(t,e,o)=>{t.exports=o(79)("./src/utils.js")},79:t=>{"use strict";t.exports=CKEditor5.dll}},e={};function o(i){var r=e[i];if(void 0!==r)return r.exports;var n=e[i]={exports:{}};return t[i](n,n.exports,o),n.exports}o.d=(t,e)=>{for(var i in e)o.o(e,i)&&!o.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var i={};(()=>{"use strict";o.r(i),o.d(i,{MultiRootEditor:()=>R});var t=o(704),e=o(209),r=o(273),n=o(492);class s extends r.EditorUI{constructor(t,e){super(t),this.view=e}init(){const t=this.view,e=this.editor.editing.view;let o;for(const e of Object.keys(t.editables))t.editables[e].name=e;t.render(),this.focusTracker.on("change:focusedElement",((t,e,i)=>{for(const t of Object.values(this.view.editables))i===t.element&&(o=t.element)})),this.focusTracker.on("change:isFocused",((t,e,i)=>{i||(o=null)}));for(const t of Object.values(this.view.editables)){const i=t.element;this.setEditableElement(t.name,i),t.bind("isFocused").to(this.focusTracker,"isFocused",this.focusTracker,"focusedElement",((t,e)=>!!t&&(e===i||o===i))),e.attachDomRoot(i,t.name)}this._initPlaceholder(),this._initToolbar(),this.fire("ready")}destroy(){super.destroy();const t=this.view,e=this.editor.editing.view;for(const t of Object.values(this.view.editables))e.detachDomRoot(t.name);t.destroy()}_initToolbar(){const t=this.editor,e=this.view;e.toolbar.fillFromConfig(t.config.get("toolbar"),this.componentFactory),this.addToolbar(e.toolbar)}_initPlaceholder(){const t=this.editor,e=t.editing.view,o=t.config.get("placeholder");if(o)for(const t of Object.values(this.view.editables)){const i=e.document.getRoot(t.name),r="string"==typeof o?o:o[t.name];r&&(0,n.enablePlaceholder)({view:e,element:i,text:r,isDirectHost:!1,keepOnFocus:!0})}}}class c extends r.EditorUIView{constructor(t,e,o,i={}){super(t);const n=t.t;this.toolbar=new r.ToolbarView(t,{shouldGroupWhenFull:i.shouldToolbarGroupWhenFull}),this.editables={};for(const s of o){const o=new r.InlineEditableUIView(t,e,i.editableElements?i.editableElements[s]:void 0,{label:t=>n("Rich Text Editor. Editing area: %0",t.name)});this.editables[s]=o}this.editable=Object.values(this.editables)[0],this.toolbar.extendTemplate({attributes:{class:["ck-reset_all","ck-rounded-corners"],dir:t.uiLanguageDirection}})}render(){super.render(),this.registerChild(Object.values(this.editables)),this.registerChild([this.toolbar])}}const l=function(t){return null!=t&&"object"==typeof t};const a="object"==typeof global&&global&&global.Object===Object&&global;var u="object"==typeof self&&self&&self.Object===Object&&self;const d=(a||u||Function("return this")()).Symbol;var h=Object.prototype,f=h.hasOwnProperty,b=h.toString,g=d?d.toStringTag:void 0;const p=function(t){var e=f.call(t,g),o=t[g];try{t[g]=void 0;var i=!0}catch(t){}var r=b.call(t);return i&&(e?t[g]=o:delete t[g]),r};var v=Object.prototype.toString;const m=function(t){return v.call(t)};var y="[object Null]",j="[object Undefined]",w=d?d.toStringTag:void 0;const E=function(t){return null==t?void 0===t?j:y:w&&w in Object(t)?p(t):m(t)};const O=function(t,e){return function(o){return t(e(o))}}(Object.getPrototypeOf,Object);var T="[object Object]",x=Function.prototype,D=Object.prototype,F=x.toString,S=D.hasOwnProperty,P=F.call(Object);const k=function(t){if(!l(t)||E(t)!=T)return!1;var e=O(t);if(null===e)return!0;var o=S.call(e,"constructor")&&e.constructor;return"function"==typeof o&&o instanceof o&&F.call(o)==P};const C=function(t){return l(t)&&1===t.nodeType&&!k(t)};class R extends((0,t.DataApiMixin)(t.Editor)){constructor(o,i={}){const r=Object.keys(o),n=0===r.length||"string"==typeof o[r[0]];if(n&&void 0!==i.initialData)throw new e.CKEditorError("editor-create-initial-data",null);if(super(i),n||(this.sourceElements=o),void 0===this.config.get("initialData")){const t={};for(const i of r)t[i]=_(l=o[i])?(0,e.getDataFromElement)(l):l;this.config.set("initialData",t)}var l;if(!n)for(const e of r)(0,t.secureSourceElement)(this,o[e]);for(const t of r)this.model.document.createRoot("$root",t);const a={shouldToolbarGroupWhenFull:!this.config.get("toolbar.shouldNotGroupWhenFull"),editableElements:n?void 0:o},u=new c(this.locale,this.editing.view,r,a);this.ui=new s(this,u)}destroy(){const t=this.config.get("updateSourceElementOnDestroy"),o={};if(this.sourceElements)for(const e of Object.keys(this.sourceElements))o[e]=t?this.getData({rootName:e}):"";return this.ui.destroy(),super.destroy().then((()=>{if(this.sourceElements)for(const t of Object.keys(this.sourceElements))(0,e.setDataInElement)(this.sourceElements[t],o[t])}))}static create(t,o={}){return new Promise((i=>{for(const o of Object.values(t))if(_(o)&&"TEXTAREA"===o.tagName)throw new e.CKEditorError("editor-wrong-element",null);const r=new this(t,o);i(r.initPlugins().then((()=>r.ui.init())).then((()=>r.data.init(r.config.get("initialData")))).then((()=>r.fire("ready"))).then((()=>r)))}))}}function _(t){return C(t)}})(),(window.CKEditor5=window.CKEditor5||{}).editorMultiRoot=i})();
|
package/package.json
CHANGED
@@ -1,19 +1,62 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ckeditor/ckeditor5-editor-multi-root",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "37.0.0-alpha.2",
|
4
4
|
"description": "Multi-root editor implementation for CKEditor 5.",
|
5
5
|
"keywords": [
|
6
6
|
"ckeditor",
|
7
7
|
"ckeditor5",
|
8
8
|
"ckeditor 5",
|
9
|
-
"ckeditor5-
|
9
|
+
"ckeditor5-editor",
|
10
|
+
"ckeditor5-dll"
|
10
11
|
],
|
12
|
+
"main": "src/index.js",
|
13
|
+
"dependencies": {
|
14
|
+
"ckeditor5": "^37.0.0-alpha.2",
|
15
|
+
"lodash-es": "^4.17.15"
|
16
|
+
},
|
17
|
+
"devDependencies": {
|
18
|
+
"@ckeditor/ckeditor5-basic-styles": "^37.0.0-alpha.2",
|
19
|
+
"@ckeditor/ckeditor5-core": "^37.0.0-alpha.2",
|
20
|
+
"@ckeditor/ckeditor5-dev-utils": "^35.0.0",
|
21
|
+
"@ckeditor/ckeditor5-engine": "^37.0.0-alpha.2",
|
22
|
+
"@ckeditor/ckeditor5-enter": "^37.0.0-alpha.2",
|
23
|
+
"@ckeditor/ckeditor5-heading": "^37.0.0-alpha.2",
|
24
|
+
"@ckeditor/ckeditor5-paragraph": "^37.0.0-alpha.2",
|
25
|
+
"@ckeditor/ckeditor5-theme-lark": "^37.0.0-alpha.2",
|
26
|
+
"@ckeditor/ckeditor5-typing": "^37.0.0-alpha.2",
|
27
|
+
"@ckeditor/ckeditor5-ui": "^37.0.0-alpha.2",
|
28
|
+
"@ckeditor/ckeditor5-undo": "^37.0.0-alpha.2",
|
29
|
+
"@ckeditor/ckeditor5-utils": "^37.0.0-alpha.2",
|
30
|
+
"typescript": "^4.8.4",
|
31
|
+
"webpack": "^5.58.1",
|
32
|
+
"webpack-cli": "^4.9.0"
|
33
|
+
},
|
34
|
+
"engines": {
|
35
|
+
"node": ">=14.0.0",
|
36
|
+
"npm": ">=5.7.1"
|
37
|
+
},
|
11
38
|
"author": "CKSource (http://cksource.com/)",
|
12
39
|
"license": "GPL-2.0-or-later",
|
13
40
|
"homepage": "https://ckeditor.com/ckeditor-5",
|
14
41
|
"bugs": "https://github.com/ckeditor/ckeditor5/issues",
|
15
42
|
"repository": {
|
16
43
|
"type": "git",
|
17
|
-
"url": "https://github.com/ckeditor/ckeditor5
|
18
|
-
|
44
|
+
"url": "https://github.com/ckeditor/ckeditor5.git",
|
45
|
+
"directory": "packages/ckeditor5-editor-multi-root"
|
46
|
+
},
|
47
|
+
"files": [
|
48
|
+
"lang",
|
49
|
+
"src/**/*.js",
|
50
|
+
"src/**/*.d.ts",
|
51
|
+
"theme",
|
52
|
+
"build",
|
53
|
+
"ckeditor5-metadata.json",
|
54
|
+
"CHANGELOG.md"
|
55
|
+
],
|
56
|
+
"scripts": {
|
57
|
+
"dll:build": "webpack",
|
58
|
+
"build": "tsc -p ./tsconfig.json",
|
59
|
+
"postversion": "npm run build"
|
60
|
+
},
|
61
|
+
"types": "src/index.d.ts"
|
19
62
|
}
|
package/src/index.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
6
|
+
* @module editor-multi-root
|
7
|
+
*/
|
8
|
+
export { default as MultiRootEditor } from './multirooteditor';
|
package/src/index.js
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
6
|
+
* @module editor-multi-root
|
7
|
+
*/
|
8
|
+
export { default as MultiRootEditor } from './multirooteditor';
|
@@ -0,0 +1,223 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
6
|
+
* @module editor-multi-root/multirooteditor
|
7
|
+
*/
|
8
|
+
import { Editor, type EditorConfig } from 'ckeditor5/src/core';
|
9
|
+
import MultiRootEditorUI from './multirooteditorui';
|
10
|
+
declare const MultiRootEditor_base: import("ckeditor5/src/utils").Mixed<typeof Editor, import("ckeditor5/src/core").DataApi>;
|
11
|
+
/**
|
12
|
+
* The {@glink installation/getting-started/predefined-builds#multi-root-editor multi-root editor} implementation.
|
13
|
+
*
|
14
|
+
* The multi-root editor provides multiple inline editable elements and a toolbar. All editable areas are controlled by one editor
|
15
|
+
* instance, which means that they share common configuration, document ID, or undo stack.
|
16
|
+
*
|
17
|
+
* This type of editor is dedicated to integrations which require a customized UI with an open structure, featuring multiple editable areas,
|
18
|
+
* allowing developers to have a control over the exact location of these editable areas.
|
19
|
+
*
|
20
|
+
* In order to create a multi-root editor instance, use the static
|
21
|
+
* {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`} method.
|
22
|
+
*
|
23
|
+
* Note that you will need to attach the editor toolbar to your web page manually, in a desired place, after the editor is initialized.
|
24
|
+
*
|
25
|
+
* # Multi-root editor and multi-root editor build
|
26
|
+
*
|
27
|
+
* The multi-root editor can be used directly from source (if you installed the
|
28
|
+
* [`@ckeditor/ckeditor5-editor-multi-root`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-multi-root) package)
|
29
|
+
* but it is also available in the
|
30
|
+
* {@glink installation/getting-started/predefined-builds#multi-root-editor multi-root editor build}.
|
31
|
+
*
|
32
|
+
* {@glink installation/getting-started/predefined-builds Builds} are ready-to-use editors with plugins bundled in.
|
33
|
+
*
|
34
|
+
* When using the editor from source you need to take care of loading all plugins by yourself
|
35
|
+
* (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
|
36
|
+
* Using the editor from source gives much better flexibility and allows for easier customization.
|
37
|
+
*
|
38
|
+
* Read more about initializing the editor from source or as a build in
|
39
|
+
* {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`}.
|
40
|
+
*/
|
41
|
+
export default class MultiRootEditor extends MultiRootEditor_base {
|
42
|
+
/**
|
43
|
+
* @inheritDoc
|
44
|
+
*/
|
45
|
+
readonly ui: MultiRootEditorUI;
|
46
|
+
/**
|
47
|
+
* The elements on which the editor has been initialized.
|
48
|
+
*/
|
49
|
+
readonly sourceElements: Record<string, HTMLElement> | undefined;
|
50
|
+
/**
|
51
|
+
* Creates an instance of the multi-root editor.
|
52
|
+
*
|
53
|
+
* **Note:** Do not use the constructor to create editor instances. Use the static
|
54
|
+
* {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`} method instead.
|
55
|
+
*
|
56
|
+
* @param sourceElementsOrData The DOM elements that will be the source for the created editor
|
57
|
+
* or the editor's initial data. The editor will initialize multiple roots with names according to the keys in the passed object.
|
58
|
+
* For more information see {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`}.
|
59
|
+
* @param config The editor configuration.
|
60
|
+
*/
|
61
|
+
protected constructor(sourceElementsOrData: Record<string, HTMLElement> | Record<string, string>, config?: EditorConfig);
|
62
|
+
/**
|
63
|
+
* Destroys the editor instance, releasing all resources used by it.
|
64
|
+
*
|
65
|
+
* Updates the original editor element with the data if the
|
66
|
+
* {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy `updateSourceElementOnDestroy`}
|
67
|
+
* configuration option is set to `true`.
|
68
|
+
*
|
69
|
+
* **Note**: The multi-root editor does not remove the toolbar and editable when destroyed. You can
|
70
|
+
* do that yourself in the destruction chain, if you need to:
|
71
|
+
*
|
72
|
+
* ```ts
|
73
|
+
* editor.destroy().then( () => {
|
74
|
+
* // Remove the toolbar from DOM.
|
75
|
+
* editor.ui.view.toolbar.element.remove();
|
76
|
+
*
|
77
|
+
* // Remove editable elements from DOM.
|
78
|
+
* for ( const editable of Object.values( editor.ui.view.editables ) ) {
|
79
|
+
* editable.element.remove();
|
80
|
+
* }
|
81
|
+
*
|
82
|
+
* console.log( 'Editor was destroyed' );
|
83
|
+
* } );
|
84
|
+
* ```
|
85
|
+
*/
|
86
|
+
destroy(): Promise<unknown>;
|
87
|
+
/**
|
88
|
+
* Creates a new multi-root editor instance.
|
89
|
+
*
|
90
|
+
* **Note:** remember that `MultiRootEditor` does not append the toolbar element to your web page, so you have to do it manually
|
91
|
+
* after the editor has been initialized.
|
92
|
+
*
|
93
|
+
* There are a few different ways to initialize the multi-root editor.
|
94
|
+
*
|
95
|
+
* # Using existing DOM elements:
|
96
|
+
*
|
97
|
+
* ```ts
|
98
|
+
* MultiRootEditor.create( {
|
99
|
+
* intro: document.querySelector( '#editor-intro' ),
|
100
|
+
* content: document.querySelector( '#editor-content' ),
|
101
|
+
* sidePanelLeft: document.querySelector( '#editor-side-left' ),
|
102
|
+
* sidePanelRight: document.querySelector( '#editor-side-right' ),
|
103
|
+
* outro: document.querySelector( '#editor-outro' )
|
104
|
+
* } )
|
105
|
+
* .then( editor => {
|
106
|
+
* console.log( 'Editor was initialized', editor );
|
107
|
+
*
|
108
|
+
* // Append the toolbar inside a provided DOM element.
|
109
|
+
* document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
|
110
|
+
* } )
|
111
|
+
* .catch( err => {
|
112
|
+
* console.error( err.stack );
|
113
|
+
* } );
|
114
|
+
* ```
|
115
|
+
*
|
116
|
+
* The elements' content will be used as the editor data and elements will become editable elements.
|
117
|
+
*
|
118
|
+
* # Creating a detached editor
|
119
|
+
*
|
120
|
+
* Alternatively, you can initialize the editor by passing the initial data directly as strings.
|
121
|
+
* In this case, you will have to manually append both the toolbar element and the editable elements to your web page.
|
122
|
+
*
|
123
|
+
* ```ts
|
124
|
+
* MultiRootEditor.create( {
|
125
|
+
* intro: '<p><strong>Exciting</strong> intro text to an article.</p>',
|
126
|
+
* content: '<p>Lorem ipsum dolor sit amet.</p>',
|
127
|
+
* sidePanelLeft: '<blockquote>Strong quotation from article.</blockquote>',
|
128
|
+
* sidePanelRight: '<p>List of similar articles...</p>',
|
129
|
+
* outro: '<p>Closing text.</p>'
|
130
|
+
* } )
|
131
|
+
* .then( editor => {
|
132
|
+
* console.log( 'Editor was initialized', editor );
|
133
|
+
*
|
134
|
+
* // Append the toolbar inside a provided DOM element.
|
135
|
+
* document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
|
136
|
+
*
|
137
|
+
* // Append DOM editable elements created by the editor.
|
138
|
+
* const editables = editor.ui.view.editables;
|
139
|
+
* const container = document.querySelector( '#editable-container' );
|
140
|
+
*
|
141
|
+
* container.appendChild( editables.intro.element );
|
142
|
+
* container.appendChild( editables.content.element );
|
143
|
+
* container.appendChild( editables.outro.element );
|
144
|
+
* } )
|
145
|
+
* .catch( err => {
|
146
|
+
* console.error( err.stack );
|
147
|
+
* } );
|
148
|
+
* ```
|
149
|
+
*
|
150
|
+
* This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
|
151
|
+
* web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
|
152
|
+
*
|
153
|
+
* # Using an existing DOM element (and data provided in `config.initialData`)
|
154
|
+
*
|
155
|
+
* You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
|
156
|
+
*
|
157
|
+
* ```ts
|
158
|
+
* MultiRootEditor.create( {
|
159
|
+
* intro: document.querySelector( '#editor-intro' ),
|
160
|
+
* content: document.querySelector( '#editor-content' ),
|
161
|
+
* sidePanelLeft: document.querySelector( '#editor-side-left' ),
|
162
|
+
* sidePanelRight: document.querySelector( '#editor-side-right' ),
|
163
|
+
* outro: document.querySelector( '#editor-outro' )
|
164
|
+
* }, {
|
165
|
+
* initialData: {
|
166
|
+
* intro: '<p><strong>Exciting</strong> intro text to an article.</p>',
|
167
|
+
* content: '<p>Lorem ipsum dolor sit amet.</p>',
|
168
|
+
* sidePanelLeft '<blockquote>Strong quotation from article.</blockquote>':
|
169
|
+
* sidePanelRight '<p>List of similar articles...</p>':
|
170
|
+
* outro: '<p>Closing text.</p>'
|
171
|
+
* }
|
172
|
+
* } )
|
173
|
+
* .then( editor => {
|
174
|
+
* console.log( 'Editor was initialized', editor );
|
175
|
+
*
|
176
|
+
* // Append the toolbar inside a provided DOM element.
|
177
|
+
* document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
|
178
|
+
* } )
|
179
|
+
* .catch( err => {
|
180
|
+
* console.error( err.stack );
|
181
|
+
* } );
|
182
|
+
* ```
|
183
|
+
*
|
184
|
+
* This method can be used to initialize the editor on an existing element with the specified content in case if your integration
|
185
|
+
* makes it difficult to set the content of the source element.
|
186
|
+
*
|
187
|
+
* Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
|
188
|
+
*
|
189
|
+
* # Configuring the editor
|
190
|
+
*
|
191
|
+
* See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
|
192
|
+
* customizing plugins, toolbar and more.
|
193
|
+
*
|
194
|
+
* # Using the editor from source
|
195
|
+
*
|
196
|
+
* The code samples listed in the previous sections of this documentation assume that you are using an
|
197
|
+
* {@glink installation/getting-started/predefined-builds editor build}
|
198
|
+
* (for example – `@ckeditor/ckeditor5-build-multi-root`).
|
199
|
+
*
|
200
|
+
* If you want to use the multi-root editor from source (`@ckeditor/ckeditor5-editor-multi-root-editor/src/multirooteditor`),
|
201
|
+
* you need to define the list of
|
202
|
+
* {@link module:core/editor/editorconfig~EditorConfig#plugins plugins to be initialized} and
|
203
|
+
* {@link module:core/editor/editorconfig~EditorConfig#toolbar toolbar items}. Read more about using the editor from
|
204
|
+
* source in the {@glink installation/advanced/alternative-setups/integrating-from-source-webpack dedicated guide}.
|
205
|
+
*
|
206
|
+
* @param sourceElementsOrData The DOM elements that will be the source for the created editor
|
207
|
+
* or the editor's initial data. The editor will initialize multiple roots with names according to the keys in the passed object.
|
208
|
+
*
|
209
|
+
* If DOM elements are passed, their content will be automatically loaded to the editor upon initialization and the elements will be
|
210
|
+
* 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
|
211
|
+
* {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy updateSourceElementOnDestroy} option
|
212
|
+
* is set to `true`.
|
213
|
+
*
|
214
|
+
* If the initial data is passed, a detached editor will be created. For each entry in the passed object, one editor root and one
|
215
|
+
* editable DOM element will be created. You will need to attach the editable elements into the DOM manually. The elements are available
|
216
|
+
* through the {@link module:editor-multi-root/multirooteditorui~MultiRootEditorUI#getEditableElement `editor.ui.getEditableElement()`}
|
217
|
+
* method.
|
218
|
+
* @param config The editor configuration.
|
219
|
+
* @returns A promise resolved once the editor is ready. The promise resolves with the created editor instance.
|
220
|
+
*/
|
221
|
+
static create(sourceElementsOrData: Record<string, HTMLElement> | Record<string, string>, config?: EditorConfig): Promise<MultiRootEditor>;
|
222
|
+
}
|
223
|
+
export {};
|
@@ -0,0 +1,293 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
6
|
+
* @module editor-multi-root/multirooteditor
|
7
|
+
*/
|
8
|
+
import { Editor, DataApiMixin, secureSourceElement } from 'ckeditor5/src/core';
|
9
|
+
import { CKEditorError, getDataFromElement, setDataInElement } from 'ckeditor5/src/utils';
|
10
|
+
import MultiRootEditorUI from './multirooteditorui';
|
11
|
+
import MultiRootEditorUIView from './multirooteditoruiview';
|
12
|
+
import { isElement as _isElement } from 'lodash-es';
|
13
|
+
/**
|
14
|
+
* The {@glink installation/getting-started/predefined-builds#multi-root-editor multi-root editor} implementation.
|
15
|
+
*
|
16
|
+
* The multi-root editor provides multiple inline editable elements and a toolbar. All editable areas are controlled by one editor
|
17
|
+
* instance, which means that they share common configuration, document ID, or undo stack.
|
18
|
+
*
|
19
|
+
* This type of editor is dedicated to integrations which require a customized UI with an open structure, featuring multiple editable areas,
|
20
|
+
* allowing developers to have a control over the exact location of these editable areas.
|
21
|
+
*
|
22
|
+
* In order to create a multi-root editor instance, use the static
|
23
|
+
* {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`} method.
|
24
|
+
*
|
25
|
+
* Note that you will need to attach the editor toolbar to your web page manually, in a desired place, after the editor is initialized.
|
26
|
+
*
|
27
|
+
* # Multi-root editor and multi-root editor build
|
28
|
+
*
|
29
|
+
* The multi-root editor can be used directly from source (if you installed the
|
30
|
+
* [`@ckeditor/ckeditor5-editor-multi-root`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-multi-root) package)
|
31
|
+
* but it is also available in the
|
32
|
+
* {@glink installation/getting-started/predefined-builds#multi-root-editor multi-root editor build}.
|
33
|
+
*
|
34
|
+
* {@glink installation/getting-started/predefined-builds Builds} are ready-to-use editors with plugins bundled in.
|
35
|
+
*
|
36
|
+
* When using the editor from source you need to take care of loading all plugins by yourself
|
37
|
+
* (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
|
38
|
+
* Using the editor from source gives much better flexibility and allows for easier customization.
|
39
|
+
*
|
40
|
+
* Read more about initializing the editor from source or as a build in
|
41
|
+
* {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`}.
|
42
|
+
*/
|
43
|
+
export default class MultiRootEditor extends DataApiMixin(Editor) {
|
44
|
+
/**
|
45
|
+
* Creates an instance of the multi-root editor.
|
46
|
+
*
|
47
|
+
* **Note:** Do not use the constructor to create editor instances. Use the static
|
48
|
+
* {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`} method instead.
|
49
|
+
*
|
50
|
+
* @param sourceElementsOrData The DOM elements that will be the source for the created editor
|
51
|
+
* or the editor's initial data. The editor will initialize multiple roots with names according to the keys in the passed object.
|
52
|
+
* For more information see {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`}.
|
53
|
+
* @param config The editor configuration.
|
54
|
+
*/
|
55
|
+
constructor(sourceElementsOrData, config = {}) {
|
56
|
+
const rootNames = Object.keys(sourceElementsOrData);
|
57
|
+
const sourceIsData = rootNames.length === 0 || typeof sourceElementsOrData[rootNames[0]] === 'string';
|
58
|
+
if (sourceIsData && config.initialData !== undefined) {
|
59
|
+
// Documented in core/editor/editorconfig.jsdoc.
|
60
|
+
// eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
|
61
|
+
throw new CKEditorError('editor-create-initial-data', null);
|
62
|
+
}
|
63
|
+
super(config);
|
64
|
+
if (!sourceIsData) {
|
65
|
+
this.sourceElements = sourceElementsOrData;
|
66
|
+
}
|
67
|
+
if (this.config.get('initialData') === undefined) {
|
68
|
+
// Create initial data object containing data from all roots.
|
69
|
+
const initialData = {};
|
70
|
+
for (const rootName of rootNames) {
|
71
|
+
initialData[rootName] = getInitialData(sourceElementsOrData[rootName]);
|
72
|
+
}
|
73
|
+
this.config.set('initialData', initialData);
|
74
|
+
}
|
75
|
+
if (!sourceIsData) {
|
76
|
+
for (const rootName of rootNames) {
|
77
|
+
secureSourceElement(this, sourceElementsOrData[rootName]);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
for (const rootName of rootNames) {
|
81
|
+
// Create root and `UIView` element for each editable container.
|
82
|
+
this.model.document.createRoot('$root', rootName);
|
83
|
+
}
|
84
|
+
const options = {
|
85
|
+
shouldToolbarGroupWhenFull: !this.config.get('toolbar.shouldNotGroupWhenFull'),
|
86
|
+
editableElements: sourceIsData ? undefined : sourceElementsOrData
|
87
|
+
};
|
88
|
+
const view = new MultiRootEditorUIView(this.locale, this.editing.view, rootNames, options);
|
89
|
+
this.ui = new MultiRootEditorUI(this, view);
|
90
|
+
}
|
91
|
+
/**
|
92
|
+
* Destroys the editor instance, releasing all resources used by it.
|
93
|
+
*
|
94
|
+
* Updates the original editor element with the data if the
|
95
|
+
* {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy `updateSourceElementOnDestroy`}
|
96
|
+
* configuration option is set to `true`.
|
97
|
+
*
|
98
|
+
* **Note**: The multi-root editor does not remove the toolbar and editable when destroyed. You can
|
99
|
+
* do that yourself in the destruction chain, if you need to:
|
100
|
+
*
|
101
|
+
* ```ts
|
102
|
+
* editor.destroy().then( () => {
|
103
|
+
* // Remove the toolbar from DOM.
|
104
|
+
* editor.ui.view.toolbar.element.remove();
|
105
|
+
*
|
106
|
+
* // Remove editable elements from DOM.
|
107
|
+
* for ( const editable of Object.values( editor.ui.view.editables ) ) {
|
108
|
+
* editable.element.remove();
|
109
|
+
* }
|
110
|
+
*
|
111
|
+
* console.log( 'Editor was destroyed' );
|
112
|
+
* } );
|
113
|
+
* ```
|
114
|
+
*/
|
115
|
+
destroy() {
|
116
|
+
const shouldUpdateSourceElement = this.config.get('updateSourceElementOnDestroy');
|
117
|
+
// Cache the data and editable DOM elements, then destroy.
|
118
|
+
// It's safe to assume that the model->view conversion will not work after `super.destroy()`,
|
119
|
+
// same as `ui.getEditableElement()` method will not return editables.
|
120
|
+
const data = {};
|
121
|
+
if (this.sourceElements) {
|
122
|
+
for (const rootName of Object.keys(this.sourceElements)) {
|
123
|
+
data[rootName] = shouldUpdateSourceElement ? this.getData({ rootName }) : '';
|
124
|
+
}
|
125
|
+
}
|
126
|
+
this.ui.destroy();
|
127
|
+
return super.destroy()
|
128
|
+
.then(() => {
|
129
|
+
if (this.sourceElements) {
|
130
|
+
for (const rootName of Object.keys(this.sourceElements)) {
|
131
|
+
setDataInElement(this.sourceElements[rootName], data[rootName]);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
});
|
135
|
+
}
|
136
|
+
/**
|
137
|
+
* Creates a new multi-root editor instance.
|
138
|
+
*
|
139
|
+
* **Note:** remember that `MultiRootEditor` does not append the toolbar element to your web page, so you have to do it manually
|
140
|
+
* after the editor has been initialized.
|
141
|
+
*
|
142
|
+
* There are a few different ways to initialize the multi-root editor.
|
143
|
+
*
|
144
|
+
* # Using existing DOM elements:
|
145
|
+
*
|
146
|
+
* ```ts
|
147
|
+
* MultiRootEditor.create( {
|
148
|
+
* intro: document.querySelector( '#editor-intro' ),
|
149
|
+
* content: document.querySelector( '#editor-content' ),
|
150
|
+
* sidePanelLeft: document.querySelector( '#editor-side-left' ),
|
151
|
+
* sidePanelRight: document.querySelector( '#editor-side-right' ),
|
152
|
+
* outro: document.querySelector( '#editor-outro' )
|
153
|
+
* } )
|
154
|
+
* .then( editor => {
|
155
|
+
* console.log( 'Editor was initialized', editor );
|
156
|
+
*
|
157
|
+
* // Append the toolbar inside a provided DOM element.
|
158
|
+
* document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
|
159
|
+
* } )
|
160
|
+
* .catch( err => {
|
161
|
+
* console.error( err.stack );
|
162
|
+
* } );
|
163
|
+
* ```
|
164
|
+
*
|
165
|
+
* The elements' content will be used as the editor data and elements will become editable elements.
|
166
|
+
*
|
167
|
+
* # Creating a detached editor
|
168
|
+
*
|
169
|
+
* Alternatively, you can initialize the editor by passing the initial data directly as strings.
|
170
|
+
* In this case, you will have to manually append both the toolbar element and the editable elements to your web page.
|
171
|
+
*
|
172
|
+
* ```ts
|
173
|
+
* MultiRootEditor.create( {
|
174
|
+
* intro: '<p><strong>Exciting</strong> intro text to an article.</p>',
|
175
|
+
* content: '<p>Lorem ipsum dolor sit amet.</p>',
|
176
|
+
* sidePanelLeft: '<blockquote>Strong quotation from article.</blockquote>',
|
177
|
+
* sidePanelRight: '<p>List of similar articles...</p>',
|
178
|
+
* outro: '<p>Closing text.</p>'
|
179
|
+
* } )
|
180
|
+
* .then( editor => {
|
181
|
+
* console.log( 'Editor was initialized', editor );
|
182
|
+
*
|
183
|
+
* // Append the toolbar inside a provided DOM element.
|
184
|
+
* document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
|
185
|
+
*
|
186
|
+
* // Append DOM editable elements created by the editor.
|
187
|
+
* const editables = editor.ui.view.editables;
|
188
|
+
* const container = document.querySelector( '#editable-container' );
|
189
|
+
*
|
190
|
+
* container.appendChild( editables.intro.element );
|
191
|
+
* container.appendChild( editables.content.element );
|
192
|
+
* container.appendChild( editables.outro.element );
|
193
|
+
* } )
|
194
|
+
* .catch( err => {
|
195
|
+
* console.error( err.stack );
|
196
|
+
* } );
|
197
|
+
* ```
|
198
|
+
*
|
199
|
+
* This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
|
200
|
+
* web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
|
201
|
+
*
|
202
|
+
* # Using an existing DOM element (and data provided in `config.initialData`)
|
203
|
+
*
|
204
|
+
* You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
|
205
|
+
*
|
206
|
+
* ```ts
|
207
|
+
* MultiRootEditor.create( {
|
208
|
+
* intro: document.querySelector( '#editor-intro' ),
|
209
|
+
* content: document.querySelector( '#editor-content' ),
|
210
|
+
* sidePanelLeft: document.querySelector( '#editor-side-left' ),
|
211
|
+
* sidePanelRight: document.querySelector( '#editor-side-right' ),
|
212
|
+
* outro: document.querySelector( '#editor-outro' )
|
213
|
+
* }, {
|
214
|
+
* initialData: {
|
215
|
+
* intro: '<p><strong>Exciting</strong> intro text to an article.</p>',
|
216
|
+
* content: '<p>Lorem ipsum dolor sit amet.</p>',
|
217
|
+
* sidePanelLeft '<blockquote>Strong quotation from article.</blockquote>':
|
218
|
+
* sidePanelRight '<p>List of similar articles...</p>':
|
219
|
+
* outro: '<p>Closing text.</p>'
|
220
|
+
* }
|
221
|
+
* } )
|
222
|
+
* .then( editor => {
|
223
|
+
* console.log( 'Editor was initialized', editor );
|
224
|
+
*
|
225
|
+
* // Append the toolbar inside a provided DOM element.
|
226
|
+
* document.querySelector( '#toolbar-container' ).appendChild( editor.ui.view.toolbar.element );
|
227
|
+
* } )
|
228
|
+
* .catch( err => {
|
229
|
+
* console.error( err.stack );
|
230
|
+
* } );
|
231
|
+
* ```
|
232
|
+
*
|
233
|
+
* This method can be used to initialize the editor on an existing element with the specified content in case if your integration
|
234
|
+
* makes it difficult to set the content of the source element.
|
235
|
+
*
|
236
|
+
* Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
|
237
|
+
*
|
238
|
+
* # Configuring the editor
|
239
|
+
*
|
240
|
+
* See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
|
241
|
+
* customizing plugins, toolbar and more.
|
242
|
+
*
|
243
|
+
* # Using the editor from source
|
244
|
+
*
|
245
|
+
* The code samples listed in the previous sections of this documentation assume that you are using an
|
246
|
+
* {@glink installation/getting-started/predefined-builds editor build}
|
247
|
+
* (for example – `@ckeditor/ckeditor5-build-multi-root`).
|
248
|
+
*
|
249
|
+
* If you want to use the multi-root editor from source (`@ckeditor/ckeditor5-editor-multi-root-editor/src/multirooteditor`),
|
250
|
+
* you need to define the list of
|
251
|
+
* {@link module:core/editor/editorconfig~EditorConfig#plugins plugins to be initialized} and
|
252
|
+
* {@link module:core/editor/editorconfig~EditorConfig#toolbar toolbar items}. Read more about using the editor from
|
253
|
+
* source in the {@glink installation/advanced/alternative-setups/integrating-from-source-webpack dedicated guide}.
|
254
|
+
*
|
255
|
+
* @param sourceElementsOrData The DOM elements that will be the source for the created editor
|
256
|
+
* or the editor's initial data. The editor will initialize multiple roots with names according to the keys in the passed object.
|
257
|
+
*
|
258
|
+
* If DOM elements are passed, their content will be automatically loaded to the editor upon initialization and the elements will be
|
259
|
+
* 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
|
260
|
+
* {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy updateSourceElementOnDestroy} option
|
261
|
+
* is set to `true`.
|
262
|
+
*
|
263
|
+
* If the initial data is passed, a detached editor will be created. For each entry in the passed object, one editor root and one
|
264
|
+
* editable DOM element will be created. You will need to attach the editable elements into the DOM manually. The elements are available
|
265
|
+
* through the {@link module:editor-multi-root/multirooteditorui~MultiRootEditorUI#getEditableElement `editor.ui.getEditableElement()`}
|
266
|
+
* method.
|
267
|
+
* @param config The editor configuration.
|
268
|
+
* @returns A promise resolved once the editor is ready. The promise resolves with the created editor instance.
|
269
|
+
*/
|
270
|
+
static create(sourceElementsOrData, config = {}) {
|
271
|
+
return new Promise(resolve => {
|
272
|
+
for (const sourceItem of Object.values(sourceElementsOrData)) {
|
273
|
+
if (isElement(sourceItem) && sourceItem.tagName === 'TEXTAREA') {
|
274
|
+
// Documented in core/editor/editor.js
|
275
|
+
// eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
|
276
|
+
throw new CKEditorError('editor-wrong-element', null);
|
277
|
+
}
|
278
|
+
}
|
279
|
+
const editor = new this(sourceElementsOrData, config);
|
280
|
+
resolve(editor.initPlugins()
|
281
|
+
.then(() => editor.ui.init())
|
282
|
+
.then(() => editor.data.init(editor.config.get('initialData')))
|
283
|
+
.then(() => editor.fire('ready'))
|
284
|
+
.then(() => editor));
|
285
|
+
});
|
286
|
+
}
|
287
|
+
}
|
288
|
+
function getInitialData(sourceElementOrData) {
|
289
|
+
return isElement(sourceElementOrData) ? getDataFromElement(sourceElementOrData) : sourceElementOrData;
|
290
|
+
}
|
291
|
+
function isElement(value) {
|
292
|
+
return _isElement(value);
|
293
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
6
|
+
* @module editor-multi-root/multirooteditorui
|
7
|
+
*/
|
8
|
+
import { type Editor } from 'ckeditor5/src/core';
|
9
|
+
import { EditorUI } from 'ckeditor5/src/ui';
|
10
|
+
import type MultiRootEditorUIView from './multirooteditoruiview';
|
11
|
+
/**
|
12
|
+
* The multi-root editor UI class.
|
13
|
+
*/
|
14
|
+
export default class MultiRootEditorUI extends EditorUI {
|
15
|
+
/**
|
16
|
+
* The main (top–most) view of the editor UI.
|
17
|
+
*/
|
18
|
+
readonly view: MultiRootEditorUIView;
|
19
|
+
/**
|
20
|
+
* Creates an instance of the multi-root editor UI class.
|
21
|
+
*
|
22
|
+
* @param editor The editor instance.
|
23
|
+
* @param view The view of the UI.
|
24
|
+
*/
|
25
|
+
constructor(editor: Editor, view: MultiRootEditorUIView);
|
26
|
+
/**
|
27
|
+
* Initializes the UI.
|
28
|
+
*/
|
29
|
+
init(): void;
|
30
|
+
/**
|
31
|
+
* @inheritDoc
|
32
|
+
*/
|
33
|
+
destroy(): void;
|
34
|
+
/**
|
35
|
+
* Initializes the editor main toolbar and its panel.
|
36
|
+
*/
|
37
|
+
private _initToolbar;
|
38
|
+
/**
|
39
|
+
* Enable the placeholder text on the editing roots, if any was configured.
|
40
|
+
*/
|
41
|
+
private _initPlaceholder;
|
42
|
+
}
|
@@ -0,0 +1,143 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, 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 { EditorUI } from 'ckeditor5/src/ui';
|
6
|
+
import { enablePlaceholder } from 'ckeditor5/src/engine';
|
7
|
+
/**
|
8
|
+
* The multi-root editor UI class.
|
9
|
+
*/
|
10
|
+
export default class MultiRootEditorUI extends EditorUI {
|
11
|
+
/**
|
12
|
+
* Creates an instance of the multi-root editor UI class.
|
13
|
+
*
|
14
|
+
* @param editor The editor instance.
|
15
|
+
* @param view The view of the UI.
|
16
|
+
*/
|
17
|
+
constructor(editor, view) {
|
18
|
+
super(editor);
|
19
|
+
this.view = view;
|
20
|
+
}
|
21
|
+
/**
|
22
|
+
* Initializes the UI.
|
23
|
+
*/
|
24
|
+
init() {
|
25
|
+
const view = this.view;
|
26
|
+
const editor = this.editor;
|
27
|
+
const editingView = editor.editing.view;
|
28
|
+
let lastFocusedEditableElement;
|
29
|
+
for (const editableName of Object.keys(view.editables)) {
|
30
|
+
view.editables[editableName].name = editableName;
|
31
|
+
}
|
32
|
+
view.render();
|
33
|
+
// Keep track of the last focused editable element. Knowing which one was focused
|
34
|
+
// is useful when the focus moves from editable to other UI components like balloons
|
35
|
+
// (especially inputs) but the editable remains the "focus context" (e.g. link balloon
|
36
|
+
// attached to a link in an editable). In this case, the editable should preserve visual
|
37
|
+
// focus styles.
|
38
|
+
this.focusTracker.on('change:focusedElement', (evt, name, focusedElement) => {
|
39
|
+
for (const editable of Object.values(this.view.editables)) {
|
40
|
+
if (focusedElement === editable.element) {
|
41
|
+
lastFocusedEditableElement = editable.element;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
});
|
45
|
+
// If the focus tracker loses focus, stop tracking the last focused editable element.
|
46
|
+
// Wherever the focus is restored, it will no longer be in the context of that editable
|
47
|
+
// because the focus "came from the outside", as opposed to the focus moving from one element
|
48
|
+
// to another within the editor UI.
|
49
|
+
this.focusTracker.on('change:isFocused', (evt, name, isFocused) => {
|
50
|
+
if (!isFocused) {
|
51
|
+
lastFocusedEditableElement = null;
|
52
|
+
}
|
53
|
+
});
|
54
|
+
for (const editable of Object.values(this.view.editables)) {
|
55
|
+
// The editable UI element in DOM is available for sure only after the editor UI view has been rendered.
|
56
|
+
// But it can be available earlier if a DOM element has been passed to `MultiRootEditor.create()`.
|
57
|
+
const editableElement = editable.element;
|
58
|
+
// Register each editable UI view in the editor.
|
59
|
+
this.setEditableElement(editable.name, editableElement);
|
60
|
+
// Let the editable UI element respond to the changes in the global editor focus
|
61
|
+
// tracker. It has been added to the same tracker a few lines above but, in reality, there are
|
62
|
+
// many focusable areas in the editor, like balloons, toolbars or dropdowns and as long
|
63
|
+
// as they have focus, the editable should act like it is focused too (although technically
|
64
|
+
// it isn't), e.g. by setting the proper CSS class, visually announcing focus to the user.
|
65
|
+
// Doing otherwise will result in editable focus styles disappearing, once e.g. the
|
66
|
+
// toolbar gets focused.
|
67
|
+
editable.bind('isFocused').to(this.focusTracker, 'isFocused', this.focusTracker, 'focusedElement', (isFocused, focusedElement) => {
|
68
|
+
// When the focus tracker is blurred, it means the focus moved out of the editor UI.
|
69
|
+
// No editable will maintain focus then.
|
70
|
+
if (!isFocused) {
|
71
|
+
return false;
|
72
|
+
}
|
73
|
+
// If the focus tracker says the editor UI is focused and currently focused element
|
74
|
+
// is the editable, then the editable should be visually marked as focused too.
|
75
|
+
if (focusedElement === editableElement) {
|
76
|
+
return true;
|
77
|
+
}
|
78
|
+
// If the focus tracker says the editor UI is focused but the focused element is
|
79
|
+
// not an editable, it is possible that the editable is still (context–)focused.
|
80
|
+
// For instance, the focused element could be an input inside of a balloon attached
|
81
|
+
// to the content in the editable. In such case, the editable should remain _visually_
|
82
|
+
// focused even though technically the focus is somewhere else. The focus moved from
|
83
|
+
// the editable to the input but the focus context remained the same.
|
84
|
+
else {
|
85
|
+
return lastFocusedEditableElement === editableElement;
|
86
|
+
}
|
87
|
+
});
|
88
|
+
// Bind the editable UI element to the editing view, making it an end– and entry–point
|
89
|
+
// of the editor's engine. This is where the engine meets the UI.
|
90
|
+
editingView.attachDomRoot(editableElement, editable.name);
|
91
|
+
}
|
92
|
+
this._initPlaceholder();
|
93
|
+
this._initToolbar();
|
94
|
+
this.fire('ready');
|
95
|
+
}
|
96
|
+
/**
|
97
|
+
* @inheritDoc
|
98
|
+
*/
|
99
|
+
destroy() {
|
100
|
+
super.destroy();
|
101
|
+
const view = this.view;
|
102
|
+
const editingView = this.editor.editing.view;
|
103
|
+
for (const editable of Object.values(this.view.editables)) {
|
104
|
+
editingView.detachDomRoot(editable.name);
|
105
|
+
}
|
106
|
+
view.destroy();
|
107
|
+
}
|
108
|
+
/**
|
109
|
+
* Initializes the editor main toolbar and its panel.
|
110
|
+
*/
|
111
|
+
_initToolbar() {
|
112
|
+
const editor = this.editor;
|
113
|
+
const view = this.view;
|
114
|
+
const toolbar = view.toolbar;
|
115
|
+
toolbar.fillFromConfig(editor.config.get('toolbar'), this.componentFactory);
|
116
|
+
// Register the toolbar so it becomes available for Alt+F10 and Esc navigation.
|
117
|
+
this.addToolbar(view.toolbar);
|
118
|
+
}
|
119
|
+
/**
|
120
|
+
* Enable the placeholder text on the editing roots, if any was configured.
|
121
|
+
*/
|
122
|
+
_initPlaceholder() {
|
123
|
+
const editor = this.editor;
|
124
|
+
const editingView = editor.editing.view;
|
125
|
+
const placeholder = editor.config.get('placeholder');
|
126
|
+
if (!placeholder) {
|
127
|
+
return;
|
128
|
+
}
|
129
|
+
for (const editable of Object.values(this.view.editables)) {
|
130
|
+
const editingRoot = editingView.document.getRoot(editable.name);
|
131
|
+
const placeholderText = typeof placeholder === 'string' ? placeholder : placeholder[editable.name];
|
132
|
+
if (placeholderText) {
|
133
|
+
enablePlaceholder({
|
134
|
+
view: editingView,
|
135
|
+
element: editingRoot,
|
136
|
+
text: placeholderText,
|
137
|
+
isDirectHost: false,
|
138
|
+
keepOnFocus: true
|
139
|
+
});
|
140
|
+
}
|
141
|
+
}
|
142
|
+
}
|
143
|
+
}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
6
|
+
* @module editor-multi-root/multirooteditoruiview
|
7
|
+
*/
|
8
|
+
import { EditorUIView, InlineEditableUIView, ToolbarView } from 'ckeditor5/src/ui';
|
9
|
+
import type { Locale } from 'ckeditor5/src/utils';
|
10
|
+
import type { View } from 'ckeditor5/src/engine';
|
11
|
+
/**
|
12
|
+
* The multi-root editor UI view. It is a virtual view providing an inline
|
13
|
+
* {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#editable} and a
|
14
|
+
* {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#toolbar}, but without any
|
15
|
+
* specific arrangement of the components in the DOM.
|
16
|
+
*
|
17
|
+
* See {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`}
|
18
|
+
* to learn more about this view.
|
19
|
+
*/
|
20
|
+
export default class MultiRootEditorUIView extends EditorUIView {
|
21
|
+
/**
|
22
|
+
* The main toolbar of the multi-root editor UI.
|
23
|
+
*/
|
24
|
+
readonly toolbar: ToolbarView;
|
25
|
+
/**
|
26
|
+
* Editable elements used by the multi-root editor UI.
|
27
|
+
*/
|
28
|
+
readonly editables: Record<string, InlineEditableUIView>;
|
29
|
+
readonly editable: InlineEditableUIView;
|
30
|
+
/**
|
31
|
+
* Creates an instance of the multi-root editor UI view.
|
32
|
+
*
|
33
|
+
* @param locale The {@link module:core/editor/editor~Editor#locale} instance.
|
34
|
+
* @param editingView The editing view instance this view is related to.
|
35
|
+
* @param editableNames Names for all editable views. For each name, one
|
36
|
+
* {@link module:ui/editableui/inline/inlineeditableuiview~InlineEditableUIView `InlineEditableUIView`} instance will be initialized.
|
37
|
+
* @param options Configuration options for the view instance.
|
38
|
+
* @param options.editableElements The editable elements to be used, assigned to their names. If not specified, they will be
|
39
|
+
* automatically created by {@link module:ui/editableui/inline/inlineeditableuiview~InlineEditableUIView `InlineEditableUIView`}
|
40
|
+
* instances.
|
41
|
+
* @param options.shouldToolbarGroupWhenFull When set to `true` enables automatic items grouping
|
42
|
+
* in the main {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#toolbar toolbar}.
|
43
|
+
* See {@link module:ui/toolbar/toolbarview~ToolbarOptions#shouldGroupWhenFull} to learn more.
|
44
|
+
*/
|
45
|
+
constructor(locale: Locale, editingView: View, editableNames: Array<string>, options?: {
|
46
|
+
editableElements?: Record<string, HTMLElement>;
|
47
|
+
shouldToolbarGroupWhenFull?: boolean;
|
48
|
+
});
|
49
|
+
/**
|
50
|
+
* @inheritDoc
|
51
|
+
*/
|
52
|
+
render(): void;
|
53
|
+
}
|
@@ -0,0 +1,73 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, 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
|
+
/**
|
6
|
+
* @module editor-multi-root/multirooteditoruiview
|
7
|
+
*/
|
8
|
+
import { EditorUIView, InlineEditableUIView, ToolbarView } from 'ckeditor5/src/ui';
|
9
|
+
/**
|
10
|
+
* The multi-root editor UI view. It is a virtual view providing an inline
|
11
|
+
* {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#editable} and a
|
12
|
+
* {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#toolbar}, but without any
|
13
|
+
* specific arrangement of the components in the DOM.
|
14
|
+
*
|
15
|
+
* See {@link module:editor-multi-root/multirooteditor~MultiRootEditor.create `MultiRootEditor.create()`}
|
16
|
+
* to learn more about this view.
|
17
|
+
*/
|
18
|
+
export default class MultiRootEditorUIView extends EditorUIView {
|
19
|
+
/**
|
20
|
+
* Creates an instance of the multi-root editor UI view.
|
21
|
+
*
|
22
|
+
* @param locale The {@link module:core/editor/editor~Editor#locale} instance.
|
23
|
+
* @param editingView The editing view instance this view is related to.
|
24
|
+
* @param editableNames Names for all editable views. For each name, one
|
25
|
+
* {@link module:ui/editableui/inline/inlineeditableuiview~InlineEditableUIView `InlineEditableUIView`} instance will be initialized.
|
26
|
+
* @param options Configuration options for the view instance.
|
27
|
+
* @param options.editableElements The editable elements to be used, assigned to their names. If not specified, they will be
|
28
|
+
* automatically created by {@link module:ui/editableui/inline/inlineeditableuiview~InlineEditableUIView `InlineEditableUIView`}
|
29
|
+
* instances.
|
30
|
+
* @param options.shouldToolbarGroupWhenFull When set to `true` enables automatic items grouping
|
31
|
+
* in the main {@link module:editor-multi-root/multirooteditoruiview~MultiRootEditorUIView#toolbar toolbar}.
|
32
|
+
* See {@link module:ui/toolbar/toolbarview~ToolbarOptions#shouldGroupWhenFull} to learn more.
|
33
|
+
*/
|
34
|
+
constructor(locale, editingView, editableNames, options = {}) {
|
35
|
+
super(locale);
|
36
|
+
const t = locale.t;
|
37
|
+
this.toolbar = new ToolbarView(locale, {
|
38
|
+
shouldGroupWhenFull: options.shouldToolbarGroupWhenFull
|
39
|
+
});
|
40
|
+
this.editables = {};
|
41
|
+
// Create `InlineEditableUIView` instance for each editable.
|
42
|
+
for (const editableName of editableNames) {
|
43
|
+
const editable = new InlineEditableUIView(locale, editingView, options.editableElements ? options.editableElements[editableName] : undefined, {
|
44
|
+
label: editable => {
|
45
|
+
return t('Rich Text Editor. Editing area: %0', editable.name);
|
46
|
+
}
|
47
|
+
});
|
48
|
+
this.editables[editableName] = editable;
|
49
|
+
}
|
50
|
+
this.editable = Object.values(this.editables)[0];
|
51
|
+
// This toolbar may be placed anywhere in the page so things like font size need to be reset in it.
|
52
|
+
// Because of the above, make sure the toolbar supports rounded corners.
|
53
|
+
// Also, make sure the toolbar has the proper dir attribute because its ancestor may not have one
|
54
|
+
// and some toolbar item styles depend on this attribute.
|
55
|
+
this.toolbar.extendTemplate({
|
56
|
+
attributes: {
|
57
|
+
class: [
|
58
|
+
'ck-reset_all',
|
59
|
+
'ck-rounded-corners'
|
60
|
+
],
|
61
|
+
dir: locale.uiLanguageDirection
|
62
|
+
}
|
63
|
+
});
|
64
|
+
}
|
65
|
+
/**
|
66
|
+
* @inheritDoc
|
67
|
+
*/
|
68
|
+
render() {
|
69
|
+
super.render();
|
70
|
+
this.registerChild(Object.values(this.editables));
|
71
|
+
this.registerChild([this.toolbar]);
|
72
|
+
}
|
73
|
+
}
|