decouplededitor.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  18. /**
  19. * The {@glink builds/guides/overview#document-editor decoupled editor} implementation.
  20. * It provides an inline editable and a toolbar. However, unlike other editors,
  21. * it does not render these components anywhere in the DOM unless configured.
  22. *
  23. * This type of an editor is dedicated to integrations which require a customized UI with an open
  24. * structure, allowing developers to specify the exact location of the interface.
  25. *
  26. * See the document editor {@glink examples/builds/document-editor demo} to learn about possible use cases
  27. * for the decoupled editor.
  28. *
  29. * In order to create a decoupled editor instance, use the static
  30. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`} method.
  31. *
  32. * # Decoupled editor and document editor build
  33. *
  34. * The decoupled editor can be used directly from source (if you installed the
  35. * [`@ckeditor/ckeditor5-editor-decoupled`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-decoupled) package)
  36. * but it is also available in the {@glink builds/guides/overview#document-editor document editor build}.
  37. *
  38. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  39. * source you need to take care of loading all plugins by yourself
  40. * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
  41. * Using the editor from source gives much better flexibility and allows for easier customization.
  42. *
  43. * Read more about initializing the editor from source or as a build in
  44. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}.
  45. *
  46. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  47. * @implements module:core/editor/editorwithui~EditorWithUI
  48. * @extends module:core/editor/editor~Editor
  49. */
  50. export default class DecoupledEditor extends Editor {
  51. /**
  52. * Creates an instance of the decoupled editor.
  53. *
  54. * **Note:** Do not use the constructor to create editor instances. Use the static
  55. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`} method instead.
  56. *
  57. * @protected
  58. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  59. * (on which the editor will be initialized) or initial data for the editor. For more information see
  60. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  61. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  62. */
  63. constructor( sourceElementOrData, config ) {
  64. super( config );
  65. if ( isElement( sourceElementOrData ) ) {
  66. this.sourceElement = sourceElementOrData;
  67. }
  68. this.data.processor = new HtmlDataProcessor();
  69. this.model.document.createRoot();
  70. const view = new DecoupledEditorUIView( this.locale, this.editing.view, this.sourceElement );
  71. this.ui = new DecoupledEditorUI( this, view );
  72. }
  73. /**
  74. * Destroys the editor instance, releasing all resources used by it.
  75. *
  76. * **Note**: The decoupled editor does not remove the toolbar and editable when destroyed. You can
  77. * do that yourself in the destruction chain:
  78. *
  79. * editor.destroy()
  80. * .then( () => {
  81. * // Remove the toolbar from DOM.
  82. * editor.ui.view.toolbar.element.remove();
  83. *
  84. * // Remove the editable from DOM.
  85. * editor.ui.view.editable.element.remove();
  86. *
  87. * console.log( 'Editor was destroyed' );
  88. * } );
  89. *
  90. * @returns {Promise}
  91. */
  92. destroy() {
  93. // Cache the data, then destroy.
  94. // It's safe to assume that the model->view conversion will not work after super.destroy().
  95. const data = this.getData();
  96. this.ui.destroy();
  97. return super.destroy()
  98. .then( () => {
  99. if ( this.sourceElement ) {
  100. setDataInElement( this.sourceElement, data );
  101. }
  102. } );
  103. }
  104. /**
  105. * Creates a `DecoupledEditor` instance.
  106. *
  107. * Remember that `DecoupledEditor` do not append the toolbar element to your web page so you have to do it manually after the editor
  108. * has been initialized.
  109. *
  110. * There are two general ways how the editor can be initialized.
  111. *
  112. * You can initialize the editor using an existing DOM element:
  113. *
  114. * DecoupledEditor
  115. * .create( document.querySelector( '#editor' ) )
  116. * .then( editor => {
  117. * console.log( 'Editor was initialized', editor );
  118. *
  119. * // Append the toolbar to the <body> element.
  120. * document.body.appendChild( editor.ui.view.toolbar.element );
  121. * } )
  122. * .catch( err => {
  123. * console.error( err.stack );
  124. * } );
  125. *
  126. * The element's content will be used as the editor data. The editable element will replace the source element on your web page.
  127. *
  128. * Alternatively, you can initialize the editor by passing the initial data directly as a `String`.
  129. * In this case, you will have to manually append to your web page both the toolbar element and the editable element.
  130. *
  131. * DecoupledEditor
  132. * .create( '<p>Hello world!</p>' )
  133. * .then( editor => {
  134. * console.log( 'Editor was initialized', editor );
  135. *
  136. * // Append the toolbar to the <body> element.
  137. * document.body.appendChild( editor.ui.view.toolbar.element );
  138. *
  139. * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
  140. * document.body.appendChild( editor.ui.element );
  141. * } )
  142. * .catch( err => {
  143. * console.error( err.stack );
  144. * } );
  145. *
  146. * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
  147. * web page content is generated on the client-side and the DOM structure is not ready at the moment when you initialize the editor.
  148. *
  149. * You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config:
  150. *
  151. * DecoupledEditor
  152. * .create( document.querySelector( '#editor' ), {
  153. * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
  154. * } )
  155. * .then( editor => {
  156. * console.log( 'Editor was initialized', editor );
  157. *
  158. * // Append the toolbar to the <body> element.
  159. * document.body.appendChild( editor.ui.view.toolbar.element );
  160. * } )
  161. * .catch( err => {
  162. * console.error( err.stack );
  163. * } );
  164. *
  165. * This method can be used to initialize the editor on an existing element with specified content in case if your integration
  166. * makes it difficult to set the content of the source element.
  167. *
  168. * Note that an error will be thrown if you pass initial data both as the first parameter and also in the config.
  169. *
  170. * See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
  171. * customizing plugins, toolbar and other.
  172. *
  173. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  174. * or the editor's initial data.
  175. *
  176. * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization.
  177. * Moreover, the editor data will be set back to the original element once the editor is destroyed.
  178. *
  179. * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
  180. * It is available under {@link module:editor-decoupled/decouplededitorui~DecoupledEditorUI#element `editor.ui.element`} property.
  181. *
  182. * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
  183. * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
  184. */
  185. static create( sourceElementOrData, config = {} ) {
  186. return new Promise( resolve => {
  187. const editor = new this( sourceElementOrData, config );
  188. resolve(
  189. editor.initPlugins()
  190. .then( () => {
  191. editor.ui.init();
  192. } )
  193. .then( () => {
  194. if ( !isElement( sourceElementOrData ) && config.initialData ) {
  195. throw new CKEditorError(
  196. 'editor-create-initial-data: ' +
  197. 'EditorConfig#initialData cannot be used together with initial data passed in Editor#create()'
  198. );
  199. }
  200. const initialData = config.initialData || getInitialData( sourceElementOrData );
  201. return editor.data.init( initialData );
  202. } )
  203. .then( () => editor.fire( 'ready' ) )
  204. .then( () => editor )
  205. );
  206. } );
  207. }
  208. }
  209. mix( DecoupledEditor, DataApiMixin );
  210. function getInitialData( sourceElementOrData ) {
  211. return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
  212. }