From d582fadf6a251af636c1177fa2bfdb1557670553 Mon Sep 17 00:00:00 2001 From: codetypes Date: Sat, 3 Aug 2024 00:12:22 +0800 Subject: [PATCH] search node def in title bar --- src/components/explorer.tsx | 59 ++++++++++++++++++++++++--------- src/components/inspector.tsx | 13 ++++---- src/components/register-node.ts | 4 +-- src/components/titlebar.tsx | 42 ++++++++++++++++++----- src/index.scss | 6 +++- 5 files changed, 90 insertions(+), 34 deletions(-) diff --git a/src/components/explorer.tsx b/src/components/explorer.tsx index 736de6d..5bd7f40 100644 --- a/src/components/explorer.tsx +++ b/src/components/explorer.tsx @@ -35,6 +35,7 @@ type MenuEvent = export type NodeTreeType = { title: string; + path: string; def?: NodeDef; icon?: React.ReactNode; isLeaf?: boolean; @@ -72,7 +73,7 @@ const findParent = (node: FileTreeType, parent?: FileTreeType): FileTreeType | u } }; -const resolveKeys = (path: string, node: FileTreeType, keys: React.Key[]) => { +const resolveKeys = (path: string, node: FileTreeType | NodeTreeType, keys: React.Key[]) => { if (node.path === path) { return true; } @@ -274,6 +275,7 @@ export const Explorer: FC = () => { const workspace = { close: useWorkspace((state) => state.close), editing: useWorkspace((state) => state.editing), + editingNodeDef: useWorkspace((state) => state.editingNodeDef), editors: useWorkspace((state) => state.editors), fileTree: useWorkspace((state) => state.fileTree), nodeDefs: useWorkspace((state) => state.nodeDefs), @@ -290,6 +292,10 @@ export const Explorer: FC = () => { const [newName, setNewName] = useState(null); const [contextMenu, setContextMenu] = useState([]); + const rootNodedefName = "nodeTree.root"; + const [selectedNodedefKeys, setSelectedNodedefKeys] = useState([]); + const [expandedNodedefKeys, setExpandedNodedefKeys] = useState([rootNodedefName]); + if (workspace.fileTree) { workspace.fileTree.icon = ( @@ -301,6 +307,7 @@ export const Explorer: FC = () => { const nodeTree = useMemo(() => { const data: NodeTreeType = { title: t("nodeDefinition"), + path: rootNodedefName, icon: ( @@ -318,6 +325,7 @@ export const Explorer: FC = () => { const type = getNodeType(nodeDef); catalog = { title: nodeDef.type, + path: `nodeTree.catalog.${nodeDef.type}`, children: [], icon: ( @@ -335,17 +343,14 @@ export const Explorer: FC = () => { title: `${nodeDef.name}(${nodeDef.desc})`, isLeaf: true, def: nodeDef, + path: `${nodeDef.name}`, icon: nodeDef.icon ? ( ) : ( @@ -362,6 +367,7 @@ export const Explorer: FC = () => { return data; }, [t, workspace.nodeDefs]); + // expand the selected tree useEffect(() => { if (workspace.editing) { const keys: React.Key[] = []; @@ -376,6 +382,21 @@ export const Explorer: FC = () => { } }, [workspace.editing]); + // expand the selected node def + useEffect(() => { + if (workspace.editingNodeDef) { + const keys: React.Key[] = []; + resolveKeys(workspace.editingNodeDef.data.name, nodeTree, keys); + for (const k of expandedNodedefKeys) { + if (keys.indexOf(k) === -1) { + keys.push(k); + } + } + setExpandedNodedefKeys(keys); + setSelectedNodedefKeys([workspace.editingNodeDef.data.name]); + } + }, [t, workspace.editingNodeDef]); + const keysRef = useHotkeys( [ Hotkey.F2, @@ -763,6 +784,8 @@ export const Explorer: FC = () => { tabIndex={-1} treeData={workspace.fileTree ? [workspace.fileTree] : []} fieldNames={{ key: "path" }} + expandedKeys={expandedKeys} + selectedKeys={selectedKeys} onExpand={(keys) => { setExpandedKeys(keys); }} @@ -840,7 +863,6 @@ export const Explorer: FC = () => { onDragStart={(e) => { e.event.dataTransfer.setData("explore-file", e.node.path); }} - defaultExpandedKeys={[workspace.fileTree.path]} draggable={ newName !== null ? false @@ -852,17 +874,19 @@ export const Explorer: FC = () => { }, } } - expandedKeys={expandedKeys} switcherIcon={} - selectedKeys={selectedKeys} /> { + setExpandedNodedefKeys(keys); + }} draggable={{ icon: false, nodeDraggable: (node) => !!node.isLeaf }} titleRender={(node) => (
@@ -879,10 +903,13 @@ export const Explorer: FC = () => { )} onSelect={(_, info) => { const node = info.node; - if (node && node.isLeaf) { - workspace.onEditingNodeDef({ - data: node.def!, - }); + if (node) { + if (node.isLeaf) { + workspace.onEditingNodeDef({ + data: node.def!, + }); + } + setSelectedNodedefKeys([node.path]); } }} onDragStart={(e) => { diff --git a/src/components/inspector.tsx b/src/components/inspector.tsx index 5756c80..2135d3d 100644 --- a/src/components/inspector.tsx +++ b/src/components/inspector.tsx @@ -2,6 +2,7 @@ import { EditNode, EditTree, useWorkspace } from "@/contexts/workspace-context"; import { NodeArg, NodeModel, TreeGraphData } from "@/misc/b3type"; import { checkOneof } from "@/misc/b3util"; import { Hotkey, isMacos } from "@/misc/keys"; +import { mergeClassNames } from "@/misc/util"; import { EditOutlined } from "@ant-design/icons"; import { AutoComplete, @@ -57,8 +58,8 @@ const TreeInspector: FC = () => { {t("tree.overview")}
@@ -263,8 +264,8 @@ const NodeInspector: FC = () => { {def.desc}
{ {t("nodeDefinition")}
= () => { const workspace = { allFiles: useWorkspace((state) => state.allFiles), fileTree: useWorkspace((state) => state.fileTree), + nodeDefs: useWorkspace((state) => state.nodeDefs), isShowingSearch: useWorkspace((state) => state.isShowingSearch), onShowingSearch: useWorkspace((state) => state.onShowingSearch), + onEditingNodeDef: useWorkspace((state) => state.onEditingNodeDef), open: useWorkspace((state) => state.open), name: useWorkspace((state) => state.path), relative: useWorkspace((state) => state.relative), @@ -41,7 +45,23 @@ export const TitleBar: FC = () => { path: file.path, }); }); - options.sort((a, b) => a.path.localeCompare(b.path)); + workspace.nodeDefs.forEach((def) => { + const value = def.name; + const desc = def.desc ?? ""; + options.push({ + label: ( +
+ {value} + + + {value} {desc} + +
+ ), + value: `${value.toLocaleLowerCase()} ${desc.toLocaleLowerCase()}`, + nodeDef: def, + }); + }); return options; }, [workspace.allFiles, workspace.fileTree]); return ( @@ -103,17 +123,23 @@ export const TitleBar: FC = () => { onChange={(_, option) => { if (!(option instanceof Array)) { workspace.onShowingSearch(false); - workspace.open(option.path); + if (option.path) { + workspace.open(option.path); + } else if (option.nodeDef) { + workspace.onEditingNodeDef({ + data: option.nodeDef, + }); + } } }} filterOption={(input, option) => (option?.value ?? "").includes(input.toLocaleLowerCase()) } - filterSort={(optionA, optionB) => - (optionA?.value ?? "") - .toLowerCase() - .localeCompare((optionB?.value ?? "").toLowerCase()) - } + // filterSort={(optionA, optionB) => + // (optionA?.value ?? "") + // .toLowerCase() + // .localeCompare((optionB?.value ?? "").toLowerCase()) + // } options={searchOptions} /> )} diff --git a/src/index.scss b/src/index.scss index d57f5de..2d7387c 100644 --- a/src/index.scss +++ b/src/index.scss @@ -107,8 +107,12 @@ body { width: 12px !important; } +.b3-inspector-content { + padding: 20px; +} + .b3-inspector .ant-form-item { - margin-bottom: 16px; + margin-bottom: 10px; } .b3-inspector .ant-form-item-label {