feat(713-3): M01 로직 목록 묶음 접기 — 같은 절 · 작업량 로직은 머리 줄 밑으로 접음(접기 순수 함수 logic/list_fold) · 찾는 중엔 펼침 · 「관련」 열 = 비고가 가리키는 로직 링크(계산 밖) · 서버 덧칸 인재경 줄 수(712-1 master_bundle) · 지킴 시험
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/* =============================================================================
|
||||
* M01_MasterData_logic_list_fold.ts
|
||||
* 로직 목록 묶음 접기 — 순수 계산(화면 · fetch 없음 · PLAN 713-3)
|
||||
* 같은 절 묶음(서버 `묶음`) = 첫 줄만 보이고 나머지는 접힘 · 작업량 로직(`부모` 있음) = 부르는 로직 밑으로
|
||||
* 찾는 글이 있으면 접지 않음(찾은 줄이 숨지 않게) · 정본은 그대로 — 보이는 차례만 정함
|
||||
* ========================================================================== */
|
||||
|
||||
export interface FoldItem {
|
||||
key: string;
|
||||
/** 묶음 열쇠(파일 + 원문번호) — 빈 글 = 묶음 없음 */
|
||||
group?: string;
|
||||
/** 작업량 로직이면 부르는 로직 키들 */
|
||||
parents?: string[];
|
||||
}
|
||||
|
||||
export interface FoldRow<T> {
|
||||
item: T;
|
||||
/** 0 = 맨 줄 · 1 = 펼친 묶음 · 작업량 줄 */
|
||||
depth: 0 | 1;
|
||||
/** 묶음 머리 · 작업량을 거느린 줄 — 접힌 줄 수 */
|
||||
hidden?: number;
|
||||
open?: boolean;
|
||||
}
|
||||
|
||||
/** `open` = 펼친 머리 줄 키 · `flat` = 접지 않음(찾는 중) */
|
||||
export function foldRows<T extends FoldItem>(
|
||||
items: T[],
|
||||
open: ReadonlySet<string>,
|
||||
flat: boolean,
|
||||
): FoldRow<T>[] {
|
||||
if (flat) return items.map((item) => ({ item, depth: 0 }));
|
||||
const present = new Set(items.map((x) => x.key));
|
||||
const isChild = (x: T): boolean => (x.parents ?? []).some((p) => present.has(p));
|
||||
const kids = new Map<string, T[]>(); // 머리 키 → 거느린 줄(묶음 나머지 + 작업량)
|
||||
const heads = new Map<string, string>(); // 묶음 열쇠 → 머리 키
|
||||
for (const x of items) {
|
||||
if (isChild(x)) {
|
||||
const parent = (x.parents ?? []).find((p) => present.has(p)) as string;
|
||||
kids.set(parent, [...(kids.get(parent) ?? []), x]);
|
||||
continue;
|
||||
}
|
||||
if (!x.group) continue;
|
||||
const head = heads.get(x.group);
|
||||
if (head === undefined) heads.set(x.group, x.key);
|
||||
else kids.set(head, [...(kids.get(head) ?? []), x]);
|
||||
}
|
||||
const out: FoldRow<T>[] = [];
|
||||
const shown = new Set<string>();
|
||||
const push = (x: T, depth: 0 | 1): void => {
|
||||
if (shown.has(x.key)) return;
|
||||
shown.add(x.key);
|
||||
const under = kids.get(x.key) ?? [];
|
||||
const isOpen = open.has(x.key);
|
||||
out.push(
|
||||
under.length ? { item: x, depth, hidden: under.length, open: isOpen } : { item: x, depth },
|
||||
);
|
||||
if (isOpen) for (const k of under) push(k, 1);
|
||||
};
|
||||
for (const x of items) {
|
||||
if (isChild(x) || (x.group && heads.get(x.group) !== x.key)) continue;
|
||||
push(x, 0);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user