Files
Aislo/A00_Common/line_stack_edit.ts
T
eomsangdonandClaude Opus 5.5 a8d6b426d0 refactor(856-4): 거꾸로 부름 끊기 본문 — 앞 커밋(bd41d8799)은 이름 옮김만 담김 · 이 커밋과 한 묶음
- 앞 커밋 글 그대로(층 · 부름 common_util_m02_call · 찾아 넣기 창 StackFind 주입 · 단위 고르기 부품 상자 · 합계 상자 A00 · 기계 칸 부품)
- 앞 커밋에서 git add 가 옛 경로에 걸려 고친 파일이 빠짐 — 두 커밋을 같이 올림

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 21:02:55 +09:00

639 lines
27 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* =============================================================================
* line_stack_edit.ts
* 줄 쌓기 왼쪽 편집(PLAN 804-2 · 요구 670~677) — 항목(띠 · 번호 · 제목 · 가나다 · [+ 줄] · 접기 · X) 안 줄 카드(⋮⋮ · X)
* 차림(804-5 · 요구 682) = 항목 머리 띠 · 줄 = 들여쓰기 + 왼쪽 세로 선 · 순서 = 손잡이 끌기(`line_stack_drag.ts`) ·
* X = 확인 없이 지우고 알림 눌러 되돌리기 한 번 · 자원 이름 = 단추(누르면 찾아 넣기 창 → 바꿈)
* 줄 = 인력 · 재료 · 기계 · 다른 단가산출 · 율 · 식(금액 줄 — 이름 · 단위 · 비목 · 수량 식 · 단가 고르기 또는 식)
* · 공식 묶음(`line_stack_rows.ts`) · 글 줄
* 고침 = 부른 쪽 `change(fn)` — fn 이 문서 · 차림을 고치면 부른 쪽이 `docOf` 로 굳히고 미리 셈 · 다시 그림(입력은 change 때)
* 자원 고르기(804-3) = 찾아 넣기 창(`line_stack_pick.ts` — M02 [항목 추가] 창 그대로) · [+ 줄] 인력 · 재료 · 기계 · 다른 단가산출 =
* 고르면 줄이 생김(닫으면 안 생김) · 있는 줄 = 자원 이름 단추
* ========================================================================== */
import { createBadge } from "@ui/ui_template_badge";
import { createButton, createIconButton, setUiDiff } from "@ui/ui_template_button";
import { createRowMenu } from "@ui/ui_template_context_menu";
import { eqText } from "@ui/ui_template_equation";
import { el, showToast } from "@ui/ui_template_elements";
import { createInputField, createSelectField } from "@ui/ui_template_input";
import { varId, type FormulaDoc, type MoneyLine } from "../common_util/common_util_formula_doc";
import { isNum, type DocAnswer } from "../common_util/common_util_formula_doc_calc";
import { lineMap, offLine, type Values } from "../common_util/common_util_formula_doc_view";
import type { DocTable } from "../common_util/common_util_formula_doc_table";
import {
coverOf,
layeredCover,
type Cover,
type CoverEntry,
} from "../common_util/common_util_formula_doc_cover";
import { HOURLY, Q_NEW, swap, workKey } from "../common_util/common_util_formula_doc_work";
import { attachHost, workAt } from "../common_util/common_util_line_stack_work";
import { machineText } from "./formula_doc_options";
import {
dropSyms,
docOf,
freshId,
kindOf,
moveItem,
moveLine,
newVar,
ownedOf,
setRow,
stackOf,
type MoneyKind,
type StackDoc,
type StackItem,
} from "../common_util/common_util_line_stack";
import { placeNames } from "../common_util/common_util_line_stack_copy";
import { askFileWide, materialMark, matEntry } from "./line_stack_material";
import { draftsOf, machineNow, pruneDrafts } from "../common_util/common_util_line_stack_draft";
import {
addPicked,
type Picked,
aliasText,
repick,
tableAlias,
unswap,
} from "../common_util/common_util_line_stack_pick";
import { shownUnitOf, type TableLook } from "../common_util/common_util_line_stack_unit";
import { createUnitPick, NO_UNIT } from "@ui/ui_template_unit_pick";
import { hiddenByWork, reach } from "./formula_doc_render";
import { attachDrag } from "./line_stack_drag";
import type { WorkSpec } from "../common_util/common_util_formula_doc_work";
import { openMachineWindow, type MachineCtx } from "./line_stack_machine";
import { dropCover } from "../common_util/common_util_formula_doc_cover_drop";
import { buildMachineCtl, lineCall } from "./line_stack_machine_ctl";
import { putWork, swapMachine } from "./line_stack_machine_work";
import { openStackPick, type StackFind, type StackTake } from "./line_stack_pick";
import { buildBlock, sourceTag } from "./line_stack_rows";
import { lt, type LsKey } from "./line_stack_text";
export interface EditHooks {
doc: StackDoc;
answer: DocAnswer;
items: StackItem[];
change: (fn: (d: StackDoc, items: StackItem[]) => void) => void;
/** 품셈 표 하나(표 고르기 창) */
table: (key: string) => Promise<DocTable | null>;
subDoc: (key: string) => FormulaDoc | null;
name: (key: string) => string;
/** 공식 묶음 펼친 자리(`항목.줄`) — 화면 상태만 */
open: Set<string>;
/** 줄 · 항목 누름 — 오른쪽 짝 강조(679) */
pair: (loc: string) => void;
/** M02 단가산출(804-4) — 다른 단가산출 · 일위대가 = 복사해 펼침 · M01 = 없음(M01 전체만) */
take?: StackTake;
/** 저장본 식 줄 {기호: 식} — 만약 식을 고쳐 굳은 줄 표지(요구 696) */
before?: ReadonlyMap<string, unknown>;
/** 827-1 기계 줄 = 기계 창(찾기 · 바꾸기 · 836-1) — 층 · 사본 줄 */
machine?: MachineCtx;
/** 836-1 기계 제어 구역 — 덮음 없이 그 기계 식 셈(원본 = 기본값) */
calc?: (doc: FormulaDoc, given: Record<string, unknown>) => DocAnswer;
/** 842-3 그 기계 줄 인력 · 잡품 목록(기본값 바탕) */
lists?: (given: Record<string, unknown>) => Record<string, unknown> | null;
/** 845-2 표 한 칸 읽기(캐시) — 「줄 따름」 값의 고른 줄 단위 */
look?: TableLook;
/** 846-2 그 기계 작업량 줄 */
work?: (machine: string) => WorkSpec[];
/** 848-3 ① 위 층 고침(SK 읽기 답 `위덮음` · M01 = 없음) */
upper?: Cover | null;
/** 848-3 M01 기초 제어 구역 엶(시스템 관리자) — M02 = 층(`machine.scope`)이 엶 */
ctlEdit?: boolean;
/** 856-4 찾아 넣기 창(페이지가 넘김) */
find: StackFind;
}
const field = (
label: string,
value: unknown,
set: (t: string) => void,
size: "s" | "m" | "l" = "m",
): HTMLElement => {
const f = createInputField({
placeholder: label,
value: value === undefined || value === null ? "" : String(value),
compact: true,
size: 24,
});
f.root.classList.add(`lstk-f-${size}`);
f.input.setAttribute("aria-label", label);
f.input.title = label;
f.input.addEventListener("change", () => set(f.input.value));
return f.root;
};
const btn = (label: string, onClick: () => void) =>
createButton({ label, variant: "ghost", size: 24, onClick });
const KIND_KEY: Record<MoneyKind, LsKey> = {
인력: "K_인력",
재료: "K_재료",
기계: "K_기계",
단가산출: "K_단가산출",
율: "K_율",
};
/** 가져온 기계 찾기 판정(서버 기계 찾기 · 문서 `가져온.기계`) — 그 단가 기호 칸 */
interface FoundMach {
기호?: string;
판정?: string;
찾기어?: string;
조합?: boolean;
후보?: { 이름?: string; 규격?: string }[];
}
function machineFound(d: FormulaDoc, sym: string): FoundMach | undefined {
const head = (d as { 가져온?: { 기계?: FoundMach[] } }).가져온;
return head?.기계?.find((x) => x.기호 === sym);
}
/** 못 이은 가져온 기계 줄 표지(고르면 사라짐 — 연결이 단가산출로 바뀜) · 조합 기계 다음 줄 */
function machineMark(f: FoundMach | undefined, link: { 갈래?: string }): HTMLElement | null {
if (!f) return null;
const picked = link.갈래 === "단가산출";
const key = f.판정 === "후보 여럿" ? "Mach_Many" : f.판정 === "못 찾음" ? "Mach_None" : null;
const badge =
key && !picked
? createBadge(lt(key), "warning", { size: 18 })
: f.조합
? createBadge(lt("Mach_Combo"), "neutral", { size: 18 })
: null;
if (badge && key && f.후보?.length)
badge.title = f.후보.map((c) => `${c.이름 ?? ""} ${c.규격 ?? ""}`.trim()).join(" · ");
return badge;
}
/** 금액 줄 몸 — 한 줄(804-7 · 요구 687-7): [종류][이름 단추 | 이름 칸 · 단가 식][수량][단위][비목] · 좁으면 접힘 */
function moneyBody(h: EditHooks, i: number): HTMLElement[] {
const d = h.doc;
const line = (d.금액 ?? [])[i];
const kind = kindOf(d, i, h.subDoc);
const own = ownedOf(d).get(i);
const v = own ? d.변수?.find((x) => varId(x) === own) : undefined;
const put = (fn: (l: MoneyLine & { 규격?: string }) => void): void =>
h.change((doc) => fn(doc.금액![i]));
// 표 값 기호 = 화면 이름(요구 683) — 칸 글은 화면 이름 ↔ 기호로 오감
const alias = tableAlias(d);
const shown = (t: unknown): string => aliasText(String(t ?? ""), alias);
const real = (t: string): string => aliasText(t.trim(), alias, true);
const head: HTMLElement[] = [];
const w = kind === "기계" ? workAt(h.answer, i) : null;
const hourly = w?.꼴 === HOURLY; // 847-1 시간당 식 줄 — 기계 = 굳은 기계 · 수량 = 읽기 글
const host = kind === "기계" ? attachHost(d, i) : null; // 848-4 부착 줄 = 수량 읽기 글
if (own && v) {
// 이름 단추(요구 687-9) — 누르면 찾아 넣기 창이 그 이름을 찾은 채 열림 · 고르면 바꿈
const link = v.연결 ?? {};
const mat = link.재료 as Record<string, unknown> | undefined;
const size = (line as { 규격?: string }).규격;
const name = String(line.이름 ?? "") || String(mat?.검색어 ?? mat?.이름 ?? "");
const res = link.키
? link.갈래 === "단가산출"
? (h.subDoc(link.키)?.이름 ?? h.name(link.키))
: h.name(link.키)
: "";
// 가져온 기계 줄(812-9) — 찾기 판정 · 조회어 = 기종 이름만(규격 뺌 · 이은 우리 기계 › 정규화 이름)
const found = kind === "기계" ? machineFound(d, own) : undefined;
const query = found?.찾기어 ?? String(mat?.검색어 ?? line.이름 ?? "");
const swap = (p: Picked): void => {
h.change((doc) => {
repick(doc, i, own, kind, p);
pruneDrafts(doc); // 827-1 안 쓰게 된 초안 뺌
});
if (kind === "재료") void askFileWide(h.machine, d, own, p); // 827-3 같은 파일 같은 재료
};
// 847-1 시간당 식 줄 = 굳은 기계를 덮음으로 바꿈(문서 줄 연결은 그대로 — 일당으로 되돌리면 옵션 기계)
const lay = layeredCover(d, h.upper); // 848-3 ① 위에 ②
const wkey = w ? workKey(lay, w) : "";
const want = hourly ? (lay[wkey] as CoverEntry | undefined)?.작업량 : undefined;
const upWork = (h.upper?.[wkey] as CoverEntry | undefined)?.작업량 ?? null;
const fixed = hourly && w?.고정 ? w.고정 : "";
const swapFixed = (p: Picked): void => {
const got = swapMachine(want ?? { 방식: HOURLY }, w!, p, p.키 === link.키, h.work?.(fixed) ?? []); // prettier-ignore
h.change((doc) => putWork(doc, wkey, got.next, upWork));
if (got.note) showToast(got.note, "info");
};
const label = fixed
? want?.이름 || machineText(fixed, () => h.change(() => undefined)) || name
: size && !name.includes(size)
? `${name} ${size}`
: name;
const pick = createButton({
label: label || "—",
variant: "ghost",
tone: "link",
size: 24,
iconStart: "search",
onClick: () =>
kind === "기계" && h.machine // 827-1 기계 창
? openMachineWindow({
ctx: h.machine,
drafts: fixed ? [] : draftsOf(d),
now: fixed || machineNow(d, link as { 갈래?: string; 입력?: Record<string, string> }), // prettier-ignore
query,
pick: fixed ? swapFixed : swap,
})
: openStackPick(h.find, kind, swap, undefined, query),
});
pick.classList.add("lstk-res");
pick.title = `${lt("Repick")}${res ? ` — ${res}` : ""}`;
pick.dataset.repick = String(i);
head.push(pick);
if (kind === "기계" && h.calc) {
// 836-1 기계 제어 구역 접기 · 고친 기계 표지
const on = h.open.has(`mctl:${i}`);
const t = createIconButton({
icon: on ? "chevronDown" : "chevronRight",
title: lt("Ctl_Open"),
size: 24,
onClick: () => {
if (on) h.open.delete(`mctl:${i}`);
else h.open.add(`mctl:${i}`);
h.change(() => undefined);
},
});
t.dataset.mctlToggle = String(i);
head.push(t);
// 848-3 고친 기계 = 이름 단추 테두리(나 = 파랑) · ㉢ 기계 한 줄 통째 [원래 값]
const m = lineCall(d, h.answer, link, i, h.upper);
const keys = [...new Set([m.work, String(m.given.기계 ?? "")])].filter(Boolean);
const mine = keys.some((k) => coverOf(d)[k]);
setUiDiff(pick, mine ? "mine" : null);
if (mine && (h.machine?.scope || h.ctlEdit)) {
const back = createButton({ label: lt("Ctl_Back"), variant: "ghost", size: 24, iconStart: "undo", onClick: () => h.change((doc) => dropCover(doc, keys)) }); // prettier-ignore
back.title = lt("Ctl_Line_Back");
back.dataset.mctlLineBack = String(i);
head.push(back);
}
}
const mark = machineMark(found, link);
if (mark) head.push(mark);
const matMark = kind === "재료" ? materialMark(matEntry(d, own), link) : null; // 827-3 판정 · 단서
if (matMark) {
head.push(matMark);
pick.title += ` — ${matMark.title}`;
}
// 823-1 가져온 줄 바꿔 끼움 = 우리 값 · 「바꿈」 + 되돌리기(원래 것 · 엑셀 값)
const sw = (link as { 바꿈?: { 이름?: string; 규격?: string } }).바꿈;
if (sw) {
const was = `${sw.이름 ?? ""} ${sw.규격 ?? ""}`.trim();
const tag = createBadge(lt("Swapped"), "info", { size: 18 });
tag.title = `${lt("Swapped_Note")} — ${was}`;
tag.dataset.swapped = String(i);
const back = createButton({
label: lt("Unswap"),
variant: "ghost",
size: 24,
iconStart: "undo",
onClick: () => h.change((doc) => unswap(doc, i, own)),
});
back.title = was;
back.dataset.unswap = String(i);
head.push(tag, back);
}
} else {
head.push(
field(lt("Name"), line.이름, (t) => put((l) => (l.이름 = t)), "m"),
field(lt("Price"), shown(line.단가), (t) => put((l) => (l.단가 = real(t))), "l"),
);
const sym = String(line.단가 ?? "").replace(/\.계$/, ""); // 827-3 못 이은 재료 판정 · 별도계상 품목
const unlinked = materialMark(matEntry(d, sym), d.변수?.find((x) => varId(x) === sym)?.연결 ?? {}); // prettier-ignore
if (unlinked) head.push(unlinked);
}
const why = (h.answer.줄?.[i] as { 까닭?: string } | undefined)?.까닭;
// 율 밑 번지(복사해 온 조각) — 칸 아래 읽는 이름(804-5)
const places = /번지\./.test(`${line.단가} ${line.수량 ?? ""}`) ? placeNames(d, h.items) : null;
const read = (t: unknown): string =>
shown(t).replace(/번지\.([^.\s()]+)\./g, (all, p: string) =>
places?.[p] ? `${places[p]} ` : all,
);
const unit = el("span", { className: "lstk-upick" });
const drawUnit = (): void =>
unit.replaceChildren(
createUnitPick({
value: String(line.단위 ?? "") || NO_UNIT,
size: 24,
free: true,
ariaLabel: lt("Unit"),
extra: [...new Set((d.금액 ?? []).map((m) => String(m.단위 ?? "")).filter(Boolean))],
onPick: (u) => put((l) => (u === NO_UNIT ? delete l.단위 : (l.단위 = u))),
}),
);
drawUnit();
const bare = !!line.합밖 && !line.비목; // 합 밖 소계 · 합계 줄(810-2) = 금액만 · 비목 칸 빈칸
return [
el("span", { className: "lstk-kind", text: lt(bare ? "K_소계" : KIND_KEY[kind]) }),
...head,
// 율 · 식 줄(자원 없음)에 수량이 없으면 칸 안 보임(한 줄 유지)
...(host !== null
? [readQty(`${lt("Follow_Host")} — ${host}`, lt("Follow_Note"), "follow")]
: hourly
? [workQtyBox(h.doc, line.수량, shown)]
: own || String(line.수량 ?? "")
? [qtyBox(h, i, shown, real)]
: []),
unit,
...(line.비목 === "묶음" || bare
? []
: [
createSelectField({
options: ["노무비", "재료비", "경비"].map((x) => ({ value: x, text: x })),
value: line.비목 ?? "노무비",
compact: true,
size: 24,
ariaLabel: lt("Cost"),
onChange: (x) => put((l) => (l.비목 = x)),
}).root,
]),
...(places
? [
el("div", {
className: "lstk-hint lstk-wide",
text: `= ${[line.수량, line.단가].filter(Boolean).map(read).join(" × ")}`,
}),
]
: []),
...(why ? [el("div", { className: "lstk-miss lstk-wide", text: why })] : []),
];
}
/** 수량 = 늘 식 글 칸(기호 하나면 그 이름 · 요구 701-1) + 그 기호에 연결이 있으면 옆 출처 꼬리표(표 = 표 고르기) ·
* 칸을 눌러도 안 바뀜 — 글을 실제로 고칠 때만: 표 값 기호 자리에 수를 치면 그 기호 값(별칭 · 「직접 입력」 · 804-9) ·
* 그 밖 = 수량 식 */
function qtyBox(
h: EditHooks,
i: number,
shown: (t: unknown) => string,
real: (t: string) => string,
): HTMLElement {
const line = (h.doc.금액 ?? [])[i];
const text = String(line.수량 ?? "");
const v = h.doc.변수?.find((x) => varId(x) === text);
const f = field(lt("Qty"), shown(text), (t) =>
h.change((doc) => {
const want = real(t);
if (v?.연결?.표 && /^-?[\d.,]+$/.test(want)) return setRow(doc, text, want);
if (want) doc.금액![i].수량 = want;
else delete doc.금액![i].수량;
}),
);
f.dataset.qty = String(i);
const val = v ? (h.answer.값 ?? {})[text] : undefined;
// 838-3 공식 · 표 값 묶음에 이미 보이는 기호 = 줄 쪽 표 칩 걷음(묶음에서 고름) · 묶음에 없으면 줄에서 고름
const inBlock = h.items.some((it) => it.줄.some((l) => l.꼴 === "공식" && l.기호.includes(text)));
const tag = v && !inBlock ? sourceTag(h, text, v.연결) : null;
if (tag && isNum(val)) {
const u = shownUnitOf(v, (h.answer.값 ?? {}) as Record<string, unknown>, h.look);
tag.title = `${eqText(val)}${u ? ` ${u}` : ""} — ${tag.title || tag.textContent}`;
}
return el("span", { className: "lstk-qty", children: tag ? [f, tag] : [f] });
}
/** 847-1 시간당 식 줄 수량 = 「1 ÷ 시간당 작업량」 읽기 글(손으로 못 고침 — 변수는 제어 구역) */
function workQtyBox(doc: FormulaDoc, qty: unknown, shown: (t: unknown) => string): HTMLElement {
return readQty(shown(swap(doc, qty)).replace(Q_NEW, lt("Work_Head")), lt("Work_Qty_Note"), "workQty"); // prettier-ignore
}
/** 읽기 수량 칸(시간당 식 줄 · 848-4 부착 줄) */
function readQty(text: string, note: string, tag: string): HTMLElement {
const f = field(lt("Qty"), text, () => undefined);
const input = f.querySelector("input");
if (input) {
input.disabled = true;
input.title = note;
}
f.dataset[tag] = "";
return el("span", { className: "lstk-qty", children: [f] });
}
/** [+ 줄] 고르개 값 → 새 줄 */
function addLine(h: EditHooks, at: number, kind: string): void {
if (["인력", "재료", "기계", "단가산출"].includes(kind))
return openStackPick(
h.find,
kind as MoneyKind,
(p) => h.change((d, items) => void addPicked(d, items[at], kind as MoneyKind, p)),
h.take,
);
h.change((d, items) => {
const it = items[at];
const n = (d.금액 ??= []).length;
const money = (line: MoneyLine): void => {
d.금액!.push(line);
it.줄.push({ 꼴: "금액", i: n });
};
if (kind === "율")
money({ 이름: "공구손료 및 경장비", 단위: "식", 비목: "경비", 단가: "노무비 × 3 %" });
else if (kind === "공식") {
const id = freshId(d, "x");
(d.변수 ??= []).push(newVar(id, { 갈래: "숫자", 값: 0 }));
it.줄.push({ 꼴: "공식", 기호: [id] });
h.open.add(`${at}.${it.줄.length - 1}`);
} else if (kind === "글") it.줄.push({ 꼴: "글", 글: "" });
});
}
/** 줄 지움 — 금액 줄은 그 줄이 가진 연결 기호까지 · 공식 묶음은 기호까지 */
function dropLine(d: StackDoc, items: StackItem[], i: number, j: number): void {
const line = items[i].줄[j];
if (line.꼴 === "금액") {
const own = ownedOf(d).get(line.i);
if (own) dropSyms(d, [own]);
} else if (line.꼴 === "공식") dropSyms(d, line.기호);
items[i].줄.splice(j, 1);
}
export function buildEdit(h: EditHooks): HTMLElement {
// [+] 줄 더함 메뉴(요구 687-13 · 18) — 인력 · 재료 · 기계 · 단가산출 · 율 · 식 · 근거 글
const add = (at: number): HTMLElement => {
const kinds: [string, LsKey][] = [
["인력", "K_인력"],
["재료", "K_재료"],
["기계", "K_기계"],
["단가산출", h.take ? "K_복사" : "K_단가산출"],
["율", "Add_율"],
["공식", "Add_식"],
["글", "K_글"],
];
const menu = createRowMenu(
kinds.map(([k, label]) => ({ label: lt(label), onClick: () => addLine(h, at, k) })),
{ icon: "plus", title: lt("Add_Line"), size: 24 },
);
menu.classList.add("lstk-add");
return menu;
};
// X 지움 — 확인 없이 · 알림 누르면 되돌림(그 뒤 다른 고침이 없을 때만)
const drop = (what: string, fn: (d: StackDoc, items: StackItem[]) => void): void => {
const snap = JSON.stringify(h.doc);
let after = "";
h.change((d, items) => {
fn(d, items);
after = JSON.stringify(docOf(d, items));
});
showToast(`${what || lt("K_줄")} ${lt("Dropped")}`, "info", 6000, () =>
h.change((d, items) => {
if (JSON.stringify(docOf(d, items)) !== after)
return void showToast(lt("Undo_Late"), "warning");
const back = JSON.parse(snap) as StackDoc;
for (const k of Object.keys(d)) delete (d as Record<string, unknown>)[k];
Object.assign(d, back);
items.splice(0, items.length, ...stackOf(back));
}),
);
};
const x = (title: string, onClick: () => void): HTMLElement => {
const b = createIconButton({ icon: "x", title, size: 24, onClick });
b.classList.add("lstk-x");
return b;
};
const grip = (): HTMLElement =>
el("span", { className: "ui-list-row__grip lstk-grip", attrs: { title: lt("Drag") } });
const root = el("div", { className: "lstk-edit" });
/** 항목 접힘 표시(화면 상태)를 항목 차례 바뀜에 맞춤 */
const refold = (fn: (flags: boolean[]) => void): void => {
const flags = h.items.map((_, k) => h.open.has(`fold:${k}`));
fn(flags);
h.items.forEach((_, k) => h.open.delete(`fold:${k}`));
flags.forEach((f, k) => f && h.open.add(`fold:${k}`));
};
// 항목 번호 — 산출근거와 같은 셈(제목 있는 항목 1. 2. · 옛 가나다 항목 = 가. 나. · 체크 걷음 요구 687-5)
let n = 0;
let sub = 0;
const nos = h.items.map((it) => {
if (!it.제목.trim()) return "";
const no = it.가나다 ? `${"가나다라마바사아자차카타파하"[sub++] ?? "?"}.` : `${++n}.`;
if (!it.가나다) sub = 0;
return no;
});
// 만약(IF) 안 탄 갈래(요구 687-10) — 갈래를 다 열면 닿고 지금 값으로는 안 닿는 기호 · 꺼진 금액 줄 = 숨김(산출근거와 같음)
const vals = (h.answer.값 ?? {}) as Values;
const taken = new Set(reach(h.doc, vals));
const every = new Set(reach(h.doc, {}, true));
const gone = hiddenByWork(h.doc, h.answer); // 847-1 시간당 식 줄이 안 읽게 된 기호
const unused = (id: string): boolean => (every.has(id) && !taken.has(id)) || gone.has(id);
const steps = lineMap(h.doc);
const off = (line: StackItem["줄"][number]): boolean =>
line.꼴 === "금액"
? offLine((h.doc.금액 ?? [])[line.i] ?? {}, vals, steps)
: line.꼴 === "공식" && line.기호.length > 0 && line.기호.every(unused);
const items = h.items.map((it, i) => {
const folded = h.open.has(`fold:${i}`);
const itemGrip = grip();
// 그룹 머리 한 줄(요구 687-6 · 688) — 손잡이 · 접기 · 번호 · 이름 · [+] · X · 접어도 자리 그대로
const head = el("div", {
className: "lstk-ihead",
children: [
itemGrip,
createIconButton({
icon: folded ? "chevronRight" : "chevronDown",
title: folded ? lt("Unfold_Item") : lt("Fold_Item"),
size: 24,
onClick: () => {
if (folded) h.open.delete(`fold:${i}`);
else h.open.add(`fold:${i}`);
h.change(() => undefined);
},
}),
el("b", { className: "lstk-no", text: nos[i] }),
field(lt("Title"), it.제목, (t) => h.change((_, items) => (items[i].제목 = t.trim())), "l"),
el("span", {
className: "lstk-hint lstk-count",
text: folded ? `${lt("K_줄")} ${it.줄.length}` : "",
}),
add(i),
x(lt("Del_Item"), () =>
drop(it.제목.trim() || lt("Item"), (d, items) => {
refold((f) => f.splice(i, 1));
for (let j = items[i].줄.length - 1; j >= 0; j--) dropLine(d, items, i, j);
items.splice(i, 1);
}),
),
],
});
const lines = it.줄.flatMap((line, j) => {
if (off(line)) return [];
const lineGrip = grip();
const del = x(lt("Del"), () =>
drop(line.꼴 === "금액" ? String(h.doc.금액?.[line.i]?.이름 ?? "") : "", (d, items) =>
dropLine(d, items, i, j),
),
);
const body =
line.꼴 === "금액"
? moneyBody(h, line.i)
: line.꼴 === "글"
? [
el("span", { className: "lstk-kind", text: lt("K_글") }),
field(
lt("Text_Ph"),
line.글,
(t) => h.change((_, items) => ((items[i].줄[j] as { 글: string }).글 = t)),
"l",
),
]
: buildBlock(h, i, j, line, unused);
// 금액 · 글 = 한 줄(X 끝) · 공식 묶음 = 머리 줄 끝에 X
const card = el("div", {
className: `lstk-line k-${line.꼴}`,
children:
line.꼴 === "공식"
? [lineGrip, ...body.slice(0, 1).map((hd) => (hd.append(del), hd)), ...body.slice(1)]
: [lineGrip, ...body, del],
});
card.dataset.loc = `${i}.${j}`;
if (line.꼴 === "금액" && h.calc && h.open.has(`mctl:${line.i}`)) {
const own = ownedOf(h.doc).get(line.i);
const zone = buildMachineCtl(
{ ...h, calc: h.calc, change: (fn) => h.change((d) => fn(d)), readOnly: !(h.machine?.scope || h.ctlEdit) }, // prettier-ignore
h.doc.변수?.find((x) => varId(x) === own)?.연결,
line.i,
);
if (zone) card.append(zone);
}
card.addEventListener("pointerdown", () => h.pair(`${i}.${j}`));
attachDrag(lineGrip, {
what: "line",
root,
self: card,
drop: (to) => {
const [k, p] = to.split(".").map(Number);
h.change((_, items) => moveLine(items, i, j, k, p));
},
});
return [card];
});
const box = el("div", {
className: `lstk-item${it.가나다 ? " is-sub" : ""}${folded ? " is-folded" : ""}`,
attrs: { "data-loc": String(i) },
children: [
head,
...(folded
? []
: [
el("div", {
className: "lstk-lines",
children: lines.length
? lines
: [el("div", { className: "lstk-empty", text: lt("Empty_Item") })],
}),
]),
],
});
head.addEventListener("pointerdown", () => h.pair(String(i)));
attachDrag(itemGrip, {
what: "item",
root,
self: box,
drop: (to) => {
const p = Number(to);
refold((f) => moveItem(f, i, p));
h.change((_, items) => moveItem(items, i, p));
},
});
return box;
});
root.append(
...items,
btn(lt("Add_Item"), () => h.change((_, items) => items.push({ 제목: "", 줄: [] }))),
);
return root;
}