|
1 |
| -import type { BlockToolConstructor, InlineToolsConfig } from '@editorjs/sdk'; |
| 1 | +import 'reflect-metadata'; |
| 2 | +import { deepMerge, isFunction, isObject, PromiseQueue } from '@editorjs/helpers'; |
| 3 | +import { Inject, Service } from 'typedi'; |
| 4 | +import { |
| 5 | + BlockToolFacade, BlockTuneFacade, |
| 6 | + InlineToolFacade, |
| 7 | + ToolsCollection, |
| 8 | + ToolsFactory |
| 9 | +} from './facades/index.js'; |
2 | 10 | import { Paragraph } from './internal/block-tools/paragraph/index.js';
|
3 |
| -import type { EditorConfig } from '@editorjs/editorjs'; |
| 11 | +import type { |
| 12 | + EditorConfig, |
| 13 | + ToolConstructable, |
| 14 | + ToolSettings |
| 15 | +} from '@editorjs/editorjs'; |
4 | 16 | import BoldInlineTool from './internal/inline-tools/bold/index.js';
|
5 | 17 | import ItalicInlineTool from './internal/inline-tools/italic/index.js';
|
| 18 | +import { BlockToolConstructor, InlineTool, InlineToolConstructor } from '@editorjs/sdk'; |
| 19 | +import { UnifiedToolConfig } from '../entities/index.js'; |
6 | 20 |
|
7 | 21 | /**
|
8 | 22 | * Works with tools
|
| 23 | + * @todo - validate tools configurations |
| 24 | + * @todo - merge internal tools |
9 | 25 | */
|
| 26 | +@Service() |
10 | 27 | export default class ToolsManager {
|
11 |
| - #tools: EditorConfig['tools']; |
| 28 | + /** |
| 29 | + * ToolsFactory instance |
| 30 | + */ |
| 31 | + #factory: ToolsFactory; |
| 32 | + |
| 33 | + /** |
| 34 | + * Unified config with internal and internal tools |
| 35 | + */ |
| 36 | + #config: UnifiedToolConfig; |
| 37 | + |
| 38 | + /** |
| 39 | + * Tools available for use |
| 40 | + */ |
| 41 | + #availableTools = new ToolsCollection(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Tools loaded but unavailable for use |
| 45 | + */ |
| 46 | + #unavailableTools = new ToolsCollection(); |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns available Tools |
| 50 | + */ |
| 51 | + public get available(): ToolsCollection { |
| 52 | + return this.#availableTools; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns unavailable Tools |
| 57 | + */ |
| 58 | + public get unavailable(): ToolsCollection { |
| 59 | + return this.#unavailableTools; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Return Tools for the Inline Toolbar |
| 64 | + */ |
| 65 | + public get inlineTools(): ToolsCollection<InlineToolFacade> { |
| 66 | + return this.available.inlineTools; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Return editor block tools |
| 71 | + */ |
| 72 | + public get blockTools(): ToolsCollection<BlockToolFacade> { |
| 73 | + return this.available.blockTools; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Return available Block Tunes |
| 78 | + * @returns - object of Inline Tool's classes |
| 79 | + */ |
| 80 | + public get blockTunes(): ToolsCollection<BlockTuneFacade> { |
| 81 | + return this.available.blockTunes; |
| 82 | + } |
12 | 83 |
|
13 | 84 | /**
|
14 |
| - * @param tools - Tools configuration passed by user |
| 85 | + * Returns internal tools |
15 | 86 | */
|
16 |
| - constructor(tools: EditorConfig['tools']) { |
17 |
| - this.#tools = tools; |
| 87 | + public get internal(): ToolsCollection { |
| 88 | + return this.available.internalTools; |
18 | 89 | }
|
19 | 90 |
|
20 | 91 | /**
|
21 |
| - * Returns a block tool by its name |
22 |
| - * @param toolName - name of a tool to resolve |
| 92 | + * @param editorConfig - EditorConfig object |
| 93 | + * @param editorConfig.tools - Tools configuration passed by user |
23 | 94 | */
|
24 |
| - public resolveBlockTool(toolName: string): BlockToolConstructor { |
25 |
| - switch (toolName) { |
26 |
| - case 'paragraph': |
27 |
| - return Paragraph; |
28 |
| - default: |
29 |
| - throw new Error(`Unknown tool: ${toolName}`); |
| 95 | + constructor(@Inject('EditorConfig') editorConfig: EditorConfig) { |
| 96 | + this.#config = this.#prepareConfig(editorConfig.tools ?? {}); |
| 97 | + |
| 98 | + this.#validateTools(); |
| 99 | + |
| 100 | + this.#factory = new ToolsFactory(this.#config, editorConfig, {}); |
| 101 | + |
| 102 | + void this.prepareTools(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Calls tools prepare method if it exists and adds tools to relevant collection (available or unavailable tools) |
| 107 | + * @returns Promise<void> |
| 108 | + */ |
| 109 | + public async prepareTools(): Promise<void> { |
| 110 | + const promiseQueue = new PromiseQueue(); |
| 111 | + |
| 112 | + Object.entries(this.#config).forEach(([toolName, config]) => { |
| 113 | + if (isFunction(config.class.prepare)) { |
| 114 | + void promiseQueue.add(async () => { |
| 115 | + try { |
| 116 | + /** |
| 117 | + * TypeScript doesn't get type guard here, so non-null assertion is used |
| 118 | + */ |
| 119 | + await config.class.prepare!({ |
| 120 | + toolName: toolName, |
| 121 | + config: config, |
| 122 | + }); |
| 123 | + |
| 124 | + const tool = this.#factory.get(toolName); |
| 125 | + |
| 126 | + if (tool.isInline()) { |
| 127 | + /** |
| 128 | + * Some Tools validation |
| 129 | + */ |
| 130 | + const inlineToolRequiredMethods = ['render']; |
| 131 | + const notImplementedMethods = inlineToolRequiredMethods.filter(method => tool.create()[method as keyof InlineTool] !== undefined); |
| 132 | + |
| 133 | + if (notImplementedMethods.length) { |
| 134 | + /** |
| 135 | + * @todo implement logger |
| 136 | + */ |
| 137 | + console.log( |
| 138 | + `Incorrect Inline Tool: ${tool.name}. Some of required methods is not implemented %o`, |
| 139 | + 'warn', |
| 140 | + notImplementedMethods |
| 141 | + ); |
| 142 | + |
| 143 | + this.#unavailableTools.set(tool.name, tool); |
| 144 | + |
| 145 | + return; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + this.#availableTools.set(toolName, tool); |
| 150 | + } catch (e) { |
| 151 | + console.error(`Tool ${toolName} failed to prepare`, e); |
| 152 | + |
| 153 | + this.#unavailableTools.set(toolName, this.#factory.get(toolName)); |
| 154 | + } |
| 155 | + }); |
| 156 | + } else { |
| 157 | + this.#availableTools.set(toolName, this.#factory.get(toolName)); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + await promiseQueue.completed; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Unify tools config |
| 166 | + * @param config - user's tools config |
| 167 | + */ |
| 168 | + #prepareConfig(config: EditorConfig['tools']): UnifiedToolConfig { |
| 169 | + const unifiedConfig: UnifiedToolConfig = {} as UnifiedToolConfig; |
| 170 | + |
| 171 | + /** |
| 172 | + * Save Tools settings to a map |
| 173 | + */ |
| 174 | + for (const toolName in config) { |
| 175 | + /** |
| 176 | + * If Tool is an object not a Tool's class then |
| 177 | + * save class and settings separately |
| 178 | + */ |
| 179 | + if (isObject(config)) { |
| 180 | + unifiedConfig[toolName] = config[toolName] as UnifiedToolConfig[string]; |
| 181 | + } else { |
| 182 | + unifiedConfig[toolName] = { class: config[toolName] as ToolConstructable }; |
| 183 | + } |
30 | 184 | }
|
| 185 | + |
| 186 | + deepMerge(unifiedConfig, this.#internalTools); |
| 187 | + |
| 188 | + return unifiedConfig; |
31 | 189 | }
|
32 | 190 |
|
33 | 191 | /**
|
34 |
| - * Returns inline tools got from the EditorConfig tools |
| 192 | + * Validate Tools configuration objects and throw Error for user if it is invalid |
35 | 193 | */
|
36 |
| - public getInlineTools(): InlineToolsConfig { |
| 194 | + #validateTools(): void { |
| 195 | + /** |
| 196 | + * Check Tools for a class containing |
| 197 | + */ |
| 198 | + for (const toolName in this.#config) { |
| 199 | + if (Object.prototype.hasOwnProperty.call(this.#config, toolName)) { |
| 200 | + // if (toolName in this.internalTools) { |
| 201 | + // return; |
| 202 | + // } |
| 203 | + |
| 204 | + const tool = this.#config[toolName]; |
| 205 | + |
| 206 | + if (!isFunction(tool) && !isFunction((tool as ToolSettings).class)) { |
| 207 | + throw Error( |
| 208 | + `Tool «${toolName}» must be a constructor function or an object with function in the «class» property` |
| 209 | + ); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Returns internal tools |
| 217 | + * Includes Bold, Italic, Link and Paragraph |
| 218 | + */ |
| 219 | + get #internalTools(): UnifiedToolConfig { |
37 | 220 | return {
|
38 |
| - bold: BoldInlineTool, |
39 |
| - italic: ItalicInlineTool, |
| 221 | + paragraph: { |
| 222 | + /** |
| 223 | + * @todo solve problems with types |
| 224 | + */ |
| 225 | + class: Paragraph as unknown as BlockToolConstructor, |
| 226 | + inlineToolbar: true, |
| 227 | + isInternal: true, |
| 228 | + }, |
| 229 | + bold: { |
| 230 | + class: BoldInlineTool as unknown as InlineToolConstructor, |
| 231 | + isInternal: true, |
| 232 | + }, |
| 233 | + italic: { |
| 234 | + class: ItalicInlineTool as unknown as InlineToolConstructor, |
| 235 | + isInternal: true, |
| 236 | + }, |
40 | 237 | };
|
41 |
| - }; |
| 238 | + } |
42 | 239 | }
|
0 commit comments