javascript - 当草稿编辑器处于只读模式时 onChange 不会触发

标签 javascript html node.js reactjs draftjs

当草稿编辑器处于只读模式时,不会触发 onChange 事件。我想保留只读模式,但它必须触发 onChange 事件,以便我可以保存我的工作。

             <Editor
                readOnly={true}
                onChange={this.onChange}
                ref={(e) => { this.editor = e; }}
                autoCapitalize="none"
                autoComplete="off"
                autoCorrect="off"
                spellCheck={false}
              />

最佳答案

如下所示,您可以在编辑器更改或模糊事件上触发保存操作(但为了性能,我建议模糊):

const [editorState, setEditorState] = useState();
const [editable, setEditable]=useState(false);
const editorRef = useRef(null);

const onChange = (editorState) => {
  setEditorState(editorState);
};

const onBlur = (state) => {
   //  convertToRaw from draft-js;
  const contentState = convertToRaw(editorState.getCurrentContent());
  // save contentState      
};

return (
  <Editor
    editorState={editorState}
    readOnly={!editable}
    onChange={onChange}
    onBlur={onBlur}
    ref={editorRef}
    autoCapitalize="none"
    autoComplete="off"
    autoCorrect="off"
    spellCheck={false}
  />
)

关于javascript - 当草稿编辑器处于只读模式时 onChange 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58687416/

相关文章:

javascript - crossorigin anonymous 无法加载图像的问题

javascript - 我的 Web 文档中未启用画中画的可能原因是什么?

javascript - Codepen 和 JSFiddle 使用相同的代码有不同的结果?

javascript - 如果未发送,请重试发送邮件

javascript - NodeJS、Express、Jade POST JSON 到数据库

javascript - 获取 Javascript 文件中的 PHP 变量

javascript - 限制 HTML 文档或 iframe 元素中允许的资源

php - 仅使用 Cookie 的多语言网站 SEO 实践

html - 叠加出现在悬停上 - 纯 CSS

javascript - express + Passport ,错误: can't post request to passport and not throw any error