- 선 — 이어 그리기 첫 점 · 구간 기억 · C = 둘째 선부터 첫 점으로 닫고 끝 · U = 마지막 한 구간만 취소 - 점 단계 글 — 선택지 글자(meta.options)만 도구로 · 나머지는 명령 이름 그대로 · 한글 받개는 점 단계에서 안 잡음 - 새 input-snap-override — END · MID · CEN · INT · PER · NEA · NON 다음 한 점만 · F3 꺼져도 · 맞는 점 없으면 클릭 버림 · Esc 풂 · 커서 옆 안내 - 원본 도구 안내문 한글(선 · 원 · 사각형 · 이동 · 복사 · 회전 · 배열 · 정렬 · 지우개 · 그림 · 폴리선 합치기 · 선택) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013QyNXY2TUKXCFMwAca2APG
154 lines
3.7 KiB
TypeScript
154 lines
3.7 KiB
TypeScript
import { assign, type MachineContext, sendTo } from 'xstate';
|
|
import type { Entity } from '../entities/Entity.ts';
|
|
import {
|
|
type BoundingBox,
|
|
getBoundingBoxOfMultipleEntities,
|
|
} from '../helpers/get-bounding-box-of-multiple-entities.ts';
|
|
import {
|
|
getSelectedEntities,
|
|
getSelectedEntityIds,
|
|
setAngleGuideOriginPoint,
|
|
setGhostHelperEntities,
|
|
setSelectedEntityIds,
|
|
setShouldDrawHelpers,
|
|
} from '../state.ts';
|
|
import type { Tool } from '../tools.ts';
|
|
import { selectToolStateMachine } from './select-tool.ts';
|
|
import type {
|
|
DrawEvent,
|
|
KeyboardEnterEvent,
|
|
MouseClickEvent,
|
|
StateEvent,
|
|
ToolContext,
|
|
} from './tool.types.ts';
|
|
|
|
export interface AlignContext extends ToolContext {}
|
|
|
|
export enum AlignState {
|
|
INIT = 'INIT',
|
|
CHECK_SELECTION = 'CHECK_SELECTION',
|
|
WAITING_FOR_SELECTION = 'WAITING_FOR_SELECTION',
|
|
}
|
|
|
|
export enum AlignAction {
|
|
INIT_ALIGN_TOOL = 'INIT_ALIGN_TOOL',
|
|
ALIGN_SELECTION = 'ALIGN_SELECTION',
|
|
DESELECT_ENTITIES = 'DESELECT_ENTITIES',
|
|
}
|
|
|
|
export function GET_ALIGN_TOOL_STATE(type: Tool): MachineContext {
|
|
return {
|
|
types: {} as {
|
|
context: AlignContext;
|
|
events: StateEvent;
|
|
},
|
|
context: {
|
|
type,
|
|
},
|
|
initial: AlignState.INIT,
|
|
states: {
|
|
[AlignState.INIT]: {
|
|
description: 'Initializing the align tool',
|
|
always: {
|
|
actions: AlignAction.INIT_ALIGN_TOOL,
|
|
target: AlignState.CHECK_SELECTION,
|
|
},
|
|
},
|
|
[AlignState.WAITING_FOR_SELECTION]: {
|
|
description: 'Select what you want to align',
|
|
meta: {
|
|
instructions: '정렬할 객체 선택 뒤 Enter',
|
|
},
|
|
invoke: {
|
|
id: 'selectToolInsideTheAlignTool',
|
|
src: selectToolStateMachine,
|
|
onDone: {
|
|
actions: assign(() => {
|
|
return {};
|
|
}),
|
|
target: AlignState.CHECK_SELECTION,
|
|
},
|
|
},
|
|
on: {
|
|
MOUSE_CLICK: {
|
|
// Forward the event to the select tool
|
|
actions: sendTo(
|
|
'selectToolInsideTheAlignTool',
|
|
({ event }: { event: MouseClickEvent }) => {
|
|
return event;
|
|
}
|
|
),
|
|
},
|
|
ESC: {
|
|
actions: [AlignAction.DESELECT_ENTITIES, AlignAction.INIT_ALIGN_TOOL],
|
|
},
|
|
ENTER: {
|
|
// Forward the event to the select tool
|
|
actions: sendTo(
|
|
'selectToolInsideTheAlignTool',
|
|
({ event }: { event: KeyboardEnterEvent }) => {
|
|
return event;
|
|
}
|
|
),
|
|
},
|
|
DRAW: {
|
|
// Forward the event to the select tool
|
|
actions: sendTo('selectToolInsideTheAlignTool', ({ event }: { event: DrawEvent }) => {
|
|
return event;
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
[AlignState.CHECK_SELECTION]: {
|
|
description: 'Check if there is something selected',
|
|
always: [
|
|
{
|
|
guard: () => {
|
|
return getSelectedEntityIds().length > 0;
|
|
},
|
|
actions: AlignAction.ALIGN_SELECTION,
|
|
},
|
|
{
|
|
guard: () => {
|
|
return getSelectedEntityIds().length === 0;
|
|
},
|
|
target: AlignState.WAITING_FOR_SELECTION,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
export function GET_ALIGN_ACTION(alignEntity: (entity: Entity, boundingBox: BoundingBox) => void) {
|
|
return {
|
|
actions: {
|
|
[AlignAction.INIT_ALIGN_TOOL]: assign(() => {
|
|
setShouldDrawHelpers(false);
|
|
setGhostHelperEntities([]);
|
|
setAngleGuideOriginPoint(null);
|
|
return {};
|
|
}),
|
|
[AlignAction.ALIGN_SELECTION]: () => {
|
|
const selectedEntities = getSelectedEntities();
|
|
if (!selectedEntities?.length) {
|
|
throw new Error('[ALIGN_LEFT] Calling align without entities selected');
|
|
}
|
|
|
|
// Align the entities
|
|
const boundingBox = getBoundingBoxOfMultipleEntities(selectedEntities);
|
|
|
|
for (const entity of selectedEntities) {
|
|
alignEntity(entity, boundingBox);
|
|
}
|
|
},
|
|
[AlignAction.DESELECT_ENTITIES]: assign(() => {
|
|
setGhostHelperEntities([]);
|
|
setSelectedEntityIds([]);
|
|
return {};
|
|
}),
|
|
...selectToolStateMachine.implementations.actions,
|
|
},
|
|
};
|
|
}
|