import colorPalette from 'antd-color-palette' ; interface colorItem { source : string ; fileName : string ; matchColors : string [ ] ; } ; export type ThemeColorState = { source : colorItem [ ] ; currentStyle : string ; } ; export const themeColor = { state : { source : [ ] , currentStyle : '' } as ThemeColorState , reducers : { setSource ( state , source ) { return { ... state , source } ; } , setStyle ( state , currentStyle ) { return { ... state , currentStyle } ; } , replaceColor ( state , payload ) { const { sourceColor , nextColor } = payload ; const styleText = state . source . map ( item => { if ( item . matchColors . includes ( payload . sourceColor ) ) { return replaceAntdColors ( item . source , sourceColor , nextColor ) ; } return '' ; } ) . join ( '' ) ; return { ... state , currentStyle : styleText } ; } } , } ; function replaceAntdColors ( styleText , sourceColor , nextColor ) { for ( let i = 0 ; i < 10 ; i ++ ) { styleText = replaceColor ( styleText , colorPalette ( sourceColor , i ) , colorPalette ( nextColor , i ) ) ; } styleText = replaceColor ( styleText , sourceColor , nextColor ) ; return styleText ; } function replaceColor ( source , color , replaceColor ) { return source . replace ( new RegExp ( `(:.*?\\s*)(${ color })(\\b.*?)(?=})` , 'mig' ) , group => { return group . replace ( new RegExp ( `${ color }` , 'mig' ) , replaceColor ) ; } ) ; }