onChange事件在草稿编辑器处于readOnly模式时不会触发。我想保留readOnly模式,但是它必须触发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}
/>
)