diff --git a/client/web/src/plugin/common/index.ts b/client/web/src/plugin/common/index.ts index 5617e61b62f..0de504bb60f 100644 --- a/client/web/src/plugin/common/index.ts +++ b/client/web/src/plugin/common/index.ts @@ -34,6 +34,7 @@ export { } from '@/utils/url-helper'; export { getServiceWorkerRegistration } from '@/utils/sw-helper'; export { postMessageEvent } from '@/utils/event-helper'; +export { panelWindowManager } from '@/utils/window-helper'; import { /** * 注意: Tailchat 内部的request不会被导出为插件可用模块,如果需要网络请求的话请自行import axios(以作为复用依赖) diff --git a/client/web/src/utils/window-helper.ts b/client/web/src/utils/window-helper.ts index 64aa1cbb301..dcd8829f74d 100644 --- a/client/web/src/utils/window-helper.ts +++ b/client/web/src/utils/window-helper.ts @@ -16,15 +16,25 @@ export function buildWindowFeatures( .join(','); } +interface OpenInNewWindowOptions { + width?: number; + height?: number; + top?: number; + left?: number; +} + /** * 在新窗口打开 * @param url 新窗口地址 */ -export function openInNewWindow(url: string) { - const width = 414; - const height = 736; - const top = (window.screen.height - height) / 2; - const left = (window.screen.width - width) / 2; +export function openInNewWindow( + url: string, + options: OpenInNewWindowOptions = {} +) { + const width = options.width ?? 414; + const height = options.height ?? 736; + const top = options.top ?? (window.screen.height - height) / 2; + const left = options.left ?? (window.screen.width - width) / 2; // 打开窗口 const win = window.open( @@ -53,15 +63,18 @@ class PanelWindowManager { * 打开一个独立窗口 * @param url 窗口Url */ - open(url: string, options?: { onClose: () => void }): void { + open( + url: string, + options: { onClose?: () => void } & OpenInNewWindowOptions = {} + ): Window { if (this.openedPanelWindows[url]) { this.openedPanelWindows[url].focus(); - return; + return this.openedPanelWindows[url]; } const win = openInNewWindow(url); if (!win) { - return; + throw new Error('Create window failed'); } const timer = setInterval(() => { @@ -74,12 +87,14 @@ class PanelWindowManager { if (typeof options?.onClose === 'function') { // 触发回调 - options?.onClose(); + options.onClose?.(); } } }, 1000); this.openedPanelWindows[url] = win; + + return win; } /**