Skip to content

Commit 9c613de

Browse files
committed
feat: insert theme in css editor
1 parent a0414ce commit 9c613de

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

apps/web/src/components/CodemirrorEditor/CssEditor.vue

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ function tabChanged(tabName: string | number) {
162162
class="flex-1"
163163
>
164164
{{ item.title }}
165-
<Edit3
166-
v-show="store.cssContentConfig.active === item.name" class="inline size-4 rounded-full p-0.5 transition-color hover:bg-gray-200 dark:hover:bg-gray-600"
167-
@click="rename(item.name)"
168-
/>
169-
<X
170-
v-show="store.cssContentConfig.active === item.name" class="inline size-4 rounded-full p-0.5 transition-color hover:bg-gray-200 dark:hover:bg-gray-600"
171-
@click.self="removeHandler(item.name)"
172-
/>
165+
<template v-if="!item.isBuiltIn">
166+
<Edit3
167+
v-show="store.cssContentConfig.active === item.name" class="inline size-4 rounded-full p-0.5 transition-color hover:bg-gray-200 dark:hover:bg-gray-600"
168+
@click="rename(item.name)"
169+
/>
170+
<X
171+
v-show="store.cssContentConfig.active === item.name" class="inline size-4 rounded-full p-0.5 transition-color hover:bg-gray-200 dark:hover:bg-gray-600"
172+
@click.self="removeHandler(item.name)"
173+
/>
174+
</template>
173175
</TabsTrigger>
174176
<TabsTrigger value="add">
175177
<Plus class="h-5 w-5" />

apps/web/src/stores/index.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { initRenderer } from '@md/core'
22
import {
33
defaultStyleConfig,
44
themeMap,
5+
themeOptionsMap,
56
widthOptions,
67
} from '@md/shared/configs'
78
import CodeMirror from 'codemirror'
@@ -28,6 +29,7 @@ import {
2829
sanitizeTitle,
2930
} from '@/utils'
3031
import { copyPlain } from '@/utils/clipboard'
32+
import { generateThemeCSS } from '@/utils/themeHelpers'
3133

3234
/**********************************
3335
* Post 结构接口
@@ -290,20 +292,57 @@ export const useStore = defineStore(`store`, () => {
290292
* @deprecated 在后续版本中将会移除
291293
*/
292294
const cssContent = useStorage(`__css_content`, DEFAULT_CSS_CONTENT)
293-
const cssContentConfig = useStorage(addPrefix(`css_content_config`), {
295+
// 初始化默认配置
296+
const defaultCssConfig = {
294297
active: `方案1`,
295298
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+
})),
296305
{
297306
title: `方案1`,
298307
name: `方案1`,
299308
// 兼容之前的方案
300309
content: cssContent.value || DEFAULT_CSS_CONTENT,
310+
isBuiltIn: false,
301311
},
302312
],
303-
})
313+
}
314+
315+
const cssContentConfig = useStorage(addPrefix(`css_content_config`), defaultCssConfig)
304316
onMounted(() => {
305317
// 清空过往历史记录
306318
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+
}
307346
})
308347
const getCurrentTab = () =>
309348
cssContentConfig.value.tabs.find((tab) => {
@@ -330,6 +369,7 @@ export const useStore = defineStore(`store`, () => {
330369
name,
331370
title: name,
332371
content: DEFAULT_CSS_CONTENT,
372+
isBuiltIn: false,
333373
})
334374
cssContentConfig.value.active = name
335375
setCssEditorValue(DEFAULT_CSS_CONTENT)
@@ -489,11 +529,18 @@ export const useStore = defineStore(`store`, () => {
489529
cssContentConfig.value = {
490530
active: `方案 1`,
491531
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+
})),
492538
{
493539
title: `方案 1`,
494540
name: `方案 1`,
495541
// 兼容之前的方案
496542
content: cssContent.value || DEFAULT_CSS_CONTENT,
543+
isBuiltIn: false,
497544
},
498545
],
499546
}

apps/web/src/utils/themeHelpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Theme } from '@md/shared'
2+
3+
export function generateThemeCSS(_theme: Theme) {
4+
return ``
5+
}

packages/shared/src/configs/theme.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,24 @@ export const themeMap = {
632632
simple: simpleTheme,
633633
}
634634

635+
export const themeOptionsMap = {
636+
default: {
637+
label: `经典`,
638+
value: `default`,
639+
desc: ``,
640+
},
641+
grace: {
642+
label: `优雅`,
643+
value: `grace`,
644+
desc: `@brzhang`,
645+
},
646+
simple: {
647+
label: `简洁`,
648+
value: `simple`,
649+
desc: `@okooo5km`,
650+
},
651+
}
652+
635653
export const themeOptions: IConfigOption<keyof typeof themeMap>[] = [
636654
{
637655
label: `经典`,

0 commit comments

Comments
 (0)