Files
Aislo/B05_Profile/B05_Profile_UI_RouteEdit_History.ts
T
eomsangdonandClaude Opus 5 f84b3d9451 feat(B05): 노선 편집 — R 라벨을 고른 자리 옆으로, 되돌리기·다시하기·초기화, 줌·팬 통일
사용자 지적 ③④⑤⑥ 과 추가 지시(되돌리기)를 한 묶음으로 처리.

- **③ R 라벨** — 모달 맨 아랫줄이던 곡선 편집칸을 걷고, 고른 꺾임점 **옆에 뜨는 라벨**로
  바꿈(`_Label.ts` 신설). 캔버스 밖으로 나가면 반대쪽으로 접어 넣음. 확대·이동·창 크기가
  바뀌어도 고른 노드를 따라감.
- **④ 노드가 손잡이보다 먼저 잡히게** — 반대였던 탓에 헤어핀처럼 곡선이 몰린 데서는
  노드를 아예 못 집었음. 손잡이는 **고른 곡선에만** 그림. 머리말 안내에 조작 여섯을 적음.
- **⑤ 줌·팬을 배수유역도와 동일하게**(`_Input.ts` 신설) — 휠은 **당기면 확대**(반대였음),
  계수 1.15/0.87, 상한은 화면 폭 16m 기준, 하한 0.5, **팬은 가운데 버튼 전용**.
  ⚠ 커서 고정 계산이 좌상단 기준이라 확대할수록 지점이 밀리던 것도 중심 기준으로 고침.
- **⑥ 등고선을 노선 ±300m 띠로** — 창 크기와 무관한 고정 띠라 창을 늘려도 안 깨짐.
  화면 밖 걸러내기는 종전대로 `drawPreparedLayer` 가 함.
- **되돌리기·다시하기·초기화**(`_History.ts` 신설) — [확인]이 무거워 되돌릴 길이 없으므로
  창 안에서 물릴 수 있게 함. **끌기 한 번이 한 걸음**이고 클릭만으로는 걸음이 안 생김.
  [초기화]는 **이 창을 연 상태**로 (≠ [예상노선으로]). Ctrl+Z / Ctrl+Y · Ctrl+Shift+Z.
- 700줄 제한 — 집기(hit test)를 `_Input.ts` 로 옮겨 본체 695줄.

실화면 검증(5174) — 라벨이 노드 오른쪽 [+18,−38]px 에 뜸 / 휠 당기니 중심에서 373.1 →
428.1px 로 확대 / 왼쪽 끌기로는 지도 안 움직임 / 가운데 버튼 끌기는 [60,40] 그대로 따라옴 /
단추 켜짐이 여섯 단계 모두 맞음. 전체 554 passed · 18 skipped, tsc 통과. 정본 안 건드림.

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

162 lines
6.4 KiB
TypeScript

/* =============================================================================
* B05_Profile_UI_RouteEdit_History.ts
* 노선 편집의 **되돌리기·다시하기·초기화** (2026-09-07 사용자 지시).
*
* 왜 필요한가 — [확인]은 배수유역부터 다시 도는 무거운 작업이라 되돌릴 길이 없다. 그러니
* **창 안에서** 실수를 물릴 수 있어야 한다. 여기서 말하는 [초기화]는 **이 창을 연 상태**로
* 돌아가는 것이지, 예상노선으로 되돌리는 것(`[예상노선으로]`, 서버 재계산)이 아니다.
*
* 값을 통째로 사진처럼 담는다(델타 아님) — 노드·곡선 켬끔·반지름이 서로 엮여 있어 델타로
* 쪼개면 되돌릴 때 어긋나기 쉽다. 노선 하나가 노드 수십 개라 사진 몇 벌은 가볍다.
* ========================================================================== */
export type Vertex = [number, number];
/** 되돌릴 수 있는 편집 상태 한 벌. */
export interface RouteEditSnapshot {
planned: Vertex[];
curveOn: boolean[];
curveRadius: Array<number | null>;
picked: number;
}
/** 쌓아 둘 사진 수 상한 — 넘으면 오래된 것부터 버린다. */
const MAX_STEPS = 100;
export interface RouteEditHistory {
/** 편집 한 번이 끝났다 — 지금 상태를 사진으로 쌓는다(다시하기 갈래는 버린다). */
commit: (snapshot: RouteEditSnapshot) => void;
/** 한 걸음 뒤로. 되돌릴 것이 없으면 null. */
undo: () => RouteEditSnapshot | null;
/** 한 걸음 앞으로. 없으면 null. */
redo: () => RouteEditSnapshot | null;
/** 창을 연 상태로. 이미 그 상태면 null. */
reset: () => RouteEditSnapshot | null;
canUndo: () => boolean;
canRedo: () => boolean;
/** 창을 연 뒤로 고친 것이 있나 — [초기화]를 켤지 정한다. */
isDirty: () => boolean;
}
/** 사진을 깊이 복사한다 — 배열을 그대로 담으면 뒤이은 편집이 과거까지 바꾼다. */
function clone(snapshot: RouteEditSnapshot): RouteEditSnapshot {
return {
planned: snapshot.planned.map(([x, y]): Vertex => [x, y]),
curveOn: [...snapshot.curveOn],
curveRadius: [...snapshot.curveRadius],
picked: snapshot.picked,
};
}
/** 두 사진이 **같은 노선**인가 — [초기화]를 켤지 정할 때 쓴다. 고른 자리는 안 본다. */
function sameRoute(a: RouteEditSnapshot, b: RouteEditSnapshot): boolean {
if (a.planned.length !== b.planned.length) return false;
for (let index = 0; index < a.planned.length; index += 1) {
if (a.planned[index][0] !== b.planned[index][0]) return false;
if (a.planned[index][1] !== b.planned[index][1]) return false;
if (a.curveOn[index] !== b.curveOn[index]) return false;
if (a.curveRadius[index] !== b.curveRadius[index]) return false;
}
return true;
}
/** 첫 사진(창을 연 상태)으로 이력을 연다. */
export function createRouteEditHistory(initial: RouteEditSnapshot): RouteEditHistory {
const steps: RouteEditSnapshot[] = [clone(initial)];
let at = 0;
return {
commit(snapshot) {
// 되돌린 뒤 새로 고치면 앞쪽 갈래는 버린다 — 흔한 되돌리기 규칙 그대로.
steps.length = at + 1;
steps.push(clone(snapshot));
if (steps.length > MAX_STEPS) steps.shift();
at = steps.length - 1;
},
undo() {
if (at <= 0) return null;
at -= 1;
return clone(steps[at]);
},
redo() {
if (at >= steps.length - 1) return null;
at += 1;
return clone(steps[at]);
},
reset() {
// 이미 연 상태 그대로면 할 일이 없다 — 눌러도 걸음만 늘어난다.
if (sameRoute(steps[at], steps[0])) return null;
// 초기화도 **되돌릴 수 있어야** 한다 — 첫 사진을 새 걸음으로 쌓는다.
steps.length = at + 1;
steps.push(clone(steps[0]));
at = steps.length - 1;
return clone(steps[at]);
},
canUndo: () => at > 0,
canRedo: () => at < steps.length - 1,
isDirty: () => !sameRoute(steps[at], steps[0]),
};
}
export interface HistoryControlsParams {
/** 단추가 들어 있는 칸 — `[data-act]` 로 찾는다. */
overlay: HTMLElement;
/** 아직 노선을 못 읽었으면 null 이다(단추는 꺼진 채로 둔다). */
getHistory: () => RouteEditHistory | null;
/** 사진 한 벌을 화면에 되살린다. */
restore: (snapshot: RouteEditSnapshot, message: string) => void;
}
/** [초기화]·[되돌리기]·[다시하기] 단추와 단축키(Ctrl+Z / Ctrl+Y / Ctrl+Shift+Z)를 붙인다.
*
* 돌려주는 `sync` 를 편집이 끝날 때마다 부르면 단추 켜짐이 맞춰진다. `dispose` 는 창을
* 닫을 때 부른다 — 단축키를 창(window)에 달았기 때문에 안 떼면 닫힌 뒤에도 살아 있다. */
export function bindHistoryControls(params: HistoryControlsParams): {
sync: () => void;
dispose: () => void;
} {
const { overlay, getHistory, restore } = params;
const undoBtn = overlay.querySelector<HTMLButtonElement>('[data-act="undo"]')!;
const redoBtn = overlay.querySelector<HTMLButtonElement>('[data-act="redo"]')!;
const resetBtn = overlay.querySelector<HTMLButtonElement>('[data-act="history-reset"]')!;
const sync = (): void => {
const history = getHistory();
undoBtn.disabled = !history?.canUndo();
redoBtn.disabled = !history?.canRedo();
resetBtn.disabled = !history?.isDirty();
};
const step = (which: "undo" | "redo" | "reset"): void => {
const history = getHistory();
if (!history) return;
const snapshot = history[which]();
if (!snapshot) return;
restore(
snapshot,
which === "undo"
? "되돌렸습니다."
: which === "redo"
? "다시 했습니다."
: "창을 연 상태로 돌렸습니다.",
);
sync();
};
undoBtn.addEventListener("click", () => step("undo"));
redoBtn.addEventListener("click", () => step("redo"));
resetBtn.addEventListener("click", () => step("reset"));
const onKey = (event: KeyboardEvent): void => {
if (!(event.ctrlKey || event.metaKey)) return;
const key = event.key.toLowerCase();
if (key === "z" && !event.shiftKey) step("undo");
else if (key === "y" || (key === "z" && event.shiftKey)) step("redo");
else return;
event.preventDefault();
};
window.addEventListener("keydown", onKey);
return { sync, dispose: () => window.removeEventListener("keydown", onKey) };
}