@bbki.ng/site 5.4.55 → 5.5.1
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 +19 -0
- package/package.json +2 -2
- package/src/blog/app.tsx +23 -2
- package/src/blog/hooks/use_fingerprint.ts +1 -1
- package/src/blog/utils/index.ts +8 -3
- package/src/core/components/SlotComp.tsx +20 -0
- package/src/core/hooks/useSlotComp.ts +20 -0
- package/src/core/pluginManager.ts +80 -0
- package/src/core/registry.ts +97 -0
- package/src/plugins/manifest.ts +8 -0
- package/src/plugins/test/components/emoji.tsx +8 -0
- package/src/plugins/test/index.ts +22 -0
- package/src/types/hostApi.ts +17 -0
- package/src/types/plugin.ts +13 -0
- package/src/types/slots.ts +2 -0
- package/tsconfig.json +2 -1
- package/vite.config.js +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @bbki.ng/site
|
|
2
2
|
|
|
3
|
+
## 5.5.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 3c2834a: track device activity
|
|
8
|
+
- Updated dependencies [3c2834a]
|
|
9
|
+
- @bbki.ng/ui@0.2.1
|
|
10
|
+
|
|
11
|
+
## 5.5.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- 2caf775: add blog plugin
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [2caf775]
|
|
20
|
+
- @bbki.ng/ui@0.2.0
|
|
21
|
+
|
|
3
22
|
## 5.4.55
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bbki.ng/site",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.1",
|
|
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
|
|
17
|
+
"@bbki.ng/ui": "0.2.1"
|
|
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
|
|
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>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, useState, useCallback } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { getStableDeviceId, FingerprintData } from '@/utils/fingerprints';
|
|
3
3
|
|
|
4
4
|
interface UseFingerprintReturn {
|
|
5
5
|
deviceId: string | null;
|
package/src/blog/utils/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { API_ENDPOINT } from '@/constants/routes';
|
|
2
2
|
import { FontType } from '@/types/font';
|
|
3
|
+
import { getFingerprint, getStableDeviceId } from './fingerprints';
|
|
3
4
|
|
|
4
5
|
type Fetcher = (resource: string, init?: any) => Promise<any>;
|
|
5
6
|
|
|
@@ -7,10 +8,13 @@ export const floatNumberToPercentageString = (num: number): string => {
|
|
|
7
8
|
return `${num * 100}%`;
|
|
8
9
|
};
|
|
9
10
|
|
|
10
|
-
export const baseFetcher = (resource: string, init: RequestInit = {}) =>
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
export const baseFetcher = async (resource: string, init: RequestInit = {}) => {
|
|
12
|
+
const headers = new Headers(init.headers || {});
|
|
13
|
+
const fp = await getFingerprint();
|
|
14
|
+
headers.set('X-Device-Fingerprint', fp.hash);
|
|
15
|
+
return fetch(resource, {
|
|
13
16
|
...init,
|
|
17
|
+
headers,
|
|
14
18
|
mode: 'cors',
|
|
15
19
|
}).then(res => {
|
|
16
20
|
if (!res.ok) {
|
|
@@ -19,6 +23,7 @@ export const baseFetcher = (resource: string, init: RequestInit = {}) =>
|
|
|
19
23
|
|
|
20
24
|
return res.json();
|
|
21
25
|
});
|
|
26
|
+
};
|
|
22
27
|
|
|
23
28
|
export const withBBApi =
|
|
24
29
|
(fetcher: Fetcher) =>
|
|
@@ -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,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
|
+
}
|
package/tsconfig.json
CHANGED