/
Cybran65
/
Project_0
Обзор
Документация
Войти
/
Cybran65
/
Project_0
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/lib/tree.ts
81 строка
3 KB
Cybran65
feat: интерактивный прототип интерфейса МИСИ-САПР
08 авг 2026, 22:59
08 авг 2026, 22:59
6df5b61
Код
Авторство
О чём код?
import type { DemoElement, TreeNode } from '../types'; /** Иконки узлов для элементов, созданных пользователем. */ const KIND_GLYPHS: Record<DemoElement['kind'], string> = { wall: '▬', column: '▮', slab: '▭', opening: '⊓', site: '▱', }; const KIND_LABELS: Record<DemoElement['kind'], string> = { wall: 'Стена', column: 'Стойка', slab: 'Плита', opening: 'Проём', site: 'Участок', }; /** * Дерево проекта с учётом элементов, построенных пользователем. * Новые узлы добавляются в тот уровень, на котором элемент размещён. */ export function buildProjectTree(base: TreeNode, placed: DemoElement[]): TreeNode { if (!placed.length) return base; const clone = (node: TreeNode): TreeNode => { const own = placed .filter((element) => element.levelId === node.id) .map<TreeNode>((element) => ({ id: element.id, label: element.label, glyph: KIND_GLYPHS[element.kind], kind: KIND_LABELS[element.kind], })); const children = [...(node.children ?? []).map(clone), ...own]; return children.length ? { ...node, children } : { ...node }; }; return clone(base); } /** Поиск узла по идентификатору. */ export function findNode(root: TreeNode, id: string): TreeNode | null { if (root.id === id) return root; for (const child of root.children ?? []) { const found = findNode(child, id); if (found) return found; } return null; } /** Поиск родителя узла (нужен для подписи «Уровень» в свойствах). */ export function findParent(root: TreeNode, id: string): TreeNode | null { for (const child of root.children ?? []) { if (child.id === id) return root; const found = findParent(child, id); if (found) return found; } return null; } /** Путь от корня до узла — используется в строке состояния. */ export function findPath(root: TreeNode, id: string): TreeNode[] { if (root.id === id) return [root]; for (const child of root.children ?? []) { const path = findPath(child, id); if (path.length) return [root, ...path]; } return []; } /** Все идентификаторы узлов, у которых есть дети. */ export function collectBranchIds(root: TreeNode, acc: string[] = []): string[] { if (root.children?.length) { acc.push(root.id); root.children.forEach((child) => collectBranchIds(child, acc)); } return acc; }