@@ -85,7 +85,6 @@ const getDefaultState = () => ({
8585 selectedComponentId : 'Website' ,
8686 themeObject : createPreviewMuiTheme ( deepmerge ( { palette : { mode : 'light' } } , getDefaultThemeConfig ( ) . light ) , false ) ,
8787 saving : false ,
88- showColorMode : true ,
8988 // 历史记录相关
9089 history : [ ] ,
9190 currentHistoryIndex : - 1 ,
@@ -124,15 +123,6 @@ export default function createStore() {
124123 saveToHistory ( ) ;
125124 } ,
126125 setSaving : ( saving ) => set ( { saving } ) ,
127- getThemeData : ( ) => {
128- const { concepts, currentConceptId } = get ( ) ;
129-
130- return {
131- concepts : cloneDeep ( concepts ) ,
132- currentConceptId,
133- } ;
134- } ,
135- setColorModeVisible : ( visible ) => set ( { showColorMode : visible } ) ,
136126
137127 // # 历史记录管理
138128 saveToHistory : ( ) => {
@@ -519,12 +509,12 @@ export default function createStore() {
519509
520510 // # ThemeOptions 编辑
521511 setThemeOption : ( path , value ) => {
522- const { saveToHistory } = get ( ) ;
512+ const { saveToHistory, getThemeMode } = get ( ) ;
523513
524514 set ( ( state ) => {
525515 const current = state . concepts . find ( ( c ) => c . id === state . currentConceptId ) ;
526516 if ( ! current || ! path ) return { } ;
527- const { mode } = current ;
517+ const mode = getThemeMode ( ) ;
528518
529519 const fieldName = getThemeFieldName ( path , mode ) ;
530520 const themeOptions = setByPath ( current . themeConfig [ fieldName ] , path , value ) ;
@@ -541,12 +531,12 @@ export default function createStore() {
541531 saveToHistory ( ) ;
542532 } ,
543533 setThemeOptions : ( configs ) => {
544- const { saveToHistory } = get ( ) ;
534+ const { saveToHistory, getThemeMode } = get ( ) ;
545535
546536 set ( ( state ) => {
547537 const current = state . concepts . find ( ( c ) => c . id === state . currentConceptId ) ;
548538 if ( ! current ) return { } ;
549- const { mode } = current ;
539+ const mode = getThemeMode ( ) ;
550540
551541 let newThemeOptions = { ...current . themeConfig } ;
552542
@@ -566,12 +556,12 @@ export default function createStore() {
566556 saveToHistory ( ) ;
567557 } ,
568558 removeThemeOption : ( path ) => {
569- const { saveToHistory } = get ( ) ;
559+ const { saveToHistory, getThemeMode } = get ( ) ;
570560
571561 set ( ( state ) => {
572562 const current = state . concepts . find ( ( c ) => c . id === state . currentConceptId ) ;
573563 if ( ! current || ! path ) return { } ;
574- const { mode } = current ;
564+ const mode = getThemeMode ( ) ;
575565
576566 const fieldName = getThemeFieldName ( path , mode ) ;
577567 const themeOptions = removeByPath ( current . themeConfig [ fieldName ] , path ) ;
@@ -588,12 +578,12 @@ export default function createStore() {
588578 saveToHistory ( ) ;
589579 } ,
590580 removeThemeOptions : ( paths ) => {
591- const { saveToHistory } = get ( ) ;
581+ const { saveToHistory, getThemeMode } = get ( ) ;
592582
593583 set ( ( state ) => {
594584 const current = state . concepts . find ( ( c ) => c . id === state . currentConceptId ) ;
595585 if ( ! current ) return { } ;
596- const { mode } = current ;
586+ const mode = getThemeMode ( ) ;
597587
598588 let newThemeOptions = { ...current . themeConfig } ;
599589 paths . forEach ( ( p ) => {
@@ -639,10 +629,17 @@ export default function createStore() {
639629 // 保存到历史记录
640630 saveToHistory ( ) ;
641631 } ,
642- setThemeMode : ( mode ) => {
632+ setThemeMode : ( mode , { root = false } = { } ) => {
643633 const { saveToHistory } = get ( ) ;
644634
645635 set ( ( state ) => {
636+ // 修改根状态
637+ if ( root === true ) {
638+ return {
639+ themeMode : mode ,
640+ } ;
641+ }
642+
646643 const current = state . concepts . find ( ( c ) => c . id === state . currentConceptId ) ;
647644 if ( ! current ) return { } ;
648645 return { concepts : state . concepts . map ( ( c ) => ( c . id === state . currentConceptId ? { ...c , mode } : c ) ) } ;
@@ -651,6 +648,25 @@ export default function createStore() {
651648 // 保存到历史记录
652649 saveToHistory ( ) ;
653650 } ,
651+ getThemeMode : ( ) => {
652+ const { themeMode, getCurrentConcept } = get ( ) ;
653+
654+ // 全局 themeMode 优先
655+ if ( themeMode ) {
656+ return themeMode ;
657+ }
658+
659+ const concept = getCurrentConcept ( ) ;
660+ if ( concept ) {
661+ return concept . mode ;
662+ }
663+ return 'light' ;
664+ } ,
665+ shouldShowThemeMode : ( ) => {
666+ const { themeMode } = get ( ) ;
667+
668+ return themeMode === undefined ;
669+ } ,
654670 updateThemeConfig : ( themeConfig ) => {
655671 const { saveToHistory } = get ( ) ;
656672
@@ -664,8 +680,19 @@ export default function createStore() {
664680 saveToHistory ( ) ;
665681 } ,
666682 getCurrentThemeOptions : ( ) => {
667- const concept = get ( ) . getCurrentConcept ( ) ;
668- return concept . themeConfig [ concept . mode ] ;
683+ const { getCurrentConcept, getThemeMode } = get ( ) ;
684+ const concept = getCurrentConcept ( ) ;
685+ const mode = getThemeMode ( ) ;
686+
687+ return concept . themeConfig [ mode ] ;
688+ } ,
689+ getThemeData : ( ) => {
690+ const { concepts, currentConceptId } = get ( ) ;
691+
692+ return {
693+ concepts : cloneDeep ( concepts ) ,
694+ currentConceptId,
695+ } ;
669696 } ,
670697
671698 // # Colors 编辑
@@ -787,20 +814,18 @@ export default function createStore() {
787814
788815 // 实时更新 themeObject
789816 store . subscribe (
790- ( state ) => [ state . concepts , state . currentConceptId , state . previewSize ] ,
817+ ( state ) => [ state . concepts , state . currentConceptId , state . previewSize , state . themeMode ] ,
791818 ( ) => {
792819 const state = store . getState ( ) ;
793820 const concept = state . getCurrentConcept ( ) ;
794821
795822 let themeObject : Theme ;
796823
797824 if ( concept ) {
825+ const mode = state . getThemeMode ( ) ;
826+
798827 themeObject = createPreviewMuiTheme (
799- deepmergeAll ( [
800- { palette : { mode : concept . mode } } ,
801- concept . themeConfig . common ,
802- concept . themeConfig [ concept . mode ] ,
803- ] ) ,
828+ deepmergeAll ( [ { palette : { mode } } , concept . themeConfig . common , concept . themeConfig [ mode ] ] ) ,
804829 state . previewSize ,
805830 ) ;
806831 } else {
0 commit comments