8
0

classiceditor.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 log from '@ckeditor/ckeditor5-utils/src/log';
  18. import { isElement } from 'lodash-es';
  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 ) );
  67. attachToForm( this );
  68. }
  69. /**
  70. * @inheritDoc
  71. */
  72. get element() {
  73. log.warn( 'deprecated-editor-element: The editor#element is deprecated.' );
  74. return this.ui.element;
  75. }
  76. /**
  77. * Destroys the editor instance, releasing all resources used by it.
  78. *
  79. * Updates the editor's source element with the data.
  80. *
  81. * @returns {Promise}
  82. */
  83. destroy() {
  84. if ( this.sourceElement ) {
  85. this.updateSourceElement();
  86. }
  87. this.ui.destroy();
  88. return super.destroy();
  89. }
  90. /**
  91. * Creates a classic editor instance.
  92. *
  93. * Creating an instance when using a {@glink builds/index CKEditor build}:
  94. *
  95. * ClassicEditor
  96. * .create( document.querySelector( '#editor' ) )
  97. * .then( editor => {
  98. * console.log( 'Editor was initialized', editor );
  99. * } )
  100. * .catch( err => {
  101. * console.error( err.stack );
  102. * } );
  103. *
  104. * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  105. *
  106. * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  107. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  108. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  109. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  110. * import ...
  111. *
  112. * ClassicEditor
  113. * .create( document.querySelector( '#editor' ), {
  114. * plugins: [ Essentials, Bold, Italic, ... ],
  115. * toolbar: [ 'bold', 'italic', ... ]
  116. * } )
  117. * .then( editor => {
  118. * console.log( 'Editor was initialized', editor );
  119. * } )
  120. * .catch( err => {
  121. * console.error( err.stack );
  122. * } );
  123. *
  124. * Creating an instance when using initial data instead of a DOM element:
  125. *
  126. * import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  127. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  128. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  129. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  130. * import ...
  131. *
  132. * ClassicEditor
  133. * .create( '<p>Hello world!</p>' )
  134. * .then( editor => {
  135. * console.log( 'Editor was initialized', editor );
  136. *
  137. * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
  138. * document.body.appendChild( editor.element );
  139. * } )
  140. * .catch( err => {
  141. * console.error( err.stack );
  142. * } );
  143. *
  144. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  145. * or the editor's initial data.
  146. *
  147. * If a source element is passed, then its contents will be automatically
  148. * {@link module:editor-classic/classiceditor~ClassicEditor#setData loaded} to the editor on startup
  149. * and the {@link module:core/editor/editorwithui~EditorWithUI#element editor element} will replace the passed element in the DOM
  150. * (the original one will be hidden and the editor will be injected next to it).
  151. *
  152. * Moreover, the data will be set back to the source element once the editor is destroyed and
  153. * (if the element is a `<textarea>`) when a form in which this element is contained is submitted (which ensures
  154. * automatic integration with native web forms).
  155. *
  156. * If the data is passed, a detached editor will be created. It means that you need to insert it into the DOM manually
  157. * (by accessing the {@link module:editor-classic/classiceditor~ClassicEditor#element `editor.element`} property).
  158. *
  159. * See the examples above to learn more.
  160. *
  161. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  162. * @returns {Promise} A promise resolved once the editor is ready.
  163. * The promise returns the created {@link module:editor-classic/classiceditor~ClassicEditor} instance.
  164. */
  165. static create( sourceElementOrData, config ) {
  166. return new Promise( resolve => {
  167. const editor = new this( sourceElementOrData, config );
  168. resolve(
  169. editor.initPlugins()
  170. .then( () => editor.ui.init( isElement( sourceElementOrData ) ? sourceElementOrData : null ) )
  171. .then( () => editor.editing.view.attachDomRoot( editor.ui.view.editable.editableElement ) )
  172. .then( () => {
  173. const initialData = isElement( sourceElementOrData ) ?
  174. getDataFromElement( sourceElementOrData ) :
  175. sourceElementOrData;
  176. return editor.data.init( initialData );
  177. } )
  178. .then( () => {
  179. editor.fire( 'dataReady' );
  180. editor.fire( 'ready' );
  181. } )
  182. .then( () => editor )
  183. );
  184. } );
  185. }
  186. }
  187. mix( ClassicEditor, DataApiMixin );
  188. mix( ClassicEditor, ElementApiMixin );