refactor: export panelWindowManager

chore/devcontainer
moonrailgun 1 year ago
parent 087713afbf
commit e0f9f5dab9

@ -34,6 +34,7 @@ export {
} from '@/utils/url-helper'; } from '@/utils/url-helper';
export { getServiceWorkerRegistration } from '@/utils/sw-helper'; export { getServiceWorkerRegistration } from '@/utils/sw-helper';
export { postMessageEvent } from '@/utils/event-helper'; export { postMessageEvent } from '@/utils/event-helper';
export { panelWindowManager } from '@/utils/window-helper';
import { import {
/** /**
* : Tailchat requestimport axios() * : Tailchat requestimport axios()

@ -16,15 +16,25 @@ export function buildWindowFeatures(
.join(','); .join(',');
} }
interface OpenInNewWindowOptions {
width?: number;
height?: number;
top?: number;
left?: number;
}
/** /**
* *
* @param url * @param url
*/ */
export function openInNewWindow(url: string) { export function openInNewWindow(
const width = 414; url: string,
const height = 736; options: OpenInNewWindowOptions = {}
const top = (window.screen.height - height) / 2; ) {
const left = (window.screen.width - width) / 2; 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( const win = window.open(
@ -53,15 +63,18 @@ class PanelWindowManager {
* *
* @param url Url * @param url Url
*/ */
open(url: string, options?: { onClose: () => void }): void { open(
url: string,
options: { onClose?: () => void } & OpenInNewWindowOptions = {}
): Window {
if (this.openedPanelWindows[url]) { if (this.openedPanelWindows[url]) {
this.openedPanelWindows[url].focus(); this.openedPanelWindows[url].focus();
return; return this.openedPanelWindows[url];
} }
const win = openInNewWindow(url); const win = openInNewWindow(url);
if (!win) { if (!win) {
return; throw new Error('Create window failed');
} }
const timer = setInterval(() => { const timer = setInterval(() => {
@ -74,12 +87,14 @@ class PanelWindowManager {
if (typeof options?.onClose === 'function') { if (typeof options?.onClose === 'function') {
// 触发回调 // 触发回调
options?.onClose(); options.onClose?.();
} }
} }
}, 1000); }, 1000);
this.openedPanelWindows[url] = win; this.openedPanelWindows[url] = win;
return win;
} }
/** /**

Loading…
Cancel
Save