2025-03-03 19:24:51 +00:00
|
|
|
interface OpenWindowOptions {
|
|
|
|
|
noopener?: boolean;
|
|
|
|
|
noreferrer?: boolean;
|
|
|
|
|
target?: "_blank" | "_parent" | "_self" | "_top" | string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 新窗口打开URL。
|
|
|
|
|
*
|
|
|
|
|
* @param url - 需要打开的网址。
|
|
|
|
|
* @param options - 打开窗口的选项。
|
|
|
|
|
*/
|
|
|
|
|
function openWindow(url: string, options: OpenWindowOptions = {}): void {
|
|
|
|
|
// 解构并设置默认值
|
|
|
|
|
const { noopener = true, noreferrer = true, target = "_blank" } = options;
|
|
|
|
|
|
|
|
|
|
// 基于选项创建特性字符串
|
|
|
|
|
const features = [noopener && "noopener=yes", noreferrer && "noreferrer=yes"].filter(Boolean).join(",");
|
|
|
|
|
|
|
|
|
|
// 打开窗口
|
|
|
|
|
window.open(url, target, features);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 在新窗口中打开路由。
|
|
|
|
|
* @param path
|
|
|
|
|
*/
|
|
|
|
|
function openRouteInNewWindow(path: string) {
|
|
|
|
|
const { hash, origin } = location;
|
2026-01-31 02:07:06 +08:00
|
|
|
let pathname = location.pathname;
|
|
|
|
|
if (pathname.endsWith("/")) {
|
|
|
|
|
pathname = pathname.slice(0, -1);
|
|
|
|
|
}
|
2025-03-03 19:24:51 +00:00
|
|
|
const fullPath = path.startsWith("/") ? path : `/${path}`;
|
2026-01-31 02:07:06 +08:00
|
|
|
const url = `${origin}${pathname}${hash ? "/#" : ""}${fullPath}`;
|
2025-03-03 19:24:51 +00:00
|
|
|
openWindow(url, { target: "_blank" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { openRouteInNewWindow, openWindow };
|