Files
frontend/apps/sn/src/features/windowPopup/lib/openWindowPopup.ts
T
2026-08-12 11:13:49 +09:00

139 lines
4.1 KiB
TypeScript

import {
isWindowPopupKey,
windowPopupDefinitions,
type WindowPopupKey,
} from "../model/popupDefinitions";
import type {
OpenWindowPopupOptions,
OpenWindowPopupParams,
WindowPopupDefinition,
} from "../model/types";
const buildQueryString = (params: OpenWindowPopupParams = {}) => {
const searchParams = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
if (value === null || value === undefined || value === "") return;
searchParams.set(key, String(value));
});
const queryString = searchParams.toString();
return queryString ? `?${queryString}` : "";
};
const clamp = (value: number, min: number, max: number) => {
if (max < min) return min;
return Math.min(Math.max(value, min), max);
};
const buildPopupFeatures = ({
width,
height,
maximize = false,
placement = "center",
alignTop = false,
offsetLeft = 0,
offsetTop = 0,
}: Pick<
WindowPopupDefinition,
"width" | "height" | "placement" | "alignTop" | "offsetLeft" | "offsetTop"
> & {
maximize?: boolean;
}) => {
const screenLeft = window.screenX ?? window.screenLeft ?? 0;
const screenTop = window.screenY ?? window.screenTop ?? 0;
const outerWidth = window.outerWidth || window.screen.width;
const outerHeight = window.outerHeight || window.screen.height;
const screen = window.screen as Screen & {
availLeft?: number;
availTop?: number;
};
const availableLeft = screen.availLeft ?? 0;
const availableTop = screen.availTop ?? 0;
const availableWidth = window.screen.availWidth || outerWidth;
const availableHeight = window.screen.availHeight || outerHeight;
const safeWidth = maximize
? availableWidth
: Math.min(width, Math.max(360, availableWidth - 48));
const safeHeight = maximize
? availableHeight
: Math.min(height, Math.max(420, availableHeight - 72));
const minLeft = availableLeft;
const minTop = availableTop + 24;
const maxLeft = availableLeft + availableWidth - safeWidth - 16;
const maxTop = availableTop + availableHeight - safeHeight - 24;
const left = maximize
? availableLeft
: placement === "browser-left"
? clamp(screenLeft + offsetLeft, minLeft, maxLeft)
: clamp(
screenLeft + Math.round((outerWidth - safeWidth) / 2) + offsetLeft,
availableLeft + 16,
maxLeft,
);
const top = maximize
? availableTop
: clamp(
alignTop
? screenTop + 24 + offsetTop
: screenTop + Math.round((outerHeight - safeHeight) / 2) + offsetTop,
minTop,
maxTop,
);
return [
`width=${safeWidth}`,
`height=${safeHeight}`,
`left=${left}`,
`top=${top}`,
"resizable=yes",
"scrollbars=yes",
"status=no",
"location=no",
"menubar=no",
"toolbar=no",
].join(",");
};
export const openWindowPopup = (
popupKey: WindowPopupKey,
params?: OpenWindowPopupParams,
options?: OpenWindowPopupOptions,
) => {
if (typeof window === "undefined") return null;
if (!isWindowPopupKey(popupKey)) return null;
const definition: WindowPopupDefinition = windowPopupDefinitions[popupKey];
const shouldMaximize = options?.maximize ?? definition.maximize ?? false;
const popupUrl = `/popup/${popupKey}${buildQueryString(params)}`;
const popupFeatures = buildPopupFeatures({
...definition,
width: options?.width ?? definition.width,
height: options?.height ?? definition.height,
maximize: shouldMaximize,
});
const popupWindow = window.open(
popupUrl,
`sn-popup-${popupKey}`,
popupFeatures,
);
if (popupWindow && shouldMaximize) {
const screen = window.screen as Screen & {
availLeft?: number;
availTop?: number;
};
try {
// 브라우저가 window.open의 최대 크기를 제한하더라도 가용 화면 전체로 한 번 더 맞춘다.
popupWindow.moveTo(screen.availLeft ?? 0, screen.availTop ?? 0);
popupWindow.resizeTo(screen.availWidth, screen.availHeight);
} catch {
// 브라우저 정책이 이동·크기 변경을 막으면 window.open feature의 최대 크기를 유지한다.
}
}
popupWindow?.focus();
return popupWindow;
};