classiceditor.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module editor-classic/classiceditor
  7. */
  8. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  9. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  10. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  11. import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform';
  12. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  13. import ClassicEditorUI from './classiceditorui';
  14. import ClassicEditorUIView from './classiceditoruiview';
  15. import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
  16. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  17. import { isElement } from 'lodash-es';
  18. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  19. /**
  20. * The {@glink builds/guides/overview#classic-editor classic editor} implementation.
  21. * It uses an inline editable and a sticky toolbar, all enclosed in a boxed UI.
  22. * See the {@glink examples/builds/classic-editor demo}.
  23. *
  24. * In order to create a classic editor instance, use the static
  25. * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method.
  26. *
  27. * # Classic editor and classic build
  28. *
  29. * The classic editor can be used directly from source (if you installed the
  30. * [`@ckeditor/ckeditor5-editor-classic`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-classic) package)
  31. * but it is also available in the {@glink builds/guides/overview#classic-editor classic build}.
  32. *
  33. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  34. * 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 easier customization.
  37. *
  38. * Read more about initializing the editor from source or as a build in
  39. * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}.
  40. *
  41. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  42. * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin
  43. * @implements module:core/editor/editorwithui~EditorWithUI
  44. * @extends module:core/editor/editor~Editor
  45. */
  46. export default class ClassicEditor extends Editor {
  47. /**
  48. * Creates an instance of the classic editor.
  49. *
  50. * **Note:** do not use the constructor to create editor instances. Use the static
  51. * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method instead.
  52. *
  53. * @protected
  54. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  55. * or the editor's initial data. For more information see
  56. * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}.
  57. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  58. */
  59. constructor( sourceElementOrData, config ) {
  60. super( config );
  61. if ( isElement( sourceElementOrData ) ) {
  62. this.sourceElement = sourceElementOrData;
  63. }
  64. this.data.processor = new HtmlDataProcessor();
  65. this.model.document.createRoot();
  66. this.ui = new ClassicEditorUI( this, new ClassicEditorUIView( this.locale, this.editing.view ) );
  67. attachToForm( this );
  68. }
  69. /**
  70. * Destroys the editor instance, releasing all resources used by it.
  71. *
  72. * Updates the editor's source element with the data.
  73. *
  74. * @returns {Promise}
  75. */
  76. destroy() {
  77. if ( this.sourceElement ) {
  78. this.updateSourceElement();
  79. }
  80. this.ui.destroy();
  81. return super.destroy();
  82. }
  83. /**
  84. * Creates a `ClassicEditor` instance.
  85. *
  86. * There are two general ways how the editor can be initialized.
  87. *
  88. * You can initialize the editor using an existing DOM element:
  89. *
  90. * ClassicEditor
  91. * .create( document.querySelector( '#editor' ) )
  92. * .then( editor => {
  93. * console.log( 'Editor was initialized', editor );
  94. * } )
  95. * .catch( err => {
  96. * console.error( err.stack );
  97. * } );
  98. *
  99. * 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.
  100. *
  101. * Alternatively, you can initialize the editor by passing the initial data directly as a `String`.
  102. * In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly:
  103. *
  104. * ClassicEditor
  105. * .create( '<p>Hello world!</p>' )
  106. * .then( editor => {
  107. * console.log( 'Editor was initialized', editor );
  108. *
  109. * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
  110. * document.body.appendChild( editor.ui.element );
  111. * } )
  112. * .catch( err => {
  113. * console.error( err.stack );
  114. * } );
  115. *
  116. * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
  117. * web page content is generated on the client-side and the DOM structure is not ready at the moment when you initialize the editor.
  118. *
  119. * You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config:
  120. *
  121. * ClassicEditor
  122. * .create( document.querySelector( '#editor' ), {
  123. * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
  124. * } )
  125. * .then( editor => {
  126. * console.log( 'Editor was initialized', editor );
  127. * } )
  128. * .catch( err => {
  129. * console.error( err.stack );
  130. * } );
  131. *
  132. * This method can be used to initialize the editor on an existing element with specified content in case if your integration
  133. * makes it difficult to set the content of the source element.
  134. *
  135. * Note that an error will be thrown if you pass initial data both as the first parameter and also in the config.
  136. *
  137. * See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
  138. * customizing plugins, toolbar and other.
  139. *
  140. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  141. * or the editor's initial data.
  142. *
  143. * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization
  144. * and the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element editor element} will replace the passed element
  145. * in the DOM (the original one will be hidden and the editor will be injected next to it).
  146. *
  147. * Moreover, the editor data will be set back to the original element once the editor is destroyed and when a form, in which
  148. * this element is contained, is submitted (if the original element is a `<textarea>`). This ensures seamless integration with native
  149. * web forms.
  150. *
  151. * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
  152. * It is available under {@link module:editor-classic/classiceditorui~ClassicEditorUI#element `editor.ui.element`} property.
  153. *
  154. * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
  155. * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
  156. */
  157. static create( sourceElementOrData, config = {} ) {
  158. return new Promise( resolve => {
  159. const editor = new this( sourceElementOrData, config );
  160. resolve(
  161. editor.initPlugins()
  162. .then( () => editor.ui.init( isElement( sourceElementOrData ) ? sourceElementOrData : null ) )
  163. .then( () => {
  164. if ( !isElement( sourceElementOrData ) && config.initialData ) {
  165. throw new CKEditorError(
  166. 'editor-create-initial-data: ' +
  167. 'EditorConfig#initialData cannot be used together with initial data passed in Editor#create()'
  168. );
  169. }
  170. const initialData = config.initialData || getInitialData( sourceElementOrData );
  171. return editor.data.init( initialData );
  172. } )
  173. .then( () => editor.fire( 'ready' ) )
  174. .then( () => editor )
  175. );
  176. } );
  177. }
  178. }
  179. mix( ClassicEditor, DataApiMixin );
  180. mix( ClassicEditor, ElementApiMixin );
  181. function getInitialData( sourceElementOrData ) {
  182. return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
  183. }