inlineeditor.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module editor-inline/inlineeditor
  7. */
  8. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  9. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  10. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  11. import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform';
  12. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  13. import InlineEditorUI from './inlineeditorui';
  14. import InlineEditorUIView from './inlineeditoruiview';
  15. import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement';
  16. import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
  17. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  18. import { isElement } from 'lodash-es';
  19. /**
  20. * The {@glink builds/guides/overview#inline-editor inline editor} implementation.
  21. * It uses an inline editable and a floating toolbar.
  22. * See the {@glink examples/builds/inline-editor demo}.
  23. *
  24. * In order to create a inline editor instance, use the static
  25. * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`} method.
  26. *
  27. * # Inline editor and inline build
  28. *
  29. * The inline editor can be used directly from source (if you installed the
  30. * [`@ckeditor/ckeditor5-editor-inline`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-inline) package)
  31. * but it is also available in the {@glink builds/guides/overview#inline-editor inline build}.
  32. *
  33. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  34. * source you need to take care of loading all plugins by yourself
  35. * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
  36. * Using the editor from source gives much better flexibility and allows easier customization.
  37. *
  38. * Read more about initializing the editor from source or as a build in
  39. * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`}.
  40. *
  41. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  42. * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin
  43. * @implements module:core/editor/editorwithui~EditorWithUI
  44. * @extends module:core/editor/editor~Editor
  45. */
  46. export default class InlineEditor extends Editor {
  47. /**
  48. * Creates an instance of the inline editor.
  49. *
  50. * **Note:** Do not use the constructor to create editor instances. Use the static
  51. * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`} method instead.
  52. *
  53. * @protected
  54. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  55. * (on which the editor will be initialized) or initial data for the editor. For more information see
  56. * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`}.
  57. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  58. */
  59. constructor( sourceElementOrData, config ) {
  60. super( config );
  61. this.data.processor = new HtmlDataProcessor();
  62. this.model.document.createRoot();
  63. if ( isElement( sourceElementOrData ) ) {
  64. this.sourceElement = sourceElementOrData;
  65. }
  66. this.ui = new InlineEditorUI( this, new InlineEditorUIView( this.locale, this.sourceElement ) );
  67. attachToForm( this );
  68. }
  69. /**
  70. * @inheritDoc
  71. */
  72. get element() {
  73. return this.ui.view.editable.element;
  74. }
  75. /**
  76. * Destroys the editor instance, releasing all resources used by it.
  77. *
  78. * Updates the original editor element with the data.
  79. *
  80. * @returns {Promise}
  81. */
  82. destroy() {
  83. // Cache the data, then destroy.
  84. // It's safe to assume that the model->view conversion will not work after super.destroy().
  85. const data = this.getData();
  86. this.ui.destroy();
  87. return super.destroy()
  88. .then( () => {
  89. if ( this.sourceElement ) {
  90. setDataInElement( this.sourceElement, data );
  91. }
  92. } );
  93. }
  94. /**
  95. * Creates an inline editor instance.
  96. *
  97. * Creating an instance when using a {@glink builds/index CKEditor build}:
  98. *
  99. * InlineEditor
  100. * .create( document.querySelector( '#editor' ) )
  101. * .then( editor => {
  102. * console.log( 'Editor was initialized', editor );
  103. * } )
  104. * .catch( err => {
  105. * console.error( err.stack );
  106. * } );
  107. *
  108. * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  109. *
  110. * import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
  111. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  112. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  113. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  114. * import ...
  115. *
  116. * InlineEditor
  117. * .create( document.querySelector( '#editor' ), {
  118. * plugins: [ Essentials, Bold, Italic, ... ],
  119. * toolbar: [ 'bold', 'italic', ... ]
  120. * } )
  121. * .then( editor => {
  122. * console.log( 'Editor was initialized', editor );
  123. * } )
  124. * .catch( err => {
  125. * console.error( err.stack );
  126. * } );
  127. *
  128. * Creating an instance when using the initial data instead of a DOM element:
  129. *
  130. * import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
  131. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  132. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  133. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  134. * import ...
  135. *
  136. * InlineEditor
  137. * .create( '<p>Hello world!</p>' )
  138. * .then( editor => {
  139. * console.log( 'Editor was initialized', editor );
  140. *
  141. * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
  142. * document.body.appendChild( editor.element );
  143. * } )
  144. * .catch( err => {
  145. * console.error( err.stack );
  146. * } );
  147. *
  148. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  149. * (on which the editor will be initialized) or the initial data for the editor.
  150. *
  151. * If a source element is passed, then its contents will be automatically
  152. * {@link module:editor-inline/inlineeditor~InlineEditor#setData loaded} to the editor on startup and the element
  153. * itself will be used as the editor's editable element.
  154. *
  155. * If data is provided, then `editor.element` will be created automatically and needs to be added
  156. * to the DOM manually.
  157. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  158. * @returns {Promise} A promise resolved once the editor is ready.
  159. * The promise returns the created {@link module:editor-inline/inlineeditor~InlineEditor} instance.
  160. */
  161. static create( sourceElementOrData, config ) {
  162. return new Promise( resolve => {
  163. const editor = new this( sourceElementOrData, config );
  164. resolve(
  165. editor.initPlugins()
  166. .then( () => {
  167. editor.ui.init();
  168. editor.fire( 'uiReady' );
  169. } )
  170. .then( () => {
  171. const initialData = isElement( sourceElementOrData ) ?
  172. getDataFromElement( sourceElementOrData ) :
  173. sourceElementOrData;
  174. return editor.data.init( initialData );
  175. } )
  176. .then( () => {
  177. editor.fire( 'dataReady' );
  178. editor.fire( 'ready' );
  179. } )
  180. .then( () => editor )
  181. );
  182. } );
  183. }
  184. }
  185. mix( InlineEditor, DataApiMixin );
  186. mix( InlineEditor, ElementApiMixin );