classiceditor.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /**
  19. * The {@glink builds/guides/overview#classic-editor classic editor} implementation.
  20. * It uses an inline editable and a sticky toolbar, all enclosed in a boxed UI.
  21. * See the {@glink examples/builds/classic-editor demo}.
  22. *
  23. * In order to create a classic editor instance, use the static
  24. * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method.
  25. *
  26. * # Classic editor and classic build
  27. *
  28. * The classic editor can be used directly from source (if you installed the
  29. * [`@ckeditor/ckeditor5-editor-classic`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-classic) package)
  30. * but it is also available in the {@glink builds/guides/overview#classic-editor classic build}.
  31. *
  32. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  33. * source you need to take care of loading all plugins by yourself
  34. * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
  35. * Using the editor from source gives much better flexibility and allows easier customization.
  36. *
  37. * Read more about initializing the editor from source or as a build in
  38. * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}.
  39. *
  40. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  41. * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin
  42. * @implements module:core/editor/editorwithui~EditorWithUI
  43. * @extends module:core/editor/editor~Editor
  44. */
  45. export default class ClassicEditor extends Editor {
  46. /**
  47. * Creates an instance of the classic editor.
  48. *
  49. * **Note:** do not use the constructor to create editor instances. Use the static
  50. * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method instead.
  51. *
  52. * @protected
  53. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  54. * or the editor's initial data. For more information see
  55. * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}.
  56. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  57. */
  58. constructor( sourceElementOrData, config ) {
  59. super( config );
  60. if ( isElement( sourceElementOrData ) ) {
  61. this.sourceElement = sourceElementOrData;
  62. }
  63. this.data.processor = new HtmlDataProcessor();
  64. this.model.document.createRoot();
  65. this.ui = new ClassicEditorUI( this, new ClassicEditorUIView( this.locale, this.editing.view ) );
  66. attachToForm( this );
  67. }
  68. /**
  69. * Destroys the editor instance, releasing all resources used by it.
  70. *
  71. * Updates the editor's source element with the data.
  72. *
  73. * @returns {Promise}
  74. */
  75. destroy() {
  76. if ( this.sourceElement ) {
  77. this.updateSourceElement();
  78. }
  79. this.ui.destroy();
  80. return super.destroy();
  81. }
  82. /**
  83. * Creates a classic editor instance.
  84. *
  85. * Creating an instance when using a {@glink builds/index CKEditor build}:
  86. *
  87. * ClassicEditor
  88. * .create( document.querySelector( '#editor' ) )
  89. * .then( editor => {
  90. * console.log( 'Editor was initialized', editor );
  91. * } )
  92. * .catch( err => {
  93. * console.error( err.stack );
  94. * } );
  95. *
  96. * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  97. *
  98. * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  99. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  100. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  101. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  102. * import ...
  103. *
  104. * ClassicEditor
  105. * .create( document.querySelector( '#editor' ), {
  106. * plugins: [ Essentials, Bold, Italic, ... ],
  107. * toolbar: [ 'bold', 'italic', ... ]
  108. * } )
  109. * .then( editor => {
  110. * console.log( 'Editor was initialized', editor );
  111. * } )
  112. * .catch( err => {
  113. * console.error( err.stack );
  114. * } );
  115. *
  116. * Creating an instance when using initial data instead of a DOM element:
  117. *
  118. * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  119. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  120. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  121. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  122. * import ...
  123. *
  124. * ClassicEditor
  125. * .create( '<p>Hello world!</p>' )
  126. * .then( editor => {
  127. * console.log( 'Editor was initialized', editor );
  128. *
  129. * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
  130. * document.body.appendChild( editor.element );
  131. * } )
  132. * .catch( err => {
  133. * console.error( err.stack );
  134. * } );
  135. *
  136. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  137. * or the editor's initial data.
  138. *
  139. * If a source element is passed, then its contents will be automatically
  140. * {@link module:editor-classic/classiceditor~ClassicEditor#setData loaded} to the editor on startup
  141. * and the {@link module:core/editor/editorui~EditorUI#getEditableElement editor element} will replace the passed element in the DOM
  142. * (the original one will be hidden and the editor will be injected next to it).
  143. *
  144. * Moreover, the data will be set back to the source element once the editor is destroyed and
  145. * (if the element is a `<textarea>`) when a form in which this element is contained is submitted (which ensures
  146. * automatic integration with native web forms).
  147. *
  148. * 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
  149. * it via the {@link module:editor-classic/classiceditorui~ClassicEditorUI#getEditableElement `editor.ui.getEditableElement()`} method).
  150. *
  151. * See the examples above to learn more.
  152. *
  153. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  154. * @returns {Promise} A promise resolved once the editor is ready.
  155. * The promise returns the created {@link module:editor-classic/classiceditor~ClassicEditor} 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. const initialData = isElement( sourceElementOrData ) ?
  165. getDataFromElement( sourceElementOrData ) :
  166. sourceElementOrData;
  167. return editor.data.init( initialData );
  168. } )
  169. .then( () => editor.fire( 'ready' ) )
  170. .then( () => editor )
  171. );
  172. } );
  173. }
  174. }
  175. mix( ClassicEditor, DataApiMixin );
  176. mix( ClassicEditor, ElementApiMixin );