8
0

ckeditor.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 './ckeditor5/editor.js';
  7. import Collection from './ckeditor5/utils/collection.js';
  8. import CKEditorError from './ckeditor5/utils/ckeditorerror.js';
  9. import isArrayLike from './ckeditor5/utils/lib/lodash/isArrayLike.js';
  10. import clone from './ckeditor5/utils/lib/lodash/clone.js';
  11. import uid from './ckeditor5/utils/uid.js';
  12. /**
  13. * This is the API entry point. The entire CKEditor code runs under this object.
  14. *
  15. * @namespace CKEDITOR
  16. */
  17. const CKEDITOR = {
  18. /**
  19. * A collection containing all editor instances created.
  20. *
  21. * @readonly
  22. * @member {utils.Collection} CKEDITOR.instances
  23. */
  24. instances: new Collection(),
  25. /**
  26. * Creates an editor instance for the provided DOM element.
  27. *
  28. * The creation of editor instances is an asynchronous operation, therefore a promise is returned by this
  29. * method.
  30. *
  31. * CKEDITOR.create( '#content' );
  32. *
  33. * CKEDITOR.create( '#content' ).then( ( editor ) => {
  34. * // Manipulate "editor" here.
  35. * } );
  36. *
  37. * @method CKEDITOR.create
  38. * @param {String|HTMLElement|HTMLCollection|NodeList|Array.<HTMLElement>|Object.<String, HTMLElement>} elements
  39. * One or more elements on which the editor will be initialized. Different creators can handle these
  40. * elements differently, but usually a creator loads the data from the element and either makes
  41. * it editable or hides it and inserts the editor UI next to it.
  42. * @returns {Promise} A promise, which will be fulfilled with the created editor.
  43. */
  44. create( elements, config ) {
  45. return new Promise( ( resolve ) => {
  46. const editor = new Editor( normalizeElements( elements ), 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. export default CKEDITOR;
  64. function normalizeElements( elements ) {
  65. let elementsObject;
  66. // If a query selector has been passed, transform it into a real element.
  67. if ( typeof elements == 'string' ) {
  68. elementsObject = toElementsObject( document.querySelectorAll( elements ) );
  69. }
  70. // Arrays and array-like objects.
  71. else if ( isArrayLike( elements ) ) {
  72. elementsObject = toElementsObject( elements );
  73. }
  74. // Single HTML element.
  75. else if ( elements instanceof HTMLElement ) {
  76. elementsObject = toElementsObject( [ elements ] );
  77. }
  78. // Object.
  79. else {
  80. elementsObject = clone( elements );
  81. }
  82. if ( !Object.keys( elementsObject ).length ) {
  83. throw new CKEditorError( 'ckeditor5-create-no-elements: No elements have been passed to CKEDITOR.create()' );
  84. }
  85. return elementsObject;
  86. }
  87. function toElementsObject( elements ) {
  88. return Array.from( elements ).reduce( ( ret, el ) => {
  89. ret[ getEditorElementName( el ) ] = el;
  90. return ret;
  91. }, {} );
  92. }
  93. function getEditorElementName( element ) {
  94. if ( element.id ) {
  95. return element.id;
  96. }
  97. if ( element.dataset.editable ) {
  98. return element.dataset.editable;
  99. }
  100. return 'editable' + uid();
  101. }