ballooneditor.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module editor-balloon/ballooneditor
  7. */
  8. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  9. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  10. import BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
  11. import BalloonEditorUI from './ballooneditorui';
  12. import BalloonEditorUIView from './ballooneditoruiview';
  13. import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement';
  14. import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
  15. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  16. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  17. import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform';
  18. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  19. import { isElement } from 'lodash-es';
  20. /**
  21. * The {@glink builds/guides/overview#balloon-editor balloon editor} implementation (Medium-like editor).
  22. * It uses an inline editable and a toolbar based on the {@link module:ui/toolbar/balloon/balloontoolbar~BalloonToolbar}.
  23. * See the {@glink examples/builds/balloon-editor demo}.
  24. *
  25. * In order to create a balloon editor instance, use the static
  26. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`} method.
  27. *
  28. * # Balloon editor and balloon build
  29. *
  30. * The balloon editor can be used directly from source (if you installed the
  31. * [`@ckeditor/ckeditor5-editor-balloon`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-balloon) package)
  32. * but it is also available in the {@glink builds/guides/overview#balloon-editor balloon 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-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  41. *
  42. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  43. * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin
  44. * @implements module:core/editor/editorwithui~EditorWithUI
  45. * @extends module:core/editor/editor~Editor
  46. */
  47. export default class BalloonEditor extends Editor {
  48. /**
  49. * Creates an instance of the balloon editor.
  50. *
  51. * **Note:** do not use the constructor to create editor instances. Use the static
  52. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`} method instead.
  53. *
  54. * @protected
  55. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  56. * (on which the editor will be initialized) or initial data for the editor. For more information see
  57. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  58. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  59. */
  60. constructor( sourceElementOrData, config ) {
  61. super( config );
  62. if ( isElement( sourceElementOrData ) ) {
  63. this.sourceElement = sourceElementOrData;
  64. }
  65. const plugins = this.config.get( 'plugins' );
  66. plugins.push( BalloonToolbar );
  67. this.config.set( 'plugins', plugins );
  68. this.config.define( 'balloonToolbar', this.config.get( 'toolbar' ) );
  69. this.data.processor = new HtmlDataProcessor();
  70. this.model.document.createRoot();
  71. this.ui = new BalloonEditorUI( this, new BalloonEditorUIView( this.locale, this.sourceElement ) );
  72. attachToForm( this );
  73. }
  74. /**
  75. * Destroys the editor instance, releasing all resources used by it.
  76. *
  77. * Updates the original editor element with the data.
  78. *
  79. * @returns {Promise}
  80. */
  81. destroy() {
  82. // Cache the data, then destroy.
  83. // It's safe to assume that the model->view conversion will not work after super.destroy().
  84. const data = this.getData();
  85. this.ui.destroy();
  86. return super.destroy()
  87. .then( () => {
  88. if ( this.sourceElement ) {
  89. setDataInElement( this.sourceElement, data );
  90. }
  91. } );
  92. }
  93. /**
  94. * Creates a balloon editor instance.
  95. *
  96. * Creating an instance when using a {@glink builds/index CKEditor build}:
  97. *
  98. * BalloonEditor
  99. * .create( document.querySelector( '#editor' ) )
  100. * .then( editor => {
  101. * console.log( 'Editor was initialized', editor );
  102. * } )
  103. * .catch( err => {
  104. * console.error( err.stack );
  105. * } );
  106. *
  107. * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  108. *
  109. * import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  110. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  111. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  112. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  113. * import ...
  114. *
  115. * BalloonEditor
  116. * .create( document.querySelector( '#editor' ), {
  117. * plugins: [ Essentials, Bold, Italic, ... ],
  118. * toolbar: [ 'bold', 'italic', ... ]
  119. * } )
  120. * .then( editor => {
  121. * console.log( 'Editor was initialized', editor );
  122. * } )
  123. * .catch( err => {
  124. * console.error( err.stack );
  125. * } );
  126. *
  127. * Creating an instance when using initial data instead of a DOM element:
  128. *
  129. * import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  130. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  131. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  132. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  133. * import ...
  134. *
  135. * BalloonEditor
  136. * .create( '<p>Hello world!</p>', {
  137. * plugins: [ Essentials, Bold, Italic, ... ],
  138. * toolbar: [ 'bold', 'italic', ... ]
  139. * } )
  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 initial data for the editor.
  152. *
  153. * If a source element is passed, then its contents will be automatically
  154. * {@link module:editor-balloon/ballooneditor~BalloonEditor#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-balloon/ballooneditor~BalloonEditor} 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. } )
  171. .then( () => {
  172. const initialData = isElement( sourceElementOrData ) ?
  173. getDataFromElement( sourceElementOrData ) :
  174. sourceElementOrData;
  175. return editor.data.init( initialData );
  176. } )
  177. .then( () => editor.fire( 'ready' ))
  178. .then( () => editor )
  179. );
  180. } );
  181. }
  182. }
  183. mix( BalloonEditor, DataApiMixin );
  184. mix( BalloonEditor, ElementApiMixin );