Files
certd/packages/ui/certd-client/src/vben/shared/utils/window.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

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;
let pathname = location.pathname;
if (pathname.endsWith("/")) {
pathname = pathname.slice(0, -1);
}
const fullPath = path.startsWith("/") ? path : `/${path}`;
const url = `${origin}${pathname}${hash ? "/#" : ""}${fullPath}`;
openWindow(url, { target: "_blank" });
}
export { openRouteInNewWindow, openWindow };