import React, { DragEvent, useEffect, useMemo, useState } from 'react'; import { Collapse, Tooltip } from "antd"; import { ProgramConfig } from "../../../index"; import './index.less'; // 组件分组配置 const assetGroups = [ { key: 'SVG', label: '矢量图(SVG)' }, { key: 'picture', label: '图片' }, { key: 'GIF', label: 'GIF' }, // { key: 'composite', label: '图形组合 (JSON)' }, { key: 'other', label: '其他' } ]; export const handleImgUrl = (val: string) => { let src = val; if (!src || src.startsWith("http")) { return src; } if (src.includes("@R@")) { const text = src.split("@R@")[1]; if (text.startsWith("http")) { return text; } src = text; } const url = encodeURI(ProgramConfig.joinPath( ProgramConfig.joinPath(ProgramConfig.api(), "/v3/files"), src )) + `?@token=${ProgramConfig.hydrocarbonToken()}&@programToken=${ProgramConfig.programCode()}&@applicationToken=${ProgramConfig.applicationCode()}`; return url; } // 不显示连接线的组件类型 const noLinkToolType = ["workstation"]; interface AssetItem { body: { path: string; suffix: string; }; type: string; // 组件类型 config?: string; // 组件配置 } const AssetList = () => { const [activeKey, setActiveKey] = useState(["SVG", "picture", "GIF", "other"]); const [component2D, setComponent2D] = useState({}); // 拖拽 const handleDragStart = (e: DragEvent, item: AssetItem) => { if (!item?.body) { return; } let defValues = {}; try { const config = JSON.parse(item.config || "[]"); config.forEach(item => { item.groups.forEach(r => r.fields?.forEach(i => { if (i.defaultValue || i.defaultValue === 0) { defValues[i.id] = i.defaultValue; } })); }); } catch { defValues = {} } e.dataTransfer.setData('defaultValues', JSON.stringify(defValues)); // 默认值配置 e.dataTransfer.setData('componentItem', JSON.stringify(item)); // 整体配置信息 e.dataTransfer.setData('src', item.body.path); // 组件地址 e.dataTransfer.setData('type', item.body.suffix); e.dataTransfer.setData('componentType', item.type || ''); // 组件类型 e.dataTransfer.setData('showPoints', noLinkToolType.includes(item.type) ? "false" : "true"); }; const get2DComponent = async () => { if (!activeKey?.length) { return; } const res = await ProgramConfig.get2DComponent(activeKey); setComponent2D(res); } useEffect(() => { get2DComponent(); }, [activeKey]) // 组件渲染 const assetNodes = useMemo(() => { const temArr = assetGroups?.map(r => ({ ...r, children: component2D[r.key]?.length ? : null }))?.filter(r => r.children); return temArr || []; }, [component2D]); return (
) }; export default AssetList;