feat: add @nocobase/plugin-ui-routes-storage

This commit is contained in:
chenos 2022-02-11 15:16:00 +08:00
parent c2725ac9ca
commit ad648431e9
9 changed files with 224 additions and 5 deletions

View File

@ -57,6 +57,7 @@ const api = new Application({
api.plugin(require('@nocobase/plugin-collection-manager').default);
api.plugin(require('@nocobase/plugin-ui-schema-storage').default);
api.plugin(require('@nocobase/plugin-ui-routes-storage').default);
// api.plugin(require('@nocobase/plugin-acl'));
if (process.argv.length < 3) {

View File

@ -15,7 +15,7 @@ export default (apiClient: APIClient) => {
},
});
const jsonSchema = {
const jsonSchema: any = {
qqzzjakwkwl: {
name: 'qqzzjakwkwl',
type: 'void',
@ -109,11 +109,12 @@ export default (apiClient: APIClient) => {
};
mock.onGet(/\/ui_schemas\:getJsonSchema\/(\w+)/).reply(function (config) {
const name = config.url.split('/').pop();
console.log(name);
if (jsonSchema[name]) {
const name = config?.url?.split('/')?.pop();
if (name && jsonSchema[name]) {
return [200, { data: jsonSchema[name] }];
}
const response = {
data: {
type: 'void',

View File

@ -54,6 +54,7 @@ export default {
type: 'belongsTo',
name: 'uiSchema',
target: 'ui_schemas',
foreignKey: 'uiSchemaUid'
},
{
type: 'json',

View File

@ -0,0 +1,7 @@
node_modules
*.log
docs
__tests__
tsconfig.json
src
.fatherrc.ts

View File

@ -0,0 +1,13 @@
{
"name": "@nocobase/plugin-ui-routes-storage",
"version": "0.5.0-alpha.38",
"main": "lib/index.js",
"license": "MIT",
"dependencies": {
"flat-to-nested": "^1.1.1"
},
"devDependencies": {
"@nocobase/test": "^0.6.0-alpha.0"
},
"gitHead": "e7df1f93c4e23b9a666d99ee7372c02bdaec97c4"
}

View File

@ -0,0 +1,18 @@
import { Context, Next } from '@nocobase/actions';
import FlatToNested from 'flat-to-nested';
const flatToNested = new FlatToNested({
id: 'key',
parent: 'parentKey',
children: 'routes',
});
export const getAccessible = async (ctx: Context, next: Next) => {
const repository = ctx.db.getRepository('uiRoutes');
const routes = await repository.find({
sort: ['sort'],
});
const data = flatToNested.convert(routes.map((route) => route.toJSON()));
ctx.body = data?.routes || [];
await next();
};

View File

@ -0,0 +1,42 @@
import { defineCollection } from '@nocobase/database';
export default defineCollection({
name: 'uiRoutes',
title: '前端路由表',
model: 'MagicAttributeModel',
autoGenId: false,
sortable: {
name: 'sort',
scopeKey: 'parentKey',
},
fields: [
{
type: 'uid',
name: 'key',
prefix: 'r_',
primaryKey: true,
},
{
type: 'string',
name: 'type',
},
{
type: 'hasMany',
name: 'routes',
target: 'uiRoutes',
sourceKey: 'key',
foreignKey: 'parentKey',
},
{
type: 'belongsTo',
name: 'uiSchema',
target: 'ui_schemas',
foreignKey: 'uiSchemaUid'
},
{
type: 'json',
name: 'options',
defaultValue: {},
},
],
});

View File

@ -0,0 +1,136 @@
import { MagicAttributeModel } from '@nocobase/database';
import { Plugin } from '@nocobase/server';
import { resolve } from 'path';
import { getAccessible } from './actions/getAccessible';
export class UiRoutesStoragePlugin extends Plugin {
beforeLoad() {
this.app.on('installing', async () => {
const repository = this.app.db.getRepository('uiRoutes');
const routes = [
{
type: 'redirect',
from: '/',
to: '/admin',
exact: true,
},
{
type: 'route',
uiSchema: {
type: 'void',
'x-component': 'Menu',
'x-component-props': {
mode: 'mix',
theme: 'dark',
// defaultSelectedUid: 'u8',
onSelect: '{{ onSelect }}',
sideMenuRefScopeKey: 'sideMenuRef',
},
properties: {
item3: {
type: 'void',
title: 'SubMenu u3',
'x-component': 'Menu.SubMenu',
'x-component-props': {},
properties: {
item6: {
type: 'void',
title: 'SubMenu u6',
'x-component': 'Menu.SubMenu',
'x-component-props': {},
properties: {
item7: {
type: 'void',
title: 'Menu Item u7',
'x-component': 'Menu.Item',
'x-component-props': {},
},
item8: {
type: 'void',
title: 'Menu Item u8',
'x-component': 'Menu.Item',
'x-component-props': {},
},
},
},
item4: {
type: 'void',
title: 'Menu Item u4',
'x-component': 'Menu.Item',
'x-component-props': {},
},
item5: {
type: 'void',
title: 'Menu Item u5',
'x-component': 'Menu.Item',
'x-component-props': {},
},
},
},
item1: {
type: 'void',
title: 'Menu Item u1',
'x-component': 'Menu.Item',
'x-component-props': {},
},
item2: {
type: 'void',
title: 'Menu Item u2',
'x-component': 'Menu.Item',
'x-component-props': {},
},
item9: {
type: 'void',
title: 'SubMenu u9',
'x-component': 'Menu.SubMenu',
'x-component-props': {},
properties: {
item10: {
type: 'void',
title: 'Menu Item u10',
'x-component': 'Menu.Item',
'x-component-props': {},
},
},
},
},
},
path: '/admin/:name(.+)?',
component: 'AdminLayout',
title: 'NocoBase Admin',
},
{
type: 'route',
component: 'AuthLayout',
routes: [
{
type: 'route',
path: '/signin',
component: 'SigninPage',
},
{
type: 'route',
path: '/signup',
component: 'SignupPage',
},
],
},
];
for (const values of routes) {
await repository.create({
values,
});
}
});
}
async load() {
this.app.resourcer.registerActionHandler('uiRoutes:getAccessible', getAccessible);
this.app.db.registerModels({ MagicAttributeModel });
this.app.db.import({
directory: resolve(__dirname, 'collections'),
});
}
}
export default UiRoutesStoragePlugin;

View File

@ -1,4 +1,4 @@
import { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
import { UiSchemaRepository } from '../../';
export async function removeSchema({ schemaInstance, options, db, params }) {
const { transaction } = options;