/* ============================================================================= * M02_MasterTemplete_DrawingToArea.ts * 구조물 도면 상세 → 같은 열 면적산출 상세(105-2) — 머리 [면적산출로 보내기] = CAD 에 고른 범위 요청 * (`EXPORT_TABLE_REQUEST`) · 받은 표 자료(`EXPORT_TABLE` · CAD 명령 TOAREA 도 같은 길)를 `onTable` 로. * 옮겨 가기 · 넣을 자리 창은 부른 쪽(`UI_Main` → `importDrawingTable`) · 저장은 사용자 [저장]. * 쓸 수 없는 면적(읽기 · 프로젝트 층)이면 단추 없음 · TOAREA 는 알림만. * ========================================================================== */ import { createButton, showToast } from "@ui/ui_template_elements"; import { EXPORT_TABLE, EXPORT_TABLE_REQUEST } from "./M02_MasterTemplete_AreaDrawing"; import type { DrawingTable } from "./M02_MasterTemplete_AreaFromDrawing"; const TEXT = { send: "면적산출로 보내기", sendTip: "도면에서 고른 범위(표 · 선으로 그린 표 · 글)를 같은 열 면적산출로 옮겨 감 — 새 시트 또는 고른 칸부터 · 연동 아님", blocked: "이 층은 면적산출을 고칠 수 없음 — 보내지 않음", }; /** `host` = 도면 상세 칸(iframe 이 든 곳) · `slot` = 머리 칸 · 떼는 함수를 돌려줌 */ export function attachDrawingToArea( host: HTMLElement, slot: HTMLElement, onTable: ((table: DrawingTable) => void) | null, ): () => void { const frame = (): HTMLIFrameElement | null => host.querySelector("iframe"); const send = createButton({ label: TEXT.send, variant: "ghost" }); send.title = TEXT.sendTip; send.dataset.drawingToArea = ""; send.addEventListener("click", () => frame()?.contentWindow?.postMessage({ type: EXPORT_TABLE_REQUEST }, window.location.origin), ); if (onTable) slot.append(send); const onMessage = (event: MessageEvent): void => { const win = frame()?.contentWindow; if (!win || event.source !== win || event.origin !== window.location.origin) return; const data = event.data as { type?: string; table?: DrawingTable } | null; if (data?.type !== EXPORT_TABLE || !data.table) return; if (onTable) onTable(data.table); else showToast(TEXT.blocked, "info"); }; window.addEventListener("message", onMessage); return () => { window.removeEventListener("message", onMessage); send.remove(); }; }