Files
Aislo/B07_DesignDetail/openwebcad/src/tools/image-import-tool.ts
T
eomsangdonandClaude Opus 5 4cb9b15939 style: 저장소 전체 포맷터 일괄 적용 (prettier·biome·ruff)
파일마다 포맷 폭이 달라(≈80 대 100) 한 줄만 고쳐도 포맷터가 무관한 줄을 대량
재포맷했음. 사용자 지시로 전체를 한 번에 맞춤. 코드 동작 변경 없음 — 포맷만.

- 프론트엔드 `.ts/.css/.html` → 저장소 prettier (`.prettierrc`, printWidth 100)
- `B07_DesignDetail/openwebcad/**` → 자체 biome (tab 들여쓰기·single quote·lineWidth 100).
  `biome format` 만 사용 — `biome lint --write` 는 포맷 아닌 코드 수정까지 하므로 제외
- 파이썬 → `ruff format` (엔진 코드는 이미 정합, resources·scratch 스크립트 24개만 변경)

두 포맷터가 서로 되돌리지 않도록 `.prettierignore` 신규 — openwebcad 와 빌드·산출물
폴더를 prettier 대상에서 뺌. `.prettierrc` 에 `endOfLine: "auto"` 추가 — 기본값 `lf` 가
`core.autocrlf=true` 로 받은 CRLF 파일을 매번 전부 다시 써서 `--list-different` 가
실제 포맷 차이를 가리고 있었음.

검증: `tsc --noEmit` 통과(루트·openwebcad 둘 다), pytest 349 passed / 17 skipped /
0 failed, CAD vitest 87건 중 81 passed / 6 failed(laptop-sub 기준선과 동일, 회귀 없음).
포맷터 재실행 시 prettier·biome 모두 변경 0건.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-02 07:08:24 +09:00

251 lines
7.0 KiB
TypeScript

import type { Point } from '@flatten-js/core';
import {
addEntities,
getActiveLayerId,
setActiveToolActor,
setAngleGuideEntities,
setAngleGuideOriginPoint,
setGhostHelperEntities,
setSelectedEntityIds,
setShouldDrawHelpers,
} from '../state';
import { Tool } from '../tools';
import { Actor, assign, createMachine } from 'xstate';
import {
ActorEvent,
type DrawEvent,
type FileSelectedEvent,
type MouseClickEvent,
type PointInputEvent,
type StateEvent,
type ToolContext,
} from './tool.types';
import { ImageEntity } from '../entities/ImageEntity';
import { getContainRectangleInsideRectangle } from './image-import-tool.helpers';
import { RectangleEntity } from '../entities/RectangleEntity';
import { selectToolStateMachine } from './select-tool';
import { boxToPolygon, twoPointBoxToPolygon } from '../helpers/box-to-polygon';
import { isPointEqual } from '../helpers/is-point-equal.ts';
import { getPointFromEvent } from '../helpers/get-point-from-event.ts';
export interface ImageImportContext extends ToolContext {
startPoint: Point | null;
imageElement: HTMLImageElement | null;
}
export enum ImageImportState {
INIT = 'INIT',
WAIT_FOR_IMAGE_DATA = 'WAIT_FOR_IMAGE_DATA',
WAITING_FOR_START_POINT = 'WAITING_FOR_START_POINT',
WAITING_FOR_END_POINT = 'WAITING_FOR_END_POINT',
}
export enum ImageImportAction {
INIT_IMAGE_IMPORT_TOOL = 'INIT_IMAGE_IMPORT_TOOL',
STORE_IMAGE_DATA = 'STORE_IMAGE_DATA',
RECORD_START_POINT = 'RECORD_START_POINT',
DRAW_TEMP_IMAGE_IMPORT = 'DRAW_TEMP_IMAGE_IMPORT',
DRAW_FINAL_IMAGE_IMPORT = 'DRAW_FINAL_IMAGE_IMPORT',
SWITCH_TO_SELECT_TOOL = 'SWITCH_TO_SELECT_TOOL',
}
export const imageImportToolStateMachine = createMachine(
{
types: {} as {
context: ImageImportContext;
events: StateEvent;
},
context: {
type: Tool.IMAGE_IMPORT,
startPoint: null,
imageElement: null,
},
initial: ImageImportState.INIT,
states: {
[ImageImportState.INIT]: {
description: 'Initializing the imageImport tool',
always: {
actions: ImageImportAction.INIT_IMAGE_IMPORT_TOOL,
target: ImageImportState.WAIT_FOR_IMAGE_DATA,
},
},
[ImageImportState.WAIT_FOR_IMAGE_DATA]: {
description: 'Select an image file to import',
meta: {
instructions: 'Select an image file to import',
},
on: {
[ActorEvent.FILE_SELECTED]: {
actions: ImageImportAction.STORE_IMAGE_DATA,
target: ImageImportState.WAITING_FOR_START_POINT,
},
ESC: {
actions: ImageImportAction.SWITCH_TO_SELECT_TOOL,
},
},
},
[ImageImportState.WAITING_FOR_START_POINT]: {
description: 'Select the start point of the imageImport',
meta: {
instructions: 'Select the start point of the imageImport',
},
on: {
MOUSE_CLICK: {
actions: ImageImportAction.RECORD_START_POINT,
target: ImageImportState.WAITING_FOR_END_POINT,
},
ABSOLUTE_POINT_INPUT: {
actions: ImageImportAction.RECORD_START_POINT,
target: ImageImportState.WAITING_FOR_END_POINT,
},
},
},
[ImageImportState.WAITING_FOR_END_POINT]: {
description: 'Select the end point of the imageImport',
meta: {
instructions: 'Select the end point of the imageImport',
},
on: {
DRAW: {
actions: ImageImportAction.DRAW_TEMP_IMAGE_IMPORT,
},
MOUSE_CLICK: {
actions: [
ImageImportAction.DRAW_FINAL_IMAGE_IMPORT,
ImageImportAction.INIT_IMAGE_IMPORT_TOOL,
ImageImportAction.SWITCH_TO_SELECT_TOOL,
],
},
NUMBER_INPUT: {
actions: [
ImageImportAction.DRAW_FINAL_IMAGE_IMPORT,
ImageImportAction.INIT_IMAGE_IMPORT_TOOL,
ImageImportAction.SWITCH_TO_SELECT_TOOL,
],
},
ABSOLUTE_POINT_INPUT: {
actions: [
ImageImportAction.DRAW_FINAL_IMAGE_IMPORT,
ImageImportAction.INIT_IMAGE_IMPORT_TOOL,
ImageImportAction.SWITCH_TO_SELECT_TOOL,
],
},
RELATIVE_POINT_INPUT: {
actions: [
ImageImportAction.DRAW_FINAL_IMAGE_IMPORT,
ImageImportAction.INIT_IMAGE_IMPORT_TOOL,
ImageImportAction.SWITCH_TO_SELECT_TOOL,
],
},
ESC: {
actions: ImageImportAction.SWITCH_TO_SELECT_TOOL,
},
},
},
},
},
{
actions: {
[ImageImportAction.INIT_IMAGE_IMPORT_TOOL]: assign(() => {
setShouldDrawHelpers(true);
setGhostHelperEntities([]);
setSelectedEntityIds([]);
setAngleGuideOriginPoint(null);
return {
startPoint: null,
imageElement: null,
};
}),
[ImageImportAction.STORE_IMAGE_DATA]: assign(({ event }) => {
return {
imageElement: (event as FileSelectedEvent).image,
};
}),
[ImageImportAction.RECORD_START_POINT]: assign(({ context, event }) => {
const startPoint = getPointFromEvent(null, event as PointInputEvent);
setAngleGuideOriginPoint(startPoint);
return {
...context,
startPoint: startPoint,
};
}),
[ImageImportAction.DRAW_TEMP_IMAGE_IMPORT]: ({ context, event }) => {
if (!context.startPoint) {
throw new Error(
'[IMAGE_IMPORT] startPoint is not set when calling DRAW_TEMP_IMAGE_IMPORT'
);
}
if (!context.imageElement) {
throw new Error(
'[IMAGE_IMPORT] imageElement is not set when calling DRAW_TEMP_IMAGE_IMPORT'
);
}
if (
isPointEqual(
context.startPoint,
(event as DrawEvent).drawController.getWorldMouseLocation()
)
) {
return; // Can't draw an image that is 0 pixels wide
}
const endPoint = getPointFromEvent(context.startPoint, event as PointInputEvent);
const containRectangle = getContainRectangleInsideRectangle(
context.imageElement.naturalWidth,
context.imageElement.naturalHeight,
context.startPoint,
endPoint
);
if (!containRectangle) {
return;
}
const activeImage = new ImageEntity(
getActiveLayerId(),
context.imageElement,
containRectangle.low,
containRectangle.high,
0
);
const draggedRectangle = new RectangleEntity(
getActiveLayerId(),
twoPointBoxToPolygon(context.startPoint, endPoint)
);
setGhostHelperEntities([activeImage]);
setAngleGuideEntities([draggedRectangle]);
},
[ImageImportAction.DRAW_FINAL_IMAGE_IMPORT]: ({ context, event }) => {
if (!context.startPoint) {
throw new Error(
'[IMAGE_IMPORT] startPoint is not set when calling DRAW_TEMP_IMAGE_IMPORT'
);
}
if (!context.imageElement) {
throw new Error(
'[IMAGE_IMPORT] imageArrayBuffer is not set when calling DRAW_TEMP_IMAGE_IMPORT'
);
}
const containRectangle = getContainRectangleInsideRectangle(
context.imageElement.naturalWidth,
context.imageElement.naturalHeight,
context.startPoint,
(event as MouseClickEvent).worldMouseLocation
);
if (!containRectangle) {
return;
}
const activeImage = new ImageEntity(
getActiveLayerId(),
context.imageElement,
boxToPolygon(containRectangle)
);
addEntities([activeImage], true);
},
[ImageImportAction.SWITCH_TO_SELECT_TOOL]: () => {
setActiveToolActor(new Actor(selectToolStateMachine));
},
},
}
);