Skip to content

Commit 394fffd

Browse files
committed
started refactoring from CodeMirror to Ace code editor
1 parent 0290bc7 commit 394fffd

10 files changed

Lines changed: 125 additions & 81 deletions

File tree

‎v3/css/opt-frontend.css‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,17 @@ table#pyOutputPane {
5151
margin-right: auto;
5252
}
5353

54-
#codeInputPane {
54+
#codeInputPane, #codeInputPaneAce {
5555
margin-top: 5px;
5656
font-size: 12pt;
5757
border: 1px solid #ddd;
5858
}
5959

60+
#codeInputPaneAce {
61+
width: 700px;
62+
height: 420px;
63+
}
64+
6065
button.smallBtn {
6166
font-size: 10pt;
6267
padding: 3px;

‎v3/js/ace/README.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This version of Ace was checked out from:
2+
https://github.com/ajaxorg/ace-builds/
3+
4+
on 2014-07-04

‎v3/js/ace/src-min-noconflict/ace.js‎

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎v3/js/ace/src-min-noconflict/mode-python.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎v3/js/composingprograms.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function executeCode(forceStartingInstr, forceRawInputLst) {
7979
showAllFrameLabels: true,
8080
}
8181

82-
executePythonCode(pyInputCodeMirror.getValue(),
82+
executePythonCode(pyInputGetValue(),
8383
backend_script, backendOptionsObj,
8484
frontendOptionsObj,
8585
'pyOutputPane',

‎v3/js/csc108h.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function executeCode(forceStartingInstr, forceRawInputLst) {
7979
updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');},
8080
}
8181

82-
executePythonCode(pyInputCodeMirror.getValue(),
82+
executePythonCode(pyInputGetValue(),
8383
backend_script, backendOptionsObj,
8484
frontendOptionsObj,
8585
'pyOutputPane',

‎v3/js/opt-frontend-common.js‎

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var appMode = 'edit'; // 'edit' or 'display'. also support
6565
// 'visualize' for backward compatibility (same as 'display')
6666

6767
var pyInputCodeMirror; // CodeMirror object that contains the input text
68+
var pyInputAceEditor; // Ace editor object that contains the input text
6869

6970

7071
// BEGIN - shared session stuff
@@ -99,7 +100,7 @@ var executeCodeSignalFromRemote = false;
99100
var togetherjsSyncRequested = false;
100101
var pendingCodeOutputScrollTop = null;
101102

102-
var codeMirrorScroller = '#codeInputPane .CodeMirror-scroll';
103+
var pyInputScroller = '#codeInputPane .CodeMirror-scroll';
103104

104105
TogetherJSConfig_ignoreForms = ['.togetherjsIgnore']; // do NOT sync these elements
105106

@@ -120,7 +121,7 @@ function syncAppState(appState) {
120121
// up because the initial states of the two forms aren't equal.
121122
var orig = TogetherJS.config.get('ignoreForms');
122123
TogetherJS.config('ignoreForms', true);
123-
setCodeMirrorVal(appState.code);
124+
pyInputSetValue(appState.code);
124125
TogetherJS.config('ignoreForms', orig);
125126

126127
if (appState.rawInputLst) {
@@ -264,10 +265,10 @@ function initTogetherJS() {
264265
updateAppDisplay(msg.appMode);
265266

266267
if (appMode == 'edit' && msg.codeInputScrollTop !== undefined &&
267-
$(codeMirrorScroller).scrollTop() != msg.codeInputScrollTop) {
268+
$(pyInputScroller).scrollTop() != msg.codeInputScrollTop) {
268269
// hack: give it a bit of time to settle first ...
269270
$.doTimeout('pyInputCodeMirrorInit', 200, function() {
270-
$(codeMirrorScroller).scrollTop(msg.codeInputScrollTop);
271+
$(pyInputScroller).scrollTop(msg.codeInputScrollTop);
271272
});
272273
}
273274
}
@@ -296,7 +297,7 @@ function initTogetherJS() {
296297
if (TogetherJS.running) {
297298
TogetherJS.send({type: "myAppState",
298299
myAppState: getAppState(),
299-
codeInputScrollTop: $('#codeInputPane .CodeMirror-scroll').scrollTop(),
300+
codeInputScrollTop: $(pyInputScroller).scrollTop(),
300301
pyCodeOutputDivScrollTop: myVisualizer ?
301302
myVisualizer.domRoot.find('#pyCodeOutputDiv').scrollTop() :
302303
undefined});
@@ -356,7 +357,7 @@ function initTogetherJS() {
356357
// value. this is hacky; ideally we have a callback function for
357358
// when setValue() completes.
358359
$.doTimeout('pyInputCodeMirrorInit', 200, function() {
359-
$(codeMirrorScroller).scrollTop(msg.codeInputScrollTop);
360+
$(pyInputScroller).scrollTop(msg.codeInputScrollTop);
360361
});
361362
}
362363
});
@@ -365,7 +366,7 @@ function initTogetherJS() {
365366
// do NOT use a msg.sameUrl guard since that will miss some signals
366367
// due to our funky URLs
367368

368-
$(codeMirrorScroller).scrollTop(msg.scrollTop);
369+
$(pyInputScroller).scrollTop(msg.scrollTop);
369370
});
370371

371372
TogetherJS.hub.on("pyCodeOutputDivScroll", function(msg) {
@@ -485,17 +486,6 @@ var try_hook = function(hook_name, args) {
485486
return [false]; // just a stub
486487
}
487488

488-
function setCodeMirrorVal(dat) {
489-
pyInputCodeMirror.setValue(dat.rtrim() /* kill trailing spaces */);
490-
$('#urlOutput,#embedCodeOutput').val('');
491-
492-
clearFrontendError();
493-
494-
// also scroll to top to make the UI more usable on smaller monitors
495-
$(document).scrollTop(0);
496-
}
497-
498-
499489
var myVisualizer = null; // singleton ExecutionVisualizer instance
500490

501491
var rawInputLst = []; // a list of strings inputted by the user in response to raw_input or mouse_input events
@@ -533,6 +523,22 @@ function supports_html5_storage() {
533523
}
534524
}
535525

526+
// abstraction so that we can swap out CodeMirror or Ace
527+
function pyInputGetValue() {
528+
return pyInputCodeMirror.getValue();
529+
}
530+
531+
function pyInputSetValue(val) {
532+
pyInputCodeMirror.setValue(dat.rtrim() /* kill trailing spaces */);
533+
$('#urlOutput,#embedCodeOutput').val('');
534+
535+
clearFrontendError();
536+
537+
// also scroll to top to make the UI more usable on smaller monitors
538+
$(document).scrollTop(0);
539+
}
540+
541+
536542
// run at the END so that everything else can be initialized first
537543
function genericOptFrontendReady() {
538544
initTogetherJS(); // initialize early
@@ -556,7 +562,7 @@ function genericOptFrontendReady() {
556562
if (TogetherJS.running && !isExecutingCode) {
557563
TogetherJS.send({type: "hashchange",
558564
appMode: appMode,
559-
codeInputScrollTop: $(codeMirrorScroller).scrollTop(),
565+
codeInputScrollTop: $(pyInputScroller).scrollTop(),
560566
myAppState: getAppState()});
561567
}
562568
});
@@ -574,6 +580,19 @@ function genericOptFrontendReady() {
574580
pyInputCodeMirror.setSize(null, '420px');
575581

576582

583+
pyInputAceEditor = ace.edit('codeInputPaneAce');
584+
var s = pyInputAceEditor.getSession();
585+
s.setMode("ace/mode/python");
586+
// tab -> 4 spaces
587+
s.setTabSize(4);
588+
s.setUseSoftTabs(true);
589+
// disable extraneous indicators:
590+
s.setFoldStyle('manual'); // no code folding indicators
591+
pyInputAceEditor.setHighlightActiveLine(false);
592+
pyInputAceEditor.setShowPrintMargin(false);
593+
pyInputAceEditor.setBehavioursEnabled(false);
594+
595+
577596
// for shared sessions
578597
pyInputCodeMirror.on("change", function(cm, change) {
579598
// only trigger when the user explicitly typed something
@@ -584,7 +603,7 @@ function genericOptFrontendReady() {
584603
}
585604
});
586605

587-
$('#codeInputPane .CodeMirror-scroll').scroll(function(e) {
606+
$(pyInputScroller).scroll(function(e) {
588607
if (TogetherJS.running) {
589608
var elt = $(this);
590609
// debounce
@@ -724,7 +743,7 @@ function parseQueryString() {
724743
var queryStrOptions = getQueryStringOptions();
725744
setToggleOptions(queryStrOptions);
726745
if (queryStrOptions.preseededCode) {
727-
setCodeMirrorVal(queryStrOptions.preseededCode);
746+
pyInputSetValue(queryStrOptions.preseededCode);
728747
}
729748
if (queryStrOptions.rawInputLst) {
730749
rawInputLst = queryStrOptions.rawInputLst; // global

0 commit comments

Comments
 (0)