ArticleDrawer.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Button, Drawer, Space, Typography } from "antd";
  2. import React, { useEffect, useState } from "react";
  3. import { Link } from "react-router";
  4. import Article, { type ArticleMode, ArticleType } from "./Article";
  5. import type { IArticleDataResponse } from "../../api/Article";
  6. const { Text } = Typography;
  7. interface IWidget {
  8. trigger?: React.ReactNode;
  9. title?: string;
  10. type?: ArticleType;
  11. book?: string;
  12. para?: string;
  13. channelId?: string;
  14. articleId?: string;
  15. anthologyId?: string;
  16. mode?: ArticleMode;
  17. open?: boolean;
  18. onClose?: Function;
  19. onTitleChange?: Function;
  20. onArticleEdit?: Function;
  21. }
  22. const ArticleDrawerWidget = ({
  23. trigger,
  24. title,
  25. type,
  26. book,
  27. para,
  28. channelId,
  29. articleId,
  30. anthologyId,
  31. mode,
  32. open,
  33. onClose,
  34. onTitleChange,
  35. onArticleEdit,
  36. }: IWidget) => {
  37. const [openDrawer, setOpenDrawer] = useState(open);
  38. const [drawerTitle, setDrawerTitle] = useState(title);
  39. useEffect(() => setOpenDrawer(open), [open]);
  40. useEffect(() => setDrawerTitle(title), [title]);
  41. const showDrawer = () => {
  42. setOpenDrawer(true);
  43. };
  44. const onDrawerClose = () => {
  45. setOpenDrawer(false);
  46. if (document.getElementsByTagName("body")[0].hasAttribute("style")) {
  47. document.getElementsByTagName("body")[0].removeAttribute("style");
  48. }
  49. if (typeof onClose !== "undefined") {
  50. onClose();
  51. }
  52. };
  53. const getUrl = (openMode?: string): string => {
  54. let url = `/article/${type}/${articleId}?mode=`;
  55. url += openMode ? openMode : mode ? mode : "read";
  56. url += channelId ? `&channel=${channelId}` : "";
  57. url += book ? `&book=${book}` : "";
  58. url += para ? `&par=${para}` : "";
  59. return url;
  60. };
  61. return (
  62. <>
  63. <span onClick={() => showDrawer()}>{trigger}</span>
  64. <Drawer
  65. title={
  66. <Text
  67. editable={{
  68. onChange: (value: string) => {
  69. setDrawerTitle(value);
  70. if (typeof onTitleChange !== "undefined") {
  71. onTitleChange(value);
  72. }
  73. },
  74. }}
  75. >
  76. {drawerTitle}
  77. </Text>
  78. }
  79. width={1000}
  80. placement="right"
  81. onClose={onDrawerClose}
  82. open={openDrawer}
  83. destroyOnHidden={true}
  84. extra={
  85. <Space>
  86. <Button>
  87. <Link to={getUrl()}>在单页面中打开</Link>
  88. </Button>
  89. <Button>
  90. <Link to={getUrl("edit")}>翻译模式</Link>
  91. </Button>
  92. </Space>
  93. }
  94. >
  95. <Article
  96. active={true}
  97. type={type as ArticleType}
  98. book={book}
  99. para={para}
  100. channelId={channelId}
  101. articleId={articleId}
  102. anthologyId={anthologyId}
  103. mode={mode}
  104. onArticleEdit={(value: IArticleDataResponse) => {
  105. setDrawerTitle(value.title_text);
  106. if (typeof onArticleEdit !== "undefined") {
  107. onArticleEdit(value);
  108. }
  109. }}
  110. />
  111. </Drawer>
  112. </>
  113. );
  114. };
  115. export default ArticleDrawerWidget;