decouplededitor.js 10 KB

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