Skip to content

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

feat: add ability to add to the personal dictionary

Elijah Potter269a8bac9269
diff --git a/src/harper/LintListItem.tsx b/src/harper/LintListItem.tsx
index b75e74542..4059465e2 100644
--- a/src/harper/LintListItem.tsx
+++ b/src/harper/LintListItem.tsx
@@ -1,9 +1,12 @@
import { Card, Button, CardBody } from '@wordpress/components';
+import { useAddToDictionary } from './usePersonalDictionary';
import { IgnorableLintBox } from './Box';
import React from 'react';
import { suggestionText } from './lintUtils';
export default function LintListItem({ box }: { box: IgnorableLintBox }) {
+ const addToDictionary = useAddToDictionary();
+
return (
<Card size="small" className="harper-lint-card">
<CardBody>
@@ -26,6 +29,19 @@ export default function LintListItem({ box }: { box: IgnorableLintBox }) {
</Button>
))}
+ {box.lint.lint_kind() === 'Spelling' ? (
+ <Button
+ onClick={() =>
+ addToDictionary(box.lint.get_problem_text())
+ }
+ variant="primary"
+ >
+ Add “{box.lint.get_problem_text()}” to the dictionary
+ </Button>
+ ) : (
+ <></>
+ )}
+
<Button variant="link" onClick={box.ignoreLint}>
Ignore
</Button>
diff --git a/src/harper/SuggestionControl.tsx b/src/harper/SuggestionControl.tsx
index 3db788879..1c66d05ac 100644
--- a/src/harper/SuggestionControl.tsx
+++ b/src/harper/SuggestionControl.tsx
@@ -1,6 +1,6 @@
import React, { useEffect, useMemo, useRef, useState } from 'react';
-import { IgnorableLintBox, isPointInBox, LintBox } from './Box';
-import { SuggestionKind } from 'harper.js';
+import { IgnorableLintBox, isPointInBox } from './Box';
+import { useAddToDictionary } from './usePersonalDictionary';
import { Button, Popover } from '@wordpress/components';
import { suggestionText } from './lintUtils';
@@ -16,6 +16,7 @@ export default function SuggestionControl({
lintBox: IgnorableLintBox;
}) {
const { x, y, width, height, lint, applySuggestion, ignoreLint } = lintBox;
+ const addToDictionary = useAddToDictionary();
const underlineRef = useRef<HTMLDivElement | null>(null);
const popoverRef = useRef<HTMLDivElement | null>(null);
@@ -98,6 +99,19 @@ export default function SuggestionControl({
</Button>
))}
+ {lint.lint_kind() === 'Spelling' ? (
+ <Button
+ onClick={() =>
+ addToDictionary(lint.get_problem_text())
+ }
+ variant="primary"
+ >
+ Add “{lint.get_problem_text()}” to the dictionary
+ </Button>
+ ) : (
+ <></>
+ )}
+
<Button variant="link" onClick={ignoreLint}>
Ignore
</Button>
diff --git a/src/harper/index.css b/src/harper/index.css
index 286a67534..9be481593 100644
--- a/src/harper/index.css
+++ b/src/harper/index.css
@@ -1,5 +1,5 @@
.harper-popover > div {
- width: 200px;
+ width: 300px;
z-index: 100;
padding: 10px;
}
diff --git a/src/harper/lintUtils.ts b/src/harper/lintUtils.ts
index 5676f8611..8464a555d 100644
--- a/src/harper/lintUtils.ts
+++ b/src/harper/lintUtils.ts
@@ -12,9 +12,9 @@ export function suggestionText(
replacementText: string
): string {
if (kind === SuggestionKind.Remove) {
- return `Remove "${problemText}"`;
+ return `Remove “${problemText}”`;
} else if (kind === SuggestionKind.Replace) {
return `Replace with “${replacementText}”`;
}
- return `Insert "${replacementText}"`;
+ return `Insert “${replacementText}”`;
}
diff --git a/src/harper/useLintBoxes.ts b/src/harper/useLintBoxes.ts
index 21cd87845..535fa5c07 100644
--- a/src/harper/useLintBoxes.ts
+++ b/src/harper/useLintBoxes.ts
@@ -1,3 +1,4 @@
+import usePersonalDictionary from './usePersonalDictionary';
import { useCallback, useEffect, useState } from 'react';
import { IgnorableLintBox, LintBox } from './Box';
import RichText from './RichText';
@@ -17,6 +18,7 @@ export default function useLintBoxes(
const linter = useLinter();
const [config] = useLintConfig();
const [ignoreState] = useIgnoredLintState();
+ const [personalDictionary] = usePersonalDictionary();
const ignoreLint = useIgnoreLint();
const [targetBoxes, setTargetBoxes] = useState<IgnorableLintBox[][]>([]);
@@ -28,6 +30,10 @@ export default function useLintBoxes(
await linter.clearIgnoredLints();
}
+ if (personalDictionary) {
+ await linter.importWords(personalDictionary);
+ }
+
if (
JSON.stringify(await linter.getLintConfig()) !==
JSON.stringify(config)
@@ -50,7 +56,7 @@ export default function useLintBoxes(
setLoading(false);
setLints(newLints);
- }, [richTexts, linter, config, ignoreState]);
+ }, [richTexts, linter, config, ignoreState, personalDictionary]);
useEffect(() => {
updateLints();
diff --git a/src/harper/usePersonalDictionary.ts b/src/harper/usePersonalDictionary.ts
new file mode 100644
index 000000000..00614e55e
--- /dev/null
+++ b/src/harper/usePersonalDictionary.ts
@@ -0,0 +1,39 @@
+import { useDispatch, useSelect } from '@wordpress/data';
+import { useCallback } from 'react';
+
+const KEY = 'personalDictionary';
+
+/** Read and add to the user's personal dictionary. */
+export default function usePersonalDictionary(): [
+ string[] | undefined,
+ (updatedDictionary: string[]) => void,
+] {
+ const personalDictionary = useSelect(
+ (select) => select('core/preferences').get('harper-wp', KEY),
+ []
+ );
+
+ const { set } = useDispatch('core/preferences');
+
+ const updateState = useCallback(
+ (updatedDictionary: string[]) =>
+ set('harper-wp', KEY, updatedDictionary),
+ [set]
+ );
+
+ return [personalDictionary, updateState];
+}
+
+/** Get a callback that adds a word to the personal dictionary. */
+export function useAddToDictionary(): (word: string) => void {
+ const [dict, setDict] = usePersonalDictionary();
+
+ return useCallback(
+ (word: string) => {
+ if (!dict?.includes(word)) {
+ setDict([...(dict ?? []), word]);
+ }
+ },
+ [dict, setDict]
+ );
+}