被雨水过滤的空气-Rain 1ff3aa94fc
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>
2023-10-27 16:55:13 +08:00

73 lines
1.5 KiB
TypeScript

import { InstallOptions, Plugin } from '@nocobase/server';
import { resolve } from 'path';
import { compact, compactDark, dark, defaultTheme } from './builtinThemes';
export class ThemeEditorPlugin extends Plugin {
theme: any;
afterAdd() {}
async beforeLoad() {
this.db.addMigrations({
namespace: 'theme-editor',
directory: resolve(__dirname, './migrations'),
context: {
plugin: this,
},
});
}
async load() {
this.db.collection({
name: 'themeConfig',
fields: [
// 主题配置内容,一个 JSON 字符串
{
type: 'json',
name: 'config',
},
// 主题是否可选
{
type: 'boolean',
name: 'optional',
},
{
type: 'boolean',
name: 'isBuiltIn',
},
{
type: 'uid',
name: 'uid',
},
],
});
this.app.acl.allow('themeConfig', 'list', 'loggedIn');
this.app.acl.registerSnippet({
name: `pm.${this.name}.themeConfig`,
actions: ['themeConfig:*'],
});
}
async install(options?: InstallOptions) {
const themeRepo = this.db.getRepository('themeConfig');
if (!themeRepo) {
throw new Error(`themeConfig repository does not exist`);
}
if ((await themeRepo.count()) === 0) {
await themeRepo.create({
values: [defaultTheme, dark, compact, compactDark],
});
}
}
async afterEnable() {}
async afterDisable() {}
async remove() {}
}
export default ThemeEditorPlugin;