8
0

editor.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import EmitterMixin from '../utils/emittermixin.js';
  7. import Config from '../utils/config.js';
  8. import PluginCollection from '../plugincollection.js';
  9. import Locale from '../utils/locale.js';
  10. import DataController from '../engine/datacontroller.js';
  11. import Document from '../engine/model/document.js';
  12. import CKEditorError from '../utils/ckeditorerror.js';
  13. import isArray from '../utils/lib/lodash/isArray.js';
  14. import mix from '../utils/mix.js';
  15. /**
  16. * Class representing a basic editor. It contains a base architecture, without much additional logic.
  17. *
  18. * See also {@link ckeditor5.editor.StandardEditor}.
  19. *
  20. * @memberOf ckeditor5.editor
  21. * @mixes utils.EmitterMixin
  22. */
  23. export default class Editor {
  24. /**
  25. * Creates a new instance of the Editor class.
  26. *
  27. * @param {Object} config The editor config.
  28. */
  29. constructor( config ) {
  30. /**
  31. * Holds all configurations specific to this editor instance.
  32. *
  33. * @readonly
  34. * @member {utils.Config} ckeditor5.Editor#config
  35. */
  36. this.config = new Config( config );
  37. /**
  38. * The plugins loaded and in use by this editor instance.
  39. *
  40. * @readonly
  41. * @member {ckeditor5.PluginCollection} ckeditor5.Editor#plugins
  42. */
  43. this.plugins = new PluginCollection( this );
  44. /**
  45. * Commands registered to the editor.
  46. *
  47. * @readonly
  48. * @member {Map.<ckeditor5.command.Command>} ckeditor5.Editor#commands
  49. */
  50. this.commands = new Map();
  51. /**
  52. * @readonly
  53. * @member {utils.Locale} ckeditor5.Editor#locale
  54. */
  55. this.locale = new Locale( this.config.get( 'lang' ) );
  56. /**
  57. * Shorthand for {@link utils.Locale#t}.
  58. *
  59. * @see utils.Locale#t
  60. * @method ckeditor5.Editor#t
  61. */
  62. this.t = this.locale.t;
  63. /**
  64. * Tree Model document managed by this editor.
  65. *
  66. * @readonly
  67. * @member {engine.model.Document} ckeditor5.Editor#document
  68. */
  69. this.document = new Document();
  70. /**
  71. * Instance of the {@link engine.DataController data controller}.
  72. *
  73. * @readonly
  74. * @member {engine.DataController} ckeditor5.Editor#data
  75. */
  76. this.data = new DataController( this.document );
  77. /**
  78. * Instance of the {@link engine.EditingController editing controller}.
  79. *
  80. * This property is set by more specialized editor classes (such as {@link ckeditor5.editor.StandardEditor}),
  81. * however, it's required for features to work as their engine-related parts will try to connect converters.
  82. *
  83. * When defining a virtual editor class, like one working in Node.js, it's possible to plug a virtual
  84. * editing controller which only instantiates necessary properties, but without any observers and listeners.
  85. *
  86. * @readonly
  87. * @member {engine.EditingController} ckeditor5.editor.Editor#editing
  88. */
  89. }
  90. /**
  91. * Loads and initilizes plugins specified in config features.
  92. *
  93. * @returns {Promise} A promise which resolves once the initialization is completed.
  94. */
  95. initPlugins() {
  96. const that = this;
  97. const config = this.config;
  98. return loadPlugins()
  99. .then( initPlugins );
  100. function loadPlugins() {
  101. let plugins = config.get( 'features' ) || [];
  102. // Handle features passed as a string.
  103. if ( !isArray( plugins ) ) {
  104. plugins = plugins.split( ',' );
  105. }
  106. return that.plugins.load( plugins );
  107. }
  108. function initPlugins( loadedPlugins ) {
  109. return loadedPlugins.reduce( ( promise, plugin ) => {
  110. return promise.then( plugin.init.bind( plugin ) );
  111. }, Promise.resolve() );
  112. }
  113. }
  114. /**
  115. * Destroys the editor instance, releasing all resources used by it.
  116. *
  117. * @fires ckeditor5.editor.Editor#destroy
  118. * @returns {Promise} A promise that resolves once the editor instance is fully destroyed.
  119. */
  120. destroy() {
  121. this.fire( 'destroy' );
  122. this.stopListening();
  123. return Promise.resolve()
  124. .then( () => {
  125. this.document.destroy();
  126. this.data.destroy();
  127. } );
  128. }
  129. /**
  130. * Executes specified command with given parameter.
  131. *
  132. * @param {String} commandName Name of command to execute.
  133. * @param {*} [commandParam] If set, command will be executed with this parameter.
  134. */
  135. execute( commandName, commandParam ) {
  136. let command = this.commands.get( commandName );
  137. if ( !command ) {
  138. /**
  139. * Specified command has not been added to the editor.
  140. *
  141. * @error editor-command-not-found
  142. */
  143. throw new CKEditorError( 'editor-command-not-found: Specified command has not been added to the editor.' );
  144. }
  145. command._execute( commandParam );
  146. }
  147. /**
  148. * Creates a basic editor instance.
  149. *
  150. * @param {Object} config See {@link ckeditor5.editor.StandardEditor}'s param.
  151. * @returns {Promise} Promise resolved once editor is ready.
  152. * @returns {ckeditor5.editor.StandardEditor} return.editor The editor instance.
  153. */
  154. static create( config ) {
  155. return new Promise( ( resolve ) => {
  156. const editor = new this( config );
  157. resolve(
  158. editor.initPlugins()
  159. .then( () => editor )
  160. );
  161. } );
  162. }
  163. }
  164. mix( Editor, EmitterMixin );
  165. /**
  166. * Fired when this editor instance is destroyed. The editor at this point is not usable and this event should be used to
  167. * perform the clean-up in any plugin.
  168. *
  169. * @event ckeditor5.editor.Editor#destroy
  170. */