@@ -29,6 +29,18 @@ import run from "./run";
2929import saveFile from "./saveFile" ;
3030import appSettings from "./settings" ;
3131
32+ let mainCSSStyleSheet = null ;
33+
34+ function getMainCSSStyleSheet ( ) {
35+ if ( mainCSSStyleSheet ) return mainCSSStyleSheet ;
36+ for ( const sheet of document . styleSheets ) {
37+ if ( sheet . href && sheet . href . endsWith ( "main.css" ) ) {
38+ return sheet ;
39+ }
40+ }
41+ return null ;
42+ }
43+
3244function syncQuickToolsVisibility ( file ) {
3345 const { $toggler } = quickTools ;
3446 const hideForFile = ! ! file ?. hideQuickTools ;
@@ -530,7 +542,43 @@ export default class EditorFile {
530542 shadow = container . attachShadow ( { mode : "open" } ) ;
531543
532544 // Add base styles to shadow DOM first
533- shadow . appendChild ( < link rel = "stylesheet" href = "build/main.css" /> ) ;
545+ const sharedSheet = getMainCSSStyleSheet ( ) ;
546+ let adopted = false ;
547+ if ( sharedSheet ) {
548+ try {
549+ shadow . adoptedStyleSheets = [ sharedSheet ] ;
550+ adopted = true ;
551+ } catch ( e ) {
552+ console . warn (
553+ "Failed to adopt document stylesheet, attempting constructed fallback" ,
554+ e ,
555+ ) ;
556+ if (
557+ typeof CSSStyleSheet !== "undefined" &&
558+ CSSStyleSheet . prototype . replaceSync
559+ ) {
560+ try {
561+ const cssText = Array . from ( sharedSheet . cssRules )
562+ . map ( ( rule ) => rule . cssText )
563+ . join ( "\n" ) ;
564+ const constructedSheet = new CSSStyleSheet ( ) ;
565+ constructedSheet . replaceSync ( cssText ) ;
566+ shadow . adoptedStyleSheets = [ constructedSheet ] ;
567+ adopted = true ;
568+ mainCSSStyleSheet = constructedSheet ;
569+ } catch ( innerError ) {
570+ console . warn (
571+ "Failed constructed stylesheet fallback" ,
572+ innerError ,
573+ ) ;
574+ }
575+ }
576+ }
577+ }
578+
579+ if ( ! adopted ) {
580+ shadow . appendChild ( < link rel = "stylesheet" href = "build/main.css" /> ) ;
581+ }
534582
535583 // Handle custom stylesheets if provided
536584 if ( options . stylesheets ) {
0 commit comments