8
0

ckeditor.js 2.1 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 '../utils/collection.js';
  8. import Config from '../utils/config.js';
  9. /**
  10. * This is the API entry point. The entire CKEditor code runs under this object.
  11. *
  12. * @namespace CKEDITOR
  13. */
  14. const CKEDITOR = {
  15. /**
  16. * A collection containing all editor instances created.
  17. *
  18. * @readonly
  19. * @member {utils.Collection} CKEDITOR.instances
  20. */
  21. instances: new Collection(),
  22. /**
  23. * Creates an editor instance for the provided DOM element.
  24. *
  25. * The creation of editor instances is an asynchronous operation, therefore a promise is returned by this
  26. * method.
  27. *
  28. * CKEDITOR.create( '#content' );
  29. *
  30. * CKEDITOR.create( '#content' ).then( ( editor ) => {
  31. * // Manipulate "editor" here.
  32. * } );
  33. *
  34. * @method CKEDITOR.create
  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. * @member {utils.Config} CKEDITOR.config
  68. */
  69. config: new Config()
  70. };
  71. export default CKEDITOR;