@@ -418,15 +418,39 @@ var CodeOceanEditor = {
418418 this . showFrame ( frame ) ;
419419 this . toggleButtonStates ( ) ;
420420 } . bind ( this ) ) ;
421- $ ( document ) . on ( 'theme:change' , function ( event ) {
421+
422+ this . installFileTreeEventHandlers ( filesInstance ) ;
423+ } ,
424+
425+ installFileTreeEventHandlers : function ( filesInstance ) {
426+ // Prevent duplicate event listeners by removing them during unload.
427+ const themeListener = this . createFileTreeThemeChangeListener ( filesInstance ) ;
428+ const jsTree = filesInstance ?. jstree ( true ) ;
429+ $ ( document ) . on ( 'theme:change' , themeListener ) ;
430+ $ ( document ) . one ( 'turbo:visit' , function ( ) {
431+ $ ( document ) . off ( 'theme:change' , themeListener ) ;
432+ if ( jsTree && jsTree . element ) {
433+ jsTree . destroy ( true ) ;
434+ }
435+ } ) ;
436+ $ ( window ) . one ( 'beforeunload' , function ( ) {
437+ $ ( document ) . off ( 'theme:change' , themeListener ) ;
438+ if ( jsTree && jsTree . element ) {
439+ jsTree . destroy ( true ) ;
440+ }
441+ } ) ;
442+ } ,
443+
444+ createFileTreeThemeChangeListener : function ( filesInstance ) {
445+ return function ( event ) {
422446 const jsTree = filesInstance ?. jstree ( true ) ;
423447
424448 if ( jsTree ) {
425449 const newColorScheme = event . detail . currentTheme ;
426450 // Update the JStree theme
427451 jsTree ?. set_theme ( newColorScheme === "dark" ? "default-dark" : "default" ) ;
428452 }
429- } ) ;
453+ }
430454 } ,
431455
432456 initializeFileTreeButtons : function ( ) {
0 commit comments