Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: highlights now react to config changes
Elijah Potterc114d37e38ff
diff --git a/package.json b/package.jsonindex 92d8b480f..94fbea00a 100644--- a/package.json+++ b/package.json@@ -25,6 +25,7 @@"@wordpress/edit-post": "^8.16.0","@wordpress/editor": "^14.16.0","harper.js": "^0.17.0",+ "lodash-es": "^4.17.21","react": "^18.0.0","react-dom": "^18.0.0"}diff --git a/src/harper/HarperContext.ts b/src/harper/HarperContext.tsdeleted file mode 100644index ba7bacb6f..000000000--- a/src/harper/HarperContext.ts+++ /dev/null@@ -1,10 +0,0 @@-import { WorkerLinter } from 'harper.js';-import { createContext, useContext } from 'react';--const HarperContext = createContext(new WorkerLinter());--export default HarperContext;--export function useLinter() {- return useContext(HarperContext);-}diff --git a/src/harper/HarperContext.tsx b/src/harper/HarperContext.tsxnew file mode 100644index 000000000..f99e9f3bd--- /dev/null+++ b/src/harper/HarperContext.tsx@@ -0,0 +1,84 @@+import { LintConfig, Linter, WorkerLinter } from 'harper.js';+import React, {+ createContext,+ ReactNode,+ useContext,+ useEffect,+ useState,+} from 'react';++type HarperContextData = {+ linter: Linter;+ config: LintConfig;+};++function createDefaultContextData(): HarperContextData {+ return {+ linter: new WorkerLinter(),+ config: {},+ };+}++const harperContext = createContext<+ [HarperContextData, React.Dispatch<HarperContextData>]+>([createDefaultContextData(), () => {}]);++export function HarperProvider({+ children,+}: {+ children: ReactNode | ReactNode[];+}) {+ const dataReadWrite = useState<HarperContextData>(+ createDefaultContextData()+ );++ return (+ <harperContext.Provider value={dataReadWrite}>+ {children}+ </harperContext.Provider>+ );+}++export function useLinter(): Linter {+ const [data, setData] = useContext(harperContext);+ const [config] = useLinterConfig();++ useEffect(() => {+ data.linter.setLintConfig(config);+ }, [config, data, setData]);++ return data.linter;+}++export function useDefaultLintConfig(): LintConfig {+ const [data] = useContext(harperContext);+ const [defaultConfig, setDefaultConfig] = useState({});++ useEffect(() => {+ data.linter.getDefaultLintConfig().then(setDefaultConfig);+ }, [data]);++ return defaultConfig;+}++/**+ * Get the global linter's configuration, in addition to a callback to modify it.+ * This is the preferred method of modifying the lint configuration in this project, since this will trigger re-renders of+ * components that depend on the global linter.+ */+export function useLinterConfig(): [+ LintConfig,+ (newConfig: LintConfig) => void,+] {+ const [data, setData] = useContext(harperContext);++ useEffect(() => {+ (async () => {+ const config = await data.linter.getLintConfig();+ const newData = { linter: data.linter, config };+ setData(newData);+ })();+ }, [data.linter, setData]);++ return [data.config, (config) => setData({ ...data, config })];+}diff --git a/src/harper/Highlighter.tsx b/src/harper/Highlighter.tsxindex 5d44f4bc1..fb6b2d69d 100644--- a/src/harper/Highlighter.tsx+++ b/src/harper/Highlighter.tsx@@ -1,12 +1,13 @@-import React, { useCallback, useState, useEffect, useContext } from 'react';+import React, { useCallback, useState, useEffect } from 'react';import { Lint } from 'harper.js';import SuggestionControl from './SuggestionControl';import { LintBox } from './Box';import RichText from './RichText';-import HarperContext from './HarperContext';+import { useLinter, useLinterConfig } from './HarperContext';export default function Highlighter({ richText }: { richText: RichText }) {- const linter = useContext(HarperContext);+ const linter = useLinter();+ const [config] = useLinterConfig();const [targetBoxes, setTargetBoxes] = useState<LintBox[]>([]);const [lints, setLints] = useState<Lint[]>([]);@@ -16,7 +17,7 @@ export default function Highlighter({ richText }: { richText: RichText }) {const contents = richText.getTextContent();const newLints = await linter.lint(contents);setLints(newLints);- }, [richText]);+ }, [richText, linter, config]);useEffect(() => {updateLints();diff --git a/src/harper/LintSettingList.tsx b/src/harper/LintSettingList.tsxindex da3f23147..bcd0cc4f5 100644--- a/src/harper/LintSettingList.tsx+++ b/src/harper/LintSettingList.tsx@@ -1,30 +1,17 @@-import React, { useEffect, useState } from 'react';-import { useLinter } from './HarperContext';+import React, { useEffect } from 'react';+import { useDefaultLintConfig, useLinterConfig } from './HarperContext';import LintSettingRow from './LintSettingRow';-import { LintConfig } from 'harper.js';export default function LintSettingList() {- const linter = useLinter();- const [lintConfig, setLintConfig] = useState<LintConfig>({});- const [defaultConfig, setDefaultConfig] = useState({});-- useEffect(() => {- linter.getLintConfig().then((config) => setLintConfig(config));- linter- .getDefaultLintConfig()- .then((config) => setDefaultConfig(config));- }, [linter]);-- useEffect(() => {- linter.setLintConfig(lintConfig);- }, [lintConfig, linter]);+ const [lintConfig, setLintConfig] = useLinterConfig();+ const defaultConfig = useDefaultLintConfig();return Object.entries(lintConfig).map(([key, value]) => (<LintSettingRowkey={key}name={key}value={value}- defaultValue={defaultConfig[key]}+ defaultValue={defaultConfig[key]!}setValue={(newValue) =>setLintConfig({ ...lintConfig, [key]: newValue })}diff --git a/src/harper/LintSettingRow.tsx b/src/harper/LintSettingRow.tsxindex 8340a1ec0..672040556 100644--- a/src/harper/LintSettingRow.tsx+++ b/src/harper/LintSettingRow.tsx@@ -1,6 +1,6 @@import React, { useEffect, useState } from 'react';import { useLinter } from './HarperContext';-import { Animate, CheckboxControl, SelectControl } from '@wordpress/components';+import { Animate, CheckboxControl } from '@wordpress/components';export default function LintSettingRow({name,diff --git a/src/harper/index.css b/src/harper/index.cssindex 053ed746c..dfda74d39 100644--- a/src/harper/index.css+++ b/src/harper/index.css@@ -8,6 +8,8 @@display: flex;flex-direction: row;justify-content: space-between;+ align-items: center;+ padding: 6px;}.harper-popover > div > button {diff --git a/src/harper/index.tsx b/src/harper/index.tsxindex 5140b2626..cfbe24963 100644--- a/src/harper/index.tsx+++ b/src/harper/index.tsx@@ -2,25 +2,21 @@ import { registerPlugin } from '@wordpress/plugins';import { PluginSidebar, PluginSidebarMoreMenuItem } from '@wordpress/edit-post';import SidebarControl from './SidebarControl';import Logo from './Logo';-import './index.css';-import React, { useRef } from 'react';-import { WorkerLinter } from 'harper.js';-import HarperContext from './HarperContext';+import React from 'react';+import { HarperProvider } from './HarperContext';function Sidebar() {- const linter = useRef(new WorkerLinter());-return (<>- <HarperContext.Provider value={linter.current}>- <PluginSidebarMoreMenuItem target="harper-sidebar" icon={Logo}>- Harper- </PluginSidebarMoreMenuItem>- <PluginSidebar name="harper-sidebar" title="Harper" icon={Logo}>+ <PluginSidebarMoreMenuItem target="harper-sidebar" icon={Logo}>+ Harper+ </PluginSidebarMoreMenuItem>+ <PluginSidebar name="harper-sidebar" title="Harper" icon={Logo}>+ <HarperProvider><SidebarControl />- </PluginSidebar>- </HarperContext.Provider>+ </HarperProvider>+ </PluginSidebar></>);}