@@ -152,6 +152,7 @@ $(function() {
152152 animationDiv = null ;
153153 }
154154 }
155+ let activeEditor = null ;
155156 CPO . makeEditor = function ( container , options ) {
156157 var initial = "" ;
157158 if ( options . hasOwnProperty ( "initial" ) ) {
@@ -196,8 +197,7 @@ $(function() {
196197 console . log ( "Using keymap: " , CodeMirror . keyMap . default , "macDefault: " , CodeMirror . keyMap . macDefault , "mac: " , mac ) ;
197198 const modifier = mac ? "Cmd" : "Ctrl" ;
198199
199- var cmOptions = {
200- extraKeys : CodeMirror . normalizeKeyMap ( {
200+ const extraKeys = {
201201 "Shift-Enter" : function ( cm ) { runFun ( cm . getValue ( ) ) ; } ,
202202 "Shift-Ctrl-Enter" : function ( cm ) { runFun ( cm . getValue ( ) ) ; } ,
203203 "Tab" : "indentAuto" ,
@@ -210,7 +210,21 @@ $(function() {
210210 "Ctrl-Right" : "goForwardToken" ,
211211 [ `${ modifier } -F` ] : "findPersistent" ,
212212 [ `${ modifier } -/` ] : "toggleComment" ,
213- } ) ,
213+ } ;
214+ if ( window . PYRET_IN_VSCODE ) {
215+ // Disable undo and redo in vscode, since they mess with the host editor's undo/redo stack
216+ // Oddly, it doesn't seem to work to add these to extraKeys; I have to
217+ // override them in the default keymap
218+ CodeMirror . keyMap . default [ `${ modifier } -Z` ] = false ;
219+ CodeMirror . keyMap . default [ `Shift-${ modifier } -Z` ] = false ;
220+ CodeMirror . keyMap . default [ `${ modifier } -Y` ] = false ;
221+ // Ctrl-U is Undo within a range
222+ CodeMirror . keyMap . default [ `${ modifier } -U` ] = false ;
223+ }
224+
225+ var cmOptions = {
226+ keyMap : 'default' ,
227+ extraKeys : CodeMirror . normalizeKeyMap ( extraKeys ) ,
214228 indentUnit : 2 ,
215229 tabSize : 2 ,
216230 viewportMargin : Infinity ,
@@ -230,6 +244,9 @@ $(function() {
230244 cmOptions = merge ( cmOptions , options . cmOptions || { } ) ;
231245
232246 var CM = CodeMirror . fromTextArea ( textarea [ 0 ] , cmOptions ) ;
247+ CM . on ( "focus" , ( ) => {
248+ activeEditor = CM ;
249+ } ) ;
233250
234251 function firstLineIsNamespace ( ) {
235252 const firstline = CM . getLine ( 0 ) ;
@@ -1410,6 +1427,10 @@ $(function() {
14101427
14111428 } ) ;
14121429
1430+ window . addEventListener ( "focus" , ( e ) => {
1431+ if ( activeEditor ) { activeEditor . focus ( ) ; }
1432+ } ) ;
1433+
14131434 function makeEvent ( ) {
14141435 const handlers = [ ] ;
14151436 function on ( handler ) {
@@ -1453,15 +1474,20 @@ $(function() {
14531474
14541475 let initialState = params [ "get" ] [ "initialState" ] ;
14551476
1477+ window . PYRET_IS_EMBEDDED = false ;
1478+ window . PYRET_IN_VSCODE = false ;
14561479 if ( typeof acquireVsCodeApi === "function" ) {
14571480 window . MESSAGES = makeEvents ( {
14581481 CPO : CPO ,
14591482 sendPort : acquireVsCodeApi ( ) ,
14601483 receivePort : window ,
14611484 initialState
14621485 } ) ;
1486+ window . PYRET_IS_EMBEDDED = true ;
1487+ window . PYRET_IN_VSCODE = true ;
14631488 }
14641489 else if ( ( window . parent && ( window . parent !== window ) ) ) {
14651490 window . MESSAGES = makeEvents ( { CPO : CPO , sendPort : window . parent , receivePort : window , initialState } ) ;
1491+ window . PYRET_IS_EMBEDDED = true ;
14661492 }
14671493} ) ;
0 commit comments