-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathplugin.js
292 lines (270 loc) · 9.58 KB
/
plugin.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module core/plugin
*/
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
/**
* The base class for CKEditor plugin classes.
*
* @implements module:core/plugin~PluginInterface
* @mixes module:utils/observablemixin~ObservableMixin
*/
export default class Plugin {
/**
* @inheritDoc
*/
constructor( editor ) {
/**
* The editor instance.
*
* Note that most editors implement the {@link module:core/editor/editorwithui~EditorWithUI} interface in addition
* to the base {@link module:core/editor/editor~Editor} interface. However, editors with an external UI
* (i.e. Bootstrap-based) or a headless editor may not implement the {@link module:core/editor/editorwithui~EditorWithUI}
* interface.
*
* Because of above, to make plugins more universal, it is recommended to split features into:
* - The "editing" part that only uses the {@link module:core/editor/editor~Editor} interface.
* - The "UI" part that uses both the {@link module:core/editor/editor~Editor} interface and
* the {@link module:core/editor/editorwithui~EditorWithUI} interface.
*
* @readonly
* @member {module:core/editor/editor~Editor} #editor
*/
this.editor = editor;
/**
* Flag indicating whether a plugin is enabled or disabled.
* A disabled plugin will not transform text.
*
* Plugin can be simply disabled like that:
*
* // Disable the plugin so that no toolbars are visible.
* editor.plugins.get( 'TextTransformation' ).isEnabled = false;
*
* You can also use {@link #forceDisabled} method.
*
* @observable
* @readonly
* @member {Boolean} #isEnabled
*/
this.set( 'isEnabled', true );
/**
* Holds identifiers for {@link #forceDisabled} mechanism.
*
* @type {Set.<String>}
* @private
*/
this._disableStack = new Set();
}
/**
* Disables the plugin.
*
* Plugin may be disabled by multiple features or algorithms (at once). When disabling a plugin, unique id should be passed
* (e.g. feature name). The same identifier should be used when {@link #clearForceDisabled enabling back} the plugin.
* The plugin becomes enabled only after all features {@link #clearForceDisabled enabled it back}.
*
* Disabling and enabling a plugin:
*
* plugin.isEnabled; // -> true
* plugin.forceDisabled( 'MyFeature' );
* plugin.isEnabled; // -> false
* plugin.clearForceDisabled( 'MyFeature' );
* plugin.isEnabled; // -> true
*
* Plugin disabled by multiple features:
*
* plugin.forceDisabled( 'MyFeature' );
* plugin.forceDisabled( 'OtherFeature' );
* plugin.clearForceDisabled( 'MyFeature' );
* plugin.isEnabled; // -> false
* plugin.clearForceDisabled( 'OtherFeature' );
* plugin.isEnabled; // -> true
*
* Multiple disabling with the same identifier is redundant:
*
* plugin.forceDisabled( 'MyFeature' );
* plugin.forceDisabled( 'MyFeature' );
* plugin.clearForceDisabled( 'MyFeature' );
* plugin.isEnabled; // -> true
*
* **Note:** some plugins or algorithms may have more complex logic when it comes to enabling or disabling certain plugins,
* so the plugin might be still disabled after {@link #clearForceDisabled} was used.
*
* @param {String} id Unique identifier for disabling. Use the same id when {@link #clearForceDisabled enabling back} the plugin.
*/
forceDisabled( id ) {
this._disableStack.add( id );
if ( this._disableStack.size == 1 ) {
this.on( 'set:isEnabled', forceDisable, { priority: 'highest' } );
this.isEnabled = false;
}
}
/**
* Clears forced disable previously set through {@link #forceDisabled}. See {@link #forceDisabled}.
*
* @param {String} id Unique identifier, equal to the one passed in {@link #forceDisabled} call.
*/
clearForceDisabled( id ) {
this._disableStack.delete( id );
if ( this._disableStack.size == 0 ) {
this.off( 'set:isEnabled', forceDisable );
this.isEnabled = true;
}
}
/**
* @inheritDoc
*/
destroy() {
this.stopListening();
}
/**
* @inheritDoc
*/
static get isContextPlugin() {
return false;
}
}
mix( Plugin, ObservableMixin );
/**
* The base interface for CKEditor plugins.
*
* In its minimal form a plugin can be a simple function that accepts {@link module:core/editor/editor~Editor the editor}
* as a parameter:
*
* // A simple plugin that enables a data processor.
* function MyPlugin( editor ) {
* editor.data.processor = new MyDataProcessor();
* }
*
* In most cases however, you will want to inherit from the {@link module:core/plugin~Plugin} class which implements the
* {@link module:utils/observablemixin~ObservableMixin} and is, therefore, more convenient:
*
* class MyPlugin extends Plugin {
* init() {
* // `listenTo()` and `editor` are available thanks to `Plugin`.
* // By using `listenTo()` you will ensure that the listener is removed when
* // the plugin is destroyed.
* this.listenTo( this.editor.data, 'ready', () => {
* // Do something when the data is ready.
* } );
* }
* }
*
* The plugin can also implement methods (e.g. {@link module:core/plugin~PluginInterface#init `init()`} or
* {@link module:core/plugin~PluginInterface#destroy `destroy()`}) which, when present, will be used to properly
* initialize and destroy the plugin.
*
* **Note:** When defined as a plain function, the plugin acts as a constructor and will be
* called in parallel with other plugins' {@link module:core/plugin~PluginInterface#constructor constructors}.
* This means the code of that plugin will be executed **before** {@link module:core/plugin~PluginInterface#init `init()`} and
* {@link module:core/plugin~PluginInterface#afterInit `afterInit()`} methods of other plugins and, for instance,
* you cannot use it to extend other plugins' {@glink framework/guides/architecture/editing-engine#schema schema}
* rules as they are defined later on during the `init()` stage.
*
* @interface PluginInterface
*/
/**
* Creates a new plugin instance. This is the first step of the plugin initialization.
* See also {@link #init} and {@link #afterInit}.
*
* A plugin is always instantiated after its {@link module:core/plugin~PluginInterface.requires dependencies} and the
* {@link #init} and {@link #afterInit} methods are called in the same order.
*
* Usually, you will want to put your plugin's initialization code in the {@link #init} method.
* The constructor can be understood as "before init" and used in special cases, just like
* {@link #afterInit} serves the special "after init" scenarios (e.g.the code which depends on other
* plugins, but which does not {@link module:core/plugin~PluginInterface.requires explicitly require} them).
*
* @method #constructor
* @param {module:core/editor/editor~Editor} editor
*/
/**
* An array of plugins required by this plugin.
*
* To keep the plugin class definition tight it is recommended to define this property as a static getter:
*
* import Image from './image.js';
*
* export default class ImageCaption {
* static get requires() {
* return [ Image ];
* }
* }
*
* @static
* @readonly
* @member {Array.<Function>|undefined} module:core/plugin~PluginInterface.requires
*/
/**
* An optional name of the plugin. If set, the plugin will be available in
* {@link module:core/plugincollection~PluginCollection#get} by its
* name and its constructor. If not, then only by its constructor.
*
* The name should reflect the constructor name.
*
* To keep the plugin class definition tight, it is recommended to define this property as a static getter:
*
* export default class ImageCaption {
* static get pluginName() {
* return 'ImageCaption';
* }
* }
*
* Note: The native `Function.name` property could not be used to keep the plugin name because
* it will be mangled during code minification.
*
* Naming a plugin is necessary to enable removing it through the
* {@link module:core/editor/editorconfig~EditorConfig#removePlugins `config.removePlugins`} option.
*
* @static
* @readonly
* @member {String|undefined} module:core/plugin~PluginInterface.pluginName
*/
/**
* The second stage (after plugin {@link #constructor}) of the plugin initialization.
* Unlike the plugin constructor this method can be asynchronous.
*
* A plugin's `init()` method is called after its {@link module:core/plugin~PluginInterface.requires dependencies} are initialized,
* so in the same order as the constructors of these plugins.
*
* **Note:** This method is optional. A plugin instance does not need to have it defined.
*
* @method #init
* @returns {null|Promise}
*/
/**
* The third (and last) stage of the plugin initialization. See also {@link #constructor} and {@link #init}.
*
* **Note:** This method is optional. A plugin instance does not need to have it defined.
*
* @method #afterInit
* @returns {null|Promise}
*/
/**
* Destroys the plugin.
*
* **Note:** This method is optional. A plugin instance does not need to have it defined.
*
* @method #destroy
* @returns {null|Promise}
*/
/**
* A flag which defines if a plugin is allowed or not allowed to be used directly by a {@link module:core/context~Context}.
*
* @static
* @readonly
* @member {Boolean} module:core/plugin~PluginInterface.isContextPlugin
*/
/**
* An array of loaded plugins.
*
* @typedef {Array.<module:core/plugin~PluginInterface>} module:core/plugin~LoadedPlugins
*/
// Helper function that forces plugin to be disabled.
function forceDisable( evt ) {
evt.return = false;
evt.stop();
}