@@ -67,6 +67,27 @@ var appMode = 'edit'; // 'edit' or 'display'. also support
6767var pyInputCodeMirror ; // CodeMirror object that contains the input code
6868var 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
531552function 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) {
807838function 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
965996function 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 }
0 commit comments