@bbki.ng/site 5.4.55 → 5.5.0

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,16 @@
1
1
  # @bbki.ng/site
2
2
 
3
+ ## 5.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 2caf775: add blog plugin
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [2caf775]
12
+ - @bbki.ng/ui@0.2.0
13
+
3
14
  ## 5.4.55
4
15
 
5
16
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbki.ng/site",
3
- "version": "5.4.55",
3
+ "version": "5.5.0",
4
4
  "description": "code behind bbki.ng",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -14,7 +14,7 @@
14
14
  "react-dom": "^18.0.0",
15
15
  "react-router-dom": "6",
16
16
  "swr": "^2.2.5",
17
- "@bbki.ng/ui": "0.1.29"
17
+ "@bbki.ng/ui": "0.2.0"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@eslint/compat": "^1.0.0",
package/src/blog/app.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useContext } from 'react';
1
+ import React, { useContext, useEffect } from 'react';
2
2
  import { Outlet, Route, Routes } from 'react-router-dom';
3
3
  import { Cover, Streaming } from './pages';
4
4
  import { Nav, NotFound, Page, Grid, ErrorBoundary, Container } from '@bbki.ng/ui';
@@ -12,6 +12,8 @@ import { GlobalLoadingContext } from '@/context/global_loading_state_provider';
12
12
  import { BotRedirect } from '@/pages/bot';
13
13
  import { BBContext } from '@/context/bbcontext';
14
14
  import { useDynamicLogo } from './hooks/use_dynamic_logo';
15
+ import { Slot } from '../core/components/SlotComp';
16
+ import { pluginManager } from '#/core/pluginManager';
15
17
 
16
18
  const Layout = () => {
17
19
  const paths = usePaths();
@@ -32,7 +34,18 @@ const Layout = () => {
32
34
  />
33
35
  }
34
36
  main={
35
- <Grid leftAside={<div />} rightAside={<div />}>
37
+ <Grid
38
+ leftAside={
39
+ <div className="py-32 px-6">
40
+ <Slot name="leftCol" data={paths} />
41
+ </div>
42
+ }
43
+ rightAside={
44
+ <div className="py-32 px-6">
45
+ <Slot name="rightCol" data={paths} />
46
+ </div>
47
+ }
48
+ >
36
49
  <Container className="py-32">
37
50
  <ErrorBoundary>
38
51
  <Outlet />
@@ -45,6 +58,14 @@ const Layout = () => {
45
58
  };
46
59
 
47
60
  export const App = () => {
61
+ // useEffect(() => {
62
+ // pluginManager.loadPlugin('test');
63
+
64
+ // return () => {
65
+ // pluginManager.disablePlugin('test');
66
+ // };
67
+ // }, []);
68
+
48
69
  return (
49
70
  <SWR>
50
71
  <BBContext>
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+ import { SlotName } from 'src/types/slots';
3
+ import { useSlotComp } from '../hooks/useSlotComp';
4
+
5
+ export interface ISlotProps {
6
+ name: SlotName;
7
+ data?: any;
8
+ }
9
+
10
+ export const Slot: React.FC<ISlotProps> = ({ name, data }) => {
11
+ const components = useSlotComp(name);
12
+
13
+ return (
14
+ <>
15
+ {components.map((Component, index) => (
16
+ <Component key={index} data={data} />
17
+ ))}
18
+ </>
19
+ );
20
+ };
@@ -0,0 +1,20 @@
1
+ import { SlotName } from 'src/types/slots';
2
+ import { useEffect, useState } from 'react';
3
+ import { registry } from '../registry';
4
+
5
+ export const useSlotComp = (slotName: SlotName) => {
6
+ const [components, setComponents] = useState<React.ComponentType<any>[]>([]);
7
+
8
+ useEffect(() => {
9
+ const unsubscribe = registry.subscribe(() => {
10
+ const comps = registry.getComponents(slotName);
11
+ setComponents(comps);
12
+ });
13
+
14
+ return () => {
15
+ unsubscribe();
16
+ };
17
+ }, []);
18
+
19
+ return components;
20
+ };
@@ -0,0 +1,80 @@
1
+ import { IHostContext } from '#/types/hostApi';
2
+ import { registry } from './registry';
3
+ import type { SlotName, HookPoint } from '#/types/slots';
4
+ import { IPlugin } from '#/types/plugin';
5
+
6
+ class PluginManager {
7
+ private activePlugins: Map<string, IPlugin> = new Map();
8
+
9
+ private createHostContext(): IHostContext {
10
+ return {
11
+ api: {
12
+ registerMiddleware: (point: HookPoint, fn: Function, pluginId: string, weight = 0) => {
13
+ registry.registerMiddleware(point, fn, pluginId, weight);
14
+ },
15
+ registerSlot: (
16
+ slotName: SlotName,
17
+ component: React.ComponentType<any>,
18
+ pluginId: string,
19
+ weight = 0
20
+ ) => {
21
+ registry.registerComponent(slotName, component, pluginId, weight);
22
+ },
23
+ },
24
+ };
25
+ }
26
+
27
+ private loading = new Set<string>();
28
+
29
+ // enable abort ctrl
30
+ async loadPlugin(pluginId: string) {
31
+ if (this.activePlugins.has(pluginId)) {
32
+ console.warn(`Plugin ${pluginId} is already loaded.`);
33
+ return;
34
+ }
35
+
36
+ if (this.loading.has(pluginId)) {
37
+ console.warn(`Plugin ${pluginId} is loading...`);
38
+ return;
39
+ }
40
+
41
+ // try load plugin
42
+ this.loading.add(pluginId);
43
+
44
+ try {
45
+ const module = await import(`../plugins/${pluginId}`);
46
+
47
+ const p: IPlugin = module.default;
48
+
49
+ const ctx = this.createHostContext();
50
+
51
+ if (p.onInstall) {
52
+ p.onInstall(ctx);
53
+ }
54
+
55
+ this.activePlugins.set(pluginId, p);
56
+ } catch (error) {
57
+ console.error(`Failed to load plugin ${pluginId}:`, error);
58
+ } finally {
59
+ this.loading.delete(pluginId);
60
+ }
61
+ }
62
+
63
+ async disablePlugin(pluginId: string) {
64
+ const plugin = this.activePlugins.get(pluginId);
65
+ if (!plugin) {
66
+ console.warn(`Plugin ${pluginId} is not loaded.`);
67
+ return;
68
+ }
69
+
70
+ if (plugin.onDisable) {
71
+ await plugin.onDisable();
72
+ }
73
+
74
+ this.activePlugins.delete(pluginId);
75
+
76
+ registry.unregisterAllByPluginId(plugin.id);
77
+ }
78
+ }
79
+
80
+ export const pluginManager = new PluginManager();
@@ -0,0 +1,97 @@
1
+ import type { SlotName, HookPoint } from 'src/types/slots';
2
+
3
+ export interface ISlotEntry {
4
+ id: string;
5
+ component: React.ComponentType<any>;
6
+ pluginId: string;
7
+ weight: number;
8
+ }
9
+
10
+ export interface IMiddlewareEntry {
11
+ id: string;
12
+ fn: Function;
13
+ pluginId: string;
14
+ weight?: number;
15
+ }
16
+
17
+ export class Registry {
18
+ private slots = new Map<SlotName, ISlotEntry[]>();
19
+
20
+ private listeners = new Set<() => void>();
21
+
22
+ private middlewares = new Map<HookPoint, IMiddlewareEntry[]>();
23
+
24
+ private broadcastChange() {
25
+ this.listeners.forEach(listener => listener());
26
+ }
27
+
28
+ subscribe(listener: () => void) {
29
+ this.listeners.add(listener);
30
+ return () => this.listeners.delete(listener);
31
+ }
32
+
33
+ registerComponent(slot: SlotName, component: React.ComponentType, pluginId: string, weight = 0) {
34
+ const existing = this.slots.get(slot) || [];
35
+
36
+ const newEntry: ISlotEntry = {
37
+ id: `${pluginId}-${component.name || 'comp'}`,
38
+ pluginId,
39
+ component,
40
+ weight,
41
+ };
42
+
43
+ // 插入并按权重排序(权重大的在前)
44
+ const newList = [...existing, newEntry].sort((a, b) => b.weight - a.weight);
45
+ this.slots.set(slot, newList);
46
+
47
+ this.broadcastChange();
48
+ }
49
+
50
+ unregisterAllByPluginId(pluginId: string) {
51
+ // 1. 清理 UI 槽位
52
+ this.slots.forEach((entries, slotName) => {
53
+ const filtered = entries.filter(entry => entry.pluginId !== pluginId);
54
+ this.slots.set(slotName, filtered);
55
+ });
56
+
57
+ // 2. 清理中间件
58
+ this.middlewares.forEach((entries, point) => {
59
+ const filtered = entries.filter(entry => entry.pluginId !== pluginId);
60
+ this.middlewares.set(point, filtered);
61
+ });
62
+
63
+ console.log(`[Registry] All resources for plugin "${pluginId}" have been cleared.`);
64
+
65
+ this.broadcastChange();
66
+ }
67
+
68
+ registerMiddleware(hookPoint: HookPoint, fn: Function, pluginId: string, weight = 0) {
69
+ const existing = this.middlewares.get(hookPoint) || [];
70
+
71
+ const newEntry: IMiddlewareEntry = {
72
+ id: `${pluginId}-${fn.name || 'middleware'}`,
73
+ pluginId,
74
+ fn,
75
+ weight,
76
+ };
77
+
78
+ // 插入并按权重排序(权重大的在前)
79
+ const newList = [...existing, newEntry].sort((a, b) => (b.weight || 0) - (a.weight || 0));
80
+ this.middlewares.set(hookPoint, newList);
81
+ }
82
+
83
+ getComponents(slotName: SlotName): React.ComponentType<any>[] {
84
+ return (this.slots.get(slotName) || []).map(entry => entry.component);
85
+ }
86
+
87
+ async runMiddleware<T>(point: HookPoint, data: T): Promise<T> {
88
+ const fns = (this.middlewares.get(point) || []).map(entry => entry.fn);
89
+ let result = data;
90
+ for (const fn of fns) {
91
+ result = await fn(result);
92
+ }
93
+ return result;
94
+ }
95
+ }
96
+
97
+ export const registry = new Registry();
@@ -0,0 +1,8 @@
1
+ export const PLUGIN_MANIFEST = [
2
+ {
3
+ name: 'test',
4
+ id: 'test',
5
+ version: '0.1.0',
6
+ description: 'A test plugin',
7
+ },
8
+ ];
@@ -0,0 +1,8 @@
1
+ import { type PathObj } from '@bbki.ng/ui';
2
+ import React from 'react';
3
+
4
+ export const Emoji = ({ data }: { data: any }) => {
5
+ const path = data as PathObj[];
6
+ console.log(path);
7
+ return <span>😊</span>;
8
+ };
@@ -0,0 +1,22 @@
1
+ import { IHostContext } from '#/types/hostApi';
2
+ import { IPlugin } from '#/types/plugin';
3
+ import { Emoji } from './components/emoji';
4
+
5
+ class TestPlugin implements IPlugin {
6
+ id: string = 'test';
7
+ name: string = 'Test Plugin';
8
+ description?: string | undefined;
9
+ version: string = '0.1.0';
10
+ author?: string | undefined;
11
+
12
+ onInstall?: ((ctx: IHostContext) => void) | undefined = (ctx: IHostContext) => {
13
+ ctx.api.registerSlot('leftCol', Emoji, this.id);
14
+ };
15
+
16
+ onDisable?: (() => Promise<void> | void) | undefined;
17
+ onDestroy?: (() => void) | undefined;
18
+ }
19
+
20
+ const testPlugin = new TestPlugin();
21
+
22
+ export default testPlugin;
@@ -0,0 +1,17 @@
1
+ import { HookPoint, SlotName } from './slots';
2
+
3
+ export interface IHostApi {
4
+ registerMiddleware: (point: HookPoint, fn: Function, pluginId: string, weight?: number) => void;
5
+ registerSlot: (
6
+ slotName: SlotName,
7
+ component: React.ComponentType<any>,
8
+ pluginId: string,
9
+ weight?: number
10
+ ) => void;
11
+ }
12
+
13
+ export type PluginInitializer = (api: IHostApi) => void;
14
+
15
+ export interface IHostContext {
16
+ api: IHostApi;
17
+ }
@@ -0,0 +1,13 @@
1
+ import { IHostContext } from './hostApi';
2
+
3
+ export interface IPlugin {
4
+ id: string;
5
+ name: string;
6
+ description?: string;
7
+ version: string;
8
+ author?: string;
9
+
10
+ onInstall?: (ctx: IHostContext) => void;
11
+ onDisable?: () => Promise<void> | void;
12
+ onDestroy?: () => void;
13
+ }
@@ -0,0 +1,2 @@
1
+ export type SlotName = 'leftCol' | 'rightCol' | 'articleActionRow';
2
+ export type HookPoint = 'filterPosts' | 'transformPostContent';
package/tsconfig.json CHANGED
@@ -7,7 +7,8 @@
7
7
  "moduleResolution": "bundler",
8
8
  "baseUrl": ".",
9
9
  "paths": {
10
- "@/*": ["src/blog/*"]
10
+ "@/*": ["src/blog/*"],
11
+ "#/*": ["src/*"]
11
12
  }
12
13
  },
13
14
  "include": ["./src", "vite.config.js", "index.d.ts"]
package/vite.config.js CHANGED
@@ -41,6 +41,7 @@ export default defineConfig({
41
41
  resolve: {
42
42
  alias: {
43
43
  '@': path.resolve(__dirname, './src/blog'),
44
+ '#': path.resolve(__dirname, './src/'),
44
45
  },
45
46
  // preserveSymlinks: true,
46
47
  },