/** * 도면층 상태 기록 — 이전 상태(LAYERP)와 이름 붙인 상태(LAYERSTATE). * 도면층 목록 자체를 통째로 복사해 둔다 (개수가 적어 비용이 무시할 만하다). */ import type { Layer } from '../App.types'; const HISTORY_LIMIT = 20; const history: Layer[][] = []; const namedStates = new Map(); const copy = (layers: Layer[]): Layer[] => layers.map((layer) => ({ ...layer })); /** 도면층을 바꾸기 직전에 부른다 */ export const pushLayerHistory = (layers: Layer[]): void => { history.push(copy(layers)); if (history.length > HISTORY_LIMIT) history.shift(); }; /** 직전 상태를 꺼낸다 (없으면 null) */ export const popLayerHistory = (): Layer[] | null => history.pop() ?? null; export const saveLayerState = (name: string, layers: Layer[]): void => { namedStates.set(name.trim().toUpperCase(), copy(layers)); }; export const restoreLayerState = (name: string): Layer[] | null => { const saved = namedStates.get(name.trim().toUpperCase()); return saved ? copy(saved) : null; }; export const getLayerStateNames = (): string[] => [...namedStates.keys()];