Skip to content

Commit fc96d12

Browse files
committed
moar ace refactoring action
1 parent ae4ffb0 commit fc96d12

File tree

3 files changed

+64
-39
lines changed

3 files changed

+64
-39
lines changed

v3/css/opt-frontend.css

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

54-
#codeInputPane, #codeInputPaneAce {
54+
#codeInputPane {
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-
6560
button.smallBtn {
6661
font-size: 10pt;
6762
padding: 3px;

v3/js/opt-frontend-common.js

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ var appMode = 'edit'; // 'edit' or 'display'. also support
6767
var pyInputCodeMirror; // CodeMirror object that contains the input code
6868
var pyInputAceEditor; // Ace editor object that contains the input code
6969

70+
var useCodeMirror = true; // true -> use CodeMirror, false -> use Ace
71+
var useCodeMirror = false; // true -> use CodeMirror, false -> use Ace
72+
73+
74+
function initAceEditor(height) {
75+
pyInputAceEditor = ace.edit('codeInputPane');
76+
var s = pyInputAceEditor.getSession();
77+
s.setMode("ace/mode/python");
78+
// tab -> 4 spaces
79+
s.setTabSize(4);
80+
s.setUseSoftTabs(true);
81+
// disable extraneous indicators:
82+
s.setFoldStyle('manual'); // no code folding indicators
83+
pyInputAceEditor.setHighlightActiveLine(false);
84+
pyInputAceEditor.setShowPrintMargin(false);
85+
pyInputAceEditor.setBehavioursEnabled(false);
86+
87+
$('#codeInputPane').css('width', '700px');
88+
$('#codeInputPane').css('height', height + 'px');
89+
}
90+
7091

7192
// BEGIN - shared session stuff
7293

@@ -529,7 +550,13 @@ function pyInputGetValue() {
529550
}
530551

531552
function pyInputSetValue(dat) {
532-
pyInputCodeMirror.setValue(dat.rtrim() /* kill trailing spaces */);
553+
if (useCodeMirror) {
554+
pyInputCodeMirror.setValue(dat.rtrim() /* kill trailing spaces */);
555+
}
556+
else {
557+
pyInputAceEditor.setValue(dat.rtrim() /* kill trailing spaces */);
558+
}
559+
533560
$('#urlOutput,#embedCodeOutput').val('');
534561

535562
clearFrontendError();
@@ -568,40 +595,44 @@ function genericOptFrontendReady() {
568595
});
569596

570597

571-
pyInputCodeMirror = CodeMirror(document.getElementById('codeInputPane'), {
572-
mode: 'python',
573-
lineNumbers: true,
574-
tabSize: 4,
575-
indentUnit: 4,
576-
// convert tab into four spaces:
577-
extraKeys: {Tab: function(cm) {cm.replaceSelection(" ", "end");}}
578-
});
579-
580-
pyInputCodeMirror.setSize(null, '420px');
581-
598+
if (useCodeMirror) {
599+
pyInputCodeMirror = CodeMirror(document.getElementById('codeInputPane'), {
600+
mode: 'python',
601+
lineNumbers: true,
602+
tabSize: 4,
603+
indentUnit: 4,
604+
// convert tab into four spaces:
605+
extraKeys: {Tab: function(cm) {cm.replaceSelection(" ", "end");}}
606+
});
582607

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);
608+
pyInputCodeMirror.setSize(null, '420px');
609+
}
610+
else {
611+
initAceEditor(420);
612+
}
594613

595614

596-
// for shared sessions
597-
pyInputCodeMirror.on("change", function(cm, change) {
598-
// only trigger when the user explicitly typed something
599-
if (change.origin != 'setValue') {
615+
if (useCodeMirror) {
616+
// for shared sessions
617+
pyInputCodeMirror.on("change", function(cm, change) {
618+
// only trigger when the user explicitly typed something
619+
if (change.origin != 'setValue') {
620+
if (TogetherJS.running) {
621+
TogetherJS.send({type: "codemirror-edit"});
622+
}
623+
}
624+
});
625+
}
626+
else {
627+
pyInputAceEditor.getSession().on("change", function(e) {
628+
// unfortunately, Ace doesn't detect whether a change was caused
629+
// by a setValue call
600630
if (TogetherJS.running) {
601631
TogetherJS.send({type: "codemirror-edit"});
602632
}
603-
}
604-
});
633+
});
634+
}
635+
605636

606637
$(pyInputScroller).scroll(function(e) {
607638
if (TogetherJS.running) {
@@ -807,7 +838,7 @@ function setToggleOptions(dat) {
807838
function getAppState() {
808839
assert(originFrontendJsFile); // global var defined in each frontend
809840

810-
var ret = {code: pyInputCodeMirror.getValue(),
841+
var ret = {code: pyInputGetValue(),
811842
mode: appMode,
812843
origin: originFrontendJsFile,
813844
cumulative: $('#cumulativeModeSelector').val(),
@@ -964,7 +995,7 @@ function updateAppDisplay(newAppMode) {
964995

965996
function executeCodeFromScratch() {
966997
// don't execute empty string:
967-
if ($.trim(pyInputCodeMirror.getValue()) == '') {
998+
if ($.trim(pyInputGetValue()) == '') {
968999
setFronendError(["Type in some Python code to visualize."]);
9691000
return;
9701001
}

v3/visualize.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@
177177
<div id="pyInputPane">
178178

179179
<div id="codeInputWarnings">Write Python code here or <a href="#" id="firstExampleDupLink">load an example</a>:</div>
180-
<div id="codeInputPane"></div> <!-- populate with a CodeMirror instance -->
181-
<div id="codeInputPaneAce"></div> <!-- populate with an Ace instance -->
180+
<div id="codeInputPane"></div> <!-- populate with a CodeMirror or Ace code editor instance -->
182181
<div id="frontendErrorOutput"></div>
183182
<div id="surveyPane"></div>
184183

0 commit comments

Comments
 (0)