From caa98f6d086b26901e135f236821bde56f5f46b6 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 8 Dec 2020 10:02:41 +0800 Subject: [PATCH] feat: improve developer mode support --- packages/app/src/api/collections/example.ts | 26 +++++++++++++++++++ packages/app/src/api/index.ts | 10 +++++++ packages/app/src/api/migrate.ts | 3 +++ .../src/hooks/collections-after-create.ts | 2 ++ .../src/interfaces/types.ts | 4 +++ .../plugin-pages/src/actions/getRoutes.ts | 3 +++ .../plugin-pages/src/collections/pages.ts | 11 ++++++++ .../plugin-users/src/collections/users.ts | 5 ++++ 8 files changed, 64 insertions(+) create mode 100644 packages/app/src/api/collections/example.ts diff --git a/packages/app/src/api/collections/example.ts b/packages/app/src/api/collections/example.ts new file mode 100644 index 0000000000..28c344422d --- /dev/null +++ b/packages/app/src/api/collections/example.ts @@ -0,0 +1,26 @@ +import { TableOptions } from '@nocobase/database'; + +export default { + title: '示例', + showInDataMenu: true, + fields: [ + { + interface: 'string', + title: '单行文本', + component: { + showInTable: true, + showInDetail: true, + showInForm: true, + }, + }, + { + interface: 'textarea', + title: '多行文本', + component: { + showInTable: true, + showInDetail: true, + showInForm: true, + }, + }, + ], +}; diff --git a/packages/app/src/api/index.ts b/packages/app/src/api/index.ts index ad7ab8d058..aa8e594c82 100644 --- a/packages/app/src/api/index.ts +++ b/packages/app/src/api/index.ts @@ -34,6 +34,14 @@ const api = Api.create({ }, }); +api.resourcer.use(async (ctx, next) => { + const { resourceName } = ctx.action.params; + const table = ctx.db.getTable(resourceName); + if (table && table.hasField('developerMode')) { + ctx.action.setParam('filter.developerMode', false); + } + await next(); +}); api.resourcer.use(associated); api.resourcer.registerActionHandlers({...actions.common, ...actions.associate}); @@ -47,6 +55,8 @@ api.resourcer.registerActionHandlers({...actions.common, ...actions.associate}); // [path.resolve(__dirname, '../../../plugin-file-manager'), {}], ]); + // await api.database.getModel('collections').load(); + api.listen(process.env.HTTP_PORT, () => { console.log(`http://localhost:${process.env.HTTP_PORT}/`); }); diff --git a/packages/app/src/api/migrate.ts b/packages/app/src/api/migrate.ts index 8014b734c3..a1348c5b15 100644 --- a/packages/app/src/api/migrate.ts +++ b/packages/app/src/api/migrate.ts @@ -113,6 +113,7 @@ const data = { path: '/settings/pages', icon: 'dashboard', sort: 100, + developerMode: true, }, { title: '数据表配置', @@ -179,5 +180,7 @@ const data = { token: "38979f07e1fca68fb3d2", }); + await database.getModel('collections').import(require('./collections/example').default); + await database.close(); })(); diff --git a/packages/plugin-collections/src/hooks/collections-after-create.ts b/packages/plugin-collections/src/hooks/collections-after-create.ts index a255aa7295..d846d44adb 100644 --- a/packages/plugin-collections/src/hooks/collections-after-create.ts +++ b/packages/plugin-collections/src/hooks/collections-after-create.ts @@ -31,6 +31,7 @@ const defaultValues = { name: 'form', title: '表单', template: 'DrawerForm', + developerMode: true, }, { type: 'details', @@ -38,6 +39,7 @@ const defaultValues = { title: '详情', template: 'Details', actionNames: ['update'], + developerMode: true, }, { type: 'table', diff --git a/packages/plugin-collections/src/interfaces/types.ts b/packages/plugin-collections/src/interfaces/types.ts index 547e20d09f..ae2808a834 100644 --- a/packages/plugin-collections/src/interfaces/types.ts +++ b/packages/plugin-collections/src/interfaces/types.ts @@ -413,6 +413,7 @@ export const primaryKey = { autoIncrement: true, primaryKey: true, filterable: true, + developerMode: true, component: { type: 'number', }, @@ -429,6 +430,7 @@ export const sort = { interface: 'sort', type: 'integer', required: true, + developerMode: true, component: { type: 'sort', showInTable: true, @@ -441,6 +443,7 @@ export const password = { options: { interface: 'password', type: 'password', + developerMode: true, hidden: true, // hidden 用来控制 api 不输出这个字段,但是可能这个字段显示在表单里 showInForm component: { type: 'password', @@ -454,6 +457,7 @@ export const json = { interface: 'json', type: 'json', dottie: true, + developerMode: true, component: { type: 'hidden', }, diff --git a/packages/plugin-pages/src/actions/getRoutes.ts b/packages/plugin-pages/src/actions/getRoutes.ts index 62509bf276..cf6c27fe47 100644 --- a/packages/plugin-pages/src/actions/getRoutes.ts +++ b/packages/plugin-pages/src/actions/getRoutes.ts @@ -35,6 +35,9 @@ export default async function getRoutes(ctx, next) { const database: Database = ctx.database; const Page = database.getModel('pages'); let pages = await Page.findAll({ + where: { + developerMode: false, + }, order: [['sort', 'asc']], }); const data = flatToTree(pages.map(row => row.toJSON()), { diff --git a/packages/plugin-pages/src/collections/pages.ts b/packages/plugin-pages/src/collections/pages.ts index c785f5adb2..01878f0edc 100644 --- a/packages/plugin-pages/src/collections/pages.ts +++ b/packages/plugin-pages/src/collections/pages.ts @@ -4,6 +4,7 @@ export default { name: 'pages', title: '页面配置', model: 'BaseModel', + developerMode: true, fields: [ { interface: 'sort', @@ -158,6 +159,16 @@ export default { showInDetail: true, }, }, + { + interface: 'boolean', + type: 'boolean', + name: 'developerMode', + title: '开发者模式', + defaultValue: false, + component: { + type: 'boolean', + }, + }, { interface: 'linkTo', type: 'hasMany', diff --git a/packages/plugin-users/src/collections/users.ts b/packages/plugin-users/src/collections/users.ts index 70b55c51b3..18d4bfca94 100644 --- a/packages/plugin-users/src/collections/users.ts +++ b/packages/plugin-users/src/collections/users.ts @@ -3,6 +3,7 @@ import { TableOptions } from '@nocobase/database'; export default { name: 'users', title: '用户', + developerMode: true, fields: [ { interface: 'string', @@ -95,18 +96,21 @@ export default { name: 'form', title: '表单', template: 'DrawerForm', + developerMode: true, }, { type: 'form', name: 'login', title: '登录', template: 'Login', + developerMode: true, }, { type: 'form', name: 'register', title: '注册', template: 'Register', + developerMode: true, }, { type: 'details', @@ -114,6 +118,7 @@ export default { title: '详情', template: 'Details', actionNames: ['update'], + developerMode: true, }, { type: 'simple',