import React, { useEffect, useMemo, useRef, useState, } from "react"; import { Collapse, Input, Space, Spin, Tooltip } from "antd"; import { useModel } from "umi"; import { Dnd } from "@antv/x6-plugin-dnd"; import insertCss from "insert-css"; import { SearchOutlined } from "@ant-design/icons"; import { CompoundedComponent } from "@/types"; import { useRequest } from "ahooks"; import ImageNode from "@/components/ImageNode"; import { basic } from "@/components/basic"; import { flowchart } from "@/components/flowchart"; import { er } from "@/components/er"; import { lane } from "@/components/lane"; export default function Libary() { const { graph, initDnd, startDrag } = useModel("graphModel"); const containerRef = useRef(null); const [search, setSearch] = useState(""); const [showSearch, setShowSearch] = useState(false); const [systemSearchResult, setSystemSearchResult] = useState< CompoundedComponent[] >([]); const [activeKeys, setActiveKeys] = useState(["1", "2", "3", "4"]); useEffect(() => { if (!containerRef.current || !graph) return; const dnd = new Dnd({ target: graph, scaled: false, dndContainer: containerRef.current, }); initDnd(dnd); }, [graph, containerRef.current]); insertCss(` .shalu-collapse-item { border-bottom: 1px solid #dfe2e5 !important; border-radius: 0 !important; } .shalu-tabs-content { height: 100%; overflow: auto; } `); const paramRef = useRef>({ appkey: "66dbfc87e4b0eb0606e13055", page: 1, size: 25, query: "", }); const [iconList, setIconList] = useState([]); const searchServer = async (): Promise => { const params = new URLSearchParams(paramRef.current).toString(); const res = await fetch(`https://iconsapi.com/api/search?${params}`).then( (res) => res.json() ); const list: CompoundedComponent[] = (res?.pages?.elements || []).map( (item: { iconName: string; url: string }) => { return { name: item.iconName, icon: item.url, node: { ...ImageNode, data: { ...ImageNode.data, fill: { ...ImageNode.data.fill, fillType: "image", imageUrl: item.url, }, }, }, }; } ); if(paramRef.current.page === 1) { setIconList(list); } else { setIconList([...iconList, ...list]); } return res?.pages || {}; }; const { data, loading, run } = useRequest(searchServer, { manual: true, cacheKey: "iconsapi", cacheTime: 1000 * 60 * 60 * 24, }); const renderItem = (data: any, key: number) => { return ( startDrag(e, data.node)} /> ); }; const items = useMemo(() => { const list = [ { key: "1", label: "基础图形", children: ( {basic.map((item, index) => renderItem(item, index))} ), }, { key: "2", label: "Flowchart流程图", children: ( {flowchart.map((item, index) => renderItem(item, index))} ), }, { key: "3", label: "实体关系图(E-R图)", children: ( {er.map((item, index) => renderItem(item, index))} ), }, { key: "4", label: "泳池/泳道", children: ( {lane.map((item, index) => renderItem(item, index))} ), }, ]; if (showSearch) { list.unshift({ key: "search-icon", label: "网络图形", children: ( {iconList.map((item, index) => renderItem(item, index))}
{ if(!(data && data?.pageCount === data?.curPage)) { handleNextPage(data.curPage + 1); } }} > { loading ? '加载中...' : data && data?.pageCount === data?.curPage ? '暂无更多' : '加载更多' }
), }); if (!activeKeys.includes("search-icon")) { setActiveKeys((state) => [...state, "search-icon"]); } } if (showSearch && systemSearchResult.length) { list.unshift({ key: "search-system", label: "系统图形", children: ( {systemSearchResult.map((item, index) => renderItem(item, index))} ), }); if (!activeKeys.includes("search-system")) { setActiveKeys((state) => [...state, "search-system"]); } } return list; }, [systemSearchResult, showSearch, data, iconList, loading]); const handleChange = (keys: string | string[]) => { setActiveKeys(Array.isArray(keys) ? keys : [keys]); }; const handleSearch = () => { if (!search) return; const allData = [...basic, ...flowchart, ...er, ...lane]; setShowSearch(true); setSystemSearchResult(allData.filter((item) => item.name.includes(search))); paramRef.current = { ...paramRef.current, page: 1, query: search, }; run(); }; const handleNextPage = (page: number) => { paramRef.current = { ...paramRef.current, page, }; run(); } useEffect(() => { if (!search) { setActiveKeys(["1", "2", "3", "4"]); setShowSearch(false); setSystemSearchResult([]); paramRef.current = { ...paramRef.current, page: 1, query: "", }; setActiveKeys((state) => state.filter( (item) => !(item === "search-system" || item === "search-icon") ) ); } }, [search]); return (
} size="small" allowClear placeholder="请输入搜索内容" value={search} onChange={(e) => setSearch(e.target.value)} onPressEnter={handleSearch} />
); }