decouplededitor.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 mix from '@ckeditor/ckeditor5-utils/src/mix';
  14. /**
  15. * The {@glink builds/guides/overview#decoupled-editor decoupled editor} implementation.
  16. * It provides an inline editable and a toolbar. However, unlike other editors,
  17. * it does not render these components anywhere in DOM unless configured.
  18. *
  19. * This type of an editor is dedicated for integrations which require a customized UI with an open
  20. * structure, allowing developers to specify the exact location of the interface.
  21. *
  22. * See the document editor {@glink examples/builds/document-editor demo} to learn about possible use cases
  23. * for the decoupled editor.
  24. *
  25. * In order to create a decoupled editor instance, use the static
  26. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`} method.
  27. *
  28. * # Decoupled editor and document build
  29. *
  30. * The decoupled editor can be used directly from source (if you installed the
  31. * [`@ckeditor/ckeditor5-editor-decoupled`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-decoupled) package)
  32. * but it is also available in the {@glink builds/guides/overview#document-editor document build}.
  33. *
  34. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  35. * source you need to take care of loading all plugins by yourself
  36. * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
  37. * Using the editor from source gives much better flexibility and allows easier customization.
  38. *
  39. * Read more about initializing the editor from source or as a build in
  40. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}.
  41. *
  42. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  43. * @implements module:core/editor/editorwithui~EditorWithUI
  44. * @extends module:core/editor/editor~Editor
  45. */
  46. export default class DecoupledEditor extends Editor {
  47. /**
  48. * Creates an instance of the decoupled editor.
  49. *
  50. * **Note:** do not use the constructor to create editor instances. Use the static
  51. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`} method instead.
  52. *
  53. * @protected
  54. * @param {String} data The data to be loaded into the editor.
  55. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  56. */
  57. constructor( config ) {
  58. super( config );
  59. this.data.processor = new HtmlDataProcessor();
  60. this.model.document.createRoot();
  61. this.ui = new DecoupledEditorUI( this, new DecoupledEditorUIView( this.locale ) );
  62. }
  63. /**
  64. * Destroys the editor instance, releasing all resources used by it.
  65. *
  66. * @returns {Promise}
  67. */
  68. destroy() {
  69. this.ui.destroy();
  70. return super.destroy();
  71. }
  72. /**
  73. * Creates a decoupled editor instance.
  74. *
  75. * Creating instance when using the {@glink builds/index CKEditor build}:
  76. *
  77. * DecoupledEditor
  78. * .create( '<p>Editor data</p>', {
  79. * // The location of the toolbar in DOM.
  80. * toolbarContainer: document.querySelector( 'body div.toolbar-container' ),
  81. *
  82. * // The location of the editable in DOM.
  83. * editableContainer: document.querySelector( 'body div.editable-container' )
  84. * } )
  85. * .then( editor => {
  86. * console.log( 'Editor was initialized', editor );
  87. * } )
  88. * .catch( err => {
  89. * console.error( err.stack );
  90. * } );
  91. *
  92. * Creating instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  93. *
  94. * import DecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor';
  95. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  96. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  97. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  98. * import ...
  99. *
  100. * DecoupledEditor
  101. * .create( '<p>Editor data</p>', {
  102. * plugins: [ Essentials, Bold, Italic, ... ],
  103. * toolbar: [ 'bold', 'italic', ... ],
  104. *
  105. * // The location of the toolbar in DOM.
  106. * toolbarContainer: document.querySelector( 'div.toolbar-container' ),
  107. *
  108. * // The location of the editable in DOM.
  109. * editableContainer: document.querySelector( 'div.editable-container' )
  110. * } )
  111. * .then( editor => {
  112. * console.log( 'Editor was initialized', editor );
  113. * } )
  114. * .catch( err => {
  115. * console.error( err.stack );
  116. * } );
  117. *
  118. * **Note**: {@link module:core/editor/editorconfig~EditorConfig#toolbarContainer `config.toolbarContainer`} and
  119. * {@link module:core/editor/editorconfig~EditorConfig#editableContainer `config.editableContainer`} are optional. It is
  120. * possible to define the location of the UI elements manually once the editor is up and running:
  121. *
  122. * DecoupledEditor
  123. * .create( '<p>Editor data</p>' )
  124. * .then( editor => {
  125. * console.log( 'Editor was initialized', editor );
  126. *
  127. * // Append the toolbar and editable straight into the <body> element.
  128. * document.body.appendChild( editor.ui.view.toolbar.element );
  129. * document.body.appendChild( editor.ui.view.editable.element );
  130. * } )
  131. * .catch( err => {
  132. * console.error( err.stack );
  133. * } );
  134. *
  135. * @param {String} data The data to be loaded into the editor.
  136. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  137. * @returns {Promise} A promise resolved once the editor is ready.
  138. * The promise returns the created {@link module:editor-decoupled/decouplededitor~DecoupledEditor} instance.
  139. */
  140. static create( data, config ) {
  141. return new Promise( resolve => {
  142. const editor = new this( config );
  143. resolve(
  144. editor.initPlugins()
  145. .then( () => {
  146. editor.ui.init();
  147. editor.fire( 'uiReady' );
  148. } )
  149. .then( () => editor.editing.view.attachDomRoot( editor.ui.view.editableElement ) )
  150. .then( () => editor.data.init( data ) )
  151. .then( () => {
  152. editor.fire( 'dataReady' );
  153. editor.fire( 'ready' );
  154. } )
  155. .then( () => editor )
  156. );
  157. } );
  158. }
  159. }
  160. mix( DecoupledEditor, DataApiMixin );
  161. /**
  162. * A configuration of the {@link module:editor-decoupled/decouplededitor~DecoupledEditor}.
  163. *
  164. * When specified, it controls the location of the {@link module:editor-decoupled/decouplededitoruiview~DecoupledEditorUIView#toolbar}:
  165. *
  166. * DecoupledEditor
  167. * .create( '<p>Hello world!</p>', {
  168. * // Append the toolbar to the <body> element.
  169. * toolbarContainer: document.body
  170. * } )
  171. * .then( editor => {
  172. * console.log( editor );
  173. * } )
  174. * .catch( error => {
  175. * console.error( error );
  176. * } );
  177. *
  178. * **Note**: If not specified, the toolbar must be manually injected into DOM. See
  179. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}
  180. * to learn more.
  181. *
  182. * @member {HTMLElement} module:core/editor/editorconfig~EditorConfig#toolbarContainer
  183. */
  184. /**
  185. * A configuration of the {@link module:editor-decoupled/decouplededitor~DecoupledEditor}.
  186. *
  187. * When specified, it controls the location of the {@link module:editor-decoupled/decouplededitoruiview~DecoupledEditorUIView#editable}:
  188. *
  189. * DecoupledEditor
  190. * .create( '<p>Hello world!</p>', {
  191. * // Append the editable to the <body> element.
  192. * editableContainer: document.body
  193. * } )
  194. * .then( editor => {
  195. * console.log( editor );
  196. * } )
  197. * .catch( error => {
  198. * console.error( error );
  199. * } );
  200. *
  201. * **Note**: If not specified, the editable must be manually injected into DOM. See
  202. * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}
  203. * to learn more.
  204. *
  205. * @member {HTMLElement} module:core/editor/editorconfig~EditorConfig#editableContainer
  206. */