Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: created sidebar to configure the linter
Elijah Potter7428245b7da0
diff --git a/src/harper/HarperContext.ts b/src/harper/HarperContext.tsindex 40db705c2..ba7bacb6f 100644--- a/src/harper/HarperContext.ts+++ b/src/harper/HarperContext.ts@@ -1,4 +1,10 @@import { WorkerLinter } from 'harper.js';-import { createContext } from 'react';+import { createContext, useContext } from 'react';-export default createContext(new WorkerLinter());+const HarperContext = createContext(new WorkerLinter());++export default HarperContext;++export function useLinter() {+ return useContext(HarperContext);+}diff --git a/src/harper/Highlighter.tsx b/src/harper/Highlighter.tsxindex b20cc4783..5d44f4bc1 100644--- a/src/harper/Highlighter.tsx+++ b/src/harper/Highlighter.tsx@@ -72,11 +72,8 @@ export default function Highlighter({ richText }: { richText: RichText }) {return (<>{visible &&- targetBoxes.map((b) => (- <SuggestionControl- lintBox={b}- key={richText.getTextContent()}- />+ targetBoxes.map((b, index) => (+ <SuggestionControl lintBox={b} key={index} />))}</>);diff --git a/src/harper/LintSettingList.tsx b/src/harper/LintSettingList.tsxnew file mode 100644index 000000000..da3f23147--- /dev/null+++ b/src/harper/LintSettingList.tsx@@ -0,0 +1,33 @@+import React, { useEffect, useState } from 'react';+import { useLinter } 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]);++ return Object.entries(lintConfig).map(([key, value]) => (+ <LintSettingRow+ key={key}+ name={key}+ value={value}+ defaultValue={defaultConfig[key]}+ setValue={(newValue) =>+ setLintConfig({ ...lintConfig, [key]: newValue })+ }+ />+ ));+}diff --git a/src/harper/LintSettingRow.tsx b/src/harper/LintSettingRow.tsxnew file mode 100644index 000000000..8340a1ec0--- /dev/null+++ b/src/harper/LintSettingRow.tsx@@ -0,0 +1,40 @@+import React, { useEffect, useState } from 'react';+import { useLinter } from './HarperContext';+import { Animate, CheckboxControl, SelectControl } from '@wordpress/components';++export default function LintSettingRow({+ name,+ value,+ defaultValue,+ setValue,+}: {+ name: string;+ value: boolean | undefined;+ defaultValue: boolean;+ setValue: (newValue: boolean | undefined) => void;+}) {+ const linter = useLinter();++ const [title, setTitle] = useState<string | null>(null);++ useEffect(() => {+ linter.toTitleCase(name.replace(/_/g, ' ')).then(setTitle);+ }, [linter, name]);++ return title ? (+ <Animate type={title === null ? undefined : 'slide-in'}>+ {({ className }) => (+ <div className={`${className} harper-lint-config-row`}>+ <h3>{title}</h3>++ <CheckboxControl+ onChange={(val) => setValue(val)}+ checked={value ?? defaultValue}+ ></CheckboxControl>+ </div>+ )}+ </Animate>+ ) : (+ <></>+ );+}diff --git a/src/harper/SidebarControl.tsx b/src/harper/SidebarControl.tsxindex ecfdfb319..a78606f5d 100644--- a/src/harper/SidebarControl.tsx+++ b/src/harper/SidebarControl.tsx@@ -1,15 +1,8 @@import DataBlock from './DataBlock';-import React, {- createContext,- useCallback,- useEffect,- useMemo,- useRef,- useState,-} from 'react';+import React, { useCallback, useEffect, useMemo, useState } from 'react';import { createPortal } from 'react-dom';import Highlighter from './Highlighter';-import { WorkerLinter } from 'harper.js';+import LintSettingList from './LintSettingList';export default function SidebarControl() {const documentContainer = useMemo<Element>(@@ -53,5 +46,10 @@ export default function SidebarControl() {));- return <>{highlights}</>;+ return (+ <>+ {highlights}+ <LintSettingList />+ </>+ );}diff --git a/src/harper/index.css b/src/harper/index.cssindex 04aa0772c..053ed746c 100644--- a/src/harper/index.css+++ b/src/harper/index.css@@ -4,6 +4,12 @@padding: 10px;}+.harper-lint-config-row {+ display: flex;+ flex-direction: row;+ justify-content: space-between;+}+.harper-popover > div > button {width: 100%;margin-top: 6px;