@bbki.ng/site 1.8.9 → 1.8.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # @bbki.ng/site
2
2
 
3
+ ## 1.8.11
4
+
5
+ ## 1.8.10
6
+
3
7
  ## 1.8.9
4
8
 
5
9
  ## 1.8.8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbki.ng/site",
3
- "version": "1.8.9",
3
+ "version": "1.8.11",
4
4
  "description": "code behind bbki.ng",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,4 +1,4 @@
1
- import React, { ReactElement } from "react";
1
+ import React, { ReactElement, useEffect } from "react";
2
2
  import {
3
3
  ContextMenu,
4
4
  ContextMenuContent,
@@ -11,8 +11,24 @@ import { LoginMenuItem } from "@/components/app_ctx_menu/LoginMenuItem";
11
11
  import { VersionMenuItem } from "@/components/app_ctx_menu/VersionMenuItem";
12
12
  import { ViewSourceMenuItem } from "@/components/app_ctx_menu/ViewSourceMenuItem";
13
13
  import { PluginsMenuItem } from "@/components/plugin/PluginsMenuItem";
14
+ import { PluginManager } from "@/plugin/PluginManager";
15
+ import { PluginEvent } from "@/plugin/PluginEvent";
14
16
 
15
17
  export const AppCtxMenu = (props: { children: ReactElement }) => {
18
+ const [showPluginEntry, setShowPluginEntry] = React.useState(false);
19
+
20
+ const showEntry = () => {
21
+ setShowPluginEntry(true);
22
+ }
23
+
24
+ useEffect(() => {
25
+ PluginManager.addEventListener(PluginEvent.INIT, showEntry);
26
+
27
+ return () => {
28
+ PluginManager.removeEventListener(PluginEvent.INIT, showEntry);
29
+ }
30
+ }, [])
31
+
16
32
  return (
17
33
  <ContextMenu>
18
34
  <ContextMenuTrigger>{props.children}</ContextMenuTrigger>
@@ -20,8 +36,12 @@ export const AppCtxMenu = (props: { children: ReactElement }) => {
20
36
  <LoginMenuItem />
21
37
  <VersionMenuItem />
22
38
  <ViewSourceMenuItem />
23
- <ContextMenuSeparator />
24
- <PluginsMenuItem />
39
+ {showPluginEntry && (
40
+ <>
41
+ <ContextMenuSeparator />
42
+ <PluginsMenuItem />
43
+ </>
44
+ )}
25
45
  </ContextMenuContent>
26
46
  </ContextMenu>
27
47
  );
@@ -3,3 +3,39 @@ export enum PluginEvent {
3
3
  INSTALL = "installed",
4
4
  UNINSTALL = "uninstalled",
5
5
  }
6
+
7
+ export class PluginEventMgr {
8
+ private static listeners: Map<PluginEvent, EventListener[]> = new Map();
9
+
10
+ public static dispatchEvent<T>(evt: PluginEvent, data: T) {
11
+ window.dispatchEvent(new CustomEvent(evt, { detail: data }));
12
+ }
13
+
14
+ public static addEventListener<T>(evt: PluginEvent, cb: (data: T) => void) {
15
+ const listener = (e: any) => {
16
+ cb((e as CustomEvent<T>).detail);
17
+ }
18
+
19
+ if (!PluginEventMgr.listeners.has(evt)) {
20
+ PluginEventMgr.listeners.set(evt, []);
21
+ }
22
+
23
+ PluginEventMgr.listeners.get(evt)?.push(listener);
24
+
25
+ window.addEventListener(evt, listener);
26
+ }
27
+
28
+ public static removeEventListener<T>(evt: PluginEvent, cb: (data: T) => void) {
29
+ const listeners = PluginEventMgr.listeners.get(evt);
30
+ if (!listeners) {
31
+ return;
32
+ }
33
+
34
+ const targetListener = listeners.find((l) => l === cb);
35
+ if (!targetListener) {
36
+ return;
37
+ }
38
+
39
+ window.removeEventListener(evt, targetListener);
40
+ }
41
+ }
@@ -1,7 +1,7 @@
1
1
  import { Plugin, PluginConfig } from "./Plugin";
2
2
  import { Dependencies } from "@/plugin/Dependencies";
3
3
  import { PluginManagerPayload } from "@/plugin/PluginManagerPayload";
4
- import { PluginEvent } from "@/plugin/PluginEvent";
4
+ import { PluginEvent, PluginEventMgr } from "@/plugin/PluginEvent";
5
5
 
6
6
  export class PluginManager {
7
7
  private readonly dependencies: Dependencies;
@@ -48,19 +48,15 @@ export class PluginManager {
48
48
  static instance: PluginManager;
49
49
 
50
50
  static dispatchEvent<T>(evt: PluginEvent, data: T) {
51
- window.dispatchEvent(new CustomEvent(evt, { detail: data }));
51
+ PluginEventMgr.dispatchEvent(evt, data);
52
52
  }
53
53
 
54
54
  static addEventListener<T>(evt: PluginEvent, cb: (data: T) => void) {
55
- window.addEventListener(evt, (e) => {
56
- cb((e as CustomEvent<T>).detail);
57
- });
55
+ PluginEventMgr.addEventListener(evt, cb);
58
56
  }
59
57
 
60
58
  static removeEventListener<T>(evt: PluginEvent, cb: (data: T) => void) {
61
- window.removeEventListener(evt, (e) => {
62
- cb((e as CustomEvent<T>).detail);
63
- });
59
+ PluginEventMgr.removeEventListener(evt, cb);
64
60
  }
65
61
 
66
62
  static async init(dependencies: Dependencies) {