ballooneditor.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
  11. import BalloonEditorUI from './ballooneditorui';
  12. import BalloonEditorUIView from './ballooneditoruiview';
  13. import setDataInElement from '@ckeditor/ckeditor5-utils/src/dom/setdatainelement';
  14. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  15. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  16. import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform';
  17. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  18. /**
  19. * The {@glink builds/guides/overview#Balloon-editor balloon editor} implementation (Medium-like editor).
  20. * It uses an inline editable and a toolbar based on the {@link module:ui/toolbar/contextual/contextualtoolbar~ContextualToolbar}.
  21. * See the {@glink examples/builds/balloon-editor demo}.
  22. *
  23. * In order to create a balloon editor instance, use the static
  24. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`} method.
  25. *
  26. * # Balloon editor and balloon build
  27. *
  28. * The balloon editor can be used directly from source (if you installed the
  29. * [`@ckeditor/ckeditor5-editor-balloon`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-balloon) package)
  30. * but it is also available in the {@glink builds/guides/overview#Balloon-editor balloon build}.
  31. *
  32. * {@glink builds/guides/overview Builds} are ready-to-use editors with plugins bundled in. When using the editor from
  33. * source you need to take care of loading all plugins by yourself
  34. * (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
  35. * Using the editor from source gives much better flexibility and allows easier customization.
  36. *
  37. * Read more about initializing the editor from source or as a build in
  38. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}.
  39. *
  40. * @mixes module:core/editor/utils/dataapimixin~DataApiMixin
  41. * @mixes module:core/editor/utils/elementapimixin~ElementApiMixin
  42. * @implements module:core/editor/editorwithui~EditorWithUI
  43. * @extends module:core/editor/editor~Editor
  44. */
  45. export default class BalloonEditor extends Editor {
  46. /**
  47. * Creates an instance of the balloon editor.
  48. *
  49. * **Note:** do not use the constructor to create editor instances. Use the static
  50. * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`} method instead.
  51. *
  52. * @protected
  53. * @param {HTMLElement} element The DOM element that will be the source for the created editor
  54. * (on which the editor will be initialized).
  55. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  56. */
  57. constructor( element, config ) {
  58. super( config );
  59. this.element = element;
  60. this.config.get( 'plugins' ).push( ContextualToolbar );
  61. this.config.define( 'contextualToolbar', this.config.get( 'toolbar' ) );
  62. this.data.processor = new HtmlDataProcessor();
  63. this.model.document.createRoot();
  64. this.ui = new BalloonEditorUI( this, new BalloonEditorUIView( this.locale, element ) );
  65. attachToForm( this );
  66. }
  67. /**
  68. * Destroys the editor instance, releasing all resources used by it.
  69. *
  70. * Updates the original editor element with the data.
  71. *
  72. * @returns {Promise}
  73. */
  74. destroy() {
  75. // Cache the data, then destroy.
  76. // It's safe to assume that the model->view conversion will not work after super.destroy().
  77. const data = this.getData();
  78. this.ui.destroy();
  79. return super.destroy()
  80. .then( () => setDataInElement( this.element, data ) );
  81. }
  82. /**
  83. * Creates a balloon editor instance.
  84. *
  85. * Creating instance when using {@glink builds/index CKEditor build}:
  86. *
  87. * BalloonEditor
  88. * .create( document.querySelector( '#editor' ) )
  89. * .then( editor => {
  90. * console.log( 'Editor was initialized', editor );
  91. * } )
  92. * .catch( err => {
  93. * console.error( err.stack );
  94. * } );
  95. *
  96. * Creating instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):
  97. *
  98. * import BalloonEditor from '@ckeditor/ckeditor5-editor-balloon/src/ballooneditor';
  99. * import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  100. * import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  101. * import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  102. * import ...
  103. *
  104. * BalloonEditor
  105. * .create( document.querySelector( '#editor' ), {
  106. * plugins: [ Essentials, Bold, Italic, ... ],
  107. * toolbar: [ 'bold', 'italic', ... ]
  108. * } )
  109. * .then( editor => {
  110. * console.log( 'Editor was initialized', editor );
  111. * } )
  112. * .catch( err => {
  113. * console.error( err.stack );
  114. * } );
  115. *
  116. * @param {HTMLElement} element The DOM element that will be the source for the created editor
  117. * (on which the editor will be initialized).
  118. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  119. * @returns {Promise} A promise resolved once the editor is ready.
  120. * The promise returns the created {@link module:editor-balloon/ballooneditor~BalloonEditor} instance.
  121. */
  122. static create( element, config ) {
  123. return new Promise( resolve => {
  124. const editor = new this( element, config );
  125. resolve(
  126. editor.initPlugins()
  127. .then( () => {
  128. editor.ui.init();
  129. editor.fire( 'uiReady' );
  130. } )
  131. .then( () => editor.loadDataFromElement() )
  132. .then( () => {
  133. editor.fire( 'dataReady' );
  134. editor.fire( 'ready' );
  135. } )
  136. .then( () => editor )
  137. );
  138. } );
  139. }
  140. }
  141. mix( BalloonEditor, DataApiMixin );
  142. mix( BalloonEditor, ElementApiMixin );