@@ -17,6 +17,7 @@ import ClassicEditorUIView from './classiceditoruiview';
1717import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement' ;
1818import mix from '@ckeditor/ckeditor5-utils/src/mix' ;
1919import { isElement } from 'lodash-es' ;
20+ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror' ;
2021
2122/**
2223 * The {@glink builds/guides/overview#classic-editor classic editor} implementation.
@@ -92,9 +93,11 @@ export default class ClassicEditor extends Editor {
9293 }
9394
9495 /**
95- * Creates a classic editor instance.
96+ * Creates a `ClassicEditor` instance.
9697 *
97- * Creating an instance when using a {@glink builds/index CKEditor build}:
98+ * There are two general ways how the editor can be initialized.
99+ *
100+ * You can initialize the editor using an existing DOM element:
98101 *
99102 * ClassicEditor
100103 * .create( document.querySelector( '#editor' ) )
@@ -105,78 +108,80 @@ export default class ClassicEditor extends Editor {
105108 * console.error( err.stack );
106109 * } );
107110 *
108- * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
111+ * The element's content will be used as the editor data and the element will be replaced by the editable element and the editor UI.
109112 *
110- * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
111- * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
112- * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
113- * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
114- * import ...
113+ * Alternatively, you can initialize the editor by passing the initial data directly as a `String`.
114+ * In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly:
115115 *
116116 * ClassicEditor
117- * .create( document.querySelector( '#editor' ), {
118- * plugins: [ Essentials, Bold, Italic, ... ],
119- * toolbar: [ 'bold', 'italic', ... ]
120- * } )
117+ * .create( '<p>Hello world!</p>' )
121118 * .then( editor => {
122119 * console.log( 'Editor was initialized', editor );
120+ *
121+ * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
122+ * document.body.appendChild( editor.ui.element );
123123 * } )
124124 * .catch( err => {
125125 * console.error( err.stack );
126126 * } );
127127 *
128- * Creating an instance when using initial data instead of a DOM element:
128+ * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
129+ * web page content is generated on the client-side and the DOM structure is not ready at the moment when you initialize the editor.
129130 *
130- * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
131- * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
132- * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
133- * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
134- * import ...
131+ * You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config:
135132 *
136133 * ClassicEditor
137- * .create( '<p>Hello world!</p>' )
134+ * .create( document.querySelector( '#editor' ), {
135+ * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
136+ * } )
138137 * .then( editor => {
139138 * console.log( 'Editor was initialized', editor );
140- *
141- * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
142- * document.body.appendChild( editor.element );
143139 * } )
144140 * .catch( err => {
145141 * console.error( err.stack );
146142 * } );
147143 *
144+ * This method can be used to initialize the editor on an existing element with specified content in case if your integration
145+ * makes it difficult to set the content of the source element.
146+ *
147+ * Note that an error will be thrown if you pass initial data both as the first parameter and also in the config.
148+ *
149+ * See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
150+ * customizing plugins, toolbar and other.
151+ *
148152 * @param {HTMLElement|String } sourceElementOrData The DOM element that will be the source for the created editor
149153 * or the editor's initial data.
150154 *
151- * If a source element is passed, then its contents will be automatically
152- * {@link module:editor-classic/classiceditor~ClassicEditor#setData loaded} to the editor on startup
153- * and the {@link module:core/editor/editorui~EditorUI#getEditableElement editor element} will replace the passed element in the DOM
154- * (the original one will be hidden and the editor will be injected next to it).
155- *
156- * Moreover, the data will be set back to the source element once the editor is destroyed and
157- * (if the element is a `<textarea>`) when a form in which this element is contained is submitted (which ensures
158- * automatic integration with native web forms).
155+ * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization
156+ * and the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element editor element} will replace the passed element
157+ * in the DOM (the original one will be hidden and the editor will be injected next to it).
159158 *
160- * If the data is passed, a detached editor will be created. It means that you need to insert it into the DOM manually (by accessing
161- * it via the {@link module:editor-classic/classiceditorui~ClassicEditorUI#getEditableElement `editor.ui.getEditableElement()`} method).
159+ * Moreover, the editor data will be set back to the original element once the editor is destroyed and when a form, in which
160+ * this element is contained, is submitted (if the original element is a `<textarea>`). This ensures seamless integration with native
161+ * web forms.
162162 *
163- * See the examples above to learn more.
163+ * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
164+ * It is available under {@link module:editor-classic/classiceditorui~ClassicEditorUI#element `editor.ui.element`} property.
164165 *
165- * @param {module:core/editor/editorconfig~EditorConfig } config The editor configuration.
166- * @returns {Promise } A promise resolved once the editor is ready.
167- * The promise returns the created {@link module:editor-classic/classiceditor~ClassicEditor} instance.
166+ * @param {module:core/editor/editorconfig~EditorConfig } [config] The editor configuration.
167+ * @returns {Promise } A promise resolved once the editor is ready. The promise resolves with the created editor instance.
168168 */
169- static create ( sourceElementOrData , config ) {
169+ static create ( sourceElementOrData , config = { } ) {
170170 return new Promise ( resolve => {
171171 const editor = new this ( sourceElementOrData , config ) ;
172172
173173 resolve (
174174 editor . initPlugins ( )
175175 . then ( ( ) => editor . ui . init ( isElement ( sourceElementOrData ) ? sourceElementOrData : null ) )
176176 . then ( ( ) => {
177- const initialData = isElement ( sourceElementOrData ) ?
178- getDataFromElement ( sourceElementOrData ) :
179- sourceElementOrData ;
177+ if ( ! isElement ( sourceElementOrData ) && config . initialData ) {
178+ throw new CKEditorError (
179+ 'editor-create-initial-data: ' +
180+ 'EditorConfig#initialData cannot be used together with initial data passed in Editor#create()'
181+ ) ;
182+ }
183+
184+ const initialData = config . initialData || getInitialData ( sourceElementOrData ) ;
180185
181186 return editor . data . init ( initialData ) ;
182187 } )
@@ -189,3 +194,7 @@ export default class ClassicEditor extends Editor {
189194
190195mix ( ClassicEditor , DataApiMixin ) ;
191196mix ( ClassicEditor , ElementApiMixin ) ;
197+
198+ function getInitialData ( sourceElementOrData ) {
199+ return isElement ( sourceElementOrData ) ? getDataFromElement ( sourceElementOrData ) : sourceElementOrData ;
200+ }
0 commit comments