Skip to content

Commit 3e16a1b

Browse files
committed
feat(playground): register Header tool
Header wasn't wired into the playground, so there was no way to see its config-driven toolbox running in the actual editor rather than only in its own unit tests. Registered with all six levels enabled via config.levels. Getting it to actually work surfaced two bugs: BlockManager.insert flattened a block's data onto its top-level properties instead of nesting it under data, dropping every tool's data on insert; and ToolboxUI.addTool only added one toolbox entry for a tool with several, instead of iterating all of them.
1 parent a818765 commit 3e16a1b

8 files changed

Lines changed: 53 additions & 21 deletions

File tree

packages/core/src/components/BlockManager.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ describe('BlocksManager (unit, mocked deps)', () => {
142142
);
143143
});
144144

145+
it('should nest data under a data property instead of flattening it onto the block', () => {
146+
blocksManager.insert({
147+
type: 'header',
148+
data: { level: 2 }
149+
});
150+
151+
expect(model.addBlock).toHaveBeenCalledWith(
152+
USER_ID,
153+
expect.objectContaining({
154+
name: 'header',
155+
data: { level: 2 }
156+
}),
157+
BLOCKS_COUNT
158+
);
159+
});
160+
145161
it('should use model.length as insertion/removal index when replace is true and index is omitted', () => {
146162
blocksManager.insert({
147163
replace: true

packages/core/src/components/BlockManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class BlocksManager {
144144
}
145145

146146
this.#model.addBlock(userId, {
147-
...data,
147+
data,
148148
id,
149149
name: type,
150150
}, newIndex);

packages/playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@editorjs/collaboration-manager": "workspace:^",
2020
"@editorjs/core": "workspace:^",
2121
"@editorjs/dom-adapters": "workspace:^",
22+
"@editorjs/header": "workspace:^",
2223
"@editorjs/model": "workspace:^",
2324
"@editorjs/sdk": "workspace:^",
2425
"@editorjs/ui": "workspace:*",

packages/playground/src/App.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Core from '@editorjs/core';
55
import { ref, onMounted } from 'vue';
66
import { Node } from './components';
77
import { EditorjsUI, BlocksUI, InlineToolbarUI, ToolboxUI, ToolbarUI } from '@editorjs/ui';
8+
import { Header } from '@editorjs/header';
89
/**
910
* Editor document for visualizing
1011
*/
@@ -68,6 +69,7 @@ onMounted(() => {
6869
.use(InlineToolbarUI)
6970
.use(ToolbarUI)
7071
.use(ToolboxUI)
72+
.use(Header, { config: { levels: [1, 2, 3, 4, 5, 6] } })
7173
.initialize();
7274
});
7375

packages/playground/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
},
4545
{
4646
"path": "../ui/tsconfig.json"
47+
},
48+
{
49+
"path": "../tools/header/tsconfig.json"
4750
}
4851
]
4952
}

packages/playground/vite.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export default defineConfig({
99
'@': path.resolve(__dirname, './src'),
1010
},
1111
},
12+
server: {
13+
fs: {
14+
allow: [path.resolve(__dirname, '../..')],
15+
},
16+
},
1217
optimizeDeps: {
1318
exclude: [
1419
'@editorjs/ui',

packages/ui/src/Toolbox/Toolbox.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
import { PopoverDesktop, PopoverEvent } from '@editorjs/ui-kit';
1515
import type { BlockSelectedUIEvent } from '../Blocks/events/index.js';
1616
import { ToolboxRenderedUIEvent, ToolboxClosedUIEvent, ToolboxOpenedUIEvent } from './events/index.js';
17-
import type { ToolboxOptionsEntry } from './ToolboxConfigEntry.js';
1817

1918
/**
2019
* UI module responsible for rendering the toolbox
@@ -136,23 +135,29 @@ export class ToolboxUI implements EditorjsPlugin {
136135
* @param tool - Block tool to add to the toolbox
137136
*/
138137
public addTool(tool: BlockToolFacade): void {
139-
const toolbox = (tool.options.toolbox ?? {}) as ToolboxOptionsEntry;
140-
141-
this.#popover.addItem(
142-
{
143-
title: tool.name,
144-
...toolbox,
145-
closeOnActivate: true,
146-
onActivate: () => {
147-
void this.#api.blocks.insert({
148-
type: tool.name,
149-
data: toolbox.data ?? {},
150-
index: this.#selectedBlockIndex === -1 ? undefined : this.#selectedBlockIndex + 1,
151-
focus: true,
152-
});
153-
},
154-
}
155-
);
138+
const entries = tool.toolbox;
139+
140+
if (entries === undefined) {
141+
return;
142+
}
143+
144+
for (const entry of entries) {
145+
this.#popover.addItem(
146+
{
147+
title: tool.name,
148+
...entry,
149+
closeOnActivate: true,
150+
onActivate: () => {
151+
void this.#api.blocks.insert({
152+
type: tool.name,
153+
data: entry.data ?? {},
154+
index: this.#selectedBlockIndex === -1 ? undefined : this.#selectedBlockIndex + 1,
155+
focus: true,
156+
});
157+
},
158+
}
159+
);
160+
}
156161
}
157162

158163
/**

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,7 @@ __metadata:
23682368
"@editorjs/collaboration-manager": "workspace:^"
23692369
"@editorjs/core": "workspace:^"
23702370
"@editorjs/dom-adapters": "workspace:^"
2371+
"@editorjs/header": "workspace:^"
23712372
"@editorjs/model": "workspace:^"
23722373
"@editorjs/sdk": "workspace:^"
23732374
"@editorjs/ui": "workspace:*"
@@ -2440,14 +2441,13 @@ __metadata:
24402441
languageName: node
24412442
linkType: hard
24422443

2443-
"@editorjs/header@workspace:packages/tools/header":
2444+
"@editorjs/header@workspace:^, @editorjs/header@workspace:packages/tools/header":
24442445
version: 0.0.0-use.local
24452446
resolution: "@editorjs/header@workspace:packages/tools/header"
24462447
dependencies:
24472448
"@codexteam/icons": "npm:^0.3.3"
24482449
"@editorjs/dom-adapters": "workspace:^"
24492450
"@editorjs/editorjs": "npm:^2.30.8"
2450-
"@editorjs/model": "workspace:^"
24512451
"@editorjs/sdk": "workspace:^"
24522452
"@jest/globals": "npm:^29.7.0"
24532453
"@types/jest": "npm:^29.5.1"

0 commit comments

Comments
 (0)