Article.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { IArticleDataResponse } from "../api/Article";
  2. import TypeArticle from "./TypeArticle";
  3. import TypeAnthology from "./TypeAnthology";
  4. import TypeTerm from "./TypeTerm";
  5. import TypePali from "./TypePali";
  6. import "./article.css";
  7. import TypePage from "./TypePage";
  8. import TypeCSPara from "./TypeCSPara";
  9. import { ISearchParams } from "../../pages/library/article/show";
  10. import TypeCourse from "./TypeCourse";
  11. import { useEffect, useState } from "react";
  12. import { fullUrl } from "../../utils";
  13. import TypeSeries from "./TypeSeries";
  14. import DiscussionCount from "../discussion/DiscussionCount";
  15. export type ArticleMode = "read" | "edit" | "wbw";
  16. export type ArticleType =
  17. | "anthology"
  18. | "article"
  19. | "series"
  20. | "chapter"
  21. | "para"
  22. | "cs-para"
  23. | "sent"
  24. | "sim"
  25. | "page"
  26. | "textbook"
  27. | "exercise"
  28. | "exercise-list"
  29. | "sent-original"
  30. | "sent-commentary"
  31. | "sent-nissaya"
  32. | "sent-translation"
  33. | "term";
  34. /**
  35. * 每种article type 对应的路由参数
  36. * article/id?anthology=id&channel=id1,id2&mode=ArticleMode
  37. * chapter/book-para?channel=id1,id2&mode=ArticleMode
  38. * para/book?par=para1,para2&channel=id1,id2&mode=ArticleMode
  39. * cs-para/book-para?channel=id1,id2&mode=ArticleMode
  40. * sent/id?channel=id1,id2&mode=ArticleMode
  41. * sim/id?channel=id1,id2&mode=ArticleMode
  42. * textbook/articleId?course=id&mode=ArticleMode
  43. * exercise/articleId?course=id&exercise=id&username=name&mode=ArticleMode
  44. * exercise-list/articleId?course=id&exercise=id&mode=ArticleMode
  45. * sent-original/id
  46. */
  47. interface IWidget {
  48. type?: ArticleType;
  49. articleId?: string;
  50. mode?: ArticleMode | null;
  51. channelId?: string | null;
  52. parentChannels?: string[];
  53. book?: string | null;
  54. para?: string | null;
  55. anthologyId?: string | null;
  56. courseId?: string | null;
  57. active?: boolean;
  58. focus?: string | null;
  59. hideInteractive?: boolean;
  60. hideTitle?: boolean;
  61. isSubWindow?: boolean;
  62. onArticleChange?: Function;
  63. onLoad?: Function;
  64. onAnthologySelect?: Function;
  65. onTitle?: Function;
  66. onArticleEdit?: Function;
  67. }
  68. const ArticleWidget = ({
  69. type,
  70. book,
  71. para,
  72. channelId,
  73. parentChannels,
  74. articleId,
  75. anthologyId,
  76. courseId,
  77. mode = "read",
  78. active = false,
  79. focus,
  80. hideInteractive = false,
  81. hideTitle = false,
  82. isSubWindow = false,
  83. onArticleChange,
  84. onLoad,
  85. onAnthologySelect,
  86. onTitle,
  87. onArticleEdit,
  88. }: IWidget) => {
  89. const [currId, setCurrId] = useState(articleId);
  90. useEffect(() => setCurrId(articleId), [articleId]);
  91. return (
  92. <div>
  93. <DiscussionCount courseId={type === "textbook" ? courseId : undefined} />
  94. {type === "article" ? (
  95. <TypeArticle
  96. isSubWindow={isSubWindow}
  97. type={type}
  98. articleId={onArticleChange ? articleId : currId}
  99. channelId={channelId}
  100. parentChannels={parentChannels}
  101. mode={mode}
  102. anthologyId={anthologyId}
  103. active={active}
  104. hideInteractive={hideInteractive}
  105. hideTitle={hideTitle}
  106. onArticleEdit={(value: IArticleDataResponse) => {
  107. if (typeof onArticleEdit !== "undefined") {
  108. onArticleEdit(value);
  109. }
  110. }}
  111. onArticleChange={onArticleChange}
  112. onLoad={(data: IArticleDataResponse) => {
  113. if (typeof onLoad !== "undefined") {
  114. onLoad(data);
  115. }
  116. if (typeof onTitle !== "undefined") {
  117. onTitle(data.title);
  118. }
  119. }}
  120. onAnthologySelect={(id: string) => {
  121. if (typeof onAnthologySelect !== "undefined") {
  122. onAnthologySelect(id);
  123. }
  124. }}
  125. />
  126. ) : type === "anthology" ? (
  127. <TypeAnthology
  128. type={type}
  129. articleId={onArticleChange ? articleId : currId}
  130. channelId={channelId}
  131. mode={mode}
  132. onArticleChange={(type: ArticleType, id: string, target: string) => {
  133. if (typeof onArticleChange !== "undefined") {
  134. onArticleChange(type, id, target);
  135. }
  136. }}
  137. onTitle={(value: string) => {
  138. if (typeof onTitle !== "undefined") {
  139. onTitle(value);
  140. }
  141. }}
  142. />
  143. ) : type === "term" ? (
  144. <TypeTerm
  145. articleId={onArticleChange ? articleId : currId}
  146. channelId={channelId}
  147. mode={mode}
  148. onArticleChange={(type: ArticleType, id: string, target: string) => {
  149. if (typeof onArticleChange !== "undefined") {
  150. onArticleChange(type, id, target);
  151. }
  152. }}
  153. />
  154. ) : type === "chapter" || type === "para" ? (
  155. <TypePali
  156. type={type}
  157. articleId={onArticleChange ? articleId : currId}
  158. channelId={channelId}
  159. mode={mode}
  160. book={book}
  161. para={para}
  162. focus={focus}
  163. onArticleChange={(
  164. type: ArticleType,
  165. id: string,
  166. target: string,
  167. param: ISearchParams[]
  168. ) => {
  169. if (typeof onArticleChange !== "undefined") {
  170. onArticleChange(type, id, target, param);
  171. }
  172. }}
  173. onLoad={(data: IArticleDataResponse) => {
  174. if (typeof onLoad !== "undefined") {
  175. onLoad(data);
  176. }
  177. }}
  178. onTitle={(value: string) => {
  179. if (typeof onTitle !== "undefined") {
  180. onTitle(value);
  181. }
  182. }}
  183. />
  184. ) : type === "series" ? (
  185. <TypeSeries
  186. articleId={onArticleChange ? articleId : currId}
  187. channelId={channelId}
  188. onArticleChange={(
  189. type: ArticleType,
  190. id: string,
  191. target: string,
  192. param: ISearchParams[]
  193. ) => {
  194. if (typeof onArticleChange !== "undefined") {
  195. onArticleChange(type, id, target, param);
  196. }
  197. }}
  198. />
  199. ) : type === "page" ? (
  200. <TypePage
  201. articleId={onArticleChange ? articleId : currId}
  202. channelId={channelId}
  203. focus={focus}
  204. mode={mode}
  205. onArticleChange={(type: ArticleType, id: string, target: string) => {
  206. if (typeof onArticleChange !== "undefined") {
  207. onArticleChange(type, id, target);
  208. } else {
  209. if (target === "_blank") {
  210. let url = `/article/page/${id}?mode=${mode}`;
  211. if (channelId) {
  212. url += `&channel=${channelId}`;
  213. }
  214. window.open(fullUrl(url), "_blank");
  215. } else {
  216. setCurrId(id);
  217. }
  218. }
  219. }}
  220. />
  221. ) : type === "cs-para" ? (
  222. <TypeCSPara
  223. articleId={onArticleChange ? articleId : currId}
  224. channelId={channelId}
  225. mode={mode}
  226. onArticleChange={(type: ArticleType, id: string, target: string) => {
  227. if (typeof onArticleChange !== "undefined") {
  228. onArticleChange(type, id, target);
  229. }
  230. }}
  231. />
  232. ) : type === "textbook" ? (
  233. <TypeCourse
  234. type={type}
  235. articleId={onArticleChange ? articleId : currId}
  236. channelId={channelId}
  237. courseId={courseId}
  238. mode={mode}
  239. onArticleChange={onArticleChange}
  240. />
  241. ) : (
  242. <></>
  243. )}
  244. </div>
  245. );
  246. };
  247. export default ArticleWidget;