decouplededitor.js 8.3 KB

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