/** * 내부 클립보드 — 잘라내기·복사·붙여넣기(기준점 포함). * 시스템 클립보드는 도면 객체를 담을 수 없어 앱 안에서만 오간다. */ import { Point } from '@flatten-js/core'; import type { Entity } from '../entities/Entity'; import { getBoundingBoxOfMultipleEntities } from './get-bounding-box-of-multiple-entities'; interface ClipboardContent { entities: Entity[]; /** 붙여넣기 기준점 — 지정하지 않으면 선택 영역의 좌하단 */ basePoint: Point; } let clipboard: ClipboardContent | null = null; export const hasClipboardContent = (): boolean => !!clipboard?.entities.length; export function copyToClipboard(entities: Entity[], basePoint?: Point): number { if (!entities.length) return 0; const box = getBoundingBoxOfMultipleEntities(entities); clipboard = { entities: entities.map((entity) => { const copy = entity.clone(); copy.lineColor = entity.lineColor; copy.lineWidth = entity.lineWidth; copy.lineDash = entity.lineDash; copy.layerId = entity.layerId; return copy; }), basePoint: basePoint ?? new Point(box.minX, box.minY), }; return clipboard.entities.length; } /** * 클립보드 내용을 새 객체로 꺼낸다. * target을 주면 기준점이 그 위치에 오도록 옮기고, 없으면 원래 좌표 그대로 둔다. */ export function pasteFromClipboard(target?: Point): Entity[] { if (!clipboard) return []; return clipboard.entities.map((entity) => { const copy = entity.clone(); copy.lineColor = entity.lineColor; copy.lineWidth = entity.lineWidth; copy.lineDash = entity.lineDash; copy.layerId = entity.layerId; if (target) { copy.move( target.x - (clipboard as ClipboardContent).basePoint.x, target.y - (clipboard as ClipboardContent).basePoint.y ); } return copy; }); }