@@ -73,13 +73,6 @@ export abstract class AbstractRegion<T> implements Region<T> {
7373 */
7474 protected static className : string ;
7575
76- /**
77- * True if the style has already been added to the document.
78- *
79- * @type {boolean }
80- */
81- protected static styleAdded : boolean = false ;
82-
8376 /**
8477 * The CSS style that needs to be added for this type of region.
8578 *
@@ -117,20 +110,27 @@ export abstract class AbstractRegion<T> implements Region<T> {
117110 this . AddStyles ( ) ;
118111 }
119112
113+ /**
114+ * @returns {string } The stylesheet ID
115+ */
116+ public static get sheetId ( ) : string {
117+ return 'MJX-' + this . name + '-styles' ;
118+ }
119+
120120 /**
121121 * @override
122122 */
123123 public AddStyles ( ) {
124- if ( this . CLASS . styleAdded ) {
124+ const id = this . CLASS . sheetId ;
125+ if (
126+ ! this . CLASS . style ||
127+ this . document . adaptor . head ( ) . querySelector ( '#' + id )
128+ ) {
125129 return ;
126130 }
127- // TODO: should that be added to document.documentStyleSheet()?
128- const node = this . document . adaptor . node ( 'style' ) ;
131+ const node = this . document . adaptor . node ( 'style' , { id } ) ;
129132 node . innerHTML = this . CLASS . style . cssText ;
130- this . document . adaptor
131- . head ( this . document . adaptor . document )
132- . appendChild ( node ) ;
133- this . CLASS . styleAdded = true ;
133+ this . document . adaptor . head ( ) . appendChild ( node ) ;
134134 }
135135
136136 /**
@@ -177,7 +177,7 @@ export abstract class AbstractRegion<T> implements Region<T> {
177177 */
178178 public Hide ( ) {
179179 if ( ! this . div ) return ;
180- this . div . parentNode . removeChild ( this . div ) ;
180+ this . div . remove ( ) ;
181181 this . div = null ;
182182 this . inner = null ;
183183 }
@@ -335,6 +335,13 @@ export class ToolTip extends StringRegion {
335335 'border-radius' : 'inherit' ,
336336 padding : '0 2px' ,
337337 } ,
338+ '@media (prefers-color-scheme: dark)' : {
339+ [ '.' + ToolTip . className ] : {
340+ 'background-color' : '#222025' ,
341+ 'box-shadow' : '0px 5px 20px #000' ,
342+ border : '1px solid #7C7C7C' ,
343+ } ,
344+ } ,
338345 } ) ;
339346}
340347
@@ -348,6 +355,43 @@ export class LiveRegion extends StringRegion {
348355 * @override
349356 */
350357 protected static style : StyleJsonSheet = new StyleJsonSheet ( {
358+ ':root' : {
359+ '--mjx-fg-red' : '255, 0, 0' ,
360+ '--mjx-fg-green' : '0, 255, 0' ,
361+ '--mjx-fg-blue' : '0, 0, 255' ,
362+ '--mjx-fg-yellow' : '255, 255, 0' ,
363+ '--mjx-fg-cyan' : '0, 255, 255' ,
364+ '--mjx-fg-magenta' : '255, 0, 255' ,
365+ '--mjx-fg-white' : '255, 255, 255' ,
366+ '--mjx-fg-black' : '0, 0, 0' ,
367+ '--mjx-bg-red' : '255, 0, 0' ,
368+ '--mjx-bg-green' : '0, 255, 0' ,
369+ '--mjx-bg-blue' : '0, 0, 255' ,
370+ '--mjx-bg-yellow' : '255, 255, 0' ,
371+ '--mjx-bg-cyan' : '0, 255, 255' ,
372+ '--mjx-bg-magenta' : '255, 0, 255' ,
373+ '--mjx-bg-white' : '255, 255, 255' ,
374+ '--mjx-bg-black' : '0, 0, 0' ,
375+ '--mjx-live-bg-color' : 'white' ,
376+ '--mjx-live-shadow-color' : '#888' ,
377+ '--mjx-live-border-color' : '#CCCCCC' ,
378+ '--mjx-bg-alpha' : 0.2 ,
379+ '--mjx-fg-alpha' : 1 ,
380+ } ,
381+ '@media (prefers-color-scheme: dark)' : {
382+ ':root' : {
383+ '--mjx-bg-blue' : '132, 132, 255' ,
384+ '--mjx-bg-white' : '0, 0, 0' ,
385+ '--mjx-bg-black' : '255, 255, 255' ,
386+ '--mjx-fg-white' : '0, 0, 0' ,
387+ '--mjx-fg-black' : '255, 255, 255' ,
388+ '--mjx-live-bg-color' : '#222025' ,
389+ '--mjx-live-shadow-color' : 'black' ,
390+ '--mjx-live-border-color' : '#7C7C7C' ,
391+ '--mjx-bg-alpha' : 0.3 ,
392+ '--mjx-fg-alpha' : 1 ,
393+ } ,
394+ } ,
351395 [ '.' + LiveRegion . className ] : {
352396 position : 'absolute' ,
353397 top : 0 ,
@@ -360,20 +404,41 @@ export class LiveRegion extends StringRegion {
360404 left : 0 ,
361405 right : 0 ,
362406 margin : '0 auto' ,
363- 'background-color' : 'white ' ,
364- 'box-shadow' : '0px 5px 20px #888 ' ,
365- border : '2px solid #CCCCCC ' ,
407+ 'background-color' : 'var(--mjx-live-bg-color) ' ,
408+ 'box-shadow' : '0px 5px 20px var(--mjx-live-shadow-color) ' ,
409+ border : '2px solid var(--mjx-live-border-color) ' ,
366410 } ,
367411 [ '.' + LiveRegion . className + '_Show' ] : {
368412 display : 'block' ,
369413 } ,
370414 } ) ;
415+
416+ /**
417+ * @param {string } type The type of alpha to set (fg or bg)
418+ * @param {number } alpha The alpha value to use
419+ * @param {Document } document The document whose CSS styles are to be adjusted
420+ */
421+ public static setAlpha ( type : string , alpha : number , document : Document ) {
422+ const style = document . head . querySelector (
423+ '#' + this . sheetId
424+ ) as HTMLStyleElement ;
425+ if ( style ) {
426+ const name = `--mjx-${ type } -alpha` ;
427+ ( style . sheet . cssRules [ 0 ] as any ) . style . setProperty ( name , alpha ) ;
428+ ( style . sheet . cssRules [ 1 ] as any ) . cssRules [ 0 ] . style . setProperty (
429+ name ,
430+ alpha ** 0.7071
431+ ) ;
432+ }
433+ }
371434}
372435
373436/**
374437 * Region class that enables auto voicing of content via SSML markup.
375438 */
376439export class SpeechRegion extends LiveRegion {
440+ protected static style : StyleJsonSheet = null ;
441+
377442 /**
378443 * Flag to activate auto voicing.
379444 */
@@ -583,6 +648,13 @@ export class HoverRegion extends AbstractRegion<HTMLElement> {
583648 [ '.' + HoverRegion . className + ' > div' ] : {
584649 overflow : 'hidden' ,
585650 } ,
651+ '@media (prefers-color-scheme: dark)' : {
652+ [ '.' + HoverRegion . className ] : {
653+ 'background-color' : '#222025' ,
654+ 'box-shadow' : '0px 5px 20px #000' ,
655+ border : '1px solid #7C7C7C' ,
656+ } ,
657+ } ,
586658 } ) ;
587659
588660 /**
0 commit comments