8
0

ballooneditor.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Copyright (c) 2003-2018, 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. this.config.get( 'plugins' ).push( BalloonToolbar );
  66. this.config.define( 'balloonToolbar', this.config.get( 'toolbar' ) );
  67. this.data.processor = new HtmlDataProcessor();
  68. this.model.document.createRoot();
  69. this.ui = new BalloonEditorUI( this, new BalloonEditorUIView( this.locale, this.sourceElement ) );
  70. attachToForm( this );
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. get element() {
  76. return this.ui.view.editable.element;
  77. }
  78. /**
  79. * Destroys the editor instance, releasing all resources used by it.
  80. *
  81. * Updates the original editor element with the data.
  82. *
  83. * @returns {Promise}
  84. */
  85. destroy() {
  86. // Cache the data, then destroy.
  87. // It's safe to assume that the model->view conversion will not work after super.destroy().
  88. const data = this.getData();
  89. this.ui.destroy();
  90. return super.destroy()
  91. .then( () => {
  92. if ( this.sourceElement ) {
  93. setDataInElement( this.sourceElement, data );
  94. }
  95. } );
  96. }
  97. /**
  98. * Creates a balloon editor instance.
  99. *
  100. * Creating an instance when using a {@glink builds/index CKEditor build}:
  101. *
  102. * BalloonEditor
  103. * .create( document.querySelector( '#editor' ) )
  104. * .then( editor => {
  105. * console.log( 'Editor was initialized', editor );
  106. * } )
  107. * .catch( err => {
  108. * console.error( err.stack );
  109. * } );
  110. *
  111. * Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  112. *
  113. * import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  114. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  115. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  116. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  117. * import ...
  118. *
  119. * BalloonEditor
  120. * .create( document.querySelector( '#editor' ), {
  121. * plugins: [ Essentials, Bold, Italic, ... ],
  122. * toolbar: [ 'bold', 'italic', ... ]
  123. * } )
  124. * .then( editor => {
  125. * console.log( 'Editor was initialized', editor );
  126. * } )
  127. * .catch( err => {
  128. * console.error( err.stack );
  129. * } );
  130. *
  131. * Creating an instance when using initial data instead of a DOM element:
  132. *
  133. * import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  134. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  135. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  136. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  137. * import ...
  138. *
  139. * BalloonEditor
  140. * .create( '<p>Hello world!</p>', {
  141. * plugins: [ Essentials, Bold, Italic, ... ],
  142. * toolbar: [ 'bold', 'italic', ... ]
  143. * } )
  144. * .then( editor => {
  145. * console.log( 'Editor was initialized', editor );
  146. *
  147. * // Initial data was provided so `editor.element` needs to be added manually to the DOM.
  148. * document.body.appendChild( editor.element );
  149. * } )
  150. * .catch( err => {
  151. * console.error( err.stack );
  152. * } );
  153. *
  154. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  155. * (on which the editor will be initialized) or initial data for the editor.
  156. *
  157. * If a source element is passed, then its contents will be automatically
  158. * {@link module:editor-balloon/ballooneditor~BalloonEditor#setData loaded} to the editor on startup and the element
  159. * itself will be used as the editor's editable element.
  160. *
  161. * If data is provided, then `editor.element` will be created automatically and needs to be added
  162. * to the DOM manually.
  163. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  164. * @returns {Promise} A promise resolved once the editor is ready.
  165. * The promise returns the created {@link module:editor-balloon/ballooneditor~BalloonEditor} instance.
  166. */
  167. static create( sourceElementOrData, config ) {
  168. return new Promise( resolve => {
  169. const editor = new this( sourceElementOrData, config );
  170. resolve(
  171. editor.initPlugins()
  172. .then( () => {
  173. editor.ui.init();
  174. editor.fire( 'uiReady' );
  175. } )
  176. .then( () => {
  177. const initialData = isElement( sourceElementOrData ) ?
  178. getDataFromElement( sourceElementOrData ) :
  179. sourceElementOrData;
  180. return editor.data.init( initialData );
  181. } )
  182. .then( () => {
  183. editor.fire( 'dataReady' );
  184. editor.fire( 'ready' );
  185. } )
  186. .then( () => editor )
  187. );
  188. } );
  189. }
  190. }
  191. mix( BalloonEditor, DataApiMixin );
  192. mix( BalloonEditor, ElementApiMixin );