feat(ui): extend shared tree actions and BIM tool menus

This commit is contained in:
Codex
2026-11-14 21:55:07 +03:00
parent 91a5ebe8da
commit 6f2f9644fe
8 changed files with 35 additions and 13 deletions
+6
View File
@@ -1,5 +1,8 @@
import {
MousePointer2,
Ruler,
Slice,
Focus,
Move,
CirclePlus,
Box,
@@ -72,6 +75,9 @@ import {
const icons = {
"add-point": CirclePlus,
ruler: Ruler,
section: Slice,
isolate: Focus,
perspective: Box,
orthographic: Scan,
select: MousePointer2,
+4 -1
View File
@@ -38,6 +38,8 @@ export interface ToolbarProps<T extends string = string> {
hideDelay?: number;
gap?: number;
label?: string;
/** Dropdown width; placement still clamps to the visible viewport. */
menuWidth?: number;
className?: string;
/** Render inside a positioned viewport; edge reveal follows that viewport. */
container?: HTMLElement;
@@ -66,6 +68,7 @@ export function Toolbar<T extends string = string>({
hideDelay = NODEDC_TOOLBAR_DEFAULTS.hideDelay,
gap = NODEDC_TOOLBAR_DEFAULTS.gap,
label = "Панель инструментов",
menuWidth = 220,
className,
container,
}: ToolbarProps<T>) {
@@ -392,7 +395,7 @@ export function Toolbar<T extends string = string>({
if(placement==="left")return {...b.toJSON(),left:t.left,right:t.left+pad,width:pad};
if(placement==="bottom")return {...b.toJSON(),top:t.bottom-pad,bottom:t.bottom,height:pad};
return {...b.toJSON(),top:t.top,bottom:t.top+pad,height:pad};
}} width={220} trigger={button} onPointerDownOutside={event=>{
}} width={Number.isFinite(menuWidth)?Math.max(180,Math.min(600,menuWidth)):220} trigger={button} onPointerDownOutside={event=>{
const target=event.target as Element;
// Dismiss a menu before a viewport tool can turn this same click into an edit.
if(container?.contains(target)&&!target.closest('button,input,select,textarea,a,[role="button"],[role="tab"]')){
+8 -7
View File
@@ -1,12 +1,12 @@
import {useLayoutEffect,useRef,useState,type CSSProperties} from 'react';
import {useLayoutEffect,useRef,useState,type CSSProperties,type ReactNode} from 'react';
import {Icon,type IconName} from './Icon.js';
export interface TreeNode {id:string;label:string;children?:TreeNode[];icon?:IconName;visible?:boolean}
export interface TreeProps {nodes:TreeNode[];selected:string|null;onSelect:(id:string)=>void;onVisibilityChange?:(id:string,visible:boolean)=>void;label:string;query?:string;revealKey?:number}
export interface TreeProps {nodes:TreeNode[];selected:string|null;onSelect:(id:string)=>void;onVisibilityChange?:(id:string,visible:boolean)=>void;label:string;query?:string;revealKey?:number;defaultExpandedDepth?:number;renderActions?:(node:TreeNode)=>ReactNode}
/** Compact outliner. Selection reveals ancestors without scrolling its host page. */
export function Tree({nodes,selected,onSelect,onVisibilityChange,label,query='',revealKey}:TreeProps){
const [collapsed,setCollapsed]=useState<Set<string>>(()=>new Set());
export function Tree({nodes,selected,onSelect,onVisibilityChange,label,query='',revealKey,defaultExpandedDepth=Infinity,renderActions}:TreeProps){
const [collapsed,setCollapsed]=useState<Map<string,boolean>>(()=>new Map());
const [focus,setFocus]=useState<string|null>(null);
const host=useRef<HTMLDivElement>(null),elements=useRef(new Map<string,HTMLDivElement>());
const parents=new Map<string,string>(),index=new Map<string,TreeNode>();
@@ -14,11 +14,11 @@ export function Tree({nodes,selected,onSelect,onVisibilityChange,label,query='',
const ancestors=new Set<string>();let parent=selected?parents.get(selected):undefined;while(parent){ancestors.add(parent);parent=parents.get(parent);}
const matches=(item:TreeNode):boolean=>!query||item.label.toLocaleLowerCase().includes(query.toLocaleLowerCase())||!!item.children?.some(matches);
const rows:{node:TreeNode;depth:number;open:boolean}[]=[];
const flatten=(items:TreeNode[],depth=0)=>{for(const node of items){if(!matches(node))continue;const open=!!query||!collapsed.has(node.id);rows.push({node,depth,open});if(open)flatten(node.children??[],depth+1);}};flatten(nodes);
const flatten=(items:TreeNode[],depth=0)=>{for(const node of items){if(!matches(node))continue;const open=!!query||!(collapsed.get(node.id)??depth>=defaultExpandedDepth);rows.push({node,depth,open});if(open)flatten(node.children??[],depth+1);}};flatten(nodes);
const reveal=(id:string)=>{const target=elements.current.get(id),container=host.current;if(!target||!container)return;const row=target.getBoundingClientRect(),box=container.getBoundingClientRect();if(row.top<box.top)container.scrollTop-=box.top-row.top;else if(row.bottom>box.bottom)container.scrollTop+=row.bottom-box.bottom;};
useLayoutEffect(()=>{if(selected){setFocus(selected);setCollapsed(old=>{if(![...ancestors].some(id=>old.has(id)))return old;const next=new Set(old);for(const id of ancestors)next.delete(id);return next;});}},[selected,revealKey]);
useLayoutEffect(()=>{if(selected){setFocus(selected);setCollapsed(old=>{if([...ancestors].every(id=>old.get(id)===false))return old;const next=new Map(old);for(const id of ancestors)next.set(id,false);return next;});}},[selected,revealKey,nodes]);
useLayoutEffect(()=>{if(selected)reveal(selected);},[selected,revealKey,query,collapsed]);
const toggle=(id:string)=>setCollapsed(old=>{const next=new Set(old);if(next.has(id))next.delete(id);else next.add(id);return next;});
const toggle=(id:string)=>setCollapsed(old=>{const next=new Map(old);const depth=rows.find(row=>row.node.id===id)?.depth??0;next.set(id,!(old.get(id)??depth>=defaultExpandedDepth));return next;});
const select=(id:string)=>{setFocus(id);onSelect(id);elements.current.get(id)?.focus({preventScroll:true});reveal(id);};
return <div ref={host} className="nodedc-tree" role="tree" aria-label={label}>
{rows.map(({node,depth,open},position)=><div key={node.id} ref={element=>{if(element)elements.current.set(node.id,element);else elements.current.delete(node.id);}} role="treeitem" aria-level={depth+1} aria-selected={selected===node.id} aria-expanded={node.children?.length?open:undefined} tabIndex={(focus??selected??rows[0]?.node.id)===node.id?0:-1} className="nodedc-tree__row" style={{'--tree-depth':depth} as CSSProperties} title={node.label} onFocus={()=>setFocus(node.id)} onClick={()=>select(node.id)} onDoubleClick={()=>node.children?.length&&toggle(node.id)} onKeyDown={event=>{
@@ -37,6 +37,7 @@ export function Tree({nodes,selected,onSelect,onVisibilityChange,label,query='',
<span className="nodedc-tree__branch">{node.children?.length?<button type="button" tabIndex={-1} aria-label={`${open?'Свернуть':'Развернуть'} ${node.label}`} onClick={event=>{event.stopPropagation();toggle(node.id);}}><Icon name={open?'chevron-down':'chevron-right'} size={14}/></button>:null}</span>
<Icon name={node.icon??(node.children?.length?'folder':'layers')} size={14}/>
<span className="nodedc-tree__label">{node.label}</span>
{renderActions&&<span className="nodedc-tree__actions" onClick={event=>event.stopPropagation()} onDoubleClick={event=>event.stopPropagation()}>{renderActions(node)}</span>}
{onVisibilityChange&&node.visible!==undefined&&<button type="button" className="nodedc-tree__visibility" tabIndex={focus===node.id?0:-1} aria-label={`${node.visible?'Скрыть':'Показать'} ${node.label}`} aria-pressed={node.visible} onClick={event=>{event.stopPropagation();onVisibilityChange(node.id,!node.visible);}}><Icon name={node.visible?'eye':'eye-off'} size={14}/></button>}
</div>)}
</div>;