inlineeditor.js 5.9 KB

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