organic_ast_explorer/src/OrgAst.tsx

157 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-01-24 02:27:12 +00:00
import React, { ReactNode, useState } from "react";
import styles from "./OrgAst.module.css";
2024-01-27 20:27:23 +00:00
import { Fragment } from 'react';
2024-01-24 02:27:12 +00:00
2024-01-27 19:30:15 +00:00
const OrgAst = (props: {
setHighlight: Function;
clearHighlights: Function;
2024-01-27 19:56:12 +00:00
astTree: any,
2024-01-27 19:30:15 +00:00
value: string;
}) => {
const [selectedNode, setSelectedNode] = useState<string>("");
function selectNode(uid: string, start: number, end: number) {
props.setHighlight(start, end);
setSelectedNode(uid);
2024-01-27 19:04:44 +00:00
}
2024-01-27 19:56:12 +00:00
if (props.astTree.status !== "success") {
return <div className={styles.OrgAst}>Error! {props.astTree.content}</div>;
2024-01-24 02:27:12 +00:00
} else {
return (
2024-01-24 03:06:47 +00:00
<div className={styles.OrgAst}>
2024-01-27 19:30:15 +00:00
<OrgAstNode
key="^"
uid="^"
2024-01-27 19:30:15 +00:00
selectNode={selectNode}
2024-01-27 19:56:12 +00:00
node={props.astTree.content}
2024-01-27 19:30:15 +00:00
selectedNode={selectedNode}
/>
2024-01-24 03:06:47 +00:00
</div>
2024-01-24 02:27:12 +00:00
);
}
2024-01-24 03:06:47 +00:00
};
2024-01-24 02:27:12 +00:00
2024-01-27 19:30:15 +00:00
const OrgAstNode = (props: {
selectNode: Function;
node: any;
uid: string;
2024-01-27 19:30:15 +00:00
selectedNode: string;
}) => {
2024-01-27 19:04:44 +00:00
function selectNode() {
2024-01-27 19:30:15 +00:00
props.selectNode(
props.uid,
2024-01-27 19:30:15 +00:00
props.node["standard-properties"]["begin"] - 1,
props.node["standard-properties"]["end"] - 1,
);
2024-01-27 19:04:44 +00:00
}
2024-01-27 19:30:15 +00:00
const nodeClassName =
props.selectedNode === props.uid
2024-01-27 19:30:15 +00:00
? styles.OrgAstNode + " " + styles.selected
: styles.OrgAstNode;
2024-01-27 19:04:44 +00:00
2024-01-24 02:27:12 +00:00
return (
2024-01-27 19:04:44 +00:00
<div className={nodeClassName}>
2024-01-27 19:30:15 +00:00
<div className={styles.OrgAstNodeType} onClick={selectNode}>
{props.node["ast-node"]}
</div>
2024-01-24 04:07:26 +00:00
<details>
2024-01-24 03:27:31 +00:00
<summary>Standard Properties</summary>
<dl>
<dt>begin</dt>
<dd>{props.node["standard-properties"]["begin"]}</dd>
<dt>contents-begin</dt>
<dd>{props.node["standard-properties"]["contents-begin"]}</dd>
<dt>contents-end</dt>
<dd>{props.node["standard-properties"]["contents-end"]}</dd>
<dt>end</dt>
<dd>{props.node["standard-properties"]["end"]}</dd>
<dt>post-blank</dt>
<dd>{props.node["standard-properties"]["post-blank"]}</dd>
</dl>
</details>
2024-01-27 20:27:23 +00:00
{!!Object.keys(props.node.properties).length ? (
<>
<details>
<summary>Properties</summary>
<OrgPropertiesList selectNode={props.selectNode}
parentUniqueId={props.uid}
selectedNode={props.selectedNode}
properties={props.node.properties}
/>
</details>
</>
) : null }
2024-01-24 03:06:47 +00:00
{Array.isArray(props.node.children) && props.node.children.length > 0 ? (
2024-01-24 04:07:26 +00:00
<details open={true}>
2024-01-24 03:27:31 +00:00
<summary>Children</summary>
2024-01-24 04:07:26 +00:00
<div className={styles.OrgAstChildren}>
2024-01-27 19:30:15 +00:00
<OrgAstNodeList
selectNode={props.selectNode}
parentUniqueId={props.uid}
2024-01-27 19:30:15 +00:00
selectedNode={props.selectedNode}
node_list={props.node.children}
/>
2024-01-24 04:07:26 +00:00
</div>
2024-01-24 03:27:31 +00:00
</details>
2024-01-24 03:06:47 +00:00
) : null}
2024-01-24 02:27:12 +00:00
</div>
);
2024-01-24 03:06:47 +00:00
};
2024-01-24 02:27:12 +00:00
2024-01-27 19:30:15 +00:00
const OrgAstNodeList = (props: {
selectNode: Function;
parentUniqueId: string;
selectedNode: string;
node_list: any[];
}): React.JSX.Element[] => {
2024-01-24 04:07:26 +00:00
return props.node_list.map((node) => {
const uid =
props.parentUniqueId +
"_" +
node["ast-node"] +
"/" +
node["standard-properties"]["begin"] +
"/" +
node["standard-properties"]["end"] +
"#";
2024-01-27 19:30:15 +00:00
return (
<OrgAstNode
key={uid}
uid={uid}
2024-01-27 19:30:15 +00:00
selectNode={props.selectNode}
selectedNode={props.selectedNode}
node={node}
/>
);
2024-01-24 04:07:26 +00:00
});
};
2024-01-27 20:27:23 +00:00
const OrgPropertiesList = (props: {
selectNode: Function;
parentUniqueId: string;
selectedNode: string;
properties: Object;
}): React.JSX.Element => {
const entries = Object.entries(props.properties).map(([key, value]) => {
return (
<Fragment key={key}>
2024-01-27 21:38:34 +00:00
<tr>
<th scope="row">{key}:</th>
<td>{JSON.stringify(value)}</td>
</tr>
2024-01-27 20:27:23 +00:00
</Fragment>
);
});
return (
2024-01-27 21:38:34 +00:00
<table className={styles.OrgAstProperties}>
<tbody>
{entries}
</tbody>
</table>
2024-01-27 20:27:23 +00:00
);
};
2024-01-24 02:27:12 +00:00
export default OrgAst;