import React, { useState, useEffect, useRef, useMemo } from "react"; import { Input, Button, ColorPicker, Select } from "antd"; import { FontSizeOutlined, BoldOutlined, LinkOutlined, GlobalOutlined, SwapOutlined, } from "@ant-design/icons"; import type { MindElixirInstance, NodeObj } from "mind-elixir"; import PostSelect from "../../models/post/PostSelect/PostSelect"; import { Lecture, PostType } from "@nice/common"; import { xmindColorPresets } from "./constant"; import { api } from "@nice/client"; import { env } from "@web/src/env"; interface NodeMenuProps { mind: MindElixirInstance; } //管理节点样式状态 const NodeMenu: React.FC = ({ mind }) => { const [isOpen, setIsOpen] = useState(false); const [selectedFontColor, setSelectedFontColor] = useState(""); const [selectedBgColor, setSelectedBgColor] = useState(""); const [selectedSize, setSelectedSize] = useState(""); const [isBold, setIsBold] = useState(false); const [urlMode, setUrlMode] = useState<"URL" | "POSTURL">("POSTURL"); const [url, setUrl] = useState(""); const [postId, setPostId] = useState(""); const containerRef = useRef(null); const { data: lecture, isLoading }: { data: Lecture; isLoading: boolean } = api.post.findFirst.useQuery( { where: { id: postId }, }, { enabled: !!postId } ); useEffect(() => { { if(lecture?.courseId && lecture?.id){ if (urlMode === "POSTURL"){ setUrl(`/course/${lecture?.courseId}/detail/${lecture?.id}`); } mind.reshapeNode(mind.currentNode, { hyperLink: `/course/${lecture?.courseId}/detail/${lecture?.id}`, }); } } }, [postId, lecture, isLoading, urlMode]); //监听思维导图节点选择事件,更新节点菜单状态 useEffect(() => { const handleSelectNode = (nodeObj: NodeObj) => { setIsOpen(true); const style = nodeObj.style || {}; setSelectedFontColor(style.color || ""); setSelectedBgColor(style.background || ""); setSelectedSize(style.fontSize || "24"); setIsBold(style.fontWeight === "bold"); setUrl(nodeObj.hyperLink || ""); }; const handleUnselectNode = () => { setIsOpen(false); }; mind.bus.addListener("selectNode", handleSelectNode); mind.bus.addListener("unselectNode", handleUnselectNode); }, [mind]); useEffect(() => { const handleSelectNode = (nodeObj: NodeObj) => { setIsOpen(true); const style = nodeObj.style || {}; setSelectedFontColor(style.color || ""); setSelectedBgColor(style.background || ""); setSelectedSize(style.fontSize || "24"); setIsBold(style.fontWeight === "bold"); setUrl(nodeObj.hyperLink || ""); }; const handleUnselectNode = () => { setIsOpen(false); }; mind.bus.addListener("selectNode", handleSelectNode); mind.bus.addListener("unselectNode", handleUnselectNode); }, [mind]); useEffect(() => { if (containerRef.current && mind.container) { mind.container.appendChild(containerRef.current); } }, [mind.container]); const handleColorChange = (type: "font" | "background", color: string) => { if (type === "font") { setSelectedFontColor(color); } else { setSelectedBgColor(color); } const patch = { style: {} as any }; if (type === "font") { patch.style.color = color; } else { patch.style.background = color; } mind.reshapeNode(mind.currentNode, patch); }; const handleSizeChange = (size: string) => { setSelectedSize(size); mind.reshapeNode(mind.currentNode, { style: { fontSize: size } }); }; const handleBoldToggle = () => { const fontWeight = isBold ? "" : "bold"; setIsBold(!isBold); mind.reshapeNode(mind.currentNode, { style: { fontWeight } }); }; const handleUrlChange = (e: React.ChangeEvent) => { const value = e.target.value; setUrl(value); mind.reshapeNode(mind.currentNode, { hyperLink: value, }); }; return (
{/* Font Size Selector */}

文字样式

} /> )} {urlMode === "URL" && url && !/^(https?:\/\/\S+|\/|\.\/|\.\.\/)?\S+$/.test(url) && (

请输入有效的URL地址

)}
); }; export default NodeMenu;