-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathToolsFactory.ts
More file actions
126 lines (111 loc) · 3.16 KB
/
ToolsFactory.ts
File metadata and controls
126 lines (111 loc) · 3.16 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
/* eslint-disable jsdoc/informative-docs */
import type { EditorAPI } from '@editorjs/sdk';
import { ToolType } from '@editorjs/sdk';
import type { ToolConstructable, ToolStaticOptions } from '@editorjs/sdk';
import {
InlineToolFacade,
BlockTuneFacade,
BlockToolFacade
} from '@editorjs/sdk';
import type {
EditorConfig
} from '@editorjs/editorjs';
type ToolConstructor = typeof InlineToolFacade | typeof BlockToolFacade | typeof BlockTuneFacade;
/**
* Full tool registration entry: the constructor class plus its options.
*/
export type ToolSettings = ToolStaticOptions & {
/**
* Tool's constructor
*/
class: ToolConstructable;
};
/**
* Factory to construct classes to work with tools
*/
export class ToolsFactory {
/**
* Tools configuration specified by user
*/
#config: Record<string, ToolSettings>;
/**
* EditorJS API Module
*/
#api: EditorAPI;
/**
* EditorJS configuration
*/
#editorConfig: EditorConfig;
/**
* Map of tool settings
*/
#toolsSettings = new Map<string, ToolSettings>();
/**
* ToolsFactory
* @param config - unified tools config for user's and internal tools
* @param editorConfig - full Editor.js configuration
* @param api - EditorJS module with all Editor methods
*/
constructor(
config: Record<string, ToolSettings>,
editorConfig: EditorConfig,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
api: any
) {
this.#api = api;
this.#config = config;
this.#editorConfig = editorConfig;
}
/**
* Register tools in the factory
* @param tools - tools to register in the factory
*/
public setTools(tools: [ToolConstructable, ToolStaticOptions | undefined][]): void {
tools.forEach(([tool, settings]) => {
this.#toolsSettings.set(tool.name, {
...(settings ?? {}),
class: tool,
});
});
}
/**
* Returns Tool object based on it's type
* @param name - tool name
*/
public get(name: string): InlineToolFacade | BlockToolFacade | BlockTuneFacade {
const toolSettings = this.#toolsSettings.get(name);
if (!toolSettings) {
throw new Error(`Tool ${name} is not registered`);
}
const { class: constructable, ...useToolOptions } = toolSettings;
const Constructor = this.#getConstructor(constructable);
// const isTune = constructable[InternalTuneSettings.IsTune];
return new Constructor({
name,
constructable,
useToolOptions,
api: {},
// api: this.api.getMethodsForTool(name, isTune),
isDefault: name === this.#editorConfig.defaultBlock,
defaultPlaceholder: this.#editorConfig.placeholder,
/**
* @todo implement api.getMethodsForTool
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
}
/**
* Find appropriate Tool object constructor for Tool constructable
* @param constructable - Tools constructable
*/
#getConstructor(constructable: ToolConstructable): ToolConstructor {
switch (constructable.type) {
case ToolType.Inline:
return InlineToolFacade;
case ToolType.Tune:
return BlockTuneFacade;
default:
return BlockToolFacade;
}
}
}