Files
Aislo/B07_DesignDetail/openwebcad/src/main.tsx
T
eomsangdonandClaude Opus 5.5 32c3081b68 fix(B07 CAD·M02): 가운데 단추 끌기 때 페이지가 같이 움직이던 것 · 집계표 미리보기는 보기 전용 조작
- 캔버스 가운데 단추 mousedown · auxclick 의 기본 동작(자동 스크롤 · 붙여넣기)을 막음 — 휠은 이미 passive:false 로 막음.
  pointerdown 은 막지 않음(막으면 mousedown 이 안 와 팬이 끊김).
- 미리보기(viewOnly — M02 구조물집계표 「도면 정보」 카드): 왼쪽 끌기 = 화면 옮기기 · 휠 확대 ·
  두 번 누름 = 전체 맞춤 · 선택 · 가까운 개체 강조 · 스냅 · 십자 커서 · 명령 안내 · 좌표 풍선 없음(손 모양 커서).
  [도면 수정] 편집 화면 · 도면 양식 · B07 은 그대로.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K7JS47SacjPxZ718QsZKyr
2026-09-28 16:27:55 +09:00

182 lines
5.9 KiB
TypeScript

import './random-uuid-polyfill';
import { Point } from '@flatten-js/core';
import React from 'react';
import ReactDOM from 'react-dom/client';
import { Actor, type MachineSnapshot } from 'xstate';
import { SNAP_POINT_DISTANCE } from './App.consts';
import App from './App.tsx';
import { TOOL_STATE_MACHINES } from './commands/registry';
import { ScreenCanvasDrawController } from './drawControllers/screenCanvas.drawController';
import { registerAutoSave } from './helpers/autosave.ts';
import { registerCadDebugHook } from './helpers/debug-hook.ts';
import { draw } from './helpers/draw';
import { findClosestEntity } from './helpers/find-closest-entity';
import { getNewLayer } from './helpers/get-new-layer.ts';
import { pickRadius } from './helpers/pick-radius';
import { scenePerf } from './helpers/scene-cache';
import { queryEntitiesNearPoint } from './helpers/spatial-index';
import { trackHoveredSnapPoint } from './helpers/track-hovered-snap-points';
import { InputController } from './inputController/input-controller.ts';
import { registerAisloDrawingBridge } from './integration/aislo-drawing-bridge.ts';
import { registerDxfClipPaste } from './integration/dxf-clip-paste.ts';
import {
getActiveToolActor,
getCanvas,
getHoveredSnapPoints,
getLastDrawTimestamp,
getScreenCanvasDrawController,
getSnapPoint,
isViewOnly,
setActiveLayerId,
setActiveToolActor,
setCanvas,
setEntities,
setHighlightedEntityIds,
setHoveredSnapPoints,
setInputController,
setLastDrawTimestamp,
setLayers,
setScreenCanvasDrawController,
} from './state';
import { syncThemeFromHost } from './theme.ts';
import { Tool } from './tools';
import { ActorEvent, type DrawEvent } from './tools/tool.types';
// 호스트 앱의 화이트/블랙 모드를 먼저 붙인 뒤 렌더한다.
syncThemeFromHost();
ReactDOM.createRoot(document.getElementById('root') as HTMLDivElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
// Hover highlight throttle: the closest-entity scan is O(entities) so it runs
// at most every HOVER_THROTTLE_MS and only when the mouse actually moved.
const HOVER_THROTTLE_MS = 30;
let lastHoverCheckAt = 0;
let lastHoverMouseX = Number.NaN;
let lastHoverMouseY = Number.NaN;
function startDrawLoop(
screenCanvasDrawController: ScreenCanvasDrawController,
timestamp: DOMHighResTimeStamp
) {
const lastDrawTimestamp = getLastDrawTimestamp();
const elapsedTime = timestamp - lastDrawTimestamp;
setLastDrawTimestamp(timestamp);
scenePerf.avgFrameMs = scenePerf.avgFrameMs * 0.9 + elapsedTime * 0.1;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const activeToolSnapshot: MachineSnapshot<any, any, any, any, any, any, any, any> | undefined =
getActiveToolActor()?.getSnapshot();
if (
activeToolSnapshot?.status === 'active' &&
activeToolSnapshot?.can({ type: ActorEvent.DRAW })
) {
getActiveToolActor()?.send({
type: ActorEvent.DRAW,
drawController: screenCanvasDrawController,
} as DrawEvent);
}
/**
* Highlight the entity closest to the mouse when the select tool is active
*/
if (getActiveToolActor()?.getSnapshot()?.context?.type === Tool.SELECT && !isViewOnly()) {
const screenCanvasDrawController = getScreenCanvasDrawController();
if (!screenCanvasDrawController) {
throw new Error('getScreenCanvasDrawController() returned null');
}
const mouseLocation = screenCanvasDrawController.getScreenMouseLocation();
const mouseMoved = mouseLocation.x !== lastHoverMouseX || mouseLocation.y !== lastHoverMouseY;
if (mouseMoved && timestamp - lastHoverCheckAt >= HOVER_THROTTLE_MS) {
lastHoverCheckAt = timestamp;
lastHoverMouseX = mouseLocation.x;
lastHoverMouseY = mouseLocation.y;
const worldMouseLocation = screenCanvasDrawController.getWorldMouseLocation();
const { distance, entity: closestEntity } = findClosestEntity(
worldMouseLocation,
// 공간 인덱스로 후보를 좁혀 O(전체) 스캔 제거
queryEntitiesNearPoint(worldMouseLocation.x, worldMouseLocation.y, pickRadius())
);
if (distance < pickRadius()) {
setHighlightedEntityIds([closestEntity.id]);
}
}
}
/**
* Track hovered snap points
*/
trackHoveredSnapPoint(
getSnapPoint(),
getHoveredSnapPoints(),
setHoveredSnapPoints,
SNAP_POINT_DISTANCE / screenCanvasDrawController.getScreenScale(),
elapsedTime
);
/**
* Draw everything on the canvas
*/
draw(screenCanvasDrawController);
requestAnimationFrame((newTimestamp: DOMHighResTimeStamp) => {
startDrawLoop(screenCanvasDrawController, newTimestamp);
});
}
function handleWindowResize() {
const canvas = getCanvas();
if (canvas) {
const bounds = canvas.getBoundingClientRect();
const width = Math.max(1, Math.round(bounds.width));
const height = Math.max(1, Math.round(bounds.height));
canvas.width = width;
canvas.height = height;
getScreenCanvasDrawController().setCanvasSize(new Point(width, height));
}
}
function initApplication() {
const canvas = document.getElementsByTagName('canvas')[0] as HTMLCanvasElement | null;
if (canvas) {
setCanvas(canvas);
const context = canvas.getContext('2d');
if (!context) return;
setEntities([], true); // Creates the first undo entry
const layers = [getNewLayer()];
setLayers(layers);
setActiveLayerId(layers[0].id);
registerAisloDrawingBridge();
registerDxfClipPaste();
registerCadDebugHook();
registerAutoSave();
const screenCanvasDrawController = new ScreenCanvasDrawController(context);
setScreenCanvasDrawController(screenCanvasDrawController);
window.addEventListener('resize', handleWindowResize);
new ResizeObserver(handleWindowResize).observe(canvas);
const inputController = new InputController();
setInputController(inputController);
handleWindowResize();
startDrawLoop(screenCanvasDrawController, 0);
const lineToolActor = new Actor(TOOL_STATE_MACHINES[Tool.LINE]);
lineToolActor.start();
setActiveToolActor(lineToolActor);
}
}
document.addEventListener('DOMContentLoaded', () => {
initApplication();
});