Skip to content

Commit 016e5c4

Browse files
committed
abstracting get|set scrollTop
1 parent 1596bb7 commit 016e5c4

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

v3/js/opt-frontend-common.js

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var pyInputCodeMirror; // CodeMirror object that contains the input code
6868
var pyInputAceEditor; // Ace editor object that contains the input code
6969

7070
var useCodeMirror = true; // true -> use CodeMirror, false -> use Ace
71-
var useCodeMirror = false; // true -> use CodeMirror, false -> use Ace
71+
var useCodeMirror = false;
7272

7373

7474
function initAceEditor(height) {
@@ -121,8 +121,6 @@ var executeCodeSignalFromRemote = false;
121121
var togetherjsSyncRequested = false;
122122
var pendingCodeOutputScrollTop = null;
123123

124-
var codeMirrorScroller = '#codeInputPane .CodeMirror-scroll';
125-
126124
TogetherJSConfig_ignoreForms = ['.togetherjsIgnore']; // do NOT sync these elements
127125

128126

@@ -286,10 +284,10 @@ function initTogetherJS() {
286284
updateAppDisplay(msg.appMode);
287285

288286
if (appMode == 'edit' && msg.codeInputScrollTop !== undefined &&
289-
$(codeMirrorScroller).scrollTop() != msg.codeInputScrollTop) {
287+
pyInputGetScrollTop() != msg.codeInputScrollTop) {
290288
// hack: give it a bit of time to settle first ...
291289
$.doTimeout('pyInputCodeMirrorInit', 200, function() {
292-
$(codeMirrorScroller).scrollTop(msg.codeInputScrollTop);
290+
pyInputSetScrollTop(msg.codeInputScrollTop);
293291
});
294292
}
295293
}
@@ -318,7 +316,7 @@ function initTogetherJS() {
318316
if (TogetherJS.running) {
319317
TogetherJS.send({type: "myAppState",
320318
myAppState: getAppState(),
321-
codeInputScrollTop: $(codeMirrorScroller).scrollTop(),
319+
codeInputScrollTop: pyInputGetScrollTop(),
322320
pyCodeOutputDivScrollTop: myVisualizer ?
323321
myVisualizer.domRoot.find('#pyCodeOutputDiv').scrollTop() :
324322
undefined});
@@ -378,7 +376,7 @@ function initTogetherJS() {
378376
// value. this is hacky; ideally we have a callback function for
379377
// when setValue() completes.
380378
$.doTimeout('pyInputCodeMirrorInit', 200, function() {
381-
$(codeMirrorScroller).scrollTop(msg.codeInputScrollTop);
379+
pyInputSetScrollTop(msg.codeInputScrollTop);
382380
});
383381
}
384382
});
@@ -387,7 +385,7 @@ function initTogetherJS() {
387385
// do NOT use a msg.sameUrl guard since that will miss some signals
388386
// due to our funky URLs
389387

390-
$(codeMirrorScroller).scrollTop(msg.scrollTop);
388+
pyInputSetScrollTop(msg.scrollTop);
391389
});
392390

393391
TogetherJS.hub.on("pyCodeOutputDivScroll", function(msg) {
@@ -566,6 +564,27 @@ function pyInputSetValue(dat) {
566564
}
567565

568566

567+
var codeMirrorScroller = '#codeInputPane .CodeMirror-scroll';
568+
569+
function pyInputGetScrollTop() {
570+
if (useCodeMirror) {
571+
return $(codeMirrorScroller).scrollTop();
572+
}
573+
else {
574+
return pyInputAceEditor.getSession().getScrollTop();
575+
}
576+
}
577+
578+
function pyInputSetScrollTop(st) {
579+
if (useCodeMirror) {
580+
$(codeMirrorScroller).scrollTop(st);
581+
}
582+
else {
583+
pyInputAceEditor.getSession().setScrollTop(st);
584+
}
585+
}
586+
587+
569588
// run at the END so that everything else can be initialized first
570589
function genericOptFrontendReady() {
571590
initTogetherJS(); // initialize early
@@ -589,7 +608,7 @@ function genericOptFrontendReady() {
589608
if (TogetherJS.running && !isExecutingCode) {
590609
TogetherJS.send({type: "hashchange",
591610
appMode: appMode,
592-
codeInputScrollTop: $(codeMirrorScroller).scrollTop(),
611+
codeInputScrollTop: pyInputGetScrollTop(),
593612
myAppState: getAppState()});
594613
}
595614
});
@@ -634,20 +653,36 @@ function genericOptFrontendReady() {
634653
}
635654

636655

637-
$(codeMirrorScroller).scroll(function(e) {
638-
if (TogetherJS.running) {
639-
var elt = $(this);
640-
// debounce
641-
$.doTimeout('codeInputScroll', 100, function() {
642-
// note that this will send a signal back and forth both ways
643-
// (there's no easy way to prevent this), but it shouldn't keep
644-
// bouncing back and forth indefinitely since no the second signal
645-
// causes no additional scrolling
646-
TogetherJS.send({type: "codeInputScroll",
647-
scrollTop: elt.scrollTop()});
648-
});
649-
}
650-
});
656+
if (useCodeMirror) {
657+
$(codeMirrorScroller).scroll(function(e) {
658+
if (TogetherJS.running) {
659+
var elt = $(this);
660+
$.doTimeout('codeInputScroll', 100, function() { // debounce
661+
// note that this will send a signal back and forth both ways
662+
// (there's no easy way to prevent this), but it shouldn't keep
663+
// bouncing back and forth indefinitely since no the second signal
664+
// causes no additional scrolling
665+
TogetherJS.send({type: "codeInputScroll",
666+
scrollTop: elt.scrollTop()});
667+
});
668+
}
669+
});
670+
}
671+
else {
672+
var s = pyInputAceEditor.getSession();
673+
s.on('changeScrollTop', function() {
674+
if (TogetherJS.running) {
675+
$.doTimeout('codeInputScroll', 100, function() { // debounce
676+
// note that this will send a signal back and forth both ways
677+
// (there's no easy way to prevent this), but it shouldn't keep
678+
// bouncing back and forth indefinitely since no the second signal
679+
// causes no additional scrolling
680+
TogetherJS.send({type: "codeInputScroll",
681+
scrollTop: s.getScrollTop()});
682+
});
683+
}
684+
});
685+
}
651686

652687

653688
// first initialize options from HTML LocalStorage. very important

0 commit comments

Comments
 (0)