From 65d1e916f84bdb7b0968e212d7d058485ade759a Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 8 Feb 2023 16:39:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E6=96=B9=E6=B3=95=E5=88=B0window=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/web/src/plugin/manager.ts | 2 + client/web/src/utils/global-helper.ts | 15 +++++++ client/web/types/global.d.ts | 63 +++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 client/web/src/utils/global-helper.ts create mode 100644 client/web/types/global.d.ts diff --git a/client/web/src/plugin/manager.ts b/client/web/src/plugin/manager.ts index b4d54c66..652fe73f 100644 --- a/client/web/src/plugin/manager.ts +++ b/client/web/src/plugin/manager.ts @@ -7,6 +7,7 @@ import { initMiniStar, loadSinglePlugin } from 'mini-star'; import _once from 'lodash/once'; import { builtinPlugins } from './builtin'; import { showPluginLoadError } from './showPluginLoadError'; +import { injectTailchatGlobalValue } from '@/utils/global-helper'; class PluginManager { /** @@ -133,3 +134,4 @@ class PluginManager { } export const pluginManager = new PluginManager(); +injectTailchatGlobalValue('installPlugin', pluginManager.installPlugin); diff --git a/client/web/src/utils/global-helper.ts b/client/web/src/utils/global-helper.ts new file mode 100644 index 00000000..29b2cf91 --- /dev/null +++ b/client/web/src/utils/global-helper.ts @@ -0,0 +1,15 @@ +type TailchatTypeMap = NonNullable; + +/** + * 注入tailchat全局变量到window + */ +export function injectTailchatGlobalValue( + key: T, + value: TailchatTypeMap[T] +) { + if (!window.tailchat) { + window.tailchat = {}; + } + + window.tailchat[key] = value; +} diff --git a/client/web/types/global.d.ts b/client/web/types/global.d.ts new file mode 100644 index 00000000..eb41d636 --- /dev/null +++ b/client/web/types/global.d.ts @@ -0,0 +1,63 @@ +/** + * Copy from `tailchat/client/shared/model/plugin.ts` + */ +interface PluginManifest { + /** + * 插件用于显示的名称 + * @example 网页面板插件 + */ + label: string; + + /** + * 插件名, 插件唯一标识 + * @example com.msgbyte.webview + */ + name: string; + + /** + * 插件地址 + */ + url: string; + + /** + * 插件图标 + * 推荐大小: 128x128 + */ + icon?: string; + + /** + * 插件版本号 + * 遵循 semver 规则 + * + * major.minor.patch + * @example 1.0.0 + */ + version: string; + + /** + * 插件维护者 + */ + author: string; + + /** + * 插件描述 + */ + description: string; + + /** + * 是否需要重启才能应用插件 + */ + requireRestart: boolean; + + /** + * 文档的链接 + * 如果是markdown则解析, 如果是html则使用iframe + */ + documentUrl?: string; +} + +declare interface Window { + tailchat?: { + installPlugin?: (manifest: PluginManifest) => Promise; + }; +}