Javascript 具有ReactJS-撤消功能的Ace编辑器
我正在将Ace编辑器与React(,版本:8.0.0)一起使用 我已经设法添加了一个撤销按钮,但是在一个新的编辑器实例上撤销会删除整个文档 到目前为止,我的代码是:Javascript 具有ReactJS-撤消功能的Ace编辑器,javascript,reactjs,ace-editor,Javascript,Reactjs,Ace Editor,我正在将Ace编辑器与React(,版本:8.0.0)一起使用 我已经设法添加了一个撤销按钮,但是在一个新的编辑器实例上撤销会删除整个文档 到目前为止,我的代码是: class AceEditor extends React.Component { constructor(props, context) { super(props, context); this.editor = React.createRef(); } componentDidMount()
class AceEditor extends React.Component {
constructor(props, context) {
super(props, context);
this.editor = React.createRef();
}
componentDidMount() {
this.setState({
code: "Test text"
})
}
render() {
const {code} = this.state;
return (
<div>
<Button onClick={() => {
this.editor.current.editor.undo()
}}/>
<AceEditor
ref={this.editor}
value={code}
// more props
onLoad={editor => {
const session = editor.getSession();
const undoManager = session.getUndoManager();
undoManager.reset();
session.setUndoManager(undoManager);
}}
/>
</div>
);
}
}
类编辑器扩展了React.Component{
构造函数(道具、上下文){
超级(道具、背景);
this.editor=React.createRef();
}
componentDidMount(){
这是我的国家({
代码:“测试文本”
})
}
render(){
const{code}=this.state;
返回(
{
this.editor.current.editor.undo()
}}/>
{
const session=editor.getSession();
const undoManager=session.getUndoManager();
撤销manager.reset();
session.setUndoManager(undoManager);
}}
/>
);
}
}
我遗漏了什么,有什么解决办法吗?这是因为在处理
value={code}
之前调用了onload,一个有问题的解决办法是:
onLoad={editor => {
editor.once("change", function() {
editor.session.getUndoManager().reset();
});
}}