decouplededitor.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module editor-decoupled/decouplededitor
  7. */
  8. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  9. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  10. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  11. import DecoupledEditorUI from './decouplededitorui';
  12. import DecoupledEditorUIView from './decouplededitoruiview';
  13. import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
  14. import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement';
  15. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  16. import { isElement } from 'lodash-es';
  17. /**
  18. * The {@glink builds/guides/overview#document-editor decoupled editor} implementation.
  19. * It provides an inline editable and a toolbar. However, unlike other editors,
  20. * it does not render these components anywhere in the DOM unless configured.
  21. *
  22. * This type of an editor is dedicated to integrations which require a customized UI with an open
  23. * structure, allowing developers to specify the exact location of the interface.
  24. *
  25. * See the document editor {@glink examples/builds/document-editor demo} to learn about possible use cases
  26. * for the decoupled editor.
  27. *
  28. * In order to create a decoupled editor instance, use the static
  29. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`} method.
  30. *
  31. * # Decoupled editor and document editor build
  32. *
  33. * The decoupled editor can be used directly from source (if you installed the
  34. * [`@ckeditor/ckeditor5-editor-decoupled`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-decoupled) package)
  35. * but it is also available in the {@glink builds/guides/overview#document-editor document editor build}.
  36. *
  37. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  38. * source you need to take care of loading all plugins by yourself
  39. * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
  40. * Using the editor from source gives much better flexibility and allows for easier customization.
  41. *
  42. * Read more about initializing the editor from source or as a build in
  43. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}.
  44. *
  45. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  46. * @implements module:core/editor/editorwithui~EditorWithUI
  47. * @extends module:core/editor/editor~Editor
  48. */
  49. export default class DecoupledEditor extends Editor {
  50. /**
  51. * Creates an instance of the decoupled editor.
  52. *
  53. * **Note:** Do not use the constructor to create editor instances. Use the static
  54. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`} method instead.
  55. *
  56. * @protected
  57. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  58. * (on which the editor will be initialized) or initial data for the editor. For more information see
  59. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  60. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  61. */
  62. constructor( sourceElementOrData, config ) {
  63. super( config );
  64. if ( isElement( sourceElementOrData ) ) {
  65. this.sourceElement = sourceElementOrData;
  66. }
  67. this.data.processor = new HtmlDataProcessor();
  68. this.model.document.createRoot();
  69. const view = new DecoupledEditorUIView( this.locale, this.sourceElement, this.editing.view );
  70. this.ui = new DecoupledEditorUI( this, view );
  71. }
  72. /**
  73. * Destroys the editor instance, releasing all resources used by it.
  74. *
  75. * **Note**: The decoupled editor does not remove the toolbar and editable when destroyed. You can
  76. * do that yourself in the destruction chain:
  77. *
  78. * editor.destroy()
  79. * .then( () => {
  80. * // Remove the toolbar from DOM.
  81. * editor.ui.view.toolbar.element.remove();
  82. *
  83. * // Remove the editable from DOM.
  84. * editor.ui.view.editable.element.remove();
  85. *
  86. * console.log( 'Editor was destroyed' );
  87. * } );
  88. *
  89. * @returns {Promise}
  90. */
  91. destroy() {
  92. // Cache the data, then destroy.
  93. // It's safe to assume that the model->view conversion will not work after super.destroy().
  94. const data = this.getData();
  95. this.ui.destroy();
  96. return super.destroy()
  97. .then( () => {
  98. if ( this.sourceElement ) {
  99. setDataInElement( this.sourceElement, data );
  100. }
  101. } );
  102. }
  103. /**
  104. * Creates a decoupled editor instance.
  105. *
  106. * Creating an instance when using a {@glink builds/index CKEditor 5 build}:
  107. *
  108. * DecoupledEditor
  109. * .create( document.querySelector( '#editor' ) )
  110. * .then( editor => {
  111. * // Append the toolbar to the <body> element.
  112. * document.body.appendChild( editor.ui.view.toolbar.element );
  113. *
  114. * console.log( 'Editor was initialized', editor );
  115. * } )
  116. * .catch( err => {
  117. * console.error( err.stack );
  118. * } );
  119. *
  120. * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  121. *
  122. * import DecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor';
  123. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  124. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  125. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  126. * import ...
  127. *
  128. * DecoupledEditor
  129. * .create( document.querySelector( '#editor' ), {
  130. * plugins: [ Essentials, Bold, Italic, ... ],
  131. * toolbar: [ 'bold', 'italic', ... ]
  132. * } )
  133. * .then( editor => {
  134. * // Append the toolbar to the <body> element.
  135. * document.body.appendChild( editor.ui.view.toolbar.element );
  136. *
  137. * console.log( 'Editor was initialized', editor );
  138. * } )
  139. * .catch( err => {
  140. * console.error( err.stack );
  141. * } );
  142. *
  143. * **Note**: It is possible to create the editor out of a pure data string. The editor will then render
  144. * an editable element that must be inserted into the DOM for the editor to work properly:
  145. *
  146. * DecoupledEditor
  147. * .create( '<p>Editor data</p>' )
  148. * .then( editor => {
  149. * // Append the toolbar to the <body> element.
  150. * document.body.appendChild( editor.ui.view.toolbar.element );
  151. *
  152. * // Append the editable to the <body> element.
  153. * document.body.appendChild( editor.ui.view.editable.element );
  154. *
  155. * console.log( 'Editor was initialized', editor );
  156. * } )
  157. * .catch( err => {
  158. * console.error( err.stack );
  159. * } );
  160. *
  161. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  162. * (on which the editor will be initialized) or initial data for the editor.
  163. *
  164. * If a source element is passed, then its contents will be automatically
  165. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor#setData loaded} to the editor on startup and the element
  166. * itself will be used as the editor's editable element.
  167. *
  168. * If data is provided, then `editor.ui.view.editable.element` will be created automatically and needs to be added
  169. * to the DOM manually.
  170. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  171. * @returns {Promise} A promise resolved once the editor is ready.
  172. * The promise returns the created {@link module:editor-decoupled/decouplededitor~DecoupledEditor} instance.
  173. */
  174. static create( sourceElementOrData, config ) {
  175. return new Promise( resolve => {
  176. const editor = new this( sourceElementOrData, config );
  177. resolve(
  178. editor.initPlugins()
  179. .then( () => {
  180. editor.ui.init();
  181. } )
  182. .then( () => {
  183. const initialData = isElement( sourceElementOrData ) ?
  184. getDataFromElement( sourceElementOrData ) :
  185. sourceElementOrData;
  186. return editor.data.init( initialData );
  187. } )
  188. .then( () => {
  189. editor.fire( 'dataReady' );
  190. editor.fire( 'ready' );
  191. } )
  192. .then( () => editor )
  193. );
  194. } );
  195. }
  196. }
  197. mix( DecoupledEditor, DataApiMixin );