forked from codex-team/editor.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.ts
More file actions
417 lines (362 loc) · 11.1 KB
/
tools.ts
File metadata and controls
417 lines (362 loc) · 11.1 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import Paragraph from '../../tools/paragraph/dist/bundle';
import Module from '../__module';
import * as _ from '../utils';
import { SanitizerConfig, ToolConfig, ToolConstructable, ToolSettings } from '../../../types';
import BoldInlineTool from '../inline-tools/inline-tool-bold';
import ItalicInlineTool from '../inline-tools/inline-tool-italic';
import LinkInlineTool from '../inline-tools/inline-tool-link';
import Stub from '../../tools/stub';
import ToolsFactory from '../tools/factory';
import InlineTool from '../tools/inline';
import BlockTool from '../tools/block';
import BlockTune from '../tools/tune';
import MoveDownTune from '../block-tunes/block-tune-move-down';
import DeleteTune from '../block-tunes/block-tune-delete';
import MoveUpTune from '../block-tunes/block-tune-move-up';
import ToolsCollection from '../tools/collection';
/**
* @module Editor.js Tools Submodule
*
* Creates Instances from Plugins and binds external config to the instances
*/
/**
* Modules that works with tools classes
*/
export default class Tools extends Module {
/**
* Name of Stub Tool
* Stub Tool is used to substitute unavailable block Tools and store their data
*
* @type {string}
*/
public stubTool = 'stub';
/**
* Returns available Tools
*/
public get available(): ToolsCollection {
return this.toolsAvailable;
}
/**
* Returns unavailable Tools
*/
public get unavailable(): ToolsCollection {
return this.toolsUnavailable;
}
/**
* Return Tools for the Inline Toolbar
*/
public get inlineTools(): ToolsCollection<InlineTool> {
return this.available.inlineTools;
}
/**
* Return editor block tools
*/
public get blockTools(): ToolsCollection<BlockTool> {
return this.available.blockTools;
}
/**
* Return available Block Tunes
*
* @returns {object} - object of Inline Tool's classes
*/
public get blockTunes(): ToolsCollection<BlockTune> {
return this.available.blockTunes;
}
/**
* Returns default Tool object
*/
public get defaultTool(): BlockTool {
return this.blockTools.get(this.config.defaultBlock);
}
/**
* Tools objects factory
*/
private factory: ToolsFactory;
/**
* Tools` classes available to use
*/
private readonly toolsAvailable: ToolsCollection = new ToolsCollection();
/**
* Tools` classes not available to use because of preparation failure
*/
private readonly toolsUnavailable: ToolsCollection = new ToolsCollection();
/**
* Returns internal tools
*/
public get internal(): ToolsCollection {
return this.available.internalTools;
}
/**
* Creates instances via passed or default configuration
*
* @returns {Promise<void>}
*/
public async prepare(): Promise<void> {
this.validateTools();
/**
* Assign internal tools
*/
this.config.tools = _.deepMerge({}, this.internalTools, this.config.tools);
if (!Object.prototype.hasOwnProperty.call(this.config, 'tools') || Object.keys(this.config.tools).length === 0) {
throw Error('Can\'t start without tools');
}
const config = this.prepareConfig();
this.factory = new ToolsFactory(config, this.config, this.Editor.API);
/**
* getting classes that has prepare method
*/
const sequenceData = this.getListOfPrepareFunctions(config);
/**
* if sequence data contains nothing then resolve current chain and run other module prepare
*/
if (sequenceData.length === 0) {
return Promise.resolve();
}
/**
* to see how it works {@link '../utils.ts#sequence'}
*/
await _.sequence(sequenceData, (data: { toolName: string }) => {
this.toolPrepareMethodSuccess(data);
}, (data: { toolName: string }) => {
this.toolPrepareMethodFallback(data);
});
this.prepareBlockTools();
}
/**
* Return general Sanitizer config for all inline tools
*/
@_.cacheable
public getAllInlineToolsSanitizeConfig(): SanitizerConfig {
const config: SanitizerConfig = {} as SanitizerConfig;
Array.from(this.inlineTools.values())
.forEach(inlineTool => {
Object.assign(config, inlineTool.sanitizeConfig);
});
return config;
}
/**
* Calls each Tool reset method to clean up anything set by Tool
*/
public destroy(): void {
Object.values(this.available).forEach(async tool => {
if (_.isFunction(tool.reset)) {
await tool.reset();
}
});
}
/**
* Returns internal tools
* Includes Bold, Italic, Link and Paragraph
*/
private get internalTools(): { [toolName: string]: ToolConstructable | ToolSettings & { isInternal?: boolean } } {
return {
bold: {
class: BoldInlineTool,
isInternal: true,
},
italic: {
class: ItalicInlineTool,
isInternal: true,
},
link: {
class: LinkInlineTool,
isInternal: true,
},
paragraph: {
class: Paragraph,
inlineToolbar: true,
isInternal: true,
},
stub: {
class: Stub,
isInternal: true,
},
moveUp: {
class: MoveUpTune,
isInternal: true,
},
delete: {
class: DeleteTune,
isInternal: true,
},
moveDown: {
class: MoveDownTune,
isInternal: true,
},
};
}
/**
* Tool prepare method success callback
*
* @param {object} data - append tool to available list
*/
private toolPrepareMethodSuccess(data: { toolName: string }): void {
const tool = this.factory.get(data.toolName);
if (tool.isInline()) {
/**
* Some Tools validation
*/
const inlineToolRequiredMethods = ['render', 'surround', 'checkState'];
const notImplementedMethods = inlineToolRequiredMethods.filter((method) => !tool.create()[method]);
if (notImplementedMethods.length) {
_.log(
`Incorrect Inline Tool: ${tool.name}. Some of required methods is not implemented %o`,
'warn',
notImplementedMethods
);
this.toolsUnavailable.set(tool.name, tool);
return;
}
}
this.toolsAvailable.set(tool.name, tool);
}
/**
* Tool prepare method fail callback
*
* @param {object} data - append tool to unavailable list
*/
private toolPrepareMethodFallback(data: { toolName: string }): void {
this.toolsUnavailable.set(data.toolName, this.factory.get(data.toolName));
}
/**
* Binds prepare function of plugins with user or default config
*
* @returns {Array} list of functions that needs to be fired sequentially
* @param config - tools config
*/
private getListOfPrepareFunctions(config: {[name: string]: ToolSettings}): {
function: (data: { toolName: string; config: ToolConfig }) => void | Promise<void>;
data: { toolName: string; config: ToolConfig };
}[] {
const toolPreparationList: {
function: (data: { toolName: string }) => void | Promise<void>;
data: { toolName: string; config: ToolConfig };
}[] = [];
Object
.entries(config)
.forEach(([toolName, settings]) => {
toolPreparationList.push({
// eslint-disable-next-line @typescript-eslint/no-empty-function
function: _.isFunction(settings.class.prepare) ? settings.class.prepare : (): void => {},
data: {
toolName,
config: settings.config,
},
});
});
return toolPreparationList;
}
/**
* Assign enabled Inline Tools and Block Tunes for Block Tool
*/
private prepareBlockTools(): void {
Array.from(this.blockTools.values()).forEach(tool => {
this.assignInlineToolsToBlockTool(tool);
this.assignBlockTunesToBlockTool(tool);
});
}
/**
* Assign enabled Inline Tools for Block Tool
*
* @param tool - Block Tool
*/
private assignInlineToolsToBlockTool(tool: BlockTool): void {
/**
* If common inlineToolbar property is false no Inline Tools should be assigned
*/
if (this.config.inlineToolbar === false) {
return;
}
/**
* If user pass just 'true' for tool, get common inlineToolbar settings
* - if common settings is an array, use it
* - if common settings is 'true' or not specified, get default order
*/
if (tool.enabledInlineTools === true) {
tool.inlineTools = new ToolsCollection<InlineTool>(
Array.isArray(this.config.inlineToolbar)
? this.config.inlineToolbar.map(name => [name, this.inlineTools.get(name)])
/**
* If common settings is 'true' or not specified (will be set as true at core.ts), get the default order
*/
: Array.from(this.inlineTools.entries())
);
return;
}
/**
* If user pass the list of inline tools for the particular tool, return it.
*/
if (Array.isArray(tool.enabledInlineTools)) {
tool.inlineTools = new ToolsCollection<InlineTool>(
tool.enabledInlineTools.map(name => [name, this.inlineTools.get(name)])
);
}
}
/**
* Assign enabled Block Tunes for Block Tool
*
* @param tool — Block Tool
*/
private assignBlockTunesToBlockTool(tool: BlockTool): void {
if (tool.enabledBlockTunes === false) {
return;
}
if (Array.isArray(tool.enabledBlockTunes)) {
const userTunes = new ToolsCollection<BlockTune>(
tool.enabledBlockTunes.map(name => [name, this.blockTunes.get(name)])
);
tool.tunes = new ToolsCollection<BlockTune>([...userTunes, ...this.blockTunes.internalTools]);
return;
}
if (Array.isArray(this.config.tunes)) {
const userTunes = new ToolsCollection<BlockTune>(
this.config.tunes.map(name => [name, this.blockTunes.get(name)])
);
tool.tunes = new ToolsCollection<BlockTune>([...userTunes, ...this.blockTunes.internalTools]);
return;
}
tool.tunes = this.blockTunes.internalTools;
}
/**
* Validate Tools configuration objects and throw Error for user if it is invalid
*/
private validateTools(): void {
/**
* Check Tools for a class containing
*/
for (const toolName in this.config.tools) {
if (Object.prototype.hasOwnProperty.call(this.config.tools, toolName)) {
if (toolName in this.internalTools) {
return;
}
const tool = this.config.tools[toolName];
if (!_.isFunction(tool) && !_.isFunction((tool as ToolSettings).class)) {
throw Error(
`Tool «${toolName}» must be a constructor function or an object with function in the «class» property`
);
}
}
}
}
/**
* Unify tools config
*/
private prepareConfig(): {[name: string]: ToolSettings} {
const config: {[name: string]: ToolSettings} = {};
/**
* Save Tools settings to a map
*/
for (const toolName in this.config.tools) {
/**
* If Tool is an object not a Tool's class then
* save class and settings separately
*/
if (_.isObject(this.config.tools[toolName])) {
config[toolName] = this.config.tools[toolName] as ToolSettings;
} else {
config[toolName] = { class: this.config.tools[toolName] as ToolConstructable };
}
}
return config;
}
}