separate save and format

This commit is contained in:
Liang Ding 2014-11-04 16:20:51 +08:00
parent ef8be349a4
commit a60ef9630a
1 changed files with 76 additions and 37 deletions

View File

@ -437,15 +437,14 @@ var wide = {
this._initLayout(); this._initLayout();
}, },
_save: function () { _save: function (path, editor) {
var currentPath = editors.getCurrentPath(); if (!path) {
if (!currentPath) {
return false; return false;
} }
var request = newWideRequest(); var request = newWideRequest();
request.file = currentPath; request.file = path;
request.code = wide.curEditor.getValue(); request.code = editor.getValue();
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
@ -453,24 +452,50 @@ var wide = {
data: JSON.stringify(request), data: JSON.stringify(request),
dataType: "json", dataType: "json",
success: function (data) { success: function (data) {
// reset the save state
editor.doc.markClean();
$(".edit-panel .tabs > div").each(function () {
var $span = $(this).find("span:eq(0)");
if ($span.attr("title") === path) {
$span.removeClass("changed");
}
});
} }
}); });
}, },
saveFile: function () { saveFile: function () {
var currentPath = editors.getCurrentPath(); var path = editors.getCurrentPath();
if (!currentPath) { if (!path) {
return false;
}
var editor = wide.curEditor;
if (editor.doc.isClean()) { // no modification
return false; return false;
} }
// 格式化后会对文件进行保存 if ("text/x-go" === editor.getOption("mode")) {
this.fmt(currentPath, wide.curEditor); wide.gofmt(path, wide.curEditor); // go fmt will save
return;
}
wide._save(path, wide.curEditor);
}, },
saveAllFiles: function () { saveAllFiles: function () {
if ($(".menu li.save-all").hasClass("disabled")) { if ($(".menu li.save-all").hasClass("disabled")) {
return false; return false;
} }
for (var i = 0, ii = editors.data.length; i < ii; i++) { for (var i = 0, ii = editors.data.length; i < ii; i++) {
this.fmt(tree.fileTree.getNodeByTId(editors.data[i].id).path, editors.data[i].editor); var path = tree.fileTree.getNodeByTId(editors.data[i].id).path;
var editor = editors.data[i].editor;
if ("text/x-go" === editor.getOption("mode")) {
wide.fmt(path, editor);
} else {
wide._save(path, editor);
}
} }
}, },
closeAllFiles: function () { closeAllFiles: function () {
@ -692,19 +717,42 @@ var wide = {
} }
}); });
}, },
fmt: function (path, curEditor) { gofmt: function (path, editor) {
if (curEditor.doc.isClean()) { // 没有修改过,不需要保存 var cursor = editor.getCursor();
return false; var scrollInfo = editor.getScrollInfo();
}
var mode = curEditor.getOption("mode");
var cursor = curEditor.getCursor();
var scrollInfo = curEditor.getScrollInfo();
var request = newWideRequest(); var request = newWideRequest();
request.file = path; request.file = path;
request.code = curEditor.getValue(); request.code = editor.getValue();
request.cursorLine = cursor.line;
request.cursorCh = cursor.ch;
$.ajax({
async: false, // sync
type: 'POST',
url: '/go/fmt',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (data.succ) {
editor.setValue(data.code);
editor.setCursor(cursor);
editor.scrollTo(null, scrollInfo.top);
wide._save(path, editor);
}
}
});
},
fmt: function (path, editor) {
var mode = editor.getOption("mode");
var cursor = editor.getCursor();
var scrollInfo = editor.getScrollInfo();
var request = newWideRequest();
request.file = path;
request.code = editor.getValue();
request.cursorLine = cursor.line; request.cursorLine = cursor.line;
request.cursorCh = cursor.ch; request.cursorCh = cursor.ch;
@ -713,7 +761,7 @@ var wide = {
switch (mode) { switch (mode) {
case "text/x-go": case "text/x-go":
$.ajax({ $.ajax({
async: false, // 同步执行 async: false, // sync
type: 'POST', type: 'POST',
url: '/go/fmt', url: '/go/fmt',
data: JSON.stringify(request), data: JSON.stringify(request),
@ -727,34 +775,25 @@ var wide = {
break; break;
case "text/html": case "text/html":
formatted = html_beautify(curEditor.getValue()); formatted = html_beautify(editor.getValue());
break; break;
case "text/javascript": case "text/javascript":
case "application/json": case "application/json":
formatted = js_beautify(curEditor.getValue()); formatted = js_beautify(editor.getValue());
break; break;
case "text/css": case "text/css":
formatted = css_beautify(curEditor.getValue()); formatted = css_beautify(editor.getValue());
break; break;
default : default :
break; break;
} }
if (formatted) { if (formatted) {
curEditor.setValue(formatted); editor.setValue(formatted);
curEditor.setCursor(cursor); editor.setCursor(cursor);
curEditor.scrollTo(null, scrollInfo.top); editor.scrollTo(null, scrollInfo.top);
wide._save(); wide._save(path, editor);
// 清除未保存状态
curEditor.doc.markClean();
$(".edit-panel .tabs > div").each(function () {
var $span = $(this).find("span:eq(0)");
if ($span.attr("title") === path) {
$span.removeClass("changed");
}
});
} }
}, },
openAbout: function () { openAbout: function () {