-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathToolsManager.ts
More file actions
220 lines (188 loc) · 5.89 KB
/
ToolsManager.ts
File metadata and controls
220 lines (188 loc) · 5.89 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
import 'reflect-metadata';
import { isFunction, isObject, PromiseQueue } from '@editorjs/helpers';
import { inject, injectable } from 'inversify';
import { TOKENS } from '../tokens.js';
import { type ToolSettings, ToolsFactory } from './ToolsFactory.js';
import type {
EditorConfig
} from '@editorjs/editorjs';
import {
InlineTool,
ToolLoadedCoreEvent,
BlockToolFacade, BlockTuneFacade,
InlineToolFacade,
ToolFacadeClass,
ToolsCollection,
EventBus,
type ToolConstructable,
type ToolStaticOptions
} from '@editorjs/sdk';
/**
* Works with tools
* @todo - validate tools configurations
*/
@injectable()
export default class ToolsManager {
/**
* ToolsFactory instance
*/
#factory: ToolsFactory;
/**
* Processed tools config
*/
#config: Record<string, ToolSettings>;
/**
* EventBus instance to exchange events between components
*/
#eventBus: EventBus;
/**
* Tools available for use
*/
#availableTools = new ToolsCollection();
/**
* Tools loaded but unavailable for use
*/
#unavailableTools = new ToolsCollection();
/**
* Returns available Tools
*/
public get available(): ToolsCollection {
return this.#availableTools;
}
/**
* Returns unavailable Tools
*/
public get unavailable(): ToolsCollection {
return this.#unavailableTools;
}
/**
* Return Tools for the Inline Toolbar
*/
public get inlineTools(): ToolsCollection<InlineToolFacade> {
return this.available.inlineTools;
}
/**
* Return editor block tools
*/
public get blockTools(): ToolsCollection<BlockToolFacade> {
return this.available.blockTools;
}
/**
* Return available Block Tunes
* @returns - object of Inline Tool's classes
*/
public get blockTunes(): ToolsCollection<BlockTuneFacade> {
return this.available.blockTunes;
}
/**
* @param editorConfig - EditorConfig object
* @param editorConfig.tools - Tools configuration passed by user
* @param eventBus - EventBus instance to exchange events between components
*/
constructor(
@inject(TOKENS.EditorConfig) editorConfig: EditorConfig,
eventBus: EventBus
) {
this.#config = this.#prepareConfig(editorConfig.tools ?? {});
this.#eventBus = eventBus;
this.#validateTools();
this.#factory = new ToolsFactory(this.#config, editorConfig, {});
}
/**
* Calls tools prepare method if it exists and adds tools to relevant collection (available or unavailable tools)
* @param tools - tools to prepare and their settings
*/
public async prepareTools(tools: [ToolConstructable, ToolStaticOptions | undefined][]): Promise<void> {
const promiseQueue = new PromiseQueue();
const setToAvailableToolsCollection = (toolName: string, tool: ToolFacadeClass): void => {
this.#availableTools.set(toolName, tool);
this.#eventBus.dispatchEvent(new ToolLoadedCoreEvent({
tool,
}));
};
this.#factory.setTools(tools);
tools.forEach(([toolConstructor]) => {
const toolName = toolConstructor.name;
if (isFunction(toolConstructor.prepare)) {
void promiseQueue.add(async () => {
try {
const tool = this.#factory.get(toolName);
/**
* Merged plugin `config` only (static `options().config` + `use(Tool, options).config`), aligned with `BaseToolFacade.prepare`.
*/
await toolConstructor.prepare!({
toolName,
config: tool.config,
});
if (tool.isInline()) {
/**
* Some Tools validation
*/
const inlineToolRequiredMethods = ['render'];
const notImplementedMethods = inlineToolRequiredMethods.filter(method => tool.create()[method as keyof InlineTool] !== undefined);
if (notImplementedMethods.length > 0) {
/**
* @todo implement logger
*/
console.log(
`Incorrect Inline Tool: ${tool.name}. Some of required methods is not implemented %o`,
'warn',
notImplementedMethods
);
this.#unavailableTools.set(tool.name, tool);
return;
}
}
setToAvailableToolsCollection(toolName, tool);
} catch (e) {
console.error(`Tool ${toolName} failed to prepare`, e);
this.#unavailableTools.set(toolName, this.#factory.get(toolName));
}
});
} else {
setToAvailableToolsCollection(toolName, this.#factory.get(toolName));
}
});
await promiseQueue.completed;
}
/**
* Unify tools config
* @param config - user's tools config
*/
#prepareConfig(config: EditorConfig['tools']): Record<string, ToolSettings> {
const preparedConfig: Record<string, ToolSettings> = {} as Record<string, ToolSettings>;
/**
* Save Tools settings to a map
*/
for (const toolName in config) {
/**
* If Tool is an object not a Tool's class then
* save class and settings separately
*/
if (isObject(config[toolName])) {
preparedConfig[toolName] = config[toolName] as object as ToolSettings;
} else {
preparedConfig[toolName] = { class: config[toolName] as ToolConstructable };
}
}
return preparedConfig;
}
/**
* Validate Tools configuration objects and throw Error for user if it is invalid
*/
#validateTools(): void {
/**
* Check Tools for a class containing
*/
for (const toolName in this.#config) {
if (Object.prototype.hasOwnProperty.call(this.#config, toolName)) {
const tool = this.#config[toolName];
if (!isFunction(tool) && !isFunction((tool).class)) {
throw Error(
`Tool «${toolName}» must be a constructor function or an object with function in the «class» property`
);
}
}
}
}
}