Skip to content

Commit b86a695

Browse files
committed
Added support of on changing from view mode to other mode will auto scroll to corresponding position
1 parent 4b81ee9 commit b86a695

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

public/js/index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,15 @@ function toggleMode() {
625625
}
626626
}
627627

628+
var lastMode = null;
629+
628630
function changeMode(type) {
629631
lockNavbar();
630632
saveInfo();
631-
if (type)
633+
if (type) {
634+
lastMode = currentMode;
632635
currentMode = type;
633-
checkEditorStyle();
636+
}
634637
var responsiveClass = "col-lg-6 col-md-6 col-sm-6";
635638
var scrollClass = "ui-scrollable";
636639
ui.area.codemirror.removeClass(scrollClass);
@@ -687,6 +690,14 @@ function changeMode(type) {
687690

688691
restoreInfo();
689692

693+
if (lastMode == modeType.view && currentMode == modeType.both) {
694+
if (!scrollMap || !lineHeightMap)
695+
buildMapInner();
696+
var scrollMapNearest = closestIndex(scrollMap, lastInfo.view.scroll.top);
697+
var lineHeightMapNearest = closestIndex(lineHeightMap, scrollMapNearest);
698+
var height = lineHeightMapNearest * defaultTextHeight;
699+
editor.scrollTo(null, height);
700+
}
690701

691702
ui.toolbar.both.removeClass("active");
692703
ui.toolbar.edit.removeClass("active");
@@ -713,6 +724,17 @@ function lockNavbar() {
713724
var unlockNavbar = _.debounce(function () {
714725
$('.navbar').removeClass('locked');
715726
}, 200);
727+
728+
function closestIndex(arr, closestTo) {
729+
var closest = Math.max.apply(null, arr); //Get the highest number in arr in case it match nothing.
730+
var index = 0;
731+
for (var i = 0; i < arr.length; i++) { //Loop the array
732+
if (arr[i] >= closestTo && arr[i] < closest) {
733+
closest = arr[i]; //Check if it's higher than your number, but lower than your closest value
734+
index = i;
735+
}
736+
}
737+
return index; // return the value
716738
}
717739

718740
//button actions

0 commit comments

Comments
 (0)