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:
parent
1ec2d00208
commit
1ff3aa94fc
@ -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': '紧凑暗黑',
|
||||
|
@ -1,12 +1,13 @@
|
||||
import { ThemeItem } from '../types';
|
||||
|
||||
/** antd 默认主题 */
|
||||
export const antd: Omit<ThemeItem, 'id'> = {
|
||||
export const defaultTheme: Omit<ThemeItem, 'id'> = {
|
||||
config: {
|
||||
name: 'Default theme of antd',
|
||||
name: 'Default',
|
||||
},
|
||||
optional: true,
|
||||
isBuiltIn: true,
|
||||
uid: 'default',
|
||||
};
|
||||
|
||||
export const dark: Omit<ThemeItem, 'id'> = {
|
||||
@ -17,6 +18,7 @@ export const dark: Omit<ThemeItem, 'id'> = {
|
||||
},
|
||||
optional: true,
|
||||
isBuiltIn: true,
|
||||
uid: 'dark',
|
||||
};
|
||||
|
||||
export const compact: Omit<ThemeItem, 'id'> = {
|
||||
@ -27,6 +29,7 @@ export const compact: Omit<ThemeItem, 'id'> = {
|
||||
},
|
||||
optional: true,
|
||||
isBuiltIn: true,
|
||||
uid: 'compact',
|
||||
};
|
||||
|
||||
/** 同时包含 `紧凑` 和 `暗黑` 两种模式 */
|
||||
@ -38,4 +41,5 @@ export const compactDark: Omit<ThemeItem, 'id'> = {
|
||||
},
|
||||
optional: true,
|
||||
isBuiltIn: true,
|
||||
uid: 'compact_dark',
|
||||
};
|
||||
|
@ -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() {}
|
||||
}
|
@ -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],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ export interface ThemeItem {
|
||||
/** 主题是否可选 */
|
||||
optional: boolean;
|
||||
isBuiltIn?: boolean;
|
||||
uid?: string;
|
||||
}
|
||||
|
||||
export type Theme = {
|
||||
|
Loading…
Reference in New Issue
Block a user