From 8be7f3c290df67df0df4ade1511f95c16fddef2e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 27 Jan 2024 14:24:32 -0500 Subject: [PATCH] Switch to using unique id for node selection. This lets us pass the selected node down from the top instead of propagating calls to set/clear the selected boolean in each node. --- src/OrgAst.tsx | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/OrgAst.tsx b/src/OrgAst.tsx index c684818..65f9a33 100644 --- a/src/OrgAst.tsx +++ b/src/OrgAst.tsx @@ -6,8 +6,11 @@ const OrgAst = (props: { setHighlight: Function; clearHighlights: Function; valu const ast_tree = parse_org(props.value); console.log(JSON.stringify(ast_tree)); - function deselectAll() { - props.clearHighlights(); + const [selectedNode, setSelectedNode] = useState(""); + + function selectNode(uid: string, start: number, end: number) { + props.setHighlight(start, end); + setSelectedNode(uid); } if (ast_tree.status !== "success") { @@ -15,22 +18,21 @@ const OrgAst = (props: { setHighlight: Function; clearHighlights: Function; valu } else { return (
- +
); } }; -const OrgAstNode = (props: { setHighlight: Function; deselectAll: Function; node: any }) => { +const OrgAstNode = (props: { selectNode: Function; node: any, parentUniqueId: string, selectedNode: string }) => { - const [selected, setSelected] = useState(false); + const uid = props.parentUniqueId + "_" + props.node["ast-node"] + "/" + props.node["standard-properties"]["begin"] + "/" + props.node["standard-properties"]["end"] + "#"; function selectNode() { - props.setHighlight(props.node["standard-properties"]["begin"] - 1, props.node["standard-properties"]["end"] - 1); - setSelected(true); + props.selectNode(uid, props.node["standard-properties"]["begin"] - 1, props.node["standard-properties"]["end"] - 1); } - const nodeClassName = selected ? styles.OrgAstNode + " " + styles.selected : styles.OrgAstNode; + const nodeClassName = props.selectedNode === uid ? styles.OrgAstNode + " " + styles.selected : styles.OrgAstNode; return (
@@ -54,7 +56,7 @@ const OrgAstNode = (props: { setHighlight: Function; deselectAll: Function; node
Children
- +
) : null} @@ -62,9 +64,9 @@ const OrgAstNode = (props: { setHighlight: Function; deselectAll: Function; node ); }; -const OrgAstNodeList = (props: { setHighlight: Function; deselectAll: Function; node_list: any[] }): React.JSX.Element[] => { +const OrgAstNodeList = (props: { selectNode: Function; parentUniqueId: string; selectedNode: string; node_list: any[] }): React.JSX.Element[] => { return props.node_list.map((node) => { - return ; + return ; }); };