From 1ff3aa94fcecc071c0f15f54328b92d7f8d0a1a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=9B=A8=E6=B0=B4=E8=BF=87=E6=BB=A4=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E6=B0=94-Rain?= <958414905@qq.com> Date: Fri, 27 Oct 2023 16:55:13 +0800 Subject: [PATCH] chore(theme-editor): add migration (#2367) * chore: change 'Default theme of antd' to 'Default' * feat(theme-editor): add migration * refactor: change file name * feat: add a uid to avoid mutiple additions * chore: add version info * chore: optimize * fix: upgrading error * chore: add comment * Update 202307250847-theme-editor.ts * Update plugin.ts * Update 202307250847-theme-editor.ts * refactor: better code --------- Co-authored-by: chenos --- .../plugin-theme-editor/src/locale/zh-CN.ts | 2 - .../src/server/builtinThemes.ts | 8 +++- .../migrations/202307250847-theme-editor.ts | 45 +++++++++++++++++++ .../plugin-theme-editor/src/server/plugin.ts | 19 ++++++-- .../plugin-theme-editor/src/types.ts | 1 + 5 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 packages/plugins/@nocobase/plugin-theme-editor/src/server/migrations/202307250847-theme-editor.ts 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 51fb1f33e..d6cf5b477 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 2deb6e79d..054f3fcb4 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 000000000..1b07c98ee --- /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 9ff0aaf5d..caeb6f044 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 fed1cde7a..8bd44ab77 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 = {