From b86a695f9c78d7698aa3617af520454401977163 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Fri, 25 Sep 2015 18:41:03 +0800 Subject: Added support of on changing from view mode to other mode will auto scroll to corresponding position --- public/js/index.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'public') diff --git a/public/js/index.js b/public/js/index.js index 4892073a..51988226 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -625,12 +625,15 @@ function toggleMode() { } } +var lastMode = null; + function changeMode(type) { lockNavbar(); saveInfo(); - if (type) + if (type) { + lastMode = currentMode; currentMode = type; - checkEditorStyle(); + } var responsiveClass = "col-lg-6 col-md-6 col-sm-6"; var scrollClass = "ui-scrollable"; ui.area.codemirror.removeClass(scrollClass); @@ -687,6 +690,14 @@ function changeMode(type) { restoreInfo(); + if (lastMode == modeType.view && currentMode == modeType.both) { + if (!scrollMap || !lineHeightMap) + buildMapInner(); + var scrollMapNearest = closestIndex(scrollMap, lastInfo.view.scroll.top); + var lineHeightMapNearest = closestIndex(lineHeightMap, scrollMapNearest); + var height = lineHeightMapNearest * defaultTextHeight; + editor.scrollTo(null, height); + } ui.toolbar.both.removeClass("active"); ui.toolbar.edit.removeClass("active"); @@ -713,6 +724,17 @@ function lockNavbar() { var unlockNavbar = _.debounce(function () { $('.navbar').removeClass('locked'); }, 200); + +function closestIndex(arr, closestTo) { + var closest = Math.max.apply(null, arr); //Get the highest number in arr in case it match nothing. + var index = 0; + for (var i = 0; i < arr.length; i++) { //Loop the array + if (arr[i] >= closestTo && arr[i] < closest) { + closest = arr[i]; //Check if it's higher than your number, but lower than your closest value + index = i; + } + } + return index; // return the value } //button actions -- cgit v1.2.3