ballooneditor.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. import secureSourceElement from '@ckeditor/ckeditor5-core/src/editor/utils/securesourceelement';
  22. /**
  23. * The {@glink builds/guides/overview#balloon-editor balloon editor} implementation (Medium-like editor).
  24. * It uses an inline editable and a toolbar based on the {@link module:ui/toolbar/balloon/balloontoolbar~BalloonToolbar}.
  25. * See the {@glink examples/builds/balloon-editor demo}.
  26. *
  27. * In order to create a balloon editor instance, use the static
  28. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`} method.
  29. *
  30. * # Balloon editor and balloon build
  31. *
  32. * The balloon editor can be used directly from source (if you installed the
  33. * [`@ckeditor/ckeditor5-editor-balloon`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-balloon) package)
  34. * but it is also available in the {@glink builds/guides/overview#balloon-editor balloon build}.
  35. *
  36. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  37. * source you need to take care of loading all plugins by yourself
  38. * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
  39. * Using the editor from source gives much better flexibility and allows easier customization.
  40. *
  41. * Read more about initializing the editor from source or as a build in
  42. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  43. *
  44. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  45. * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin
  46. * @implements module:core/editor/editorwithui~EditorWithUI
  47. * @extends module:core/editor/editor~Editor
  48. */
  49. export default class BalloonEditor extends Editor {
  50. /**
  51. * Creates an instance of the balloon editor.
  52. *
  53. * **Note:** do not use the constructor to create editor instances. Use the static
  54. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`} method instead.
  55. *
  56. * @protected
  57. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  58. * (on which the editor will be initialized) or initial data for the editor. For more information see
  59. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  60. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  61. */
  62. constructor( sourceElementOrData, config ) {
  63. super( config );
  64. if ( isElement( sourceElementOrData ) ) {
  65. this.sourceElement = sourceElementOrData;
  66. secureSourceElement( this );
  67. }
  68. const plugins = this.config.get( 'plugins' );
  69. plugins.push( BalloonToolbar );
  70. this.config.set( 'plugins', plugins );
  71. this.config.define( 'balloonToolbar', this.config.get( 'toolbar' ) );
  72. this.data.processor = new HtmlDataProcessor( this.data.viewDocument );
  73. this.model.document.createRoot();
  74. const view = new BalloonEditorUIView( this.locale, this.editing.view, this.sourceElement );
  75. this.ui = new BalloonEditorUI( this, view );
  76. attachToForm( this );
  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 new balloon editor instance.
  99. *
  100. * There are three general ways how the editor can be initialized.
  101. *
  102. * # Using an existing DOM element (and loading data from it)
  103. *
  104. * You can initialize the editor using an existing DOM element:
  105. *
  106. * BalloonEditor
  107. * .create( document.querySelector( '#editor' ) )
  108. * .then( editor => {
  109. * console.log( 'Editor was initialized', editor );
  110. * } )
  111. * .catch( err => {
  112. * console.error( err.stack );
  113. * } );
  114. *
  115. * The element's content will be used as the editor data and the element will become the editable element.
  116. *
  117. * # Creating a detached editor
  118. *
  119. * Alternatively, you can initialize the editor by passing the initial data directly as a string.
  120. * In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly:
  121. *
  122. * BalloonEditor
  123. * .create( '<p>Hello world!</p>' )
  124. * .then( editor => {
  125. * console.log( 'Editor was initialized', editor );
  126. *
  127. * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
  128. * document.body.appendChild( editor.ui.element );
  129. * } )
  130. * .catch( err => {
  131. * console.error( err.stack );
  132. * } );
  133. *
  134. * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
  135. * web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
  136. *
  137. * # Using an existing DOM element (and data provided in `config.initialData`)
  138. *
  139. * You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
  140. *
  141. * BalloonEditor
  142. * .create( document.querySelector( '#editor' ), {
  143. * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
  144. * } )
  145. * .then( editor => {
  146. * console.log( 'Editor was initialized', editor );
  147. * } )
  148. * .catch( err => {
  149. * console.error( err.stack );
  150. * } );
  151. *
  152. * This method can be used to initialize the editor on an existing element with the specified content in case if your integration
  153. * makes it difficult to set the content of the source element.
  154. *
  155. * Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
  156. *
  157. * # Configuring the editor
  158. *
  159. * See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
  160. * customizing plugins, toolbar and more.
  161. *
  162. * # Using the editor from source
  163. *
  164. * The code samples listed in the previous sections of this documentation assume that you are using an
  165. * {@glink builds/guides/overview editor build} (for example – `@ckeditor/ckeditor5-build-balloon`).
  166. *
  167. * If you want to use the balloon editor from source (`@ckeditor/ckeditor5-editor-balloon/src/ballooneditor`),
  168. * you need to define the list of
  169. * {@link module:core/editor/editorconfig~EditorConfig#plugins plugins to be initialized} and
  170. * {@link module:core/editor/editorconfig~EditorConfig#toolbar toolbar items}. Read more about using the editor from
  171. * source in the {@glink builds/guides/integration/advanced-setup "Advanced setup" guide}.
  172. *
  173. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  174. * or the editor's initial data.
  175. *
  176. * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization.
  177. * Moreover, the editor data will be set back to the original element once the editor is destroyed.
  178. *
  179. * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
  180. * It is available under the {@link module:editor-balloon/ballooneditorui~BalloonEditorUI#element `editor.ui.element`} property.
  181. *
  182. * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
  183. * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
  184. */
  185. static create( sourceElementOrData, config = {} ) {
  186. return new Promise( resolve => {
  187. const isHTMLElement = isElement( sourceElementOrData );
  188. if ( isHTMLElement && sourceElementOrData.tagName === 'TEXTAREA' ) {
  189. // Documented in core/editor/editor.js
  190. throw new CKEditorError(
  191. 'editor-wrong-element: This type of editor cannot be initialized inside <textarea> element.',
  192. null
  193. );
  194. }
  195. const editor = new this( sourceElementOrData, config );
  196. resolve(
  197. editor.initPlugins()
  198. .then( () => {
  199. editor.ui.init();
  200. } )
  201. .then( () => {
  202. if ( !isHTMLElement && config.initialData ) {
  203. // Documented in core/editor/editorconfig.jdoc.
  204. throw new CKEditorError(
  205. 'editor-create-initial-data: ' +
  206. 'The config.initialData option cannot be used together with initial data passed in Editor.create().',
  207. null
  208. );
  209. }
  210. const initialData = config.initialData || getInitialData( sourceElementOrData );
  211. return editor.data.init( initialData );
  212. } )
  213. .then( () => editor.fire( 'ready' ) )
  214. .then( () => editor )
  215. );
  216. } );
  217. }
  218. }
  219. mix( BalloonEditor, DataApiMixin );
  220. mix( BalloonEditor, ElementApiMixin );
  221. function getInitialData( sourceElementOrData ) {
  222. return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
  223. }