DictInfoCopyRef.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { Button, message, Segmented, Typography } from "antd";
  2. import type { SegmentedValue } from "antd/lib/segmented"
  3. import { useState } from "react";
  4. import { CopyOutlined } from "@ant-design/icons";
  5. import type { IWordByDict } from "./WordCardByDict"
  6. import { useIntl } from "react-intl";
  7. const { Text } = Typography;
  8. interface IWidget {
  9. data: IWordByDict;
  10. }
  11. const DictInfoCopyRef = ({ data }: IWidget) => {
  12. const apaStr = `${data.meta?.author}. (${data.meta?.published}). ${data.dictname}. ${data.meta?.publisher}.`;
  13. const mlaStr = `${data.meta?.author}. ${data.dictname}. ${data.meta?.publisher}, ${data.meta?.published}.`;
  14. const [text, setText] = useState(apaStr);
  15. const intl = useIntl();
  16. return (
  17. <div>
  18. <div style={{ textAlign: "center", padding: 20 }}>
  19. <Segmented
  20. options={["APA", "MLA"]}
  21. onChange={(value: SegmentedValue) => {
  22. switch (value) {
  23. case "APA":
  24. setText(apaStr);
  25. break;
  26. case "MLA":
  27. setText(mlaStr);
  28. break;
  29. default:
  30. break;
  31. }
  32. }}
  33. />
  34. </div>
  35. <div>
  36. <Text>{text}</Text>
  37. </div>
  38. <div style={{ textAlign: "center", padding: 20 }}>
  39. <Button
  40. type="primary"
  41. style={{ width: 200 }}
  42. icon={<CopyOutlined />}
  43. onClick={() => {
  44. navigator.clipboard.writeText(text).then(() => {
  45. message.success("链接地址已经拷贝到剪贴板");
  46. });
  47. }}
  48. >
  49. {intl.formatMessage({
  50. id: "buttons.copy",
  51. })}
  52. </Button>
  53. </div>
  54. </div>
  55. );
  56. };
  57. export default DictInfoCopyRef;