QuoteLink.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { Popover, Typography } from "antd";
  2. import { ArticleCtl, type TDisplayStyle } from "./Article"
  3. import { type IWidgetTermCtl, TermCtl } from "./Term"
  4. import { useEffect, useState } from "react";
  5. const { Text } = Typography;
  6. interface IWidgetQuoteLinkCtl {
  7. type: string;
  8. bookName?: string;
  9. volume?: number;
  10. page?: number;
  11. style: TDisplayStyle;
  12. book?: number;
  13. para?: number;
  14. term?: IWidgetTermCtl;
  15. title?: string;
  16. chapter?: string;
  17. found: boolean;
  18. }
  19. const QuoteLinkCtl = ({
  20. type,
  21. bookName,
  22. volume,
  23. page,
  24. style,
  25. book,
  26. para,
  27. term,
  28. title,
  29. chapter,
  30. found,
  31. }: IWidgetQuoteLinkCtl) => {
  32. const [tpl, setTpl] = useState<string>();
  33. let textShow = volume === 0 ? ` ${page}` : ` ${volume}.${page}`;
  34. if (type === "c") {
  35. textShow = ` ${chapter}`;
  36. }
  37. console.debug("found", found);
  38. useEffect(() => {
  39. if (type === "c") {
  40. setTpl(`{{ql|type=${type}|book=${book}|para=${para}}}`);
  41. }
  42. }, [book, para, type]);
  43. useEffect(() => {
  44. if (
  45. typeof type !== "undefined" &&
  46. typeof bookName !== "undefined" &&
  47. typeof volume !== "undefined" &&
  48. typeof page !== "undefined"
  49. ) {
  50. setTpl(
  51. `{{ql|type=${type}|bookname=${bookName}|volume=${volume}|page=${page}}}`
  52. );
  53. }
  54. }, [bookName, found, page, type, volume]);
  55. return (
  56. <>
  57. {found ? (
  58. <ArticleCtl
  59. book={book?.toString()}
  60. paragraphs={para?.toString()}
  61. title={
  62. title ? (
  63. title
  64. ) : (
  65. <>
  66. <TermCtl {...term} compact={true} /> {textShow}
  67. </>
  68. )
  69. }
  70. type={type === "c" ? "para" : "page"}
  71. focus={book && para ? `${book}-${para}` : undefined}
  72. id={
  73. type === "c"
  74. ? `${book}-${para}`
  75. : `${type}_${bookName}_${volume}_${page}`
  76. }
  77. style={style}
  78. modalExtra={
  79. <Text style={{ marginRight: 8 }} copyable={{ text: tpl }}>
  80. 复制模版
  81. </Text>
  82. }
  83. />
  84. ) : (
  85. <Popover
  86. placement="top"
  87. arrowPointAtCenter
  88. content={
  89. <>
  90. <TermCtl {...term} compact={true} />{" "}
  91. <Text copyable={{ text: tpl }}>{page}</Text>
  92. </>
  93. }
  94. trigger="hover"
  95. >
  96. <Text>
  97. {title ? (
  98. title
  99. ) : (
  100. <>
  101. <TermCtl {...term} compact={true} /> {textShow}
  102. </>
  103. )}
  104. </Text>
  105. </Popover>
  106. )}
  107. </>
  108. );
  109. };
  110. interface IWidget {
  111. props: string;
  112. }
  113. const Widget = ({ props }: IWidget) => {
  114. const prop = JSON.parse(atob(props)) as IWidgetQuoteLinkCtl;
  115. console.debug("QuoteLink", prop);
  116. return (
  117. <>
  118. <QuoteLinkCtl {...prop} />
  119. </>
  120. );
  121. };
  122. export default Widget;