This commit is contained in:
Van 2014-11-18 10:51:26 +08:00
parent 1b60c8712e
commit 6381e16287
2 changed files with 25 additions and 41 deletions

View File

@ -442,34 +442,11 @@ var editors = {
return; return;
} }
var cursorLine = data.cursorLine;
var cursorCh = data.cursorCh;
var request = newWideRequest();
request.path = data.path;
// TODO: refactor
$.ajax({
type: 'POST',
url: '/file',
data: JSON.stringify(request),
dataType: "json",
success: function (data) {
if (!data.succ) {
$("#dialogAlert").dialog("open", data.msg);
return false;
}
var tId = tree.getTIdByPath(data.path); var tId = tree.getTIdByPath(data.path);
wide.curNode = tree.fileTree.getNodeByTId(tId); wide.curNode = tree.fileTree.getNodeByTId(tId);
tree.fileTree.selectNode(wide.curNode); tree.fileTree.selectNode(wide.curNode);
data.cursorLine = cursorLine; tree.openFile(wide.curNode, CodeMirror.Pos(data.cursorLine - 1, data.cursorCh - 1));
data.cursorCh = cursorCh;
tree.openFile(wide.curNode);
}
});
} }
}); });
}; };
@ -574,7 +551,7 @@ var editors = {
$(".bottom-window-group .search").focus(); $(".bottom-window-group .search").focus();
}, },
// 新建一个编辑器 Tab如果已经存在 Tab 则切换到该 Tab. // 新建一个编辑器 Tab如果已经存在 Tab 则切换到该 Tab.
newEditor: function (data) { newEditor: function (data, cursor) {
$(".toolbars").show(); $(".toolbars").show();
var id = wide.curNode.tId; var id = wide.curNode.tId;
@ -805,11 +782,12 @@ var editors = {
"id": id "id": id
}); });
var cursor = CodeMirror.Pos(0, 0);
if (data.cursorLine && data.cursorCh) {
cursor = CodeMirror.Pos(data.cursorLine - 1, data.cursorCh - 1);
}
$(".footer .cursor").text('| ' + (cursor.line + 1) + ':' + (cursor.ch + 1) + ' |'); $(".footer .cursor").text('| ' + (cursor.line + 1) + ':' + (cursor.ch + 1) + ' |');
var half = Math.floor(wide.curEditor.getScrollInfo().clientHeight / wide.curEditor.defaultTextHeight() / 2);
var cursorCoords = wide.curEditor.cursorCoords({line: cursor.line - half, ch: 0}, "local");
wide.curEditor.scrollTo(0, cursorCoords.top);
editor.setCursor(cursor); editor.setCursor(cursor);
editor.focus(); editor.focus();
} }

View File

@ -242,8 +242,9 @@ var tree = {
this._initSearch(); this._initSearch();
}, },
openFile: function (treeNode) { openFile: function (treeNode, cursor) {
wide.curNode = treeNode; wide.curNode = treeNode;
var tempCursor = cursor;
for (var i = 0, ii = editors.data.length; i < ii; i++) { for (var i = 0, ii = editors.data.length; i < ii; i++) {
// 该节点文件已经打开 // 该节点文件已经打开
@ -251,12 +252,14 @@ var tree = {
editors.tabs.setCurrent(treeNode.tId); editors.tabs.setCurrent(treeNode.tId);
wide.curEditor = editors.data[i].editor; wide.curEditor = editors.data[i].editor;
var cursor = wide.curEditor.getCursor(); if (!tempCursor) {
$(".footer .cursor").text('| ' + (cursor.line + 1) + ':' + (cursor.ch + 1) + ' |'); tempCursor = wide.curEditor.getCursor();
}
$(".footer .cursor").text('| ' + (tempCursor.line + 1) + ':' + (tempCursor.ch + 1) + ' |');
wide.curEditor.setCursor(cursor); wide.curEditor.setCursor(tempCursor);
var half = Math.floor(wide.curEditor.getScrollInfo().clientHeight / wide.curEditor.defaultTextHeight() / 2); var half = Math.floor(wide.curEditor.getScrollInfo().clientHeight / wide.curEditor.defaultTextHeight() / 2);
var cursorCoords = wide.curEditor.cursorCoords({line: cursor.line - half, ch: 0}, "local"); var cursorCoords = wide.curEditor.cursorCoords({line: tempCursor.line - half, ch: 0}, "local");
wide.curEditor.scrollTo(0, cursorCoords.top); wide.curEditor.scrollTo(0, cursorCoords.top);
wide.curEditor.focus(); wide.curEditor.focus();
@ -287,7 +290,10 @@ var tree = {
return false; return false;
} }
editors.newEditor(data); if (!tempCursor) {
tempCursor = CodeMirror.Pos(0, 0);
}
editors.newEditor(data, tempCursor);
} }
}); });
} }