SentEditMenu.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { Button, Dropdown, message } from "antd";
  2. import { useState } from "react";
  3. import {
  4. EditOutlined,
  5. CopyOutlined,
  6. MoreOutlined,
  7. FieldTimeOutlined,
  8. LinkOutlined,
  9. CommentOutlined,
  10. FileMarkdownOutlined,
  11. } from "@ant-design/icons";
  12. import type { MenuProps } from "antd";
  13. import { ISentence } from "../SentEdit";
  14. import SentHistoryModal from "../../corpus/SentHistoryModal";
  15. import { HandOutlinedIcon, JsonOutlinedIcon } from "../../../assets/icon";
  16. import { useIntl } from "react-intl";
  17. interface IWidget {
  18. data: ISentence;
  19. children?: React.ReactNode;
  20. onModeChange?: Function;
  21. onConvert?: Function;
  22. onMenuClick?: Function;
  23. }
  24. const SentEditMenuWidget = ({
  25. data,
  26. children,
  27. onModeChange,
  28. onConvert,
  29. onMenuClick,
  30. }: IWidget) => {
  31. const [isHover, setIsHover] = useState(false);
  32. const [timelineOpen, setTimelineOpen] = useState(false);
  33. const intl = useIntl();
  34. const onClick: MenuProps["onClick"] = (e) => {
  35. if (typeof onMenuClick !== "undefined") {
  36. onMenuClick(e.key);
  37. }
  38. switch (e.key) {
  39. case "json":
  40. if (typeof onConvert !== "undefined") {
  41. onConvert("json");
  42. }
  43. break;
  44. case "markdown":
  45. if (typeof onConvert !== "undefined") {
  46. onConvert("markdown");
  47. }
  48. break;
  49. case "timeline":
  50. setTimelineOpen(true);
  51. break;
  52. default:
  53. break;
  54. }
  55. };
  56. const items: MenuProps["items"] = [
  57. {
  58. key: "timeline",
  59. label: "时间线",
  60. icon: <FieldTimeOutlined />,
  61. },
  62. {
  63. type: "divider",
  64. },
  65. {
  66. key: "suggestion",
  67. label: "suggestion",
  68. icon: <HandOutlinedIcon />,
  69. },
  70. {
  71. key: "discussion",
  72. label: "discussion",
  73. icon: <CommentOutlined />,
  74. },
  75. {
  76. type: "divider",
  77. },
  78. {
  79. key: "markdown",
  80. label: "To Markdown",
  81. icon: <FileMarkdownOutlined />,
  82. disabled: data.contentType === "markdown",
  83. },
  84. {
  85. key: "json",
  86. label: "To Json",
  87. icon: <JsonOutlinedIcon />,
  88. disabled: data.contentType === "json",
  89. },
  90. {
  91. type: "divider",
  92. },
  93. {
  94. key: "copy-link",
  95. label: intl.formatMessage({
  96. id: "buttons.copy.link",
  97. }),
  98. icon: <LinkOutlined />,
  99. },
  100. ];
  101. return (
  102. <div
  103. onMouseEnter={() => {
  104. setIsHover(true);
  105. }}
  106. onMouseLeave={() => {
  107. setIsHover(false);
  108. }}
  109. >
  110. <SentHistoryModal
  111. open={timelineOpen}
  112. onClose={() => setTimelineOpen(false)}
  113. sentId={data.id}
  114. />
  115. <div
  116. style={{
  117. marginTop: 0,
  118. right: 30,
  119. position: "absolute",
  120. display: isHover ? "block" : "none",
  121. }}
  122. >
  123. <Button
  124. icon={<EditOutlined />}
  125. size="small"
  126. title="edit"
  127. onClick={() => {
  128. if (typeof onModeChange !== "undefined") {
  129. onModeChange("edit");
  130. }
  131. }}
  132. />
  133. <Button
  134. icon={<CopyOutlined />}
  135. size="small"
  136. onClick={() => {
  137. if (data.content) {
  138. navigator.clipboard.writeText(data.content).then(() => {
  139. message.success("已经拷贝到剪贴板");
  140. });
  141. } else {
  142. message.success("内容为空");
  143. }
  144. }}
  145. />
  146. <Dropdown menu={{ items, onClick }} placement="bottomRight">
  147. <Button icon={<MoreOutlined />} size="small" />
  148. </Dropdown>
  149. </div>
  150. {children}
  151. </div>
  152. );
  153. };
  154. export default SentEditMenuWidget;