/** 명령 실행기 — 리본 버튼·명령행·단축키가 모두 이 경로로 들어온다. */ import { toast } from 'react-toastify'; import { Actor } from 'xstate'; import { HtmlEvent } from '../App.types'; import { getSelectedEntities, isDrawingReadOnly, notifyWindow, setActiveToolActor } from '../state'; import type { CadCommand } from './command.types'; import { getCommandById, isViewOnlyCommand, resolveCommandInput } from './registry'; const COMMAND_HISTORY_LIMIT = 200; let lastCommandId: string | null = null; const commandHistory: string[] = []; export const getLastCommandId = (): string | null => lastCommandId; export const getCommandHistory = (): string[] => commandHistory; function log(line: string) { commandHistory.push(line); if (commandHistory.length > COMMAND_HISTORY_LIMIT) { commandHistory.splice(0, commandHistory.length - COMMAND_HISTORY_LIMIT); } notifyWindow(HtmlEvent.UPDATE_STATE); } /** 명령 한 건 실행. 도구형이면 도구를 활성화하고, 즉시형이면 run()을 부른다. */ export function runCommand(command: CadCommand): string { // 확정한 도면은 읽기 전용이다 — 보는 명령만 통과시킨다. 리본·명령행·단축키가 모두 // 이 한 곳을 지나므로 여기서 한 번 막으면 새는 길이 없다 (2026-09-01 사용자 확정). if (isDrawingReadOnly() && !isViewOnlyCommand(command)) { toast.info(`확정한 도면입니다. 고치려면 [수정]을 먼저 누르세요. (${command.label})`); log(`${command.id}: 확정 도면 — 실행하지 않음`); return ''; } if (command.needsSelection && getSelectedEntities().length === 0) { toast.info(`${command.label}: 객체를 먼저 선택하십시오.`); log(`${command.id}: 선택 없음`); return ''; } lastCommandId = command.id; if (command.machine) { setActiveToolActor(new Actor(command.machine)); // 도구를 켠 뒤 준비 동작이 필요한 명령(파일 선택 등)은 run()도 함께 부른다 command.run?.(); log(`${command.id} ${command.label}`); return command.id; } const result = command.run?.(); log(`${command.id} ${typeof result === 'string' ? result : command.label}`); return typeof result === 'string' ? result : command.id; } /** 명령 이름·별칭 문자열로 실행 (명령행 입력) */ export function runCommandInput(input: string): boolean { const command = resolveCommandInput(input); if (!command) return false; runCommand(command); return true; } /** 직전 명령 반복 (AutoCAD의 빈 ENTER 동작) */ export function repeatLastCommand(): boolean { if (!lastCommandId) return false; const command = getCommandById(lastCommandId); if (!command) return false; runCommand(command); return true; }