/** * CAD → 부모 화면 표 보내기(M02 83장 · 도면 → 면적산출 한 번 옮기기 · 연동 아님). 짝 = `M02_MasterTemplete_AreaDrawing.ts`. * export-table-request 부모 [면적산출로 보내기] 단추 → 고른 범위를 표 자료로 * export-table{table} 표 자료(`helpers/table-from-drawing.ts` 꼴) → 부모가 새 시트로 * CAD 명령 TOAREA(「면적산출로 보내기」)도 같은 길. 보기 전용 도면에서도 됨(도면을 안 바꿈). * 고른 것이 없거나 옮길 글이 없으면 부모에 안 보내고 안내만. */ import { toast } from 'react-toastify'; import type { DrawingEntityJson } from '../helpers/table-from-drawing.ts'; import { type DrawingTable, tableFromDrawing } from '../helpers/table-from-drawing.ts'; import { getSelectedEntities } from '../state.ts'; export const AISLO_EXPORT_TABLE_REQUEST_MESSAGE = 'aislo:b08:export-table-request'; export const AISLO_EXPORT_TABLE_MESSAGE = 'aislo:b08:export-table'; const TEXT = { empty: '면적산출로 보낼 범위를 먼저 고르세요(누름 · 끌기).', noText: '고른 범위에 글이 없어 표로 옮길 것이 없습니다.', standalone: '면적산출 화면에서 연 도면에서만 보낼 수 있습니다.', }; type Notify = (type: string, payload: Record) => void; function notifyParent(type: string, payload: Record): void { if (window.parent === window) return; window.parent.postMessage({ type, ...payload }, window.location.origin); } /** 고른 범위 → 표 자료 · 고른 것이 없으면 'empty' · 글이 없으면 'no-text' */ export async function selectionTable(): Promise { const selected = getSelectedEntities(); if (!selected.length) return 'empty'; const json = (await Promise.all(selected.map((entity) => entity.toJson()))).filter( (entity): entity is NonNullable => Boolean(entity) ); return tableFromDrawing(json as DrawingEntityJson[]) ?? 'no-text'; } /** 고른 범위를 표로 만들어 부모에 보냄 — 보냈으면 true */ export async function sendSelectionTable(notify: Notify = notifyParent): Promise { const result = await selectionTable(); if (result === 'empty') toast.info(TEXT.empty); else if (result === 'no-text') toast.info(TEXT.noText); else { notify(AISLO_EXPORT_TABLE_MESSAGE, { table: result }); return true; } return false; } /** CAD 명령 TOAREA — 부모 없이 띄운 CAD(단독)면 안내만 */ export function runSendSelectionTable(): string { if (window.parent === window) { toast.info(TEXT.standalone); return '부모 화면 없음'; } void sendSelectionTable(); return '면적산출로 보내기'; } /** 부모 요청 메시지면 처리하고 true */ export async function handleTableExportMessage( data: unknown, notify: Notify = notifyParent ): Promise { const message = data as { type?: string } | null; if ( !message || typeof message !== 'object' || message.type !== AISLO_EXPORT_TABLE_REQUEST_MESSAGE ) return false; await sendSelectionTable(notify); return true; } /** 부모 [면적산출로 보내기] 요청을 받음 — 같은 출처 · 부모 창만 */ export function registerAisloTableExport(): void { window.addEventListener('message', (event: MessageEvent) => { if (event.origin !== window.location.origin || event.source !== window.parent) return; void handleTableExportMessage(event.data); }); }