-
Notifications
You must be signed in to change notification settings - Fork 8
/
ckeditorinspector.js
266 lines (227 loc) · 8.22 KB
/
ckeditorinspector.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* global document, window, CKEDITOR_INSPECTOR_VERSION */
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { reducer } from './data/reducer';
import { setEditors } from './data/actions';
import { updateModelState } from './model/data/actions';
import { updateViewState } from './view/data/actions';
import { updateCommandsState } from './commands/data/actions';
import EditorListener from './data/utils';
import InspectorUI from './ui';
import Logger from './logger';
import {
normalizeArguments,
getFirstEditorName
} from './utils';
import './ckeditorinspector.css';
// From changelog -> webpack.
window.CKEDITOR_INSPECTOR_VERSION = CKEDITOR_INSPECTOR_VERSION;
export default class CKEditorInspector {
constructor() {
Logger.warn(
'[CKEditorInspector] Whoops! Looks like you tried to create an instance of the CKEditorInspector class. ' +
'To attach the inspector, use the static CKEditorInspector.attach( editor ) method instead. ' +
'For the latest API, please refer to https://github.com/ckeditor/ckeditor5-inspector/blob/master/README.md. '
);
}
/**
* Attaches the inspector to an editor instance.
*
* ClassicEditor
* .create( ... )
* .then( editor => {
* CKEditorInspector.attach( editor );
* } )
* .catch( error => {
* console.error( error );
* } );
*
* **Note:** You can attach to multiple editors at a time under unique names:
*
* CKEditorInspector.attach( {
* 'header-editor': editor1,
* 'footer-editor': editor2,
* // ...
* } );
*
* **Note:** You can pass global configuration options when attaching:
*
* CKEditorInspector.attach( editor, { option: 'value', ... } );
* CKEditorInspector.attach( {
* 'header-editor': editor1,
* 'footer-editor': editor2
* }, { option: 'value', ... } );
*
* @param {Editor|Object} editorOrEditors If an editor instance is passed, the inspect will attach to the editor
* with an auto–generated name. It is possible to pass an object with `name: instance` pairs to attach to
* multiple editors at a time with unique names.
* @param {CKEditorInspectorConfig} [options] An object of global configuration options controlling the
* behavior of the inspector.
* @returns {Array.<String>} Names of the editors the inspector attached to. Useful when using `CKEditorInspector.detach()`
* with generated editor names.
*/
static attach( ...args ) {
const { editors, options } = normalizeArguments( args );
for ( const editorName in editors ) {
const editorInstance = editors[ editorName ];
Logger.group( '%cAttached the inspector to a CKEditor 5 instance. To learn more, visit https://ckeditor.com/docs/ckeditor5.',
'font-weight: bold;' );
Logger.log( `Editor instance "${ editorName }"`, editorInstance );
Logger.groupEnd();
CKEditorInspector._editors.set( editorName, editorInstance );
editorInstance.on( 'destroy', () => {
CKEditorInspector.detach( editorName );
} );
CKEditorInspector._mount( options );
CKEditorInspector._updateEditorsState();
}
return Object.keys( editors );
}
/**
* Attaches the inspector to all CKEditor instances discovered in DOM.
*
* Editor instances are named `editor-1`, `editor-2`, etc..
*
* **Note:** This method requires CKEditor 12.3.0 or later.
*
* **Note:** You can pass global configuration options when attaching:
*
* CKEditorInspector.attachToAll( { option: 'value', ... } );
*
* @param {CKEditorInspectorConfig} [options] An object of global configuration options controlling the
* behavior of the inspector.
* @returns {Array.<String>} Names of the editors the inspector attached to. Useful when using `CKEditorInspector.detach()`
* with generated editor names.
*/
static attachToAll( options ) {
const domEditables = document.querySelectorAll( '.ck.ck-content.ck-editor__editable' );
const attachedEditorNames = [];
for ( const domEditable of domEditables ) {
const editor = domEditable.ckeditorInstance;
if ( editor && !CKEditorInspector._isAttachedTo( editor ) ) {
attachedEditorNames.push( ...CKEditorInspector.attach( editor, options ) );
}
}
return attachedEditorNames;
}
/**
* Detaches the inspector from an editor instance.
*
* CKEditorInspector.attach( { 'my-editor': editor } );
*
* // The inspector will no longer inspect the "editor".
* CKEditorInspector.detach( 'my-editor' );
*
* @param {String} string Name of the editor to detach.
*/
static detach( name ) {
if ( !CKEditorInspector._wrapper ) {
return;
}
CKEditorInspector._editors.delete( name );
CKEditorInspector._updateEditorsState();
}
/**
* Destroys the entire inspector application and removes it from DOM.
*/
static destroy() {
if ( !CKEditorInspector._wrapper ) {
return;
}
ReactDOM.unmountComponentAtNode( CKEditorInspector._wrapper );
CKEditorInspector._editors.clear();
CKEditorInspector._wrapper.remove();
const state = CKEditorInspector._store.getState();
const currentEditor = state.editors.get( state.currentEditorName );
if ( currentEditor ) {
CKEditorInspector._editorListener.stopListening( currentEditor );
}
CKEditorInspector._editorListener = null;
CKEditorInspector._wrapper = null;
CKEditorInspector._store = null;
}
static _updateEditorsState() {
CKEditorInspector._store.dispatch( setEditors( CKEditorInspector._editors ) );
}
static _mount( options ) {
if ( CKEditorInspector._wrapper ) {
return;
}
const container = CKEditorInspector._wrapper = document.createElement( 'div' );
let previousEditor;
container.className = 'ck-inspector-wrapper';
document.body.appendChild( container );
// Create a listener that will trigger the store action when the model
// is changing or the view is being rendered.
CKEditorInspector._editorListener = new EditorListener( {
onModelChange() {
CKEditorInspector._store.dispatch( updateModelState() );
CKEditorInspector._store.dispatch( updateCommandsState() );
},
onViewRender() {
CKEditorInspector._store.dispatch( updateViewState() );
}
} );
// Create a global Redux store for the entire application. The store is extended by model, view and
// commands reducers. See the reducer() function to learn more.
CKEditorInspector._store = createStore( reducer, {
editors: CKEditorInspector._editors,
currentEditorName: getFirstEditorName( CKEditorInspector._editors ),
ui: {
isCollapsed: options.isCollapsed
}
} );
// Watch for changes of the current editor in the global store, and update the
// EditorListener accordingly. This ensures the EditorListener instance listens
// to events from the current editor only (but not the previous one).
CKEditorInspector._store.subscribe( () => {
const state = CKEditorInspector._store.getState();
const currentEditor = state.editors.get( state.currentEditorName );
// Either going from
// * no editor to a new editor
// * from one editor to another,
// * from one editor to no editor,
if ( previousEditor !== currentEditor ) {
// If there was no editor before, there's nothing to stop listening to.
if ( previousEditor ) {
CKEditorInspector._editorListener.stopListening( previousEditor );
}
// If going from one editor to no editor, there's nothing to start listening to.
if ( currentEditor ) {
CKEditorInspector._editorListener.startListening( currentEditor );
}
previousEditor = currentEditor;
}
} );
ReactDOM.render(
<Provider store={CKEditorInspector._store}>
<InspectorUI />
</Provider>,
container
);
}
static _isAttachedTo( editor ) {
return [ ...CKEditorInspector._editors.values() ].includes( editor );
}
}
CKEditorInspector._editors = new Map();
CKEditorInspector._wrapper = null;
/**
* The configuration options of the inspector.
*
* @interface CKEditorInspectorConfig
*/
/**
* Controls the initial collapsed state of the inspector. Allows attaching to an editor instance without
* expanding the UI.
*
* **Note**: Works when `attach()` is called for the first time only.
*
* @member {Boolean} CKEditorInspectorConfig#isCollapsed
*/