organic_ast_explorer/src/OrgAst.tsx

74 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-01-24 02:27:12 +00:00
import React, { ReactNode, useState } from "react";
2024-01-24 03:06:47 +00:00
import { parse_org } from "../../organic/target/wasm32-unknown-unknown/js/wasm";
2024-01-24 02:27:12 +00:00
import styles from "./OrgAst.module.css";
2024-01-27 19:04:44 +00:00
const OrgAst = (props: { setHighlight: Function; clearHighlights: Function; value: string }) => {
2024-01-24 02:27:12 +00:00
const ast_tree = parse_org(props.value);
console.log(JSON.stringify(ast_tree));
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-24 02:27:12 +00:00
if (ast_tree.status !== "success") {
2024-01-24 03:06:47 +00:00
return <div className={styles.OrgAst}>Error! {ast_tree.content}</div>;
2024-01-24 02:27:12 +00:00
} else {
return (
2024-01-24 03:06:47 +00:00
<div className={styles.OrgAst}>
<OrgAstNode selectNode={selectNode} node={ast_tree.content} parentUniqueId="^" 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
const OrgAstNode = (props: { selectNode: Function; node: any, parentUniqueId: string, selectedNode: string }) => {
2024-01-27 19:04:44 +00:00
const uid = props.parentUniqueId + "_" + props.node["ast-node"] + "/" + props.node["standard-properties"]["begin"] + "/" + props.node["standard-properties"]["end"] + "#";
2024-01-27 19:04:44 +00:00
function selectNode() {
props.selectNode(uid, props.node["standard-properties"]["begin"] - 1, props.node["standard-properties"]["end"] - 1);
2024-01-27 19:04:44 +00:00
}
const nodeClassName = props.selectedNode === uid ? 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}>
<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-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}>
<OrgAstNodeList selectNode={props.selectNode} parentUniqueId={props.parentUniqueId} 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
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) => {
return <OrgAstNode selectNode={props.selectNode} parentUniqueId={props.parentUniqueId} selectedNode={props.selectedNode} node={node} />;
2024-01-24 04:07:26 +00:00
});
};
2024-01-24 02:27:12 +00:00
export default OrgAst;