1+ import  { ThemeConfig }  from  "./types" ; 
2+ import  { Dispatch }  from  "../Dispatch" ; 
3+ import  { CSSUtils }  from  "../utils" ; 
4+ import  { EventBus }  from  "../EventBus" ; 
5+ 
6+ export  class  ThemeContext { 
7+ 
8+     public  static  EVENT_CHANGE_CONTENT_FONT_SIZE  =  'theme-changeContentFontSize' ; 
9+     public  static  EVENT_CHANGE_THEME  =  'theme-changeTheme' ; 
10+ 
11+     private  theme : ThemeConfig ; 
12+     private  readonly  dispatch : Dispatch < ThemeConfig > ; 
13+ 
14+     constructor ( theme : ThemeConfig , dispatch : Dispatch < ThemeConfig > )  { 
15+         this . theme  =  theme ; 
16+         this . dispatch  =  dispatch ; 
17+     } 
18+ 
19+     public  getTheme  =  ( )  =>  { 
20+         return  this . theme ; 
21+     } 
22+ 
23+     // 更新状态数据 
24+     public  syncTheme  =  ( theme : ThemeConfig )  =>  { 
25+         this . theme  =  theme ; 
26+     } 
27+ 
28+     public  setLargeFontSize  =  ( )  =>  { 
29+         const  fontSize  =  CSSUtils . getRootVariable ( '--content-font-size-large' ) ; 
30+         CSSUtils . setRootVariable ( '--content-font-size' ,  fontSize ) ; 
31+         this . setFontSize ( fontSize ) ; 
32+     } 
33+ 
34+     public  setMiddleFontSize  =  ( ) => { 
35+         const  fontSize  =  CSSUtils . getRootVariable ( '--content-font-size-middle' ) ; 
36+         CSSUtils . setRootVariable ( '--content-font-size' ,  fontSize ) ; 
37+         this . setFontSize ( fontSize ) ; 
38+     } 
39+ 
40+     public  setSmallFontSize  =  ( ) => { 
41+         const  fontSize  =  CSSUtils . getRootVariable ( '--content-font-size-small' ) ; 
42+         CSSUtils . setRootVariable ( '--content-font-size' ,  fontSize ) ; 
43+         this . setFontSize ( fontSize ) ; 
44+     } 
45+ 
46+     /** 
47+      * 设置字体大小 
48+      * @param  fontSize 
49+      */ 
50+     public  setFontSize  =  ( fontSize : string )  =>  { 
51+         this . dispatch ( ( prevState )  =>  { 
52+             return  { 
53+                 ...prevState , 
54+                 token : { 
55+                     ...prevState . token , 
56+                     contentFontSize : fontSize 
57+                 } 
58+             } 
59+         } ) ; 
60+         EventBus . getInstance ( ) . emit ( ThemeContext . EVENT_CHANGE_CONTENT_FONT_SIZE ,  fontSize ) ; 
61+     } 
62+ 
63+     /** 
64+      * 设置主题 
65+      * @param  theme 
66+      */ 
67+     public  changeTheme  =  ( theme : ThemeConfig )  =>  { 
68+         this . dispatch ( ( prevState )  =>  { 
69+             return  { 
70+                 ...prevState , 
71+                 ...theme 
72+             } 
73+         } ) ; 
74+         EventBus . getInstance ( ) . emit ( ThemeContext . EVENT_CHANGE_THEME ,  theme ) ; 
75+     } 
76+ } 
0 commit comments