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 <chenlinxh@gmail.com>
This commit is contained in:
被雨水过滤的空气-Rain 2023-10-27 16:55:13 +08:00 committed by GitHub
parent 1ec2d00208
commit 1ff3aa94fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 7 deletions

View File

@ -16,8 +16,6 @@ const locale = {
'Edit based on current theme': '基于当前主题进行编辑', 'Edit based on current theme': '基于当前主题进行编辑',
'Create a brand new theme': '创建一个全新的主题', 'Create a brand new theme': '创建一个全新的主题',
// 内置主题的名字
'Default theme of antd': 'antd 默认主题',
Dark: '暗黑', Dark: '暗黑',
Compact: '紧凑', Compact: '紧凑',
'Compact dark': '紧凑暗黑', 'Compact dark': '紧凑暗黑',

View File

@ -1,12 +1,13 @@
import { ThemeItem } from '../types'; import { ThemeItem } from '../types';
/** antd 默认主题 */ /** antd 默认主题 */
export const antd: Omit<ThemeItem, 'id'> = { export const defaultTheme: Omit<ThemeItem, 'id'> = {
config: { config: {
name: 'Default theme of antd', name: 'Default',
}, },
optional: true, optional: true,
isBuiltIn: true, isBuiltIn: true,
uid: 'default',
}; };
export const dark: Omit<ThemeItem, 'id'> = { export const dark: Omit<ThemeItem, 'id'> = {
@ -17,6 +18,7 @@ export const dark: Omit<ThemeItem, 'id'> = {
}, },
optional: true, optional: true,
isBuiltIn: true, isBuiltIn: true,
uid: 'dark',
}; };
export const compact: Omit<ThemeItem, 'id'> = { export const compact: Omit<ThemeItem, 'id'> = {
@ -27,6 +29,7 @@ export const compact: Omit<ThemeItem, 'id'> = {
}, },
optional: true, optional: true,
isBuiltIn: true, isBuiltIn: true,
uid: 'compact',
}; };
/** 同时包含 `紧凑` 和 `暗黑` 两种模式 */ /** 同时包含 `紧凑` 和 `暗黑` 两种模式 */
@ -38,4 +41,5 @@ export const compactDark: Omit<ThemeItem, 'id'> = {
}, },
optional: true, optional: true,
isBuiltIn: true, isBuiltIn: true,
uid: 'compact_dark',
}; };

View File

@ -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() {}
}

View File

@ -1,12 +1,21 @@
import { InstallOptions, Plugin } from '@nocobase/server'; 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 { export class ThemeEditorPlugin extends Plugin {
theme: any; theme: any;
afterAdd() {} afterAdd() {}
beforeLoad() {} async beforeLoad() {
this.db.addMigrations({
namespace: 'theme-editor',
directory: resolve(__dirname, './migrations'),
context: {
plugin: this,
},
});
}
async load() { async load() {
this.db.collection({ this.db.collection({
@ -26,6 +35,10 @@ export class ThemeEditorPlugin extends Plugin {
type: 'boolean', type: 'boolean',
name: 'isBuiltIn', name: 'isBuiltIn',
}, },
{
type: 'uid',
name: 'uid',
},
], ],
}); });
this.app.acl.allow('themeConfig', 'list', 'loggedIn'); this.app.acl.allow('themeConfig', 'list', 'loggedIn');
@ -44,7 +57,7 @@ export class ThemeEditorPlugin extends Plugin {
if ((await themeRepo.count()) === 0) { if ((await themeRepo.count()) === 0) {
await themeRepo.create({ await themeRepo.create({
values: [antd, dark, compact, compactDark], values: [defaultTheme, dark, compact, compactDark],
}); });
} }
} }

View File

@ -9,6 +9,7 @@ export interface ThemeItem {
/** 主题是否可选 */ /** 主题是否可选 */
optional: boolean; optional: boolean;
isBuiltIn?: boolean; isBuiltIn?: boolean;
uid?: string;
} }
export type Theme = { export type Theme = {