standardeditor.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Editor from './editor.js';
  6. import KeystrokeHandler from '../keystrokehandler.js';
  7. import EditingController from '../engine/editingcontroller.js';
  8. import getDataFromElement from '../utils/dom/getdatafromelement.js';
  9. import setDataInElement from '../utils/dom/setdatainelement.js';
  10. /**
  11. * Class representing a typical browser-based editor. It handles a single source element and
  12. * uses {@link engine.EditingController}.
  13. *
  14. * @memberOf ckeditor5.editor
  15. */
  16. export default class StandardEditor extends Editor {
  17. /**
  18. * Creates a new instance of the standard editor.
  19. *
  20. * @param {HTMLElement} element The DOM element that will be the source
  21. * for the created editor.
  22. * @param {Object} config The editor config.
  23. */
  24. constructor( element, config ) {
  25. super( config );
  26. /**
  27. * The element on which the editor has been initialized.
  28. *
  29. * @readonly
  30. * @member {HTMLElement} ckeditor5.editor.StandardEditor#element
  31. */
  32. this.element = element;
  33. // Documented in Editor.
  34. this.editing = new EditingController( this.document );
  35. /**
  36. * Instance of the {@link ckeditor5.KeystrokeHandler}.
  37. *
  38. * @readonly
  39. * @member {engine.treecontroller.DataController} ckeditor5.editor.StandardEditor#keystrokes
  40. */
  41. this.keystrokes = new KeystrokeHandler( this );
  42. /**
  43. * Editor UI instance.
  44. *
  45. * This property is set by more specialized editor constructors. However, it's required
  46. * for features to work (their UI-related part will try to interact with editor UI),
  47. * so every editor class which is meant to work with default features should set this property.
  48. *
  49. * @readonly
  50. * @member {ui.editorUI.EditorUI} ckeditor5.editor.StandardEditor#ui
  51. */
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. destroy() {
  57. return Promise.resolve()
  58. .then( () => this.editing.destroy() )
  59. .then( super.destroy() );
  60. }
  61. /**
  62. * Sets the data in the editor's main root.
  63. *
  64. * @param {*} data The data to load.
  65. */
  66. setData( data ) {
  67. this.data.set( data );
  68. }
  69. /**
  70. * Gets the data from the editor's main root.
  71. */
  72. getData() {
  73. return this.data.get();
  74. }
  75. /**
  76. * Updates the {@link ckeditor5.editor.StandardEditor#element editor element}'s content with the data.
  77. */
  78. updateEditorElement() {
  79. setDataInElement( this.element, this.getData() );
  80. }
  81. /**
  82. * Loads the data from the {@link ckeditor5.editor.StandardEditor#element editor element} to the main root.
  83. */
  84. loadDataFromEditorElement() {
  85. this.setData( getDataFromElement( this.element ) );
  86. }
  87. /**
  88. * Creates a standard editor instance.
  89. *
  90. * @param {HTMLElement} element See {@link ckeditor5.editor.StandardEditor}'s param.
  91. * @param {Object} config See {@link ckeditor5.editor.StandardEditor}'s param.
  92. * @returns {Promise} Promise resolved once editor is ready.
  93. * @returns {ckeditor5.editor.StandardEditor} return.editor The editor instance.
  94. */
  95. static create( element, config ) {
  96. return new Promise( ( resolve ) => {
  97. const editor = new this( element, config );
  98. resolve(
  99. editor.initPlugins()
  100. .then( () => editor )
  101. );
  102. } );
  103. }
  104. }