【问题标题】:How turn off spell check in QuillJS (React)如何在 QuillJS (React) 中关闭拼写检查
【发布时间】:2020-08-26 23:36:36
【问题描述】:

如何在 React 中关闭 quill.js 中的拼写检查?

我发现this GitHub page 显示了如何在普通 JavaScript 中禁用 quill 的拼写检查器:

const quill = new Quill('#editor-container')
quill.root.setAttribute('spellcheck', false)

但是,我看不到如何使用 React 组件来实现它。

我的 React 组件(实际上是 Preact):

import { h, FunctionalComponent } from 'preact';
import './contentEditor.scss';
import Quill from 'react-quill';
import 'react-quill/dist/quill.snow.css';

interface ContentEditorProps {
  content: string | undefined;
  onChange(value: string): void;
}

const modules = {
  toolbar: [
    [{ header: [1, 2, 3, 4, false] }],
    ['bold', 'italic', 'underline', 'blockquote'],
    [{ color: [] }],
    [{ align: [] }],
    [{ list: 'ordered' }, { list: 'bullet' }],
    [{ indent: '-1' }, { indent: '+1' }],
    ['link', 'image'],
  ],
};

const formats = [
  'header',
  'bold',
  'color',
  'italic',
  'underline',
  'strike',
  'blockquote',
  'list',
  'bullet',
  'align',
  'indent',
  'link',
  'image',
];

export const ContentEditor: FunctionalComponent<ContentEditorProps> = ({
  content,
  onChange,
}) => {
  return (
    <Quill
      theme='snow'
      value={content}
      onChange={onChange}
      modules={modules}
      formats={formats}
    />
  );
};

【问题讨论】:

  • 从未使用过,但由于quill.root.setAttribute() 设置了元素属性,请尝试将其添加到&lt;Quill spellcheck={false}... 的道具中
  • 这是个好主意——事实上,我认为它会起作用......不幸的是,它没有。不过,谢谢你的想法。

标签: javascript reactjs quill preact


【解决方案1】:

为了访问Quill 编辑器,您必须为&lt;Quill /&gt; 组件创建一个ref,然后使用它来设置属性。这是sn-p代码:


// For typings
import Quill from "react-quill";
import QuillEditor from "quill"

export const ContentEditor = ({ content, onChange }) => {
  const ref = React.useRef<Quill & { editor: QuillEditor }>(null);

  // Disable spellcheck as component is mounted
  React.useEffect(() => {
    ref.current?.editor.root.setAttribute("spellcheck", "false");
  }, []);

  return (
    <Quill
      // set the ref to access to quill editor
      ref={ref}
      theme="snow"
      value={content}
      onChange={onChange}
      modules={modules}
      formats={formats}
    />
  );
};

我也做了一个示例:https://codesandbox.io/s/stoic-mendel-4g3jw?file=/src/App.js

【讨论】:

  • 我在 ReactAdmin 中使用 quill,其中默认情况下未启用拼写检查。我启用了它const configureQuill = (quill) =&gt; quill.root.setAttribute("spellcheck", "true")
猜你喜欢
  • 1970-01-01
  • 2012-04-18
  • 2017-01-06
  • 1970-01-01
  • 2012-02-02
  • 2010-09-25
  • 2012-06-07
  • 2013-08-19
  • 2010-09-20
相关资源
最近更新 更多