ballooneditor.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  21. /**
  22. * The {@glink builds/guides/overview#balloon-editor balloon editor} implementation (Medium-like editor).
  23. * It uses an inline editable and a toolbar based on the {@link module:ui/toolbar/balloon/balloontoolbar~BalloonToolbar}.
  24. * See the {@glink examples/builds/balloon-editor demo}.
  25. *
  26. * In order to create a balloon editor instance, use the static
  27. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`} method.
  28. *
  29. * # Balloon editor and balloon build
  30. *
  31. * The balloon editor can be used directly from source (if you installed the
  32. * [`@ckeditor/ckeditor5-editor-balloon`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-balloon) package)
  33. * but it is also available in the {@glink builds/guides/overview#balloon-editor balloon build}.
  34. *
  35. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  36. * source you need to take care of loading all plugins by yourself
  37. * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
  38. * Using the editor from source gives much better flexibility and allows easier customization.
  39. *
  40. * Read more about initializing the editor from source or as a build in
  41. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  42. *
  43. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  44. * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin
  45. * @implements module:core/editor/editorwithui~EditorWithUI
  46. * @extends module:core/editor/editor~Editor
  47. */
  48. export default class BalloonEditor extends Editor {
  49. /**
  50. * Creates an instance of the balloon editor.
  51. *
  52. * **Note:** do not use the constructor to create editor instances. Use the static
  53. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`} method instead.
  54. *
  55. * @protected
  56. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  57. * (on which the editor will be initialized) or initial data for the editor. For more information see
  58. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  59. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  60. */
  61. constructor( sourceElementOrData, config ) {
  62. super( config );
  63. if ( isElement( sourceElementOrData ) ) {
  64. this.sourceElement = sourceElementOrData;
  65. }
  66. const plugins = this.config.get( 'plugins' );
  67. plugins.push( BalloonToolbar );
  68. this.config.set( 'plugins', plugins );
  69. this.config.define( 'balloonToolbar', this.config.get( 'toolbar' ) );
  70. this.data.processor = new HtmlDataProcessor();
  71. this.model.document.createRoot();
  72. const view = new BalloonEditorUIView( this.locale, this.editing.view, this.sourceElement );
  73. this.ui = new BalloonEditorUI( this, view );
  74. attachToForm( this );
  75. }
  76. /**
  77. * Destroys the editor instance, releasing all resources used by it.
  78. *
  79. * Updates the original editor element with the data.
  80. *
  81. * @returns {Promise}
  82. */
  83. destroy() {
  84. // Cache the data, then destroy.
  85. // It's safe to assume that the model->view conversion will not work after super.destroy().
  86. const data = this.getData();
  87. this.ui.destroy();
  88. return super.destroy()
  89. .then( () => {
  90. if ( this.sourceElement ) {
  91. setDataInElement( this.sourceElement, data );
  92. }
  93. } );
  94. }
  95. /**
  96. * Creates a `BalloonEditor` instance.
  97. *
  98. * There are two general ways how the editor can be initialized.
  99. *
  100. * You can initialize the editor using an existing DOM element:
  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. * The element's content will be used as the editor data and the element will become the editable element.
  112. *
  113. * Alternatively, you can initialize the editor by passing the initial data directly as a `String`.
  114. * In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly:
  115. *
  116. * BalloonEditor
  117. * .create( '<p>Hello world!</p>' )
  118. * .then( editor => {
  119. * console.log( 'Editor was initialized', editor );
  120. *
  121. * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
  122. * document.body.appendChild( editor.ui.element );
  123. * } )
  124. * .catch( err => {
  125. * console.error( err.stack );
  126. * } );
  127. *
  128. * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
  129. * web page content is generated on the client-side and the DOM structure is not ready at the moment when you initialize the editor.
  130. *
  131. * You can also mix those two ways by providing a DOM element to be used and passing the initial data through the config:
  132. *
  133. * BalloonEditor
  134. * .create( document.querySelector( '#editor' ), {
  135. * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
  136. * } )
  137. * .then( editor => {
  138. * console.log( 'Editor was initialized', editor );
  139. * } )
  140. * .catch( err => {
  141. * console.error( err.stack );
  142. * } );
  143. *
  144. * This method can be used to initialize the editor on an existing element with specified content in case if your integration
  145. * makes it difficult to set the content of the source element.
  146. *
  147. * Note that an error will be thrown if you pass initial data both as the first parameter and also in the config.
  148. *
  149. * See also the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
  150. * customizing plugins, toolbar and other.
  151. *
  152. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  153. * or the editor's initial data.
  154. *
  155. * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization.
  156. * Moreover, the editor data will be set back to the original element once the editor is destroyed.
  157. *
  158. * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
  159. * It is available under {@link module:editor-balloon/ballooneditorui~BalloonEditorUI#element `editor.ui.element`} property.
  160. *
  161. * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
  162. * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
  163. */
  164. static create( sourceElementOrData, config = {} ) {
  165. return new Promise( resolve => {
  166. const editor = new this( sourceElementOrData, config );
  167. resolve(
  168. editor.initPlugins()
  169. .then( () => {
  170. editor.ui.init();
  171. } )
  172. .then( () => {
  173. if ( !isElement( sourceElementOrData ) && config.initialData ) {
  174. throw new CKEditorError(
  175. 'editor-create-initial-data: ' +
  176. 'EditorConfig#initialData cannot be used together with initial data passed in Editor#create()'
  177. );
  178. }
  179. const initialData = config.initialData || getInitialData( sourceElementOrData );
  180. return editor.data.init( initialData );
  181. } )
  182. .then( () => editor.fire( 'ready' ) )
  183. .then( () => editor )
  184. );
  185. } );
  186. }
  187. }
  188. mix( BalloonEditor, DataApiMixin );
  189. mix( BalloonEditor, ElementApiMixin );
  190. function getInitialData( sourceElementOrData ) {
  191. return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
  192. }