/** * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /** * @module editor-classic/classiceditor */ import Editor from '@ckeditor/ckeditor5-core/src/editor/editor'; import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin'; import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin'; 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 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. * It uses an inline editable and a sticky toolbar, all enclosed in a boxed UI. * See the {@glink examples/builds/classic-editor demo}. * * In order to create a classic editor instance, use the static * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method. * * # Classic editor and classic build * * The classic editor can be used directly from source (if you installed the * [`@ckeditor/ckeditor5-editor-classic`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-classic) package) * but it is also available in the {@glink builds/guides/overview#classic-editor classic build}. * * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from * source you need to take care of loading all plugins by yourself * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option). * Using the editor from source gives much better flexibility and allows easier customization. * * Read more about initializing the editor from source or as a build in * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}. * * @mixes module:core/editor/utils/dataapimixin~DataApiMixin * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin * @implements module:core/editor/editorwithui~EditorWithUI * @extends module:core/editor/editor~Editor */ export default class ClassicEditor extends Editor { /** * Creates an instance of the classic editor. * * **Note:** do not use the constructor to create editor instances. Use the static * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method instead. * * @protected * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor * or the editor's initial data. For more information see * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration. */ constructor( sourceElementOrData, config ) { super( config ); if ( isElement( sourceElementOrData ) ) { this.sourceElement = sourceElementOrData; } this.data.processor = new HtmlDataProcessor(); this.model.document.createRoot(); this.ui = new ClassicEditorUI( this, new ClassicEditorUIView( this.locale, this.editing.view ) ); attachToForm( this ); } /** * Destroys the editor instance, releasing all resources used by it. * * Updates the editor's source element with the data. * * @returns {Promise} */ destroy() { if ( this.sourceElement ) { this.updateSourceElement(); } this.ui.destroy(); return super.destroy(); } /** * Creates a `ClassicEditor` instance. * * There are two general ways how the editor can be initialized. * * You can initialize the editor using an existing DOM element: * * ClassicEditor * .create( document.querySelector( '#editor' ) ) * .then( editor => { * console.log( 'Editor was initialized', editor ); * } ) * .catch( err => { * console.error( err.stack ); * } ); * * 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. * * 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 for the editor to work properly: * * ClassicEditor * .create( '
Hello world!
' ) * .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 ); * } ); * * 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. * * You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config: * * ClassicEditor * .create( document.querySelector( '#editor' ), { * initialData: 'Foo bar.
' * } ) * .then( editor => { * console.log( 'Editor was initialized', editor ); * } ) * .catch( err => { * console.error( err.stack ); * } ); * * This method can be used to initialize the editor on an existing element with 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 initial data both as the first parameter and also in the config. * * See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about * customizing plugins, toolbar and other. * * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor * or the editor's initial data. * * 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 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 `