@@ -18,6 +18,7 @@ import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement
1818import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement' ;
1919import mix from '@ckeditor/ckeditor5-utils/src/mix' ;
2020import { isElement } from 'lodash-es' ;
21+ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror' ;
2122
2223/**
2324 * The {@glink builds/guides/overview#inline-editor inline editor} implementation.
@@ -99,9 +100,11 @@ export default class InlineEditor extends Editor {
99100 }
100101
101102 /**
102- * Creates an inline editor instance.
103+ * Creates an `InlineEditor` instance.
103104 *
104- * Creating an instance when using a {@glink builds/index CKEditor build}:
105+ * There are two general ways how the editor can be initialized.
106+ *
107+ * You can initialize the editor using an existing DOM element:
105108 *
106109 * InlineEditor
107110 * .create( document.querySelector( '#editor' ) )
@@ -112,60 +115,60 @@ export default class InlineEditor extends Editor {
112115 * console.error( err.stack );
113116 * } );
114117 *
115- * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
118+ * The element's content will be used as the editor data and the element will become the editable element.
116119 *
117- * import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
118- * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
119- * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
120- * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
121- * import ...
120+ * Alternatively, you can initialize the editor by passing the initial data directly as a `String`.
121+ * In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly:
122122 *
123123 * InlineEditor
124- * .create( document.querySelector( '#editor' ), {
125- * plugins: [ Essentials, Bold, Italic, ... ],
126- * toolbar: [ 'bold', 'italic', ... ]
127- * } )
124+ * .create( '<p>Hello world!</p>' )
128125 * .then( editor => {
129126 * console.log( 'Editor was initialized', editor );
127+ *
128+ * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
129+ * document.body.appendChild( editor.ui.element );
130130 * } )
131131 * .catch( err => {
132132 * console.error( err.stack );
133133 * } );
134134 *
135- * Creating an instance when using the initial data instead of a DOM element:
135+ * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
136+ * web page content is generated on the client-side and the DOM structure is not ready at the moment when you initialize the editor.
136137 *
137- * import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
138- * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
139- * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
140- * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
141- * import ...
138+ * You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config:
142139 *
143140 * InlineEditor
144- * .create( '<p>Hello world!</p>' )
141+ * .create( document.querySelector( '#editor' ), {
142+ * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
143+ * } )
145144 * .then( editor => {
146145 * console.log( 'Editor was initialized', editor );
147- *
148- * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
149- * document.body.appendChild( editor.element );
150146 * } )
151147 * .catch( err => {
152148 * console.error( err.stack );
153149 * } );
154150 *
151+ * This method can be used to initialize the editor on an existing element with specified content in case if your integration
152+ * makes it difficult to set the content of the source element.
153+ *
154+ * Note that an error will be thrown if you pass initial data both as the first parameter and also in the config.
155+ *
156+ * See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
157+ * customizing plugins, toolbar and other.
158+ *
155159 * @param {HTMLElement|String } sourceElementOrData The DOM element that will be the source for the created editor
156- * (on which the editor will be initialized) or the initial data for the editor .
160+ * or the editor's initial data.
157161 *
158- * If a source element is passed, then its contents will be automatically
159- * {@link module:editor-inline/inlineeditor~InlineEditor#setData loaded} to the editor on startup and the element
160- * itself will be used as the editor's editable element.
162+ * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization.
163+ * Moreover, the editor data will be set back to the original element once the editor is destroyed.
161164 *
162- * If data is provided, then ` editor.element` will be created automatically and needs to be added
163- * to the DOM manually .
164- * @param { module:core/editor/editorconfig~EditorConfig } config The editor configuration.
165- * @returns { Promise } A promise resolved once the editor is ready .
166- * The promise returns the created { @link module: editor-inline/inlineeditor~InlineEditor} instance.
165+ * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
166+ * It is available under { @link module:editor-inline/inlineeditorui~InlineEditorUI#element `editor.ui.element`} property .
167+ *
168+ * @param { module:core/editor/editorconfig~EditorConfig } [config] The editor configuration .
169+ * @returns { Promise } A promise resolved once the editor is ready. The promise resolves with the created editor instance.
167170 */
168- static create ( sourceElementOrData , config ) {
171+ static create ( sourceElementOrData , config = { } ) {
169172 return new Promise ( resolve => {
170173 const editor = new this ( sourceElementOrData , config ) ;
171174
@@ -175,9 +178,14 @@ export default class InlineEditor extends Editor {
175178 editor . ui . init ( ) ;
176179 } )
177180 . then ( ( ) => {
178- const initialData = isElement ( sourceElementOrData ) ?
179- getDataFromElement ( sourceElementOrData ) :
180- sourceElementOrData ;
181+ if ( ! isElement ( sourceElementOrData ) && config . initialData ) {
182+ throw new CKEditorError (
183+ 'editor-create-initial-data: ' +
184+ 'EditorConfig#initialData cannot be used together with initial data passed in Editor#create()'
185+ ) ;
186+ }
187+
188+ const initialData = config . initialData || getInitialData ( sourceElementOrData ) ;
181189
182190 return editor . data . init ( initialData ) ;
183191 } )
@@ -190,3 +198,7 @@ export default class InlineEditor extends Editor {
190198
191199mix ( InlineEditor , DataApiMixin ) ;
192200mix ( InlineEditor , ElementApiMixin ) ;
201+
202+ function getInitialData ( sourceElementOrData ) {
203+ return isElement ( sourceElementOrData ) ? getDataFromElement ( sourceElementOrData ) : sourceElementOrData ;
204+ }
0 commit comments