import React, { ForwardedRef, forwardRef, useImperativeHandle, useState, useMemo, useEffect } from 'react'; import { Button, Space } from "antd"; import HydrocarbonService from "../../tmpl/hcservice-v3"; import ProgramConfig from "../../units"; import LowcodeConstant from "../assets/lowcode-constant"; import { useLocale } from "../../locale/useLocale"; import './index.less'; const { serverKey, sourceId } = LowcodeConstant.fileUpload; // 处理背景图片地址 const handleImgUrl = (basePath: string) => { if (!basePath) { return ""; } if (basePath.startsWith("http")) { return basePath; } const url = encodeURI(ProgramConfig.joinPath( ProgramConfig.joinPath(ProgramConfig.api(), "/v3/files"), basePath )) + `?@token=${ProgramConfig.hydrocarbonToken()}&@programToken=${ProgramConfig.programCode()}&@applicationToken=${ProgramConfig.applicationCode()}`; return url; } const iterateLayerLink = (layerHeader: string, res: string[], layerConfigs: any) => { let layer: ILayerItem = layerConfigs[layerHeader]; if (layer) { res.push(layer.id); if (layer.childHeader) iterateLayerLink(layer.childHeader, res, layerConfigs); let nextLayer = layerConfigs[layer.next]; while (nextLayer) { if (nextLayer.childHeader) iterateLayerLink(nextLayer.childHeader, res, layerConfigs); res.push(nextLayer.id); nextLayer = layerConfigs[nextLayer.next]; } } } const findAllChildLayer = (groupIds: string[], hasSelf: boolean = true, layerConfigs: any): string[] => { let layerIdArr: string[] = []; groupIds.forEach((id) => { layerIdArr.push(id); let layer: ILayerItem = layerConfigs[id]; if (layer.childHeader) iterateLayerLink(layer.childHeader, layerIdArr, layerConfigs); }); if (!hasSelf) layerIdArr = layerIdArr.filter((id) => !groupIds.includes(id)); return [...new Set(layerIdArr)]; } // 控制组件显隐 const handleCompsHide = (ids: string[], type: string) => { if (!ids || ids.length === 0) return; // @ts-ignore const { layerConfigs, updateLayer } = window.LC_VIEW?.layerManager || {}; const toBeUpdate = ids.map((id: string) => { const item = layerConfigs[id]; const hide = type === "show" ? false : true; if (item.type === "group") { const childLayerIds = findAllChildLayer([item.id], true, layerConfigs); return childLayerIds.map(id => ({ ...layerConfigs[id], hide })); } return [{ ...item, hide }]; }).flat(); // eslint-disable-next-line no-unused-expressions updateLayer?.(toBeUpdate); } interface ILayerItem { id?: string | undefined; hide?: boolean; childHeader?: string; next?: string; } interface ItemStyle { backgroundColor?: string; backgroundImage?: string; color?: string; fontSize?: string; localImgCode?: string; } export interface ComponentStyle { items?: { text: string, compsIds: string[] }[]; // 标签项 size?: number; // 间距大小 direction?: "vertical" | "horizontal"; // 间距方向 defaultActiveKey?: string; // 默认选中项 defaultStyle?: ItemStyle; // 默认样式 activeStyle?: ItemStyle; // 选中样式 } export interface ComponentProps { style?: ComponentStyle; isDesignMode?: boolean; // 是否编辑模式 } export interface ComponentRef { updateConfig: (newConfig: ComponentProps) => void; } const ButtonComponent = forwardRef((props: ComponentProps, ref: ForwardedRef) => { const { style, isDesignMode } = props; const [config, setConfig] = useState(style || {}); const [activeKey, setActiveKey] = useState(style.defaultActiveKey); const [defaultStyle, setDefaultStyle] = useState({ ...style.defaultStyle }); const [activeStyle, setActiveStyle] = useState({ ...style.activeStyle }); const [localImgDel, setLocalImgDel] = useState<{ active: boolean, default: boolean }>({ active: false, default: false }); const { translate } = useLocale(); useImperativeHandle(ref, () => ({ updateConfig: (newConfig) => { // 组件更新配置 const { style } = newConfig; setConfig({ ...(style || {}) }); setActiveKey(style.defaultActiveKey); setActiveStyle({ ...style.activeStyle }); setDefaultStyle({ ...style.defaultStyle }); const activeItem = style.items.find((r, index) => `key${index}` === style.defaultActiveKey); onClick({ ...activeItem, key: style.defaultActiveKey }); }, })); const onClick = (item) => { setActiveKey(item.key); const showCompsIds = item.compsIds; const hideCompsIds = style.items.filter((r, index) => `key${index}` !== item.key).map(r => r.compsIds).flat().filter(Boolean); handleCompsHide(showCompsIds, "show"); handleCompsHide(hideCompsIds, "hide"); }; const activeBackgroundImage = useMemo(() => handleImgUrl(activeStyle?.backgroundImage), [activeStyle]); const defaultBackgroundImage = useMemo(() => handleImgUrl(defaultStyle?.backgroundImage), [defaultStyle]); // 编辑页判断本地图片是否存在 const handleDesignModeLocalImg = async (type: "active" | "default") => { const localImgCode = type === "active" ? activeStyle.localImgCode : defaultStyle.localImgCode; if (!localImgCode) { setLocalImgDel(val => ({ ...val, [type]: false })); return; } const { entity } = await HydrocarbonService.requestDtmplConfig(serverKey, sourceId, localImgCode); setLocalImgDel(val => ({ ...val, [type]: !entity })); } useEffect(() => { if (isDesignMode) { handleDesignModeLocalImg("active"); handleDesignModeLocalImg("default"); } }, [activeStyle, defaultStyle]); return ( {config.items?.map((item, index) => )} ); }); export default ButtonComponent;