vantezzen / quill-languagetool

✒️ LanguageTool integration for Quill.js editors
https://vantezzen.github.io/quill-languagetool
14 stars 15 forks source link

Run spellcheck on defaultValue is broken #6

Open 3nt3 opened 1 year ago

3nt3 commented 1 year ago

Just using this code, the spell checking is only done initially with a single "\n" (wherever that comes from) and then after the user changes the text.

The text initially supplied to <ReactQuill /> is never checked.

function App() {
  const [value, setValue] = useState('asdfasdf');

  return <ReactQuill theme="snow" value={value} onChange={setValue}
    modules={{
      languageTool: {
        server:
          'some server',
        language: 'de-DE',
        cooldownTime: 2000,
      }
    }}
  />;
}

I checked using a debugger and I think this line https://github.com/vantezzen/quill-languagetool/blob/1bc67cee6c4e0652057dbb12c32e4500de4173fc/src/index.ts#L55 is responsible. The value supplied in value={...} ends up in the onTextChange handler, but then ignored because the source is 'api'.

Looking at this constructor https://github.com/vantezzen/quill-languagetool/blob/1bc67cee6c4e0652057dbb12c32e4500de4173fc/src/index.ts#L51-L61 and specifically the call to this.checkSpelling() it seems to me that the default text is supposed to also be checked.

3nt3 commented 1 year ago

Workaround for now: use an empty string as the default value, then populate manually after 100 miliseconds or so

​​​const [value, setValue] = useState('');
const editorRef = useRef<ReactQuill | null>(null);

useEffect(() => {
  if (editorRef) {
    setTimeout(() => {
      editorRef.current?.getEditor().setText("This is the actual text", "user");
    }, 100);
  }
}, []);