@@ -2,6 +2,7 @@ import { initRenderer } from '@md/core'
2
2
import {
3
3
defaultStyleConfig ,
4
4
themeMap ,
5
+ themeOptionsMap ,
5
6
widthOptions ,
6
7
} from '@md/shared/configs'
7
8
import CodeMirror from 'codemirror'
@@ -28,6 +29,7 @@ import {
28
29
sanitizeTitle ,
29
30
} from '@/utils'
30
31
import { copyPlain } from '@/utils/clipboard'
32
+ import { generateThemeCSS } from '@/utils/themeHelpers'
31
33
32
34
/**********************************
33
35
* Post 结构接口
@@ -290,20 +292,57 @@ export const useStore = defineStore(`store`, () => {
290
292
* @deprecated 在后续版本中将会移除
291
293
*/
292
294
const cssContent = useStorage ( `__css_content` , DEFAULT_CSS_CONTENT )
293
- const cssContentConfig = useStorage ( addPrefix ( `css_content_config` ) , {
295
+ // 初始化默认配置
296
+ const defaultCssConfig = {
294
297
active : `方案1` ,
295
298
tabs : [
299
+ ...Object . entries ( themeMap ) . map ( ( [ key , value ] ) => ( {
300
+ title : themeOptionsMap [ key as keyof typeof themeOptionsMap ] . label ,
301
+ name : themeOptionsMap [ key as keyof typeof themeOptionsMap ] . label ,
302
+ content : generateThemeCSS ( value ) ,
303
+ isBuiltIn : true ,
304
+ } ) ) ,
296
305
{
297
306
title : `方案1` ,
298
307
name : `方案1` ,
299
308
// 兼容之前的方案
300
309
content : cssContent . value || DEFAULT_CSS_CONTENT ,
310
+ isBuiltIn : false ,
301
311
} ,
302
312
] ,
303
- } )
313
+ }
314
+
315
+ const cssContentConfig = useStorage ( addPrefix ( `css_content_config` ) , defaultCssConfig )
304
316
onMounted ( ( ) => {
305
317
// 清空过往历史记录
306
318
cssContent . value = ``
319
+
320
+ // 动态合并新增的内置主题
321
+ const existingBuiltInThemes = new Set (
322
+ cssContentConfig . value . tabs
323
+ . filter ( tab => tab . isBuiltIn )
324
+ . map ( tab => tab . name ) ,
325
+ )
326
+
327
+ // 检查是否有新增的主题
328
+ const newThemes = Object . entries ( themeMap )
329
+ . filter ( ( [ key ] ) => ! existingBuiltInThemes . has ( themeOptionsMap [ key as keyof typeof themeOptionsMap ] . label ) )
330
+ . map ( ( [ key , value ] ) => ( {
331
+ title : themeOptionsMap [ key as keyof typeof themeOptionsMap ] . label ,
332
+ name : themeOptionsMap [ key as keyof typeof themeOptionsMap ] . label ,
333
+ content : generateThemeCSS ( value ) ,
334
+ isBuiltIn : true ,
335
+ } ) )
336
+
337
+ // 如果有新主题,将其插入到内置主题区域
338
+ if ( newThemes . length > 0 ) {
339
+ // 找到第一个非内置主题的索引
340
+ const firstCustomThemeIndex = cssContentConfig . value . tabs . findIndex ( tab => ! tab . isBuiltIn )
341
+ const insertIndex = firstCustomThemeIndex === - 1 ? cssContentConfig . value . tabs . length : firstCustomThemeIndex
342
+
343
+ // 插入新主题
344
+ cssContentConfig . value . tabs . splice ( insertIndex , 0 , ...newThemes )
345
+ }
307
346
} )
308
347
const getCurrentTab = ( ) =>
309
348
cssContentConfig . value . tabs . find ( ( tab ) => {
@@ -330,6 +369,7 @@ export const useStore = defineStore(`store`, () => {
330
369
name,
331
370
title : name ,
332
371
content : DEFAULT_CSS_CONTENT ,
372
+ isBuiltIn : false ,
333
373
} )
334
374
cssContentConfig . value . active = name
335
375
setCssEditorValue ( DEFAULT_CSS_CONTENT )
@@ -489,11 +529,18 @@ export const useStore = defineStore(`store`, () => {
489
529
cssContentConfig . value = {
490
530
active : `方案 1` ,
491
531
tabs : [
532
+ ...Object . entries ( themeMap ) . map ( ( [ key , value ] ) => ( {
533
+ title : themeOptionsMap [ key as keyof typeof themeOptionsMap ] . label ,
534
+ name : themeOptionsMap [ key as keyof typeof themeOptionsMap ] . label ,
535
+ content : generateThemeCSS ( value ) ,
536
+ isBuiltIn : true ,
537
+ } ) ) ,
492
538
{
493
539
title : `方案 1` ,
494
540
name : `方案 1` ,
495
541
// 兼容之前的方案
496
542
content : cssContent . value || DEFAULT_CSS_CONTENT ,
543
+ isBuiltIn : false ,
497
544
} ,
498
545
] ,
499
546
}
0 commit comments