From 9577f26cbab2acd237d9ab2ba9f6d3c064e9ae08 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 5 Nov 2014 00:27:43 +0800 Subject: [PATCH] viewport enhancement --- editor/editors.go | 4 ++-- static/js/editors.js | 28 +++++++++++++++++++++++++--- static/js/wide.js | 28 ++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/editor/editors.go b/editor/editors.go index 04edc38..e152c1e 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -301,8 +301,8 @@ func FindDeclarationHandler(w http.ResponseWriter, r *http.Request) { part := found[:strings.LastIndex(found, ":")] cursorSep := strings.LastIndex(part, ":") path = found[:cursorSep] - cursorLine := found[cursorSep+1 : strings.LastIndex(found, ":")] - cursorCh := found[strings.LastIndex(found, ":")+1:] + cursorLine, _ := strconv.Atoi(found[cursorSep+1 : strings.LastIndex(found, ":")]) + cursorCh, _ := strconv.Atoi(found[strings.LastIndex(found, ":")+1:]) data["path"] = path data["cursorLine"] = cursorLine diff --git a/static/js/editors.js b/static/js/editors.js index fc1df44..d2222d3 100644 --- a/static/js/editors.js +++ b/static/js/editors.js @@ -495,7 +495,6 @@ var editors = { $(".toolbars").show(); var id = wide.curNode.tId; - // 光标位置 var cursor = CodeMirror.Pos(0, 0); if (data.cursorLine && data.cursorCh) { cursor = CodeMirror.Pos(data.cursorLine - 1, data.cursorCh - 1); @@ -505,8 +504,28 @@ var editors = { if (editors.data[i].id === id) { editors.tabs.setCurrent(id); wide.curEditor = editors.data[i].editor; - wide.curEditor.setCursor(cursor); - wide.curEditor.focus(); + var editor = wide.curEditor; + var oldLine = editor.getCursor().line + 1; + + if (oldLine === data.cursorLine) { + editor.focus(); + + return false; + } + + editor.setCursor(cursor); + + var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2); + if (oldLine > data.cursorLine) { + var offset = data.cursorLine - half; + if (offset > 0) { + editor.scrollIntoView(CodeMirror.Pos(offset, 0)); + } + } else if (oldLine < data.cursorLine) { + editor.scrollIntoView(CodeMirror.Pos(data.cursorLine + half, 0)); + } + + editor.focus(); return false; } @@ -621,6 +640,9 @@ var editors = { editor.setCursor(cursor); + var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2); + editor.scrollIntoView(CodeMirror.Pos(cursor.line + half, 0)); + wide.curEditor = editor; editors.data.push({ "editor": editor, diff --git a/static/js/wide.js b/static/js/wide.js index 8d7ebf5..73656a0 100644 --- a/static/js/wide.js +++ b/static/js/wide.js @@ -208,8 +208,28 @@ var wide = { "ok": function () { var line = parseInt($("#dialogGoLinePrompt > input").val()); $("#dialogGoLinePrompt").dialog("close"); - wide.curEditor.setCursor(CodeMirror.Pos(line - 1, 0)); - wide.curEditor.focus(); + var editor = wide.curEditor; + var oldLine = editor.getCursor().line + 1; + + if (oldLine === line) { + editor.focus(); + + return; + } + + editor.setCursor(CodeMirror.Pos(line - 1, 0)); + + var half = Math.floor(editor.getScrollInfo().clientHeight / editor.defaultTextHeight() / 2); + if (oldLine > line) { + var offset = line - half; + if (offset > 0) { + editor.scrollIntoView(CodeMirror.Pos(offset, 0)); + } + } else if (oldLine < line) { + editor.scrollIntoView(CodeMirror.Pos(line + half, 0)); + } + + editor.focus(); } }); @@ -453,7 +473,7 @@ var wide = { dataType: "json", success: function (data) { // reset the save state - + editor.doc.markClean(); $(".edit-panel .tabs > div").each(function () { var $span = $(this).find("span:eq(0)"); @@ -469,7 +489,7 @@ var wide = { if (!path) { return false; } - + var editor = wide.curEditor; if (editor.doc.isClean()) { // no modification return false;