Skip to content

Commit

Permalink
Add a spell-checker context (#2413)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Jul 25, 2023
1 parent f9bc1e0 commit 9ffd0a8
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Autocomplete, TextField } from "@mui/material";
import React, { ReactElement, useEffect } from "react";
import React, { ReactElement, useContext, useEffect } from "react";
import { Key } from "ts-key-enum";

import { WritingSystem } from "api";
import SpellChecker from "utilities/spellChecker";
import SpellCheckerContext from "utilities/spellCheckerContext";

interface GlossWithSuggestionsProps {
isNew?: boolean;
Expand All @@ -16,7 +16,6 @@ interface GlossWithSuggestionsProps {
analysisLang: WritingSystem;
textFieldId: string;
onUpdate?: () => void;
spellChecker?: SpellChecker;
}

/**
Expand All @@ -25,6 +24,8 @@ interface GlossWithSuggestionsProps {
export default function GlossWithSuggestions(
props: GlossWithSuggestionsProps
): ReactElement {
const spellChecker = useContext(SpellCheckerContext);

const maxSuggestions = 5;

useEffect(() => {
Expand All @@ -44,7 +45,7 @@ export default function GlossWithSuggestions(
}
// freeSolo allows use of a typed entry not available as a drop-down option
freeSolo
options={props.spellChecker?.getSpellingSuggestions(props.gloss) ?? []}
options={spellChecker.getSpellingSuggestions(props.gloss)}
value={props.gloss}
onBlur={() => {
if (props.onBlur) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import renderer from "react-test-renderer";

import GlossWithSuggestions from "components/DataEntry/DataEntryTable/EntryCellComponents/GlossWithSuggestions";
import { newWritingSystem } from "types/writingSystem";
import SpellChecker from "utilities/spellChecker";

// A work-around for this console error: https://github.com/mui/material-ui/issues/28687#issuecomment-1513741911
jest.mock("@mui/base/node/useAutocomplete/useAutocomplete", () => () => ({
Expand Down Expand Up @@ -60,20 +59,4 @@ describe("GlossWithSuggestions", () => {
);
});
});

it("renders with spell checker", () => {
renderer.act(() => {
renderer.create(
<GlossWithSuggestions
gloss={""}
glossInput={React.createRef<HTMLDivElement>()}
updateGlossField={jest.fn()}
handleEnterAndTab={jest.fn()}
analysisLang={newWritingSystem()}
textFieldId={"test-gloss"}
spellChecker={new SpellChecker("en")}
/>
);
});
});
});
3 changes: 0 additions & 3 deletions src/components/DataEntry/DataEntryTable/NewEntry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import VernDialog from "components/DataEntry/DataEntryTable/NewEntry/VernDialog"
import Pronunciations from "components/Pronunciations/PronunciationsComponent";
import { StoreState } from "types";
import theme from "types/theme";
import SpellChecker from "utilities/spellChecker";

const idAffix = "new-entry";

Expand All @@ -44,7 +43,6 @@ const gridItemStyle = (spacing: number): CSSProperties => ({
interface NewEntryProps {
analysisLang: WritingSystem;
vernacularLang: WritingSystem;
spellChecker?: SpellChecker;
// Parent component handles new entry state:
addNewEntry: () => Promise<void>;
updateWordWithNewGloss: (wordId: string) => Promise<void>;
Expand Down Expand Up @@ -287,7 +285,6 @@ export default function NewEntry(props: NewEntryProps): ReactElement {
analysisLang={analysisLang}
textFieldId={`${idAffix}-gloss`}
onUpdate={() => conditionalFocus(FocusTarget.Gloss)}
spellChecker={props.spellChecker}
/>
</Grid>
<Grid item xs={1} style={gridItemStyle(1)}>
Expand Down
3 changes: 0 additions & 3 deletions src/components/DataEntry/DataEntryTable/RecentEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import Pronunciations from "components/Pronunciations/PronunciationsComponent";
import theme from "types/theme";
import { newGloss } from "types/word";
import SpellChecker from "utilities/spellChecker";
import { firstGlossText } from "utilities/wordUtilities";

const idAffix = "recent-entry";
Expand All @@ -30,7 +29,6 @@ export interface RecentEntryProps {
analysisLang: WritingSystem;
vernacularLang: WritingSystem;
disabled?: boolean;
spellChecker?: SpellChecker;
}

/**
Expand Down Expand Up @@ -109,7 +107,6 @@ export default function RecentEntry(props: RecentEntryProps): ReactElement {
}}
analysisLang={props.analysisLang}
textFieldId={`${idAffix}-${props.rowIndex}-gloss`}
spellChecker={props.spellChecker}
/>
</Grid>
<Grid
Expand Down
7 changes: 3 additions & 4 deletions src/components/DataEntry/DataEntryTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ReactElement,
RefObject,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
Expand Down Expand Up @@ -35,7 +36,7 @@ import { useAppSelector } from "types/hooks";
import theme from "types/theme";
import { newNote, newSense, newWord, simpleWord } from "types/word";
import { defaultWritingSystem } from "types/writingSystem";
import SpellChecker from "utilities/spellChecker";
import SpellCheckerContext from "utilities/spellCheckerContext";
import { LevenshteinDistance } from "utilities/utilities";
import { firstGlossText } from "utilities/wordUtilities";

Expand Down Expand Up @@ -233,7 +234,7 @@ export default function DataEntryTable(

const levDist = useMemo(() => new LevenshteinDistance(), []);
const newVernInput = useRef<HTMLDivElement>(null);
const spellChecker = useMemo(() => new SpellChecker(), []);
const spellChecker = useContext(SpellCheckerContext);
useEffect(() => {
spellChecker.updateLang(analysisLang.bcp47);
}, [analysisLang.bcp47, spellChecker]);
Expand Down Expand Up @@ -876,7 +877,6 @@ export default function DataEntryTable(
deleteAudioFromWord={(wordId: string, fileName: string) =>
deleteAudioFromWord(wordId, fileName)
}
spellChecker={spellChecker}
focusNewEntry={() => focusInput(newVernInput)}
analysisLang={analysisLang}
vernacularLang={vernacularLang}
Expand All @@ -889,7 +889,6 @@ export default function DataEntryTable(

<Grid item xs={12}>
<NewEntry
spellChecker={spellChecker}
analysisLang={analysisLang}
vernacularLang={vernacularLang}
// Parent handles new entry state of child:
Expand Down
7 changes: 7 additions & 0 deletions src/utilities/spellCheckerContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createContext } from "react";

import SpellChecker from "utilities/spellChecker";

const SpellCheckerContext = createContext(new SpellChecker());

export default SpellCheckerContext;

0 comments on commit 9ffd0a8

Please sign in to comment.