Skip to content

Commit 270e323

Browse files
authored
chore(ui,core,sdk,dom-adapters): dom-adapters depends on event bus (#104)
* dom-adapters depents on event bus * fix eslint * lint
1 parent 8899cd1 commit 270e323

File tree

26 files changed

+174
-87
lines changed

26 files changed

+174
-87
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
8-
"build": "tsc --build tsconfig.build.json",
8+
"build": "yarn clear && tsc --build tsconfig.build.json",
99
"dev": "yarn build --watch",
1010
"lint": "eslint ./src",
1111
"lint:ci": "yarn lint --max-warnings 0",

packages/core/src/components/BlockManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { BlockToolAdapter, CaretAdapter, FormattingAdapter } from '@editorjs/dom
55
import ToolsManager from '../tools/ToolsManager.js';
66
import { BlockAPI, BlockToolData } from '@editorjs/editorjs';
77
import { CoreConfigValidated } from '../entities/Config.js';
8-
import { BlockAddedCoreEvent, BlockRemovedCoreEvent, EventBus } from './EventBus/index.js';
9-
8+
import { BlockAddedCoreEvent, BlockRemovedCoreEvent } from './EventBus/index.js';
9+
import { EventBus } from '@editorjs/sdk';
1010
/**
1111
* Parameters for the BlocksManager.insert() method
1212
*/
@@ -164,6 +164,7 @@ export class BlocksManager {
164164
const blockToolAdapter = new BlockToolAdapter(
165165
this.#config,
166166
this.#model,
167+
this.#eventBus,
167168
this.#caretAdapter,
168169
index.blockIndex,
169170
this.#formattingAdapter,
Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,6 @@
1-
import 'reflect-metadata';
2-
import { Service } from 'typedi';
3-
4-
export type Event<Channel extends string = string, Name extends string = string> = `${Channel}:${Name}`;
1+
import type { Event } from '@editorjs/sdk';
52

63
export type CoreEvent<Name extends string = string> = Event<'core', Name>;
74

8-
/**
9-
* Extension for the EventTarget interface to allow for custom events.
10-
*/
11-
declare global {
12-
/**
13-
* EventTarget interface extension
14-
*/
15-
interface EventTarget {
16-
/**
17-
* Adds an event listener for the specified event type
18-
* @param type - a string representing the event type to listen for
19-
* @param callback - the function to call when the event is triggered
20-
* @param options - an options object that specifies characteristics about the event listener
21-
*/
22-
// eslint-disable-next-line n/no-unsupported-features/node-builtins
23-
addEventListener(type: Event, callback: ((event: CustomEvent) => void) | null, options?: AddEventListenerOptions | boolean): void;
24-
/**
25-
* Removes an event listener for the specified event type
26-
* @param type - a string representing the event type to stop listening for
27-
* @param callback - the event callback to remove
28-
* @param options - an options object that specifies characteristics about the event listener
29-
*/
30-
// eslint-disable-next-line n/no-unsupported-features/node-builtins
31-
removeEventListener(type: Event, callback: ((event: CustomEvent) => void) | null, options?: EventListenerOptions | boolean): void;
32-
}
33-
}
34-
35-
/**
36-
* EventBus class to handle events between components
37-
* Extends native EventTarget class
38-
*/
39-
@Service()
40-
export class EventBus extends EventTarget {}
41-
425
export * from './core-events/index.js';
436
export * from './ui-events/index.js';

packages/core/src/components/SelectionManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type { CaretManagerEvents, InlineFragment, InlineToolName } from '@editor
44
import { CaretManagerCaretUpdatedEvent, Index, EditorJSModel, createInlineToolData, createInlineToolName } from '@editorjs/model';
55
import { EventType } from '@editorjs/model';
66
import { Service } from 'typedi';
7-
import { CoreEventType, EventBus, ToolLoadedCoreEvent } from './EventBus/index.js';
7+
import { CoreEventType, ToolLoadedCoreEvent } from './EventBus/index.js';
8+
import { EventBus } from '@editorjs/sdk';
89
import { SelectionChangedCoreEvent } from './EventBus/core-events/SelectionChangedCoreEvent.js';
910
import { InlineTool, InlineToolFormatData } from '@editorjs/sdk';
1011

packages/core/src/entities/EditorjsPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EventBus } from '../components/EventBus/index.js';
1+
import { EventBus } from '@editorjs/sdk';
22
import type { CoreConfigValidated } from './Config.js';
33
import type { EditorAPI } from '../api/index.js';
44

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './Config.js';
22
export * from './UnifiedToolConfig.js';
33
export * from './EditorjsPlugin.js';
4-
export * from './Ui.js';

packages/core/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { CollaborationManager } from '@editorjs/collaboration-manager';
22
import { EditorJSModel, EventType } from '@editorjs/model';
33
import type { ContainerInstance } from 'typedi';
44
import { Container } from 'typedi';
5-
import { CoreEventType, EventBus } from './components/EventBus/index.js';
5+
import { CoreEventType } from './components/EventBus/index.js';
6+
import { EventBus, UiComponentType } from '@editorjs/sdk';
67
import { composeDataFromVersion2 } from './utils/composeDataFromVersion2.js';
78
import ToolsManager from './tools/ToolsManager.js';
89
import { CaretAdapter, FormattingAdapter } from '@editorjs/dom-adapters';
@@ -12,7 +13,6 @@ import { BlocksManager } from './components/BlockManager.js';
1213
import { SelectionManager } from './components/SelectionManager.js';
1314
import type { EditorjsPluginConstructor } from './entities/EditorjsPlugin.js';
1415
import { EditorAPI } from './api/index.js';
15-
import { UiComponentType } from './entities/Ui.js';
1616
import { generateId } from './utils/uid.js';
1717

1818
/**
@@ -79,6 +79,9 @@ export default class Core {
7979

8080
this.#iocContainer.set('EditorConfig', this.#config);
8181

82+
const eventBus = new EventBus();
83+
this.#iocContainer.set(EventBus, eventBus);
84+
8285
this.#model = new EditorJSModel();
8386
this.#iocContainer.set(EditorJSModel, this.#model);
8487

@@ -103,8 +106,6 @@ export default class Core {
103106
});
104107
}
105108

106-
const eventBus = this.#iocContainer.get(EventBus);
107-
108109
eventBus.addEventListener(`core:${CoreEventType.Undo}`, () => {
109110
this.#collaborationManager.undo();
110111
});

packages/core/src/tools/ToolsManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import type { UnifiedToolConfig } from '../entities/index.js';
2020
import BoldInlineTool from './internal/inline-tools/bold/index.js';
2121
import ItalicInlineTool from './internal/inline-tools/italic/index.js';
2222
import LinkInlineTool from './internal/inline-tools/link/index.js';
23-
import { EventBus, ToolLoadedCoreEvent } from '../components/EventBus/index.js';
23+
import { ToolLoadedCoreEvent } from '../components/EventBus/index.js';
24+
import { EventBus } from '@editorjs/sdk';
2425

2526
/**
2627
* Works with tools

packages/dom-adapters/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"types": "dist/index.d.ts",
77
"type": "module",
88
"scripts": {
9-
"build": "tsc --build tsconfig.build.json",
9+
"build": "yarn clear && tsc --build tsconfig.build.json",
1010
"dev": "yarn build --watch",
1111
"lint": "eslint ./src",
1212
"lint:ci": "yarn lint --max-warnings 0",

packages/dom-adapters/src/BlockToolAdapter/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { InputType } from './types/InputType.js';
2323
import type { BlockToolAdapter as BlockToolAdapterInterface, CoreConfig } from '@editorjs/sdk';
2424
import type { FormattingAdapter } from '../FormattingAdapter/index.js';
25+
import type { EventBus } from '@editorjs/sdk';
2526

2627
/**
2728
* BlockToolAdapter is using inside Block tools to connect browser DOM elements to the model
@@ -64,12 +65,13 @@ export class BlockToolAdapter implements BlockToolAdapterInterface {
6465
*
6566
* @param config - Editor's config
6667
* @param model - EditorJSModel instance
68+
* @param eventBus - Editor EventBus instance
6769
* @param caretAdapter - CaretAdapter instance
6870
* @param blockIndex - index of the block that this adapter is connected to
6971
* @param formattingAdapter - needed to render formatted text
7072
* @param toolName - tool name of the block
7173
*/
72-
constructor(config: CoreConfig, model: EditorJSModel, caretAdapter: CaretAdapter, blockIndex: number, formattingAdapter: FormattingAdapter, toolName: string) {
74+
constructor(config: CoreConfig, model: EditorJSModel, eventBus: EventBus, caretAdapter: CaretAdapter, blockIndex: number, formattingAdapter: FormattingAdapter, toolName: string) {
7375
this.#config = config;
7476
this.#model = model;
7577
this.#blockIndex = blockIndex;

0 commit comments

Comments
 (0)