|
|
@@ -1,6 +1,6 @@
|
|
|
/**
|
|
|
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
|
|
|
- * For licensing, see LICENSE.md.
|
|
|
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
|
|
|
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
@@ -14,10 +14,10 @@ import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform
|
|
|
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
|
|
|
import ClassicEditorUI from './classiceditorui';
|
|
|
import ClassicEditorUIView from './classiceditoruiview';
|
|
|
-import ElementReplacer from '@ckeditor/ckeditor5-utils/src/elementreplacer';
|
|
|
import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
|
|
|
import mix from '@ckeditor/ckeditor5-utils/src/mix';
|
|
|
import { isElement } from 'lodash-es';
|
|
|
+import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
|
|
|
|
/**
|
|
|
* The {@glink builds/guides/overview#classic-editor classic editor} implementation.
|
|
|
@@ -66,30 +66,15 @@ export default class ClassicEditor extends Editor {
|
|
|
this.sourceElement = sourceElementOrData;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * The element replacer instance used to hide the editor's source element.
|
|
|
- *
|
|
|
- * @protected
|
|
|
- * @member {module:utils/elementreplacer~ElementReplacer}
|
|
|
- */
|
|
|
- this._elementReplacer = new ElementReplacer();
|
|
|
-
|
|
|
this.data.processor = new HtmlDataProcessor();
|
|
|
|
|
|
this.model.document.createRoot();
|
|
|
|
|
|
- this.ui = new ClassicEditorUI( this, new ClassicEditorUIView( this.locale ) );
|
|
|
+ this.ui = new ClassicEditorUI( this, new ClassicEditorUIView( this.locale, this.editing.view ) );
|
|
|
|
|
|
attachToForm( this );
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- get element() {
|
|
|
- return this.ui.view.element;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Destroys the editor instance, releasing all resources used by it.
|
|
|
*
|
|
|
@@ -102,16 +87,19 @@ export default class ClassicEditor extends Editor {
|
|
|
this.updateSourceElement();
|
|
|
}
|
|
|
|
|
|
- this._elementReplacer.restore();
|
|
|
this.ui.destroy();
|
|
|
|
|
|
return super.destroy();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Creates a classic editor instance.
|
|
|
+ * Creates a new classic editor instance.
|
|
|
+ *
|
|
|
+ * There are three ways how the editor can be initialized.
|
|
|
*
|
|
|
- * Creating an instance when using a {@glink builds/index CKEditor build}:
|
|
|
+ * # Replacing a DOM element (and loading data from it)
|
|
|
+ *
|
|
|
+ * You can initialize the editor using an existing DOM element:
|
|
|
*
|
|
|
* ClassicEditor
|
|
|
* .create( document.querySelector( '#editor' ) )
|
|
|
@@ -122,93 +110,103 @@ export default class ClassicEditor extends Editor {
|
|
|
* console.error( err.stack );
|
|
|
* } );
|
|
|
*
|
|
|
- * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
|
|
|
+ * The element's content will be used as the editor data and the element will be replaced by the editor UI.
|
|
|
+ *
|
|
|
+ * # Creating a detached editor
|
|
|
*
|
|
|
- * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
|
|
|
- * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
|
|
|
- * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
|
|
|
- * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
|
|
|
- * import ...
|
|
|
+ * Alternatively, you can initialize the editor by passing the initial data directly as a string.
|
|
|
+ * In this case, the editor will render an element that must be inserted into the DOM:
|
|
|
*
|
|
|
* ClassicEditor
|
|
|
- * .create( document.querySelector( '#editor' ), {
|
|
|
- * plugins: [ Essentials, Bold, Italic, ... ],
|
|
|
- * toolbar: [ 'bold', 'italic', ... ]
|
|
|
- * } )
|
|
|
+ * .create( '<p>Hello world!</p>' )
|
|
|
* .then( editor => {
|
|
|
* console.log( 'Editor was initialized', editor );
|
|
|
+ *
|
|
|
+ * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
|
|
|
+ * document.body.appendChild( editor.ui.element );
|
|
|
* } )
|
|
|
* .catch( err => {
|
|
|
* console.error( err.stack );
|
|
|
* } );
|
|
|
*
|
|
|
- * Creating an instance when using initial data instead of a DOM element:
|
|
|
+ * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
|
|
|
+ * web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
|
|
|
*
|
|
|
- * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
|
|
|
- * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
|
|
|
- * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
|
|
|
- * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
|
|
|
- * import ...
|
|
|
+ * # Replacing a DOM element (and data provided in `config.initialData`)
|
|
|
+ *
|
|
|
+ * You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
|
|
|
*
|
|
|
* ClassicEditor
|
|
|
- * .create( '<p>Hello world!</p>' )
|
|
|
+ * .create( document.querySelector( '#editor' ), {
|
|
|
+ * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
|
|
|
+ * } )
|
|
|
* .then( editor => {
|
|
|
* console.log( 'Editor was initialized', editor );
|
|
|
- *
|
|
|
- * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
|
|
|
- * document.body.appendChild( editor.element );
|
|
|
* } )
|
|
|
* .catch( err => {
|
|
|
* console.error( err.stack );
|
|
|
* } );
|
|
|
*
|
|
|
+ * This method can be used to initialize the editor on an existing element with the specified content in case if your integration
|
|
|
+ * makes it difficult to set the content of the source element.
|
|
|
+ *
|
|
|
+ * Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
|
|
|
+ *
|
|
|
+ * # Configuring the editor
|
|
|
+ *
|
|
|
+ * See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
|
|
|
+ * customizing plugins, toolbar and more.
|
|
|
+ *
|
|
|
+ * # Using the editor from source
|
|
|
+ *
|
|
|
+ * The code samples listed in the previous sections of this documentation assume that you are using an
|
|
|
+ * {@glink builds/guides/overview editor build} (for example – `@ckeditor/ckeditor5-build-classic`).
|
|
|
+ *
|
|
|
+ * If you want to use the classic editor from source (`@ckeditor/ckeditor5-editor-classic/src/classiceditor`),
|
|
|
+ * you need to define the list of
|
|
|
+ * {@link module:core/editor/editorconfig~EditorConfig#plugins plugins to be initialized} and
|
|
|
+ * {@link module:core/editor/editorconfig~EditorConfig#toolbar toolbar items}. Read more about using the editor from
|
|
|
+ * source in the {@glink builds/guides/integration/advanced-setup "Advanced setup" guide}.
|
|
|
+ *
|
|
|
* @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
|
|
|
* or the editor's initial data.
|
|
|
*
|
|
|
- * If a source element is passed, then its contents will be automatically
|
|
|
- * {@link module:editor-classic/classiceditor~ClassicEditor#setData loaded} to the editor on startup
|
|
|
- * and the {@link module:core/editor/editorwithui~EditorWithUI#element editor element} will replace the passed element in the DOM
|
|
|
- * (the original one will be hidden and the editor will be injected next to it).
|
|
|
+ * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization
|
|
|
+ * and the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element editor element} will replace the passed element
|
|
|
+ * in the DOM (the original one will be hidden and the editor will be injected next to it).
|
|
|
*
|
|
|
- * Moreover, the data will be set back to the source element once the editor is destroyed and
|
|
|
- * (if the element is a `<textarea>`) when a form in which this element is contained is submitted (which ensures
|
|
|
- * automatic integration with native web forms).
|
|
|
+ * Moreover, the editor data will be set back to the original element once the editor is destroyed and when a form, in which
|
|
|
+ * this element is contained, is submitted (if the original element is a `<textarea>`). This ensures seamless integration with native
|
|
|
+ * web forms.
|
|
|
*
|
|
|
- * 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 the {@link module:editor-classic/classiceditor~ClassicEditor#element `editor.element`} property).
|
|
|
+ * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
|
|
|
+ * It is available under the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element `editor.ui.element`} property.
|
|
|
*
|
|
|
- * See the examples above to learn more.
|
|
|
- *
|
|
|
- * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
|
|
|
- * @returns {Promise} A promise resolved once the editor is ready.
|
|
|
- * The promise returns the created {@link module:editor-classic/classiceditor~ClassicEditor} instance.
|
|
|
+ * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
|
|
|
+ * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
|
|
|
*/
|
|
|
- static create( sourceElementOrData, config ) {
|
|
|
+ static create( sourceElementOrData, config = {} ) {
|
|
|
return new Promise( resolve => {
|
|
|
const editor = new this( sourceElementOrData, config );
|
|
|
|
|
|
resolve(
|
|
|
editor.initPlugins()
|
|
|
- .then( () => editor.ui.init() )
|
|
|
+ .then( () => editor.ui.init( isElement( sourceElementOrData ) ? sourceElementOrData : null ) )
|
|
|
.then( () => {
|
|
|
- if ( isElement( sourceElementOrData ) ) {
|
|
|
- editor._elementReplacer.replace( sourceElementOrData, editor.element );
|
|
|
+ if ( !isElement( sourceElementOrData ) && config.initialData ) {
|
|
|
+ // Documented in core/editor/editorconfig.jdoc.
|
|
|
+ throw new CKEditorError(
|
|
|
+ 'editor-create-initial-data: ' +
|
|
|
+ 'The config.initialData option cannot be used together with initial data passed in Editor.create().',
|
|
|
+ null
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
- editor.fire( 'uiReady' );
|
|
|
- } )
|
|
|
- .then( () => editor.editing.view.attachDomRoot( editor.ui.view.editableElement ) )
|
|
|
- .then( () => {
|
|
|
- const initialData = isElement( sourceElementOrData ) ?
|
|
|
- getDataFromElement( sourceElementOrData ) :
|
|
|
- sourceElementOrData;
|
|
|
+ const initialData = config.initialData || getInitialData( sourceElementOrData );
|
|
|
|
|
|
return editor.data.init( initialData );
|
|
|
} )
|
|
|
- .then( () => {
|
|
|
- editor.fire( 'dataReady' );
|
|
|
- editor.fire( 'ready' );
|
|
|
- } )
|
|
|
+ .then( () => editor.fire( 'ready' ) )
|
|
|
.then( () => editor )
|
|
|
);
|
|
|
} );
|
|
|
@@ -217,3 +215,7 @@ export default class ClassicEditor extends Editor {
|
|
|
|
|
|
mix( ClassicEditor, DataApiMixin );
|
|
|
mix( ClassicEditor, ElementApiMixin );
|
|
|
+
|
|
|
+function getInitialData( sourceElementOrData ) {
|
|
|
+ return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
|
|
|
+}
|