-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
584e085
commit c8c6f0c
Showing
15 changed files
with
121 additions
and
85 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
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
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
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
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
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
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ import type { IBlockDefinition } from "@pkg/block/basic"; | |
import type { Editor } from "@pkg/view/editor"; | ||
import type { SpanStyle } from "./spanRegistry"; | ||
import type { EmbedDefinition } from "./embedRegistry"; | ||
import { Subject } from "rxjs"; | ||
|
||
export type AfterFn = () => void; | ||
|
||
|
@@ -36,12 +37,24 @@ export interface IPlugin { | |
/** | ||
* Will be triggered when the editor is initialized. | ||
*/ | ||
onInitialized?(editor: Editor): void; | ||
onInitialized?(context: PluginContext): void; | ||
|
||
onDispose?(context: PluginContext): void; | ||
} | ||
|
||
export class PluginContext { | ||
dispose$ = new Subject<void>(); | ||
constructor(public editor: Editor) {} | ||
|
||
dispose() { | ||
this.dispose$.next(); | ||
} | ||
} | ||
|
||
export class PluginRegistry { | ||
#hook: HookMethods = Object.create(null); | ||
#markedPlugin: Set<string> = new Set(); | ||
plugins: Map<string, IPlugin> = new Map(); | ||
private contexts: Map<string, PluginContext> = new Map(); | ||
|
||
constructor(plugins?: IPlugin[]) { | ||
for (const name of allowHookName) { | ||
|
@@ -55,10 +68,10 @@ export class PluginRegistry { | |
|
||
register(plugin: IPlugin) { | ||
const pluginName = plugin.name; | ||
if (this.#markedPlugin.has(pluginName)) { | ||
if (this.plugins.has(pluginName)) { | ||
throw new Error(`duplicated plugin: ${pluginName}`); | ||
} | ||
this.#markedPlugin.add(pluginName); | ||
this.plugins.set(pluginName, plugin); | ||
|
||
for (const name of allowHookName) { | ||
// @ts-ignore | ||
Check warning on line 77 in packages/blocky-core/src/registry/pluginRegistry.ts GitHub Actions / build (18.x)
|
||
|
@@ -68,18 +81,21 @@ export class PluginRegistry { | |
} | ||
} | ||
|
||
private genEmit = (hookName: string) => | ||
((...args: any[]) => { | ||
const plugins = this.#hook[hookName]!; | ||
for (const plugin of plugins) { | ||
try { | ||
// @ts-ignore | ||
plugin[hookName]!(...args); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
}) as any; | ||
unload(name: string) { | ||
this.plugins.delete(name); | ||
|
||
emitInitPlugins: (editor: Editor) => void = this.genEmit(hookOnInitialized); | ||
const context = this.contexts.get(name); | ||
if (context) { | ||
context.dispose(); | ||
this.contexts.delete(name); | ||
} | ||
} | ||
|
||
initAllPlugins(editor: Editor) { | ||
for (const plugin of this.plugins.values()) { | ||
const context = new PluginContext(editor); | ||
this.contexts.set(plugin.name, context); | ||
plugin.onInitialized?.(context); | ||
} | ||
} | ||
} |
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
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
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
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
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
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.
c8c6f0c
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.
Successfully deployed to the following URLs:
blocky-editor – ./
blocky-editor.vercel.app
blocky-editor-git-master-vincentdchan.vercel.app
blocky-editor-vincentdchan.vercel.app
www.blocky-editor.dev
blocky-editor.dev