();
- console.log('DatabaseField', field.componentProps);
+ console.log('DatabaseField', field.value);
const [activeKey, setActiveKey] = useState(null);
return (
@@ -26,6 +186,7 @@ export const DatabaseField: any = observer((props) => {
onChange={(key) => {
setActiveKey(key);
}}
+ className={cls({ empty: !field.value?.length })}
accordion
>
{field.value?.map((item, index) => {
@@ -35,9 +196,13 @@ export const DatabaseField: any = observer((props) => {
- {(item.ui && item.ui.title) || 未命名}{' '}
+ {(item.ui && item.ui.title) || (
+ 未命名
+ )}{' '}
{schema.title}
- {item.name}
+
+ {item.name}
+
>
}
extra={[
@@ -97,13 +262,11 @@ export const DatabaseField: any = observer((props) => {
console.log('info.key', info.key, schema);
}}
>
- {options.map(option => (
+ {options.map((option) => (
- {
- option.children.map(item => (
- {item.title}
- ))
- }
+ {option.children.map((item) => (
+ {item.title}
+ ))}
))}
diff --git a/packages/client/src/schemas/database-field/style.less b/packages/client/src/schemas/database-field/style.less
new file mode 100644
index 000000000..68535f5fa
--- /dev/null
+++ b/packages/client/src/schemas/database-field/style.less
@@ -0,0 +1,3 @@
+.ant-collapse.empty {
+ border: 0;
+}
diff --git a/packages/client/src/schemas/grid/index.tsx b/packages/client/src/schemas/grid/index.tsx
index 1deab02e3..c881de031 100644
--- a/packages/client/src/schemas/grid/index.tsx
+++ b/packages/client/src/schemas/grid/index.tsx
@@ -21,7 +21,7 @@ import {
import './style.less';
import cls from 'classnames';
-import { createSchema, useDesignable, useSchemaPath } from '../';
+import { createSchema, removeSchema, useDesignable, useSchemaPath } from '../';
import {
useDrag,
useDrop,
@@ -37,6 +37,10 @@ export const GridBlockContext = createContext({
dragRef: null,
});
+function isGridRowOrCol(schema: ISchema) {
+ return ['Grid.Row', 'Grid.Col'].includes(schema['x-component']);
+}
+
const RowDivider = ({ name, onDrop }) => {
const uid = useDragDropUID();
const { isOver, dropRef } = useDrop({
@@ -111,7 +115,11 @@ export const Grid: any = observer((props) => {
});
await createSchema(data);
console.log('prepend', data);
- deepRemove(path);
+ const removed = deepRemove(path);
+ const last = removed.pop();
+ if (isGridRowOrCol(last)) {
+ await removeSchema(last);
+ }
}}
/>
{schema.mapProperties((property, key, index) => {
@@ -143,8 +151,12 @@ export const Grid: any = observer((props) => {
[...gridPath, key],
);
await createSchema(data);
- console.log('insertAfter', data);
- deepRemove(path);
+ const removed = deepRemove(path);
+ const last = removed.pop();
+ if (isGridRowOrCol(last)) {
+ await removeSchema(last);
+ }
+ console.log('insertAfter', data, removed);
}}
/>
>
@@ -185,7 +197,11 @@ Grid.Row = observer((props) => {
});
await createSchema(data);
const path = [...e.dragItem.path];
- deepRemove(path);
+ const removed = deepRemove(path);
+ const last = removed.pop();
+ if (isGridRowOrCol(last)) {
+ await removeSchema(last);
+ }
}}
/>
{schema.mapProperties((property, key, index) => {
@@ -207,10 +223,14 @@ Grid.Row = observer((props) => {
},
[...rowPath, key],
);
- console.log('ColDivider', data)
+ console.log('ColDivider', data);
await createSchema(data);
const path = [...e.dragItem.path];
- deepRemove(path);
+ const removed = deepRemove(path);
+ const last = removed.pop();
+ if (isGridRowOrCol(last)) {
+ await removeSchema(last);
+ }
}}
onDragEnd={(e) => {
designableSchema.mapProperties((s, key, index) => {
diff --git a/packages/client/src/schemas/index.tsx b/packages/client/src/schemas/index.tsx
index e95be4799..abaebe818 100644
--- a/packages/client/src/schemas/index.tsx
+++ b/packages/client/src/schemas/index.tsx
@@ -18,6 +18,23 @@ export const request = extend({
timeout: 1000,
});
+export async function createOrUpdateCollection(data: any) {
+ return await request('collections:createOrUpdate', {
+ method: 'post',
+ data,
+ });
+}
+
+export async function deleteCollection(name) {
+ await request('collections:destroy', {
+ method: 'post',
+ params: {
+ filter: {
+ name,
+ }
+ },
+ });
+}
export async function createSchema(schema: ISchema) {
if (!schema['key']) {
diff --git a/packages/plugin-collections/src/actions/index.ts b/packages/plugin-collections/src/actions/index.ts
new file mode 100644
index 000000000..da68068e9
--- /dev/null
+++ b/packages/plugin-collections/src/actions/index.ts
@@ -0,0 +1,24 @@
+import { Model, ModelCtor } from '@nocobase/database';
+import { actions, middlewares } from '@nocobase/actions';
+import { sort } from '@nocobase/actions/src/actions/common';
+import { cloneDeep, omit } from 'lodash';
+
+export const createOrUpdate = async (ctx: actions.Context, next: actions.Next) => {
+ const { values } = ctx.action.params;
+ const Collection = ctx.db.getModel('collections');
+ let collection;
+ if (values.name) {
+ collection = await Collection.findByPk(values.name);
+ }
+ try {
+ if (!collection) {
+ collection = await Collection.create(values);
+ } else {
+ await collection.update(values);
+ }
+ } catch (error) {
+ console.log('error.errors', error.errors)
+ }
+
+ ctx.body = collection;
+}
\ No newline at end of file
diff --git a/packages/plugin-collections/src/server.ts b/packages/plugin-collections/src/server.ts
index 9519b62ad..6a8acecba 100644
--- a/packages/plugin-collections/src/server.ts
+++ b/packages/plugin-collections/src/server.ts
@@ -2,6 +2,7 @@ import path from 'path';
import { Application } from '@nocobase/server';
import { registerModels, Table } from '@nocobase/database';
import * as models from './models';
+import { createOrUpdate } from './actions';
export default async function (this: Application, options = {}) {
const database = this.database;
@@ -16,4 +17,6 @@ export default async function (this: Application, options = {}) {
model.set('name', model.get('key'));
}
});
+
+ this.resourcer.registerActionHandler('collections:createOrUpdate', createOrUpdate);
}
diff --git a/packages/plugin-ui-schema/src/actions/index.ts b/packages/plugin-ui-schema/src/actions/index.ts
index 3f0e0985a..0e513ea31 100644
--- a/packages/plugin-ui-schema/src/actions/index.ts
+++ b/packages/plugin-ui-schema/src/actions/index.ts
@@ -18,14 +18,19 @@ export const create = async (ctx: actions.Context, next: actions.Next) => {
payload: 'replace',
},
);
- await actions.common.create(ctx, async () => {});
+ await actions.common.create(ctx, async () => { });
+ const sticky = values['__prepend__'];
const targetKey = values['__insertAfter__'] || values['__insertBefore__'];
- if (targetKey) {
+ if (sticky || targetKey) {
console.log({
associatedKey: values.parentKey,
resourceKey: ctx.body.key,
- values: {
+ values: sticky ? {
field: 'sort',
+ sticky: true,
+ } : {
+ field: 'sort',
+ insertAfter: !!values['__insertAfter__'],
target: {
key: targetKey,
},
@@ -35,7 +40,11 @@ export const create = async (ctx: actions.Context, next: actions.Next) => {
{
associatedKey: values.parentKey,
resourceKey: ctx.body.key,
- values: {
+ values: sticky ? {
+ field: 'sort',
+ sticky: true,
+ target: {},
+ } : {
field: 'sort',
insertAfter: !!values['__insertAfter__'],
target: {
@@ -47,8 +56,8 @@ export const create = async (ctx: actions.Context, next: actions.Next) => {
payload: 'replace',
},
);
- await middlewares.associated(ctx, async () => {});
- await sort(ctx, async () => {});
+ await middlewares.associated(ctx, async () => { });
+ await sort(ctx, async () => { });
}
await next();
};