Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

refactor: properly memoize top level structs

Elijah Potterf1e37665923c
diff --git a/src/harper/SidebarControl.tsx b/src/harper/SidebarControl.tsx
index ca3ca94a5..37434924b 100644
--- a/src/harper/SidebarControl.tsx
+++ b/src/harper/SidebarControl.tsx
@@ -1,40 +1,50 @@
import DataBlock from './DataBlock';
-import { ReactPortal, useCallback, useMemo, useRef } from 'react';
-import useFrameCount from './useFrameCount';
-import { getNodesFromQuerySelector, getRichTextContainers } from './domUtils';
+import { useCallback, useEffect, useMemo, useState } from 'react';
import { createPortal } from 'react-dom';
import Highlighter from './Highlighter';
import React from 'react';
-function getDocumentContainer(): Element | null {
- const iframe = document.querySelector( 'iframe[name="editor-canvas"]' );
- const iframeDocument =
- iframe?.contentDocument || iframe?.contentWindow.document;
- const container =
- iframeDocument?.body ||
- document.querySelector( '.edit-post-visual-editor > div' );
- return container;
-}
-
export default function SidebarControl() {
- let documentContainer = useMemo( getDocumentContainer, [] );
+ let documentContainer = useMemo< Element >(
+ () => DataBlock.getContainer(),
+ []
+ );
+
+ const [ blocks, setBlocks ] = useState< DataBlock[] >( [] );
+ const updateBlocks = useCallback(
+ () => setBlocks( DataBlock.getAllDataBlocks() ),
+ []
+ );
+
+ useEffect( updateBlocks, [] );
- let blocks = DataBlock.getAllDataBlocks();
- let richTexts = blocks.flatMap( ( block ) => block.getAllRichText() );
+ useEffect( () => {
+ let observer = new MutationObserver( updateBlocks );
+
+ observer.observe( documentContainer, {
+ subtree: true,
+ childList: true,
+ } );
+
+ return () => observer.disconnect();
+ }, [ documentContainer, updateBlocks ] );
+
+ const richTexts = useMemo(
+ () => blocks.flatMap( ( block ) => block.getAllRichText() ),
+ [ blocks ]
+ );
let highlights =
documentContainer &&
richTexts.map( ( richText ) =>
createPortal(
- <Highlighter richText={ richText } />,
+ <Highlighter
+ richText={ richText }
+ key={ richText.getTextContent() }
+ />,
documentContainer
)
);
- return (
- <>
- { highlights }
- <p>This is a test. Eventually, lints will show up here as well.</p>
- </>
- );
+ return <>{ highlights }</>;
}
diff --git a/src/harper/SidebarLintCard.tsx b/src/harper/SidebarLintCard.tsx
new file mode 100644
index 000000000..e2684fda7
--- /dev/null
+++ b/src/harper/SidebarLintCard.tsx
@@ -0,0 +1,6 @@
+import { Lint } from 'harper.js';
+import React from 'react';
+
+export default function SidebarLintCard( { lint }: { lint: Lint } ) {
+ return <></>;
+}
diff --git a/src/harper/SuggestionControl.tsx b/src/harper/SuggestionControl.tsx
index 4c6755e61..8fa899a86 100644
--- a/src/harper/SuggestionControl.tsx
+++ b/src/harper/SuggestionControl.tsx
@@ -10,7 +10,7 @@ function suggestionText(
if ( kind == SuggestionKind.Remove ) {
return `Remove "${ problemText }"`;
} else if ( kind == SuggestionKind.Replace ) {
- return `Replace with "${ replacementText }"`;
+ return `Replace with “${ replacementText }”`;
} else {
return `Insert "${ replacementText }"`;
}