8
0

inlineeditor.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @license Copyright (c) 2003-2019, 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-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 'lodash-es';
  19. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  20. /**
  21. * The {@glink builds/guides/overview#inline-editor inline editor} implementation.
  22. * It uses an inline editable and a floating toolbar.
  23. * See the {@glink examples/builds/inline-editor demo}.
  24. *
  25. * In order to create a inline editor instance, use the static
  26. * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`} method.
  27. *
  28. * # Inline editor and inline build
  29. *
  30. * The inline editor can be used directly from source (if you installed the
  31. * [`@ckeditor/ckeditor5-editor-inline`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-inline) package)
  32. * but it is also available in the {@glink builds/guides/overview#inline-editor inline 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-inline/inlineeditor~InlineEditor.create `InlineEditor.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 InlineEditor extends Editor {
  48. /**
  49. * Creates an instance of the inline editor.
  50. *
  51. * **Note:** Do not use the constructor to create editor instances. Use the static
  52. * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.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-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`}.
  58. * @param {module:core/editor/editorconfig~EditorConfig} config The editor configuration.
  59. */
  60. constructor( sourceElementOrData, config ) {
  61. super( config );
  62. this.data.processor = new HtmlDataProcessor();
  63. this.model.document.createRoot();
  64. if ( isElement( sourceElementOrData ) ) {
  65. this.sourceElement = sourceElementOrData;
  66. }
  67. const view = new InlineEditorUIView( this.locale, this.editing.view, this.sourceElement );
  68. this.ui = new InlineEditorUI( this, view );
  69. attachToForm( this );
  70. }
  71. /**
  72. * Destroys the editor instance, releasing all resources used by it.
  73. *
  74. * Updates the original editor element with the data.
  75. *
  76. * @returns {Promise}
  77. */
  78. destroy() {
  79. // Cache the data, then destroy.
  80. // It's safe to assume that the model->view conversion will not work after super.destroy().
  81. const data = this.getData();
  82. this.ui.destroy();
  83. return super.destroy()
  84. .then( () => {
  85. if ( this.sourceElement ) {
  86. setDataInElement( this.sourceElement, data );
  87. }
  88. } );
  89. }
  90. /**
  91. * Creates a new inline editor instance.
  92. *
  93. * There are three general ways how the editor can be initialized.
  94. *
  95. * # Using an existing DOM element (and loading data from it)
  96. *
  97. * You can initialize the editor using an existing DOM element:
  98. *
  99. * InlineEditor
  100. * .create( document.querySelector( '#editor' ) )
  101. * .then( editor => {
  102. * console.log( 'Editor was initialized', editor );
  103. * } )
  104. * .catch( err => {
  105. * console.error( err.stack );
  106. * } );
  107. *
  108. * The element's content will be used as the editor data and the element will become the editable element.
  109. *
  110. * # Creating a detached editor
  111. *
  112. * Alternatively, you can initialize the editor by passing the initial data directly as a `String`.
  113. * In this case, the editor will render an element that must be inserted into the DOM for the editor to work properly:
  114. *
  115. * InlineEditor
  116. * .create( '<p>Hello world!</p>' )
  117. * .then( editor => {
  118. * console.log( 'Editor was initialized', editor );
  119. *
  120. * // Initial data was provided so the editor UI element needs to be added manually to the DOM.
  121. * document.body.appendChild( editor.ui.element );
  122. * } )
  123. * .catch( err => {
  124. * console.error( err.stack );
  125. * } );
  126. *
  127. * This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
  128. * web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
  129. *
  130. * # Using an existing DOM element (and data provided in `config.initialData`)
  131. *
  132. * You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
  133. *
  134. * InlineEditor
  135. * .create( document.querySelector( '#editor' ), {
  136. * initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
  137. * } )
  138. * .then( editor => {
  139. * console.log( 'Editor was initialized', editor );
  140. * } )
  141. * .catch( err => {
  142. * console.error( err.stack );
  143. * } );
  144. *
  145. * This method can be used to initialize the editor on an existing element with the specified content in case if your integration
  146. * makes it difficult to set the content of the source element.
  147. *
  148. * Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
  149. *
  150. * # Configuring the editor
  151. *
  152. * See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
  153. * customizing plugins, toolbar and more.
  154. *
  155. * # Using the editor from source
  156. *
  157. * The code samples listed in the previous sections of this documentation assume that you are using an
  158. * {@glink builds/guides/overview editor build} (for example – `@ckeditor/ckeditor5-build-inline`).
  159. *
  160. * If you want to use the inline editor from source (`@ckeditor/ckeditor5-editor-inline/src/inlineeditor`),
  161. * you need to define the list of
  162. * {@link module:core/editor/editorconfig~EditorConfig#plugins plugins to be initialized} and
  163. * {@link module:core/editor/editorconfig~EditorConfig#toolbar toolbar items}. Read more about using the editor from
  164. * source in the {@glink builds/guides/integration/advanced-setup "Advanced setup" guide}.
  165. *
  166. * @param {HTMLElement|String} sourceElementOrData The DOM element that will be the source for the created editor
  167. * or the editor's initial data.
  168. *
  169. * If a DOM element is passed, its content will be automatically loaded to the editor upon initialization.
  170. * Moreover, the editor data will be set back to the original element once the editor is destroyed.
  171. *
  172. * If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
  173. * It is available under the {@link module:editor-inline/inlineeditorui~InlineEditorUI#element `editor.ui.element`} property.
  174. *
  175. * @param {module:core/editor/editorconfig~EditorConfig} [config] The editor configuration.
  176. * @returns {Promise} A promise resolved once the editor is ready. The promise resolves with the created editor instance.
  177. */
  178. static create( sourceElementOrData, config = {} ) {
  179. return new Promise( resolve => {
  180. Editor._assertAllowedSourceElement( sourceElementOrData );
  181. const editor = new this( sourceElementOrData, config );
  182. resolve(
  183. editor.initPlugins()
  184. .then( () => {
  185. editor.ui.init();
  186. } )
  187. .then( () => {
  188. if ( !isElement( sourceElementOrData ) && config.initialData ) {
  189. // Documented in core/editor/editorconfig.jdoc.
  190. throw new CKEditorError(
  191. 'editor-create-initial-data: ' +
  192. 'The config.initialData option cannot be used together with initial data passed in Editor.create().'
  193. );
  194. }
  195. const initialData = config.initialData || getInitialData( sourceElementOrData );
  196. return editor.data.init( initialData );
  197. } )
  198. .then( () => editor.fire( 'ready' ) )
  199. .then( () => editor )
  200. );
  201. } );
  202. }
  203. }
  204. mix( InlineEditor, DataApiMixin );
  205. mix( InlineEditor, ElementApiMixin );
  206. function getInitialData( sourceElementOrData ) {
  207. return isElement( sourceElementOrData ) ? getDataFromElement( sourceElementOrData ) : sourceElementOrData;
  208. }