8
0

classiceditor.js 8.2 KB

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