diff --git a/packages/plugins/@nocobase/plugin-theme-editor/src/locale/zh-CN.ts b/packages/plugins/@nocobase/plugin-theme-editor/src/locale/zh-CN.ts index 51fb1f33ee..d6cf5b4778 100644 --- a/packages/plugins/@nocobase/plugin-theme-editor/src/locale/zh-CN.ts +++ b/packages/plugins/@nocobase/plugin-theme-editor/src/locale/zh-CN.ts @@ -16,8 +16,6 @@ const locale = { 'Edit based on current theme': '基于当前主题进行编辑', 'Create a brand new theme': '创建一个全新的主题', - // 内置主题的名字 - 'Default theme of antd': 'antd 默认主题', Dark: '暗黑', Compact: '紧凑', 'Compact dark': '紧凑暗黑', diff --git a/packages/plugins/@nocobase/plugin-theme-editor/src/server/builtinThemes.ts b/packages/plugins/@nocobase/plugin-theme-editor/src/server/builtinThemes.ts index 2deb6e79dc..054f3fcb44 100644 --- a/packages/plugins/@nocobase/plugin-theme-editor/src/server/builtinThemes.ts +++ b/packages/plugins/@nocobase/plugin-theme-editor/src/server/builtinThemes.ts @@ -1,12 +1,13 @@ import { ThemeItem } from '../types'; /** antd 默认主题 */ -export const antd: Omit = { +export const defaultTheme: Omit = { config: { - name: 'Default theme of antd', + name: 'Default', }, optional: true, isBuiltIn: true, + uid: 'default', }; export const dark: Omit = { @@ -17,6 +18,7 @@ export const dark: Omit = { }, optional: true, isBuiltIn: true, + uid: 'dark', }; export const compact: Omit = { @@ -27,6 +29,7 @@ export const compact: Omit = { }, optional: true, isBuiltIn: true, + uid: 'compact', }; /** 同时包含 `紧凑` 和 `暗黑` 两种模式 */ @@ -38,4 +41,5 @@ export const compactDark: Omit = { }, optional: true, isBuiltIn: true, + uid: 'compact_dark', }; diff --git a/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250847-theme-editor.ts b/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250847-theme-editor.ts new file mode 100644 index 0000000000..1b07c98ee1 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250847-theme-editor.ts @@ -0,0 +1,45 @@ +import { Migration } from '@nocobase/server'; +import { compact, compactDark, dark, defaultTheme } from '../builtinThemes'; + +export default class ThemeEditorMigration extends Migration { + async up() { + const result = await this.app.version.satisfies('<0.14.0-alpha.8'); + + if (!result) { + return; + } + + const themeRepo = this.db.getRepository('themeConfig'); + if (!themeRepo) { + return; + } + + themeRepo.collection.addField('uid', { + type: 'uid', + }); + await this.db.sync(); + + const themes = [defaultTheme, dark, compact, compactDark]; + const updates = []; + const creates = []; + + for (const theme of themes) { + const { uid } = theme; + const { name } = theme.config; + const filter = { 'config.name': name === 'Default' ? 'Default theme of antd' : name }; + const values = name === 'Default' ? { uid, config: { name } } : { uid }; + const existingTheme = await themeRepo.findOne({ filter }); + + if (existingTheme) { + updates.push(themeRepo.update({ filter, values })); + } else { + creates.push(themeRepo.create({ values: [theme] })); + } + } + + await Promise.all(updates); + await Promise.all(creates); + } + + async down() {} +} diff --git a/packages/plugins/@nocobase/plugin-theme-editor/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-theme-editor/src/server/plugin.ts index 9ff0aaf5d7..caeb6f0443 100644 --- a/packages/plugins/@nocobase/plugin-theme-editor/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-theme-editor/src/server/plugin.ts @@ -1,12 +1,21 @@ import { InstallOptions, Plugin } from '@nocobase/server'; -import { antd, compact, compactDark, dark } from './builtinThemes'; +import { resolve } from 'path'; +import { compact, compactDark, dark, defaultTheme } from './builtinThemes'; export class ThemeEditorPlugin extends Plugin { theme: any; afterAdd() {} - beforeLoad() {} + async beforeLoad() { + this.db.addMigrations({ + namespace: 'theme-editor', + directory: resolve(__dirname, './migrations'), + context: { + plugin: this, + }, + }); + } async load() { this.db.collection({ @@ -26,6 +35,10 @@ export class ThemeEditorPlugin extends Plugin { type: 'boolean', name: 'isBuiltIn', }, + { + type: 'uid', + name: 'uid', + }, ], }); this.app.acl.allow('themeConfig', 'list', 'loggedIn'); @@ -44,7 +57,7 @@ export class ThemeEditorPlugin extends Plugin { if ((await themeRepo.count()) === 0) { await themeRepo.create({ - values: [antd, dark, compact, compactDark], + values: [defaultTheme, dark, compact, compactDark], }); } } diff --git a/packages/plugins/@nocobase/plugin-theme-editor/src/types.ts b/packages/plugins/@nocobase/plugin-theme-editor/src/types.ts index fed1cde7a3..8bd44ab77b 100644 --- a/packages/plugins/@nocobase/plugin-theme-editor/src/types.ts +++ b/packages/plugins/@nocobase/plugin-theme-editor/src/types.ts @@ -9,6 +9,7 @@ export interface ThemeItem { /** 主题是否可选 */ optional: boolean; isBuiltIn?: boolean; + uid?: string; } export type Theme = {