summaryrefslogtreecommitdiff
path: root/public/js/index.js
diff options
context:
space:
mode:
authorWu Cheng-Han2015-09-25 18:41:03 +0800
committerWu Cheng-Han2015-09-25 18:41:03 +0800
commitb86a695f9c78d7698aa3617af520454401977163 (patch)
tree1397b71e1f789d761a7329010489963e6d8764e7 /public/js/index.js
parent4b81ee9104f812ad9009fae6a4b527bc291b90e1 (diff)
Added support of on changing from view mode to other mode will auto scroll to corresponding position
Diffstat (limited to 'public/js/index.js')
-rw-r--r--public/js/index.js26
1 files changed, 24 insertions, 2 deletions
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