8
0

ckeditor.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * This is the API entry point. The entire CKEditor code runs under this object.
  8. *
  9. * @class CKEDITOR
  10. * @singleton
  11. */
  12. CKEDITOR.define( [ 'editor', 'collection', 'config' ], ( Editor, Collection, Config ) => {
  13. const CKEDITOR = {
  14. /**
  15. * A collection containing all editor instances created.
  16. *
  17. * @readonly
  18. * @property {Collection}
  19. */
  20. instances: new Collection(),
  21. /**
  22. * Creates an editor instance for the provided DOM element.
  23. *
  24. * The creation of editor instances is an asynchronous operation, therefore a promise is returned by this
  25. * method.
  26. *
  27. * CKEDITOR.create( '#content' );
  28. *
  29. * CKEDITOR.create( '#content' ).then( ( editor ) => {
  30. * // Manipulate "editor" here.
  31. * } );
  32. *
  33. * @param {String|HTMLElement} element An element selector or a DOM element, which will be the source for the
  34. * created instance.
  35. * @returns {Promise} A promise, which will be fulfilled with the created editor.
  36. */
  37. create( element, config ) {
  38. return new Promise( ( resolve, reject ) => {
  39. // If a query selector has been passed, transform it into a real element.
  40. if ( typeof element == 'string' ) {
  41. element = document.querySelector( element );
  42. if ( !element ) {
  43. return reject( new Error( 'Element not found' ) );
  44. }
  45. }
  46. const editor = new Editor( element, config );
  47. this.instances.add( editor );
  48. // Remove the editor from `instances` when destroyed.
  49. editor.once( 'destroy', () => {
  50. this.instances.remove( editor );
  51. } );
  52. resolve(
  53. // Initializes the editor, which returns a promise.
  54. editor.init()
  55. .then( () => {
  56. // After initialization, return the created editor.
  57. return editor;
  58. } )
  59. );
  60. } );
  61. },
  62. /**
  63. * Holds global configuration defaults, which will be used by editor instances when such configurations are not
  64. * available on them directly.
  65. */
  66. config: new Config(),
  67. /**
  68. * Gets the full URL path for the specified plugin.
  69. *
  70. * Note that the plugin is not checked to exist. It is a pure path computation.
  71. *
  72. * @param {String} name The plugin name.
  73. * @returns {String} The full URL path of the plugin.
  74. */
  75. getPluginPath( name ) {
  76. return this.basePath + 'plugins/' + name + '/';
  77. }
  78. };
  79. return CKEDITOR;
  80. } );