8
0

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