2022-02-22 11:17:24 +08:00
|
|
|
import { ISchema, Schema, useFieldSchema } from '@formily/react';
|
|
|
|
import { uid } from '@formily/shared';
|
|
|
|
import { SchemaInitializerItemOptions } from '../';
|
|
|
|
import { useCollection } from '../../collection-manager';
|
|
|
|
import { useDesignable } from '../../schema-component';
|
|
|
|
|
|
|
|
export const gridRowColWrap = (schema: ISchema) => {
|
|
|
|
return {
|
|
|
|
type: 'void',
|
|
|
|
'x-component': 'Grid.Row',
|
|
|
|
properties: {
|
|
|
|
[uid()]: {
|
|
|
|
type: 'void',
|
|
|
|
'x-component': 'Grid.Col',
|
|
|
|
properties: {
|
|
|
|
[schema.name || uid()]: schema,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removeTableColumn = (schema, cb) => {
|
|
|
|
cb(schema, {
|
|
|
|
removeParentsIfNoChildren: true,
|
2022-02-25 23:09:42 +08:00
|
|
|
// breakRemoveOn: (s) => {
|
|
|
|
// return s['x-component'].startsWith('Table.');
|
|
|
|
// },
|
2022-02-22 11:17:24 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removeGridFormItem = (schema, cb) => {
|
|
|
|
cb(schema, {
|
|
|
|
removeParentsIfNoChildren: true,
|
|
|
|
breakRemoveOn: {
|
|
|
|
'x-component': 'Grid',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-25 23:09:42 +08:00
|
|
|
const findTableColumn = (schema: Schema, key: string, action: string, deepth: number = 0) => {
|
|
|
|
return schema.reduceProperties((buf, s) => {
|
|
|
|
if (s[key] === action) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
const c = s.reduceProperties((buf, s) => {
|
|
|
|
if (s[key] === action) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
if (c) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-22 11:17:24 +08:00
|
|
|
export const useTableColumnInitializerFields = () => {
|
|
|
|
const { name, fields } = useCollection();
|
2022-03-02 13:22:30 +08:00
|
|
|
return fields
|
|
|
|
.filter((field) => field?.interface !== 'subTable')
|
|
|
|
.map((field) => {
|
|
|
|
if (field.interface === 'linkTo') {
|
2022-02-22 11:17:24 +08:00
|
|
|
return {
|
2022-03-02 13:22:30 +08:00
|
|
|
field,
|
2022-02-22 11:17:24 +08:00
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
2022-03-02 13:22:30 +08:00
|
|
|
find: findTableColumn,
|
|
|
|
remove: removeTableColumn,
|
|
|
|
component: 'LinkToFieldInitializer',
|
2022-02-22 11:17:24 +08:00
|
|
|
schema: {
|
|
|
|
name: field.name,
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
'x-component': 'CollectionField',
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
2022-03-02 13:22:30 +08:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
|
|
|
component: 'CollectionFieldInitializer',
|
|
|
|
find: findTableColumn,
|
|
|
|
remove: removeTableColumn,
|
|
|
|
schema: {
|
|
|
|
name: field.name,
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
'x-component': 'CollectionField',
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
|
|
|
});
|
2022-02-22 11:17:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useFormItemInitializerFields = () => {
|
|
|
|
const { name, fields } = useCollection();
|
|
|
|
return fields?.map((field) => {
|
2022-03-02 13:22:30 +08:00
|
|
|
if (field.interface === 'linkTo') {
|
|
|
|
return {
|
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
|
|
|
component: 'LinkToFieldInitializer',
|
|
|
|
remove: removeGridFormItem,
|
|
|
|
field,
|
|
|
|
schema: {
|
|
|
|
name: field.name,
|
|
|
|
'x-designer': 'FormItem.Designer',
|
|
|
|
'x-component': 'CollectionField',
|
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
|
|
|
}
|
2022-02-25 23:09:42 +08:00
|
|
|
if (field.interface === 'subTable') {
|
|
|
|
return {
|
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
|
|
|
component: 'SubTableFieldInitializer',
|
|
|
|
remove: removeGridFormItem,
|
|
|
|
field,
|
|
|
|
schema: {
|
|
|
|
type: 'void',
|
|
|
|
name: field.name,
|
|
|
|
'x-designer': 'FormItem.Designer',
|
|
|
|
'x-component': 'div',
|
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
|
|
|
}
|
2022-02-22 11:17:24 +08:00
|
|
|
return {
|
|
|
|
type: 'item',
|
|
|
|
title: field?.uiSchema?.title || field.name,
|
|
|
|
component: 'CollectionFieldInitializer',
|
|
|
|
remove: removeGridFormItem,
|
|
|
|
schema: {
|
|
|
|
name: field.name,
|
|
|
|
'x-designer': 'FormItem.Designer',
|
|
|
|
'x-component': 'CollectionField',
|
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-collection-field': `${name}.${field.name}`,
|
|
|
|
},
|
|
|
|
} as SchemaInitializerItemOptions;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const findSchema = (schema: Schema, key: string, action: string) => {
|
|
|
|
return schema.reduceProperties((buf, s) => {
|
|
|
|
if (s[key] === action) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
const c = findSchema(s, key, action);
|
|
|
|
if (c) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeSchema = (schema, cb) => {
|
|
|
|
return cb(schema);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useCurrentSchema = (action: string, key: string, find = findSchema, rm = removeSchema) => {
|
|
|
|
const fieldSchema = useFieldSchema();
|
|
|
|
const { remove } = useDesignable();
|
|
|
|
const schema = find(fieldSchema, key, action);
|
|
|
|
return {
|
|
|
|
schema,
|
|
|
|
exists: !!schema,
|
|
|
|
remove() {
|
|
|
|
schema && rm(schema, remove);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|