index.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import React, { useMemo, useState } from "react";
  2. import { Button, Input, Dropdown, Tooltip, MenuProps, Divider } from "antd";
  3. import { LeftOutlined, MenuOutlined } from "@ant-design/icons";
  4. import logo from "@/assets/logo.png";
  5. import { useModel, Icon } from "umi";
  6. import { addTopic } from "../../mindMap";
  7. import { TopicType } from "@/enum";
  8. import { selectTopic, addBorder, addSummary } from "@/utils/mindmapHander";
  9. export default function index() {
  10. const {
  11. mindProjectInfo,
  12. setMindProjectInfo,
  13. canRedo,
  14. canUndo,
  15. onRedo,
  16. onUndo,
  17. enableFormatBrush,
  18. toggleFormatBrush,
  19. graph,
  20. selectedCell,
  21. setCorrelationEdgeInfo,
  22. } = useModel("mindMapModel");
  23. const currentNode = useMemo(() => {
  24. return selectedCell.filter(cell => cell.isNode());
  25. }, [selectedCell]);
  26. const [title, setTitle] = useState<string>(mindProjectInfo?.name || "");
  27. const handleChangeTitle = () => {
  28. mindProjectInfo && setMindProjectInfo({
  29. ...mindProjectInfo,
  30. name: title,
  31. })
  32. }
  33. // 创建新作品 todo
  34. const createNew = (type: string) => {};
  35. // 预览 todo
  36. const handlePreview = () => {};
  37. // 保存 todo
  38. const handleSave = () => {};
  39. // 克隆 todo
  40. const handleClone = () => {};
  41. // 历史记录 todo
  42. const handleHistory = () => {};
  43. // 查找替换
  44. const handleReplace = () => {};
  45. // 增加子主题
  46. const handleAddSubTopic = () => {
  47. if(!currentNode.length) return;
  48. const topic = addTopic(
  49. currentNode[0].data.type === TopicType.main ? TopicType.branch : TopicType.sub,
  50. setMindProjectInfo,
  51. currentNode[0]
  52. );
  53. graph && selectTopic(graph, topic);
  54. };
  55. // 添加边框
  56. const handleAddBorder = () => {
  57. addBorder(currentNode.filter(item => item.data.parentId));
  58. }
  59. // 添加概要
  60. const handleAddSummary = () => {
  61. addSummary(currentNode.filter(item => item.data.parentId))
  62. }
  63. // 添加关联线
  64. const handleAddCorrelation = () => {
  65. if(!currentNode.length) return;
  66. setCorrelationEdgeInfo(currentNode[0]);
  67. };
  68. const menuData: MenuProps["items"] = [
  69. {
  70. key: "1",
  71. label: "新建",
  72. icon: <i className="w-20px iconfont icon-zengjiahuamian-1" />,
  73. children: [
  74. {
  75. key: "1-1-1",
  76. label: "流程图",
  77. icon: <Icon width="20" className="mt-5px" icon="local:flow" />,
  78. onClick: () => {
  79. createNew("flow");
  80. },
  81. },
  82. {
  83. key: "1-1-2",
  84. label: "思维导图",
  85. icon: <Icon width="20" className="mt-5px" icon="local:mind" />,
  86. onClick: () => {
  87. createNew("mind");
  88. },
  89. },
  90. {
  91. key: "1-1-3",
  92. label: "UML",
  93. icon: <Icon width="20" className="mt-5px" icon="local:uml" />,
  94. onClick: () => {
  95. createNew("uml");
  96. },
  97. },
  98. {
  99. key: "1-1-4",
  100. label: "网络拓扑图",
  101. icon: <Icon width="20" className="mt-5px" icon="local:net" />,
  102. onClick: () => {
  103. createNew("net");
  104. },
  105. },
  106. {
  107. key: "1-1-5",
  108. label: "组织结构图",
  109. icon: <Icon width="20" className="mt-5px" icon="local:flow" />,
  110. onClick: () => {
  111. createNew("org");
  112. },
  113. },
  114. {
  115. key: "1-1-6",
  116. label: "BPMN",
  117. icon: <Icon width="20" className="mt-5px" icon="local:bpmn" />,
  118. onClick: () => {
  119. createNew("bpmn");
  120. },
  121. },
  122. ],
  123. },
  124. {
  125. key: "2",
  126. label: "预览",
  127. icon: <i className="w-20px iconfont icon-yulan" />,
  128. onClick: handlePreview,
  129. },
  130. {
  131. key: "3",
  132. label: (
  133. <div className="flex justify-between">
  134. <span>保存</span>
  135. <span>Ctrl+S</span>
  136. </div>
  137. ),
  138. icon: <i className="w-20px iconfont icon-shangchuan" />,
  139. onClick: handleSave,
  140. },
  141. {
  142. key: "4",
  143. label: "克隆",
  144. icon: <i className="w-20px iconfont icon-niantie-1" />,
  145. onClick: handleClone,
  146. },
  147. {
  148. key: "5",
  149. label: "查找替换",
  150. icon: <i className="w-20px iconfont icon-lishijilu" />,
  151. onClick: handleReplace,
  152. },
  153. {
  154. key: "6",
  155. label: "历史记录",
  156. icon: <i className="w-20px iconfont icon-lishijilu" />,
  157. onClick: handleHistory,
  158. },
  159. // {
  160. // key: "1-8",
  161. // type: "divider",
  162. // },
  163. ];
  164. const noParent = useMemo(() => {
  165. const nodes = selectedCell?.filter(cell => cell.isNode());
  166. return !!(nodes.length && !nodes.find(node => !node.data?.parentId));
  167. }, [selectedCell]);
  168. return (
  169. <div className="absolute absolute top-8px left-8px bg-white shadow-md flex items-center gap-4px rounded-4px px-8px">
  170. <Button type="text" icon={<LeftOutlined />}></Button>
  171. <Dropdown
  172. trigger={["hover"]}
  173. menu={{ items: menuData, style: { width: 200 } }}
  174. >
  175. <div className="flex items-center gap-4px">
  176. <img className="w-22px" src={logo} />
  177. <MenuOutlined />
  178. </div>
  179. </Dropdown>
  180. <div className="flex flex-col leading-32px">
  181. {mindProjectInfo && (
  182. <Input
  183. className="text-16px max-w-100px"
  184. variant="borderless"
  185. value={title}
  186. onChange={(e) =>
  187. setTitle(e.target.value)
  188. }
  189. onBlur={handleChangeTitle}
  190. onPressEnter={handleChangeTitle}
  191. />
  192. )}
  193. </div>
  194. <Tooltip placement="bottom" title="撤销">
  195. <Button
  196. type="text"
  197. icon={<i className="iconfont icon-undo"></i>}
  198. disabled={!canUndo}
  199. onClick={onUndo}
  200. />
  201. </Tooltip>
  202. <Tooltip placement="bottom" title="恢复">
  203. <Button
  204. type="text"
  205. icon={<i className="iconfont icon-redo"></i>}
  206. disabled={!canRedo}
  207. onClick={onRedo}
  208. />
  209. </Tooltip>
  210. <Tooltip
  211. placement="bottom"
  212. title={`格式刷${enableFormatBrush ? "生效中按ESC取消" : "(Ctrl+Shift+C)"}`}
  213. >
  214. <Button
  215. type="text"
  216. icon={<i className="iconfont icon-geshishua"></i>}
  217. disabled={!currentNode?.length}
  218. className={
  219. enableFormatBrush && currentNode?.length ? "active" : ""
  220. }
  221. onClick={() => graph && toggleFormatBrush(graph)}
  222. />
  223. </Tooltip>
  224. <Divider type="vertical" />
  225. <Tooltip placement="bottom" title="子主题">
  226. <Button
  227. type="text"
  228. icon={<i className="iconfont icon-zizhuti"></i>}
  229. disabled={!currentNode.length}
  230. onClick={handleAddSubTopic}
  231. />
  232. </Tooltip>
  233. <Tooltip placement="bottom" title="关联线">
  234. <Button
  235. type="text"
  236. icon={<i className="iconfont icon-guanlianxian"></i>}
  237. disabled={!currentNode.length}
  238. onClick={handleAddCorrelation}
  239. />
  240. </Tooltip>
  241. <Tooltip placement="bottom" title="概要">
  242. <Button
  243. type="text"
  244. icon={<i className="iconfont icon-summary-outline"></i>}
  245. disabled={!noParent}
  246. onClick={handleAddSummary}
  247. />
  248. </Tooltip>
  249. <Tooltip placement="bottom" title="外框">
  250. <Button
  251. type="text"
  252. icon={<i className="iconfont icon-waikuang"></i>}
  253. disabled={!noParent}
  254. onClick={handleAddBorder}
  255. />
  256. </Tooltip>
  257. </div>
  258. );
  259. }