decouplededitor.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. this.ui = new DecoupledEditorUI( this, new DecoupledEditorUIView( this.locale, this.sourceElement ) );
  70. }
  71. /**
  72. * Destroys the editor instance, releasing all resources used by it.
  73. *
  74. * **Note**: The decoupled editor does not remove the toolbar and editable when destroyed. You can
  75. * do that yourself in the destruction chain:
  76. *
  77. * editor.destroy()
  78. * .then( () => {
  79. * // Remove the toolbar from DOM.
  80. * editor.ui.view.toolbar.element.remove();
  81. *
  82. * // Remove the editable from DOM.
  83. * editor.ui.view.editable.element.remove();
  84. *
  85. * console.log( 'Editor was destroyed' );
  86. * } );
  87. *
  88. * @returns {Promise}
  89. */
  90. destroy() {
  91. // Cache the data, then destroy.
  92. // It's safe to assume that the model->view conversion will not work after super.destroy().
  93. const data = this.getData();
  94. this.ui.destroy();
  95. return super.destroy()
  96. .then( () => {
  97. if ( this.sourceElement ) {
  98. setDataInElement( this.sourceElement, data );
  99. }
  100. } );
  101. }
  102. /**
  103. * Creates a decoupled editor instance.
  104. *
  105. * Creating an instance when using a {@glink builds/index CKEditor 5 build}:
  106. *
  107. * DecoupledEditor
  108. * .create( document.querySelector( '#editor' ) )
  109. * .then( editor => {
  110. * // Append the toolbar to the <body> element.
  111. * document.body.appendChild( editor.ui.view.toolbar.element );
  112. *
  113. * console.log( 'Editor was initialized', editor );
  114. * } )
  115. * .catch( err => {
  116. * console.error( err.stack );
  117. * } );
  118. *
  119. * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  120. *
  121. * import DecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor';
  122. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  123. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  124. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  125. * import ...
  126. *
  127. * DecoupledEditor
  128. * .create( document.querySelector( '#editor' ), {
  129. * plugins: [ Essentials, Bold, Italic, ... ],
  130. * toolbar: [ 'bold', 'italic', ... ]
  131. * } )
  132. * .then( editor => {
  133. * // Append the toolbar to the <body> element.
  134. * document.body.appendChild( editor.ui.view.toolbar.element );
  135. *
  136. * console.log( 'Editor was initialized', editor );
  137. * } )
  138. * .catch( err => {
  139. * console.error( err.stack );
  140. * } );
  141. *
  142. * **Note**: It is possible to create the editor out of a pure data string. The editor will then render
  143. * an editable element that must be inserted into the DOM for the editor to work properly:
  144. *
  145. * DecoupledEditor
  146. * .create( '<p>Editor data</p>' )
  147. * .then( editor => {
  148. * // Append the toolbar to the <body> element.
  149. * document.body.appendChild( editor.ui.view.toolbar.element );
  150. *
  151. * // Append the editable to the <body> element.
  152. * document.body.appendChild( editor.ui.view.editable.element );
  153. *
  154. * console.log( 'Editor was initialized', editor );
  155. * } )
  156. * .catch( err => {
  157. * console.error( err.stack );
  158. * } );
  159. *
  160. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  161. * (on which the editor will be initialized) or initial data for the editor.
  162. *
  163. * If a source element is passed, then its contents will be automatically
  164. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor#setData loaded} to the editor on startup and the element
  165. * itself will be used as the editor's editable element.
  166. *
  167. * If data is provided, then `editor.ui.view.editable.element` will be created automatically and needs to be added
  168. * to the DOM manually.
  169. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  170. * @returns {Promise} A promise resolved once the editor is ready.
  171. * The promise returns the created {@link module:editor-decoupled/decouplededitor~DecoupledEditor} instance.
  172. */
  173. static create( sourceElementOrData, config ) {
  174. return new Promise( resolve => {
  175. const editor = new this( sourceElementOrData, config );
  176. resolve(
  177. editor.initPlugins()
  178. .then( () => {
  179. editor.ui.init();
  180. } )
  181. .then( () => {
  182. const initialData = isElement( sourceElementOrData ) ?
  183. getDataFromElement( sourceElementOrData ) :
  184. sourceElementOrData;
  185. return editor.data.init( initialData );
  186. } )
  187. .then( () => {
  188. editor.fire( 'dataReady' );
  189. editor.fire( 'ready' );
  190. } )
  191. .then( () => editor )
  192. );
  193. } );
  194. }
  195. }
  196. mix( DecoupledEditor, DataApiMixin );