forked from ckeditor/ckeditor5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathckeditor.js
128 lines (110 loc) · 3.51 KB
/
ckeditor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
'use strict';
import Editor from './ckeditor5/editor.js';
import Collection from './ckeditor5/utils/collection.js';
import Config from './ckeditor5/utils/config.js';
import CKEditorError from './ckeditor5/utils/ckeditorerror.js';
import isArrayLike from './ckeditor5/utils/lib/lodash/isArrayLike.js';
import clone from './ckeditor5/utils/lib/lodash/clone.js';
import utils from './ckeditor5/utils/utils.js';
/**
* This is the API entry point. The entire CKEditor code runs under this object.
*
* @namespace CKEDITOR
*/
const CKEDITOR = {
/**
* A collection containing all editor instances created.
*
* @readonly
* @member {utils.Collection} CKEDITOR.instances
*/
instances: new Collection(),
/**
* Holds global configuration defaults, which will be used by editor instances when such configurations are not
* available on them directly.
*
* @readonly
* @member {utils.Config} CKEDITOR.config
*/
config: new Config(),
/**
* Creates an editor instance for the provided DOM element.
*
* The creation of editor instances is an asynchronous operation, therefore a promise is returned by this
* method.
*
* CKEDITOR.create( '#content' );
*
* CKEDITOR.create( '#content' ).then( ( editor ) => {
* // Manipulate "editor" here.
* } );
*
* @method CKEDITOR.create
* @param {String|HTMLElement|HTMLCollection|NodeList|Array.<HTMLElement>|Object.<String, HTMLElement>} elements
* One or more elements on which the editor will be initialized. Different creators can handle these
* elements differently, but usually a creator loads the data from the element and either makes
* it editable or hides it and inserts the editor UI next to it.
* @returns {Promise} A promise, which will be fulfilled with the created editor.
*/
create( elements, config ) {
return new Promise( ( resolve ) => {
const editor = new Editor( normalizeElements( elements ), config );
this.instances.add( editor );
// Remove the editor from `instances` when destroyed.
editor.once( 'destroy', () => {
this.instances.remove( editor );
} );
resolve(
// Initializes the editor, which returns a promise.
editor.init()
.then( () => {
// After initialization, return the created editor.
return editor;
} )
);
} );
}
};
export default CKEDITOR;
function normalizeElements( elements ) {
let elementsObject;
// If a query selector has been passed, transform it into a real element.
if ( typeof elements == 'string' ) {
elementsObject = toElementsObject( document.querySelectorAll( elements ) );
}
// Arrays and array-like objects.
else if ( isArrayLike( elements ) ) {
elementsObject = toElementsObject( elements );
}
// Single HTML element.
else if ( elements instanceof HTMLElement ) {
elementsObject = toElementsObject( [ elements ] );
}
// Object.
else {
elementsObject = clone( elements );
}
if ( !Object.keys( elementsObject ).length ) {
throw new CKEditorError( 'ckeditor5-create-no-elements: No elements have been passed to CKEDITOR.create()' );
}
return elementsObject;
}
function toElementsObject( elements ) {
return Array.from( elements ).reduce( ( ret, el ) => {
ret[ getEditorElementName( el ) ] = el;
return ret;
}, {} );
}
function getEditorElementName( element ) {
if ( element.id ) {
return element.id;
}
if ( element.dataset.editable ) {
return element.dataset.editable;
}
return 'editable' + utils.uid();
}