ckeditor.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. 'use strict';
  7. /**
  8. * This is the API entry point. The entire CKEditor code runs under this object.
  9. *
  10. * @class CKEDITOR
  11. * @singleton
  12. */
  13. CKEDITOR.define( [ 'editor', 'collection', 'promise', 'config' ], function( Editor, Collection, Promise, Config ) {
  14. var CKEDITOR = {
  15. /**
  16. * A collection containing all editor instances created.
  17. *
  18. * @readonly
  19. * @property {Collection}
  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( function( editor ) {
  31. * // Manipulate "editor" here.
  32. * } );
  33. *
  34. * @param {String|HTMLElement} element An element selector or a DOM element, which will be the source for the
  35. * created instance.
  36. * @returns {Promise} A promise, which will be fulfilled with the created editor.
  37. */
  38. create: function( element, config ) {
  39. var that = this;
  40. return new Promise( function( 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. var editor = new Editor( element, config );
  49. that.instances.add( editor );
  50. // Remove the editor from `instances` when destroyed.
  51. editor.once( 'destroy', function() {
  52. that.instances.remove( editor );
  53. } );
  54. resolve(
  55. // Initializes the editor, which returns a promise.
  56. editor.init()
  57. .then( function() {
  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. * Gets the full URL path for the specified plugin.
  71. *
  72. * Note that the plugin is not checked to exist. It is a pure path computation.
  73. *
  74. * @param {String} name The plugin name.
  75. * @returns {String} The full URL path of the plugin.
  76. */
  77. getPluginPath: function( name ) {
  78. return this.basePath + 'plugins/' + name + '/';
  79. }
  80. };
  81. return CKEDITOR;
  82. } );