Only re-parse the ast when value changes.

This commit is contained in:
Tom Alexander 2024-01-27 19:31:29 -05:00
parent 8627181480
commit cc48040f11
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -1,4 +1,4 @@
import React, { ReactNode, useState } from "react"; import React, { ReactNode, useMemo, useState } from "react";
import "./Editor.css"; import "./Editor.css";
import { Highlight } from "./highlight"; import { Highlight } from "./highlight";
import { buildShadow } from "./shadow"; import { buildShadow } from "./shadow";
@ -31,6 +31,12 @@ function Editor({ defaultValue = default_org_source }) {
const [highlights, setHighlights] = useState<Array<Highlight>>([]); const [highlights, setHighlights] = useState<Array<Highlight>>([]);
const astTree = useMemo(() => {
const astTree = parse_org(value);
console.log(JSON.stringify(astTree));
return astTree;
}, [value]);
function setHighlight(nodes: OrgNodeReference[]) { function setHighlight(nodes: OrgNodeReference[]) {
let new_highlights = nodes.map((node: OrgNodeReference) => { let new_highlights = nodes.map((node: OrgNodeReference) => {
return new Highlight(node.start - 1, node.end - 1); return new Highlight(node.start - 1, node.end - 1);
@ -57,9 +63,6 @@ function Editor({ defaultValue = default_org_source }) {
setHighlights([]); setHighlights([]);
} }
const astTree = parse_org(value);
console.log(JSON.stringify(astTree));
return ( return (
<div className="Editor"> <div className="Editor">
<div className="Editor-textwrapper"> <div className="Editor-textwrapper">