-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Core event bus * Add try catch for getFragments call * Fix build & rename uiContent to ui * Fix lint
- Loading branch information
Showing
18 changed files
with
339 additions
and
66 deletions.
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
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
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 UI - type of the UI content | ||
*/ | ||
export interface BlockAddedCoreEventPayload<UI = 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 ui: UI; | ||
/** | ||
* Index of the added Block | ||
*/ | ||
readonly index: number; | ||
} | ||
|
||
/** | ||
* Class for event that is being fired after the block is added | ||
*/ | ||
export class BlockAddedCoreEvent<UI = unknown> extends CoreEventBase<BlockAddedCoreEventPayload<UI>> { | ||
/** | ||
* BlockAddedCoreEvent constructor function | ||
* @param payload - BlockAdded event payload with tool name, block data, index and UI content | ||
*/ | ||
constructor(payload: BlockAddedCoreEventPayload<UI>) { | ||
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.
20f2ef7
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.
Coverage report for
./packages/model
Test suite run success
389 tests passing in 24 suites.
Report generated by 🧪jest coverage report action from 20f2ef7
20f2ef7
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.
Coverage report for
./packages/collaboration-manager
Test suite run success
6 tests passing in 1 suite.
Report generated by 🧪jest coverage report action from 20f2ef7