ckeditor.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Editor from './editor.js';
  7. import Collection from './collection.js';
  8. import Config from './config.js';
  9. /**
  10. * This is the API entry point. The entire CKEditor code runs under this object.
  11. *
  12. * @class CKEDITOR
  13. * @singleton
  14. */
  15. const CKEDITOR = {
  16. /**
  17. * A collection containing all editor instances created.
  18. *
  19. * @readonly
  20. * @property {Collection}
  21. */
  22. instances: new Collection(),
  23. /**
  24. * Creates an editor instance for the provided DOM element.
  25. *
  26. * The creation of editor instances is an asynchronous operation, therefore a promise is returned by this
  27. * method.
  28. *
  29. * CKEDITOR.create( '#content' );
  30. *
  31. * CKEDITOR.create( '#content' ).then( ( editor ) => {
  32. * // Manipulate "editor" here.
  33. * } );
  34. *
  35. * @param {String|HTMLElement} element An element selector or a DOM element, which will be the source for the
  36. * created instance.
  37. * @returns {Promise} A promise, which will be fulfilled with the created editor.
  38. */
  39. create( element, config ) {
  40. return new Promise( ( resolve, reject ) => {
  41. // If a query selector has been passed, transform it into a real element.
  42. if ( typeof element == 'string' ) {
  43. element = document.querySelector( element );
  44. if ( !element ) {
  45. return reject( new Error( 'Element not found' ) );
  46. }
  47. }
  48. const editor = new Editor( element, config );
  49. this.instances.add( editor );
  50. // Remove the editor from `instances` when destroyed.
  51. editor.once( 'destroy', () => {
  52. this.instances.remove( editor );
  53. } );
  54. resolve(
  55. // Initializes the editor, which returns a promise.
  56. editor.init()
  57. .then( () => {
  58. // After initialization, return the created editor.
  59. return editor;
  60. } )
  61. );
  62. } );
  63. },
  64. /**
  65. * Holds global configuration defaults, which will be used by editor instances when such configurations are not
  66. * available on them directly.
  67. */
  68. config: new Config()
  69. };
  70. export default CKEDITOR;