Skip to content

Commit

Permalink
refactor: export panelWindowManager
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Nov 16, 2023
1 parent 087713a commit e0f9f5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions client/web/src/plugin/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(以作为复用依赖)
Expand Down
33 changes: 24 additions & 9 deletions client/web/src/utils/window-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(() => {
Expand All @@ -74,12 +87,14 @@ class PanelWindowManager {

if (typeof options?.onClose === 'function') {
// 触发回调
options?.onClose();
options.onClose?.();
}
}
}, 1000);

this.openedPanelWindows[url] = win;

return win;
}

/**
Expand Down

0 comments on commit e0f9f5d

Please sign in to comment.