-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Core event bus #89
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
05fab71
Add Core event bus
gohabereg 83b8fbb
Merge branch 'main' of github.com:editor-js/document-model into feat/…
gohabereg 42e1156
Add try catch for getFragments call
gohabereg fcc93db
Fix build & rename uiContent to ui
gohabereg dc21fb3
Fix lint
gohabereg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/core/src/components/EventBus/core-events/BlockAddedCoreEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { BlockToolData } from '@editorjs/editorjs'; | ||
import { CoreEventBase } from './CoreEventBase.js'; | ||
import { CoreEventType } from './CoreEventType.js'; | ||
|
||
/** | ||
* Payload of BlockAddedCoreEvent custom event | ||
* Contains added block data: name, data, index and UI content to be rendered on tha page | ||
* @template UiContent - type of the UI content | ||
*/ | ||
export interface BlockAddedCoreEventPayload<UiContent = unknown> { | ||
/** | ||
* Name of the added Block Tool | ||
*/ | ||
readonly tool: string; | ||
/** | ||
* Added Block data | ||
*/ | ||
readonly data: BlockToolData; | ||
/** | ||
* UI content to be rendered on the page | ||
*/ | ||
readonly uiContent: UiContent; | ||
/** | ||
* Index of the added Block | ||
*/ | ||
readonly index: number; | ||
} | ||
|
||
/** | ||
* Class for event that is being fired after the block is added | ||
*/ | ||
export class BlockAddedCoreEvent<UiContent = unknown> extends CoreEventBase<BlockAddedCoreEventPayload<UiContent>> { | ||
/** | ||
* BlockAddedCoreEvent constructor function | ||
* @param payload - BlockAdded event payload with tool name, block data, index and UI content | ||
*/ | ||
constructor(payload: BlockAddedCoreEventPayload<UiContent>) { | ||
super(CoreEventType.BlockAdded, payload); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/core/src/components/EventBus/core-events/BlockRemovedCoreEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { CoreEventBase } from './CoreEventBase.js'; | ||
import { CoreEventType } from './CoreEventType.js'; | ||
|
||
/** | ||
* Payload of BlockRemovedCoreEvent custom event | ||
*/ | ||
export interface BlockRemovedCoreEventPayload { | ||
/** | ||
* Block Tool name | ||
*/ | ||
readonly tool: string; | ||
/** | ||
* Index of the removed block | ||
*/ | ||
readonly index: number; | ||
} | ||
|
||
/** | ||
* Class for event that is being fired after the block is removed | ||
*/ | ||
export class BlockRemovedCoreEvent extends CoreEventBase<BlockRemovedCoreEventPayload> { | ||
/** | ||
* BlockRemovedCoreEvent constructor function | ||
* @param payload - BlockRemoved event payload with toola name and block index | ||
*/ | ||
constructor(payload: BlockRemovedCoreEventPayload) { | ||
super(CoreEventType.BlockRemoved, payload); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/core/src/components/EventBus/core-events/CoreEventBase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Represents a base class for core events. | ||
* @template Payload - The type of the event payload. | ||
*/ | ||
// eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
export class CoreEventBase<Payload = unknown> extends CustomEvent<Payload> { | ||
/** | ||
* CoreEventBase constructor function | ||
* @param name - type of the core event | ||
* @param payload - payload of the core event, can contain any data | ||
*/ | ||
constructor(name: string, payload: Payload) { | ||
super(`core:${name}`, { | ||
detail: payload, | ||
}); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/core/src/components/EventBus/core-events/CoreEventType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Enumeration of core events | ||
*/ | ||
export enum CoreEventType { | ||
/** | ||
* Event is fired when a block is added to the Editor | ||
*/ | ||
BlockAdded = 'block:added', | ||
/** | ||
* Event is fired when a block is removed from the Editor | ||
*/ | ||
BlockRemoved = 'block:removed', | ||
/** | ||
* Event is fired when a tool is loaded | ||
*/ | ||
ToolLoaded = 'tool:loaded' | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/core/src/components/EventBus/core-events/ToolLoadedCoreEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { ToolFacadeClass } from '../../../tools/facades/index.js'; | ||
import { CoreEventBase } from './CoreEventBase.js'; | ||
import { CoreEventType } from './CoreEventType.js'; | ||
|
||
/** | ||
* Payload of ToolLoadedCoreEvent custom event | ||
* Contains laoded tool facade instance | ||
* @todo replace facade object with API wrapper | ||
*/ | ||
export interface ToolLoadedCoreEventPayload { | ||
/** | ||
* Loaded tool facade instance | ||
*/ | ||
readonly tool: ToolFacadeClass; | ||
} | ||
|
||
/** | ||
* Class for event that is being fired after the tool is loaded | ||
*/ | ||
export class ToolLoadedCoreEvent extends CoreEventBase<ToolLoadedCoreEventPayload> { | ||
/** | ||
* ToolLoadedCoreEvent constructor function | ||
* @param payload - ToolLoaded event payload with loaded tool facade instance | ||
*/ | ||
constructor(payload: ToolLoadedCoreEventPayload) { | ||
super(CoreEventType.ToolLoaded, payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './BlockAddedCoreEvent.js'; | ||
export * from './BlockRemovedCoreEvent.js'; | ||
export * from './ToolLoadedCoreEvent.js'; | ||
export * from './CoreEventType.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import 'reflect-metadata'; | ||
import { Service } from 'typedi'; | ||
|
||
export type Event<Channel extends string = string, Name extends string = string> = `${Channel}:${Name}`; | ||
|
||
export type CoreEvent<Name extends string = string> = Event<'core', Name>; | ||
|
||
/** | ||
* Extension for the EventTarget interface to allow for custom events. | ||
*/ | ||
declare global { | ||
/** | ||
* EventTarget interface extension | ||
*/ | ||
interface EventTarget { | ||
/** | ||
* Adds an event listener for the specified event type | ||
* @param type - a string representing the event type to listen for | ||
* @param callback - the function to call when the event is triggered | ||
* @param options - an options object that specifies characteristics about the event listener | ||
*/ | ||
// eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
addEventListener(type: Event, callback: ((event: CustomEvent) => void) | null, options?: AddEventListenerOptions | boolean): void; | ||
/** | ||
* Removes an event listener for the specified event type | ||
* @param type - a string representing the event type to stop listening for | ||
* @param callback - the event callback to remove | ||
* @param options - an options object that specifies characteristics about the event listener | ||
*/ | ||
// eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
removeEventListener(type: Event, callback: ((event: CustomEvent) => void) | null, options?: EventListenerOptions | boolean): void; | ||
} | ||
} | ||
|
||
/** | ||
* EventBus class to handle events between components | ||
* Extends native EventTarget class | ||
*/ | ||
@Service() | ||
export class EventBus extends EventTarget {} | ||
|
||
export * from './core-events/index.js'; | ||
export * from './ui-events/index.js'; |
17 changes: 17 additions & 0 deletions
17
packages/core/src/components/EventBus/ui-events/UIEventBase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Represents a base class for UI events. | ||
* @template Payload - The type of the event payload. | ||
*/ | ||
// eslint-disable-next-line n/no-unsupported-features/node-builtins | ||
export class UIEventBase<Payload = unknown> extends CustomEvent<Payload> { | ||
/** | ||
* UIEventBase constructor function | ||
* @param name - type of the core event | ||
* @param payload - payload of the core event, can contain any data | ||
*/ | ||
constructor(name: string, payload: Payload) { | ||
super(`ui:${name}`, { | ||
detail: payload, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './UIEventBase.js'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
element
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be not only HTMLElement, basically anything the tool returns