javascript ACE 编辑器更改事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19670178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ACE editor change event
提问by ensignos
I'm quite new to the ACEeditor and javascript in general however I have managed to achieve most of what I intended apart from the following:
总的来说,我对ACE编辑器和 javascript还是很陌生,但是除了以下内容之外,我已经设法实现了我的大部分意图:
I would like to enable or disable a 'save' button depending on whether there are outstanding modifications to the document and I have attempted to do this with 'change' event:
我想根据文档是否有未完成的修改来启用或禁用“保存”按钮,并且我尝试使用“更改”事件执行此操作:
UndoManager.reset();
$('#save').addClass("disabled");
editor.on('change', function() {
if (UndoManager.hasUndo()) {
$('#save').removeClass("disabled");
}
else {
$('#save').addClass("disabled");
}
});
On loading a document the 'disabled' class is removed immediately.
加载文档时,“禁用”类会立即删除。
Many thanks in advance if anyone can show me how it should be done.
如果有人能告诉我应该如何做,非常感谢。
回答by a user
Call editor.session.getUndoManager().reset()
after loading the document
and use isClean
, markClean
methods on undoManager
.
editor.session.getUndoManager().reset()
加载文件后调用并使用isClean
,markClean
方法undoManager
。
var saveButton = document.getElementById("save")
var editor = ace.edit("editor")
// using input event instead of change since it's called with some timeout
editor.on("input", function() {
saveButton.disabled = editor.session.getUndoManager().isClean()
});
saveButton.addEventListener("click", function() {
editor.session.getUndoManager().markClean()
saveButton.disabled = editor.session.getUndoManager().isClean()
})
<script src="http://ajaxorg.github.io/ace-builds/src/ace.js"></script>
<button id="save">save</button>
<div id="editor" style="height:200px"></div>
回答by Tyler Long
Your solution has a disadvantage: after saving, you are not able to undo. Most modern editors allow undo after saving.
您的解决方案有一个缺点:保存后,您无法撤消。大多数现代编辑器都允许在保存后撤消。
I suggest that you record down the original text and do a comparision whenever text changes. if text equals origin, disable save button.
建议大家把原文记录下来,有变化的时候再对比一下。如果文本等于原点,则禁用保存按钮。