inlineeditor.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. this.ui = null;
  88. return super.destroy()
  89. .then( () => {
  90. if ( this.sourceElement ) {
  91. setDataInElement( this.sourceElement, data );
  92. this.sourceElement = null;
  93. }
  94. } );
  95. }
  96. /**
  97. * Creates an inline editor instance.
  98. *
  99. * Creating an instance when using a {@glink builds/index CKEditor build}:
  100. *
  101. * InlineEditor
  102. * .create( document.querySelector( '#editor' ) )
  103. * .then( editor => {
  104. * console.log( 'Editor was initialized', editor );
  105. * } )
  106. * .catch( err => {
  107. * console.error( err.stack );
  108. * } );
  109. *
  110. * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  111. *
  112. * import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
  113. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  114. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  115. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  116. * import ...
  117. *
  118. * InlineEditor
  119. * .create( document.querySelector( '#editor' ), {
  120. * plugins: [ Essentials, Bold, Italic, ... ],
  121. * toolbar: [ 'bold', 'italic', ... ]
  122. * } )
  123. * .then( editor => {
  124. * console.log( 'Editor was initialized', editor );
  125. * } )
  126. * .catch( err => {
  127. * console.error( err.stack );
  128. * } );
  129. *
  130. * Creating an instance when using the initial data instead of a DOM element:
  131. *
  132. * import InlineEditor from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
  133. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  134. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  135. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  136. * import ...
  137. *
  138. * InlineEditor
  139. * .create( '<p>Hello world!</p>' )
  140. * .then( editor => {
  141. * console.log( 'Editor was initialized', editor );
  142. *
  143. * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
  144. * document.body.appendChild( editor.element );
  145. * } )
  146. * .catch( err => {
  147. * console.error( err.stack );
  148. * } );
  149. *
  150. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  151. * (on which the editor will be initialized) or the initial data for the editor.
  152. *
  153. * If a source element is passed, then its contents will be automatically
  154. * {@link module:editor-inline/inlineeditor~InlineEditor#setData loaded} to the editor on startup and the element
  155. * itself will be used as the editor's editable element.
  156. *
  157. * If data is provided, then `editor.element` will be created automatically and needs to be added
  158. * to the DOM manually.
  159. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  160. * @returns {Promise} A promise resolved once the editor is ready.
  161. * The promise returns the created {@link module:editor-inline/inlineeditor~InlineEditor} instance.
  162. */
  163. static create( sourceElementOrData, config ) {
  164. return new Promise( resolve => {
  165. const editor = new this( sourceElementOrData, config );
  166. resolve(
  167. editor.initPlugins()
  168. .then( () => {
  169. editor.ui.init();
  170. editor.fire( 'uiReady' );
  171. } )
  172. .then( () => {
  173. const initialData = isElement( sourceElementOrData ) ?
  174. getDataFromElement( sourceElementOrData ) :
  175. sourceElementOrData;
  176. return editor.data.init( initialData );
  177. } )
  178. .then( () => {
  179. editor.fire( 'dataReady' );
  180. editor.fire( 'ready' );
  181. } )
  182. .then( () => editor )
  183. );
  184. } );
  185. }
  186. }
  187. mix( InlineEditor, DataApiMixin );
  188. mix( InlineEditor, ElementApiMixin );