8
0

decouplededitor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /**
  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 new decoupled editor instance.
  106. *
  107. * Remember that `DecoupledEditor` does 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 ways how the editor can be initialized.
  111. *
  112. * # Using an existing DOM element (and loading data from it)
  113. *
  114. * You can initialize the editor using an existing DOM element:
  115. *
  116. * DecoupledEditor
  117. * .create( document.querySelector( '#editor' ) )
  118. * .then( editor => {
  119. * console.log( 'Editor was initialized', editor );
  120. *
  121. * // Append the toolbar to the <body> element.
  122. * document.body.appendChild( editor.ui.view.toolbar.element );
  123. * } )
  124. * .catch( err => {
  125. * console.error( err.stack );
  126. * } );
  127. *
  128. * The element's content will be used as the editor data and the element will become the editable element.
  129. *
  130. * # Creating a detached editor
  131. *
  132. * Alternatively, you can initialize the editor by passing the initial data directly as a string.
  133. * In this case, you will have to manually append both the toolbar element and the editable element to your web page.
  134. *
  135. * DecoupledEditor
  136. * .create( '<p>Hello world!</p>' )
  137. * .then( editor => {
  138. * console.log( 'Editor was initialized', editor );
  139. *
  140. * // Append the toolbar to the <body> element.
  141. * document.body.appendChild( editor.ui.view.toolbar.element );
  142. *
  143. * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
  144. * document.body.appendChild( editor.ui.view.editable.element );
  145. * } )
  146. * .catch( err => {
  147. * console.error( err.stack );
  148. * } );
  149. *
  150. * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
  151. * web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
  152. *
  153. * # Using an existing DOM element (and data provided in `config.initialData`)
  154. *
  155. * You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
  156. *
  157. * DecoupledEditor
  158. * .create( document.querySelector( '#editor' ), {
  159. * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
  160. * } )
  161. * .then( editor => {
  162. * console.log( 'Editor was initialized', editor );
  163. *
  164. * // Append the toolbar to the <body> element.
  165. * document.body.appendChild( editor.ui.view.toolbar.element );
  166. * } )
  167. * .catch( err => {
  168. * console.error( err.stack );
  169. * } );
  170. *
  171. * This method can be used to initialize the editor on an existing element with the specified content in case if your integration
  172. * makes it difficult to set the content of the source element.
  173. *
  174. * Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
  175. *
  176. * # Configuring the editor
  177. *
  178. * See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
  179. * customizing plugins, toolbar and more.
  180. *
  181. * # Using the editor from source
  182. *
  183. * The code samples listed in the previous sections of this documentation assume that you are using an
  184. * {@glink builds/guides/overview editor build} (for example – `@ckeditor/ckeditor5-build-decoupled`).
  185. *
  186. * If you want to use the decoupled editor from source (`@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor`),
  187. * you need to define the list of
  188. * {@link module:core/editor/editorconfig~EditorConfig#plugins plugins to be initialized} and
  189. * {@link module:core/editor/editorconfig~EditorConfig#toolbar toolbar items}. Read more about using the editor from
  190. * source in the {@glink builds/guides/integration/advanced-setup "Advanced setup" guide}.
  191. *
  192. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  193. * or the editor's initial data.
  194. *
  195. * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization.
  196. * Moreover, the editor data will be set back to the original element once the editor is destroyed.
  197. *
  198. * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
  199. * It is available under the {@link module:editor-decoupled/decouplededitorui~DecoupledEditorUI#element `editor.ui.element`} property.
  200. *
  201. * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
  202. * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
  203. */
  204. static create( sourceElementOrData, config = {} ) {
  205. return new Promise( resolve => {
  206. const isHTMLElement = isElement( sourceElementOrData );
  207. if ( isHTMLElement && sourceElementOrData.tagName === 'TEXTAREA' ) {
  208. // Documented in core/editor/editor.js
  209. throw new CKEditorError(
  210. 'editor-wrong-element: This type of editor cannot be initialized inside <textarea> element.', null );
  211. }
  212. const editor = new this( sourceElementOrData, config );
  213. resolve(
  214. editor.initPlugins()
  215. .then( () => {
  216. editor.ui.init();
  217. } )
  218. .then( () => {
  219. if ( !isHTMLElement && config.initialData ) {
  220. // Documented in core/editor/editorconfig.jdoc.
  221. throw new CKEditorError(
  222. 'editor-create-initial-data: ' +
  223. 'The config.initialData option cannot be used together with initial data passed in Editor.create().',
  224. null
  225. );
  226. }
  227. const initialData = config.initialData || getInitialData( sourceElementOrData );
  228. return editor.data.init( initialData );
  229. } )
  230. .then( () => editor.fire( 'ready' ) )
  231. .then( () => editor )
  232. );
  233. } );
  234. }
  235. }
  236. mix( DecoupledEditor, DataApiMixin );
  237. function getInitialData( sourceElementOrData ) {
  238. return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
  239. }