这是我的代码
class DraftQuestion extends React.Component {
constructor(props) {
super(props);
this.state = {readOnly: true};
const content = window.localStorage.getItem('content');
if (content) {
this.state.editorState = EditorState.createWithContent(convertFromRaw(JSON.parse(content)));
} else {
this.state.editorState = EditorState.createEmpty();
}
this.handleKeyCommand = this.handleKeyCommand.bind(this);
this.mathjaxPlugin = createMathjaxPlugin();
}
saveContent = (content) => {
window.localStorage.setItem('content', JSON.stringify(convertToRaw(content)));
}
onChange = (editorState) => {
let content = this.state.editorState.getCurrentContent()
this.saveContent(content);
this.setState({ editorState })
}
render() {
this.plugins = [
this.mathjaxPlugin,
]
return (
<div>
<Editor
editorState={this.state.editorState}
handleKeyCommand={this.handleKeyCommand}
readOnly={true}
onChange={this.onChange}
plugins={this.plugins}
readOnly={this.state.readOnly}
/>
</div>
);
}
}
在这里你可以看到 readOnly 是true
,但是当我执行代码并显示文本时,文本是不可编辑的(正如预期的那样,因为 readOnly 字段)但是添加的数学方程(例如 \alpha 或 \sigma)mathjaxPlugin
仍然是可编辑。