2016-01-23 87 views
0
editor.getSession().on("change",function(editing){ 
    if (ide.curOp && ide.curOp.command.name){ 
     console.log("change when pressing keys"); 
    } 
    else { 
     console.log("Changed when Click on autocomplete list or programically."); 
     // This change is programmatically but if its via click on autocomplete list or not? 
     // If its via click on autocomplete I want to save document else want to ignore. 
    } 
}); 

我在代碼中的評論很好地解釋了我的問題。王牌編輯器檢測是否通過自動完成發生更改

回答

2

答案在很大程度上取決於你所謂的「程序化」,任何編輯器都是通過api調用完成的,所以一切都是「程序化」的。例如。如果有人增加了一個<button onclick='editor.setValue("")'>將會導致它的變化是「程序化」還是不變。

如果您想區分您的代碼與其他人進行的API調用,請在調用ace api之前使用布爾變量並將其設置爲true,然後將其設置爲false。

var ignoreChanges = false 
editor.session.on("change", function(delta){ 
    if (ignoreChanges) return console.log("ignore changes made by me") 
    console.log("handle changes made in some other way")  
}) 
function applyChange() { 
    try { 
     ignoreChanges = true 
     // call some editor api here 
     editor.insert("...") 
    } finally { 
     ignoreChanges = false 
    } 
} 
+0

我剛剛在上面的代碼中更改了註釋,以編程方式表示通過api進行更改。我的問題是我想保存文件,如果用戶做了更改,否則不保存。現在,當我從自動完成列表中單擊時,它將轉到其他情況,而我在用戶所做的更改情況下需要它。希望我解釋得很好 –

+0

您是否在嘗試實施協作編輯? –

+0

是非常正確,聰明的人。:)。我們有解決方案嗎?現在你也可以理解我的另外一個問題,就是禁用undomanager,以免不合作者與不同合作者之間的衝突。這兩個最後一個問題將完成我的編輯的第一個階段,如果你能幫忙,我會非常感激。 –