ToolButtonToc.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { MenuOutlined } from "@ant-design/icons";
  2. import type { Key } from "antd/lib/table/interface"
  3. import AnthologyTocTree from "../anthology/AnthologyTocTree";
  4. import type { ArticleType } from "./Article"
  5. import PaliTextToc from "./PaliTextToc";
  6. import ToolButton from "./ToolButton";
  7. import TextBookToc from "../anthology/TextBookToc";
  8. import { useIntl } from "react-intl";
  9. interface IWidget {
  10. type?: ArticleType;
  11. articleId?: string;
  12. anthologyId?: string | null;
  13. courseId?: string | null;
  14. channels?: string[];
  15. onSelect?: Function;
  16. }
  17. const ToolButtonTocWidget = ({
  18. type,
  19. articleId,
  20. anthologyId,
  21. courseId,
  22. channels,
  23. onSelect,
  24. }: IWidget) => {
  25. const intl = useIntl();
  26. //TODO 都放return里面
  27. let tocWidget = <></>;
  28. if (type === "chapter" || type === "para") {
  29. if (articleId) {
  30. const sentId = articleId.split("-");
  31. if (sentId.length > 1) {
  32. tocWidget = (
  33. <PaliTextToc
  34. book={parseInt(sentId[0])}
  35. para={parseInt(sentId[1])}
  36. onSelect={(selectedKeys: Key[]) => {
  37. if (typeof onSelect !== "undefined" && selectedKeys.length > 0) {
  38. onSelect(selectedKeys[0]);
  39. }
  40. }}
  41. />
  42. );
  43. }
  44. }
  45. } else if (type === "article") {
  46. if (anthologyId) {
  47. tocWidget = (
  48. <AnthologyTocTree
  49. anthologyId={anthologyId}
  50. channels={channels}
  51. onClick={(_anthology: string, article: string, target: string) => {
  52. if (typeof onSelect !== "undefined") {
  53. onSelect(article, target);
  54. }
  55. }}
  56. />
  57. );
  58. }
  59. } else if (type === "textbook") {
  60. tocWidget = (
  61. <TextBookToc
  62. courseId={courseId}
  63. channels={channels}
  64. onClick={(article: string, target: string) => {
  65. console.debug("TextBookToc onClick", article);
  66. if (typeof onSelect !== "undefined") {
  67. onSelect(article, target);
  68. }
  69. }}
  70. />
  71. );
  72. }
  73. return (
  74. <ToolButton
  75. title={intl.formatMessage({
  76. id: "labels.table-of-content",
  77. })}
  78. icon={<MenuOutlined />}
  79. content={tocWidget}
  80. />
  81. );
  82. };
  83. export default ToolButtonTocWidget;