🔵 line locate refactor

This commit is contained in:
Liang Ding 2014-11-07 14:13:55 +08:00
parent e7c47bf4ae
commit 1f1d77a974
2 changed files with 16 additions and 44 deletions

View File

@ -512,21 +512,16 @@ var editors = {
tree.openFile(tree.fileTree.getNodeByTId(tId)); tree.openFile(tree.fileTree.getNodeByTId(tId));
tree.fileTree.selectNode(wide.curNode); tree.fileTree.selectNode(wide.curNode);
var oldLine = wide.curEditor.getCursor().line;
var line = $it.find(".position").data("line") - 1; var line = $it.find(".position").data("line") - 1;
var cursor = CodeMirror.Pos(line, $it.find(".position").data("ch") - 1); var cursor = CodeMirror.Pos(line, $it.find(".position").data("ch") - 1);
wide.curEditor.setCursor(cursor);
var half = Math.floor(wide.curEditor.getScrollInfo().clientHeight / wide.curEditor.defaultTextHeight() / 2); var editor = wide.curEditor;
if (oldLine > line) { editor.setCursor(cursor);
var offset = line - half;
if (offset > 0) { var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2);
wide.curEditor.scrollIntoView(CodeMirror.Pos(offset, 0)); var cursorCoords = editor.cursorCoords({line: cursor.line - half, ch: 0}, "local");
} editor.scrollTo(0, cursorCoords.top);
} else if (oldLine < line) {
wide.curEditor.scrollIntoView(CodeMirror.Pos(line + half, 0));
}
wide.curEditor.focus(); wide.curEditor.focus();
}); });
@ -563,25 +558,12 @@ var editors = {
editors.tabs.setCurrent(id); editors.tabs.setCurrent(id);
wide.curEditor = editors.data[i].editor; wide.curEditor = editors.data[i].editor;
var editor = wide.curEditor; var editor = wide.curEditor;
var oldLine = editor.getCursor().line + 1;
if (oldLine === data.cursorLine) {
editor.focus();
return false;
}
editor.setCursor(cursor); editor.setCursor(cursor);
var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2); var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2);
if (oldLine > data.cursorLine) { var cursorCoords = editor.cursorCoords({line: cursor.line - half, ch: 0}, "local");
var offset = data.cursorLine - half; editor.scrollTo(0, cursorCoords.top);
if (offset > 0) {
editor.scrollIntoView(CodeMirror.Pos(offset, 0));
}
} else if (oldLine < data.cursorLine) {
editor.scrollIntoView(CodeMirror.Pos(data.cursorLine + half, 0));
}
editor.focus(); editor.focus();
@ -721,7 +703,8 @@ var editors = {
editor.setCursor(cursor); editor.setCursor(cursor);
var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2); var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2);
editor.scrollIntoView(CodeMirror.Pos(cursor.line + half, 0)); var cursorCoords = editor.cursorCoords({line: cursor.line - half, ch: 0}, "local");
editor.scrollTo(0, cursorCoords.top);
wide.curEditor = editor; wide.curEditor = editor;
editors.data.push({ editors.data.push({

View File

@ -208,28 +208,17 @@ var wide = {
$("#dialogGoLinePrompt").closest(".dialog-main").find(".dialog-footer > button:eq(0)").prop("disabled", true); $("#dialogGoLinePrompt").closest(".dialog-main").find(".dialog-footer > button:eq(0)").prop("disabled", true);
}, },
"ok": function () { "ok": function () {
var line = parseInt($("#dialogGoLinePrompt > input").val()); var line = parseInt($("#dialogGoLinePrompt > input").val()) - 1;
$("#dialogGoLinePrompt").dialog("close"); $("#dialogGoLinePrompt").dialog("close");
var editor = wide.curEditor; var editor = wide.curEditor;
var oldLine = editor.getCursor().line + 1; var cursor = editor.getCursor();
if (oldLine === line) { editor.setCursor(CodeMirror.Pos(line, cursor.ch));
editor.focus();
return;
}
editor.setCursor(CodeMirror.Pos(line - 1, 0));
var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2); var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2);
if (oldLine > line) { var cursorCoords = editor.cursorCoords({line: line - half, ch: cursor.ch}, "local");
var offset = line - half; editor.scrollTo(0, cursorCoords.top);
if (offset > 0) {
editor.scrollIntoView(CodeMirror.Pos(offset, 0));
}
} else if (oldLine < line) {
editor.scrollIntoView(CodeMirror.Pos(line + half, 0));
}
editor.focus(); editor.focus();
} }