import { DeleteOutlined, DownOutlined, EditOutlined, UpOutlined } from '@ant-design/icons';
import { css } from '@emotion/css';
import { SchemaOptionsContext } from '@formily/react';
import { uid } from '@formily/shared';
import {
CollectionCategroriesContext,
CollectionProvider,
PopoverWithStopPropagation,
SchemaComponent,
SchemaComponentProvider,
Select,
collection,
useCollectionManager,
useCompile,
useCurrentAppInfo,
useRecord,
} from '@nocobase/client';
import { Badge, Tag } from 'antd';
import lodash from 'lodash';
import React, { useContext, useRef, useState } from 'react';
import {
useAsyncDataSource,
useCancelAction,
useDestroyActionAndRefreshCM,
useDestroyFieldActionAndRefreshCM,
useUpdateCollectionActionAndRefreshCM,
useValuesFromRecord,
} from '../action-hooks';
import useStyles from '../style';
import { getPopupContainer, useGCMTranslation } from '../utils';
import { AddFieldAction } from './AddFieldAction';
import { CollectionNodeProvder } from './CollectionNodeProvder';
import { ConnectAssociationAction } from './ConnectAssociationAction';
import { ConnectChildAction } from './ConnectChildAction';
import { ConnectParentAction } from './ConnectParentAction';
import { EditCollectionAction } from './EditCollectionAction';
import { DeleteCollectionAction } from './DeleteCollectionAction';
import { EditFieldAction } from './EditFieldAction';
import { FieldSummary } from './FieldSummary';
import { OverrideFieldAction } from './OverrideFieldAction';
import { ViewFieldAction } from './ViewFieldAction';
const OperationButton: any = React.memo((props: any) => {
const { property, loadCollections, collectionData, setTargetNode, node, handelOpenPorts, title, name, targetGraph } =
props;
const isInheritField = !(property.collectionName !== name);
const options = useContext(SchemaOptionsContext);
const isAssociationField = ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(property.type);
const isShowAssocition =
isAssociationField &&
!(property.through ? targetGraph.hasCell(property.through) : targetGraph.hasCell(property.target));
const {
data: { database },
} = useCurrentAppInfo();
const useNewId = (prefix) => {
return `${prefix || ''}${uid()}`;
};
// 获取当前字段列表
const useCurrentFields = () => {
const record = useRecord();
const { getCollectionFields } = useCollectionManager();
const fields = getCollectionFields(record.collectionName || record.name) as any[];
return fields;
};
return (
,
FieldSummary,
AddFieldAction,
OverrideFieldAction,
ViewFieldAction,
EditFieldAction,
ConnectAssociationAction,
...options.components,
}}
scope={{
useAsyncDataSource,
loadCollections,
useCancelAction,
useNewId,
useCurrentFields,
useValuesFromRecord,
useUpdateCollectionActionAndRefreshCM,
isInheritField,
isShowAssocition,
...options.scope,
}}
>
handelOpenPorts(true)}
>
useDestroyFieldActionAndRefreshCM({
collectionName: property.collectionName,
name: property.name,
}),
},
},
override: {
type: 'void',
'x-action': 'create',
'x-visible': '{{!isInheritField}}',
'x-component': 'OverrideFieldAction',
'x-component-props': {
icon: 'ReconciliationOutlined',
item: {
...property,
title,
__parent: collectionData.current,
targetCollection: name,
},
},
},
view: {
type: 'void',
'x-action': 'view',
'x-visible': '{{!isInheritField}}',
'x-component': 'ViewFieldAction',
'x-component-props': {
icon: 'ReconciliationOutlined',
item: {
...property,
title,
__parent: collectionData.current,
},
},
},
connectAssociation: {
type: 'void',
'x-action': 'view',
'x-visible': '{{isShowAssocition}}',
'x-component': 'ConnectAssociationAction',
'x-component-props': {
item: {
...property,
title,
__parent: collectionData.current,
},
targetGraph,
},
},
},
}}
/>
);
});
const PopoverContent = React.forwardRef((props: any, ref) => {
const { property, node, ...other } = props;
const {
store: {
data: { title, name, sourcePort, associated, targetPort },
},
} = node;
const compile = useCompile();
const { styles } = useStyles();
const { getInterface } = useCollectionManager();
const [isHovered, setIsHovered] = useState(false);
const CollectionConten = React.useCallback((data) => {
const { type, name, primaryKey, allowNull, autoIncrement } = data;
return (
name: {name}
type: {type}
{primaryKey && PRIMARY}
{allowNull && ALLOWNULL}
{autoIncrement && AUTOINCREMENT}
);
}, []);
const operatioBtnProps = {
title,
name,
node,
...other,
};
const typeColor = (v) => {
if (v.isForeignKey || v.primaryKey || v.interface === 'id') {
return 'red';
} else if (['obo', 'oho', 'o2o', 'o2m', 'm2o', 'm2m', 'linkTo'].includes(v.interface)) {
return 'orange';
}
};
return (
{
setIsHovered(true);
}}
onMouseLeave={() => setIsHovered(false)}
>
{compile(property.uiSchema?.title)}
{compile(getInterface(property.interface)?.title)}
}
key={property.id}
placement="right"
>
{compile(property.uiSchema?.title)}
{compile(getInterface(property.interface)?.title)}
{isHovered &&
}
);
});
const PortsCom = React.memo(({ targetGraph, collectionData, setTargetNode, node, loadCollections }) => {
const {
store: {
data: { item, ports, data },
},
} = node;
const [collapse, setCollapse] = useState(false);
const { t } = useGCMTranslation();
const portsData = lodash.groupBy(ports.items, (v) => {
if (
v.isForeignKey ||
v.primaryKey ||
['obo', 'oho', 'o2o', 'o2m', 'm2o', 'm2m', 'linkTo', 'id'].includes(v.interface)
) {
return 'initPorts';
} else {
return 'morePorts';
}
});
const handelOpenPorts = (isCollapse?) => {
targetGraph.getCellById(item.name)?.toFront();
setCollapse(isCollapse);
const collapseNodes = targetGraph.collapseNodes || [];
collapseNodes.push({
[item.name]: isCollapse,
});
targetGraph.collapseNodes = collapseNodes;
targetGraph.getCellById(item.name).setData({ collapse: true });
};
const isCollapse = collapse && data?.collapse;
const popoverProps = {
collectionData,
setTargetNode,
loadCollections,
handelOpenPorts,
node,
targetGraph,
};
return (
);
});
const Entity: React.FC<{
node?: Node | any;
setTargetNode: Function | any;
targetGraph: any;
}> = (props) => {
const { styles } = useStyles();
const options = useContext(SchemaOptionsContext);
const { node, setTargetNode, targetGraph } = props;
const {
store: {
data: { title, name, item, attrs, select, actived },
},
id,
} = node;
const {
data: { database },
} = useCurrentAppInfo();
const collectionData = useRef();
const categoryData = useContext(CollectionCategroriesContext);
collectionData.current = { ...item, title, inherits: item.inherits && new Proxy(item.inherits, {}) };
const { category } = item;
const compile = useCompile();
const loadCollections = async (field: any) => {
return targetGraph.collections?.map((collection: any) => ({
label: compile(collection.title),
value: collection.name,
}));
};
const loadCategories = async () => {
return categoryData?.data.map((item: any) => ({
label: compile(item.name),
value: item.id,
}));
};
const portsProps = {
targetGraph,
collectionData,
setTargetNode,
node,
loadCollections,
};
return (
{category.map((v, index) => {
return (
);
})}
{compile(title)}
{
return useDestroyActionAndRefreshCM({ name, id });
},
},
},
},
}}
/>
);
};
export default Entity;