-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathToolbox.ts
More file actions
102 lines (85 loc) · 2.19 KB
/
Toolbox.ts
File metadata and controls
102 lines (85 loc) · 2.19 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
import { make } from '@editorjs/dom';
import type {
BlockToolFacade,
EditorjsPlugin,
EditorjsPluginParams,
EventBus,
EditorAPI,
ToolLoadedCoreEvent
} from '@editorjs/sdk';
import {
CoreEventType,
UiComponentType
} from '@editorjs/sdk';
import { ToolboxRenderedUIEvent } from './ToolboxRenderedUIEvent.js';
/**
* UI module responsible for rendering the toolbox
* - renders tool buttons in the toolbox
* - listens to the click event on the tool buttons to insert blocks
*/
export class ToolboxUI implements EditorjsPlugin {
/**
* Plugin type
*/
public static readonly type = UiComponentType.Toolbox;
/**
* EditorAPI instance to insert blocks
*/
#api: EditorAPI;
/**
* EventBus instance to exchange events between components
*/
#eventBus: EventBus;
/**
* Object with Toolbox HTML nodes
*/
#nodes: Record<string, HTMLElement> = {};
/**
* ToolboxUI class constructor
* @param params - Plugin parameters
*/
constructor({
api,
eventBus,
}: EditorjsPluginParams) {
this.#api = api;
this.#eventBus = eventBus;
this.#render();
this.#eventBus.addEventListener(`core:${CoreEventType.ToolLoaded}`, (event: ToolLoadedCoreEvent) => {
const { tool } = event.detail;
if (tool?.isBlock?.() === true) {
this.addTool(tool);
}
});
}
/**
* Cleanup when plugin is destroyed
*/
public destroy(): void {
this.#nodes.holder?.remove();
}
/**
* Renders Toolbox UI and dispatches an event
*/
#render(): void {
this.#nodes.holder = make('div');
this.#nodes.holder.style.display = 'flex';
this.#nodes.holder.style.marginBottom = '10px';
this.#eventBus.dispatchEvent(new ToolboxRenderedUIEvent({
toolbox: this.#nodes.holder,
}));
}
/**
* Renders tool button in the toolbox
* @param tool - Block tool to add to the toolbox
*/
public addTool(tool: BlockToolFacade): void {
const toolButton = make('button');
toolButton.textContent = tool.name;
toolButton.addEventListener('click', () => {
void this.#api.blocks.insert(tool.name);
});
this.#nodes.holder.appendChild(toolButton);
}
}
export * from './ToolboxRenderedUIEvent.js';