Article.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import type { 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 type { 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. import TypeTask from "./TypeTask";
  16. interface IWidget {
  17. type?: ArticleType;
  18. articleId?: string;
  19. mode?: ArticleMode | null;
  20. channelId?: string | null;
  21. parentChannels?: string[];
  22. book?: string | null;
  23. para?: string | null;
  24. anthologyId?: string | null;
  25. courseId?: string | null;
  26. active?: boolean;
  27. focus?: string | null;
  28. hideInteractive?: boolean;
  29. hideTitle?: boolean;
  30. isSubWindow?: boolean;
  31. onArticleChange?: (
  32. type: ArticleType,
  33. id: string,
  34. target: string,
  35. param?: ISearchParams[]
  36. ) => void;
  37. onLoad?: Function;
  38. onAnthologySelect?: Function;
  39. onTitle?: Function;
  40. onArticleEdit?: Function;
  41. }
  42. const ArticleWidget = ({
  43. type,
  44. book,
  45. para,
  46. channelId,
  47. parentChannels,
  48. articleId,
  49. anthologyId,
  50. courseId,
  51. mode = "read",
  52. active = false,
  53. focus,
  54. hideInteractive = false,
  55. hideTitle = false,
  56. isSubWindow = false,
  57. onArticleChange,
  58. onLoad,
  59. onAnthologySelect,
  60. onTitle,
  61. onArticleEdit,
  62. }: IWidget) => {
  63. const [currId, setCurrId] = useState(articleId);
  64. useEffect(() => setCurrId(articleId), [articleId]);
  65. return (
  66. <div>
  67. <DiscussionCount courseId={type === "textbook" ? courseId : undefined} />
  68. {type === "article" ? (
  69. <TypeArticle
  70. isSubWindow={isSubWindow}
  71. type={type}
  72. articleId={onArticleChange ? articleId : currId}
  73. channelId={channelId}
  74. parentChannels={parentChannels}
  75. mode={mode}
  76. anthologyId={anthologyId}
  77. active={active}
  78. hideInteractive={hideInteractive}
  79. hideTitle={hideTitle}
  80. onArticleEdit={(value: IArticleDataResponse) => {
  81. if (typeof onArticleEdit !== "undefined") {
  82. onArticleEdit(value);
  83. }
  84. }}
  85. onArticleChange={onArticleChange}
  86. onLoad={(data: IArticleDataResponse) => {
  87. if (typeof onLoad !== "undefined") {
  88. onLoad(data);
  89. }
  90. if (typeof onTitle !== "undefined") {
  91. onTitle(data.title);
  92. }
  93. }}
  94. onAnthologySelect={(id: string) => {
  95. if (typeof onAnthologySelect !== "undefined") {
  96. onAnthologySelect(id);
  97. }
  98. }}
  99. />
  100. ) : type === "anthology" ? (
  101. <TypeAnthology
  102. type={type}
  103. articleId={onArticleChange ? articleId : currId}
  104. channelId={channelId}
  105. mode={mode}
  106. onArticleChange={(type: ArticleType, id: string, target: string) => {
  107. if (typeof onArticleChange !== "undefined") {
  108. onArticleChange(type, id, target);
  109. }
  110. }}
  111. onTitle={(value: string) => {
  112. if (typeof onTitle !== "undefined") {
  113. onTitle(value);
  114. }
  115. }}
  116. />
  117. ) : type === "term" ? (
  118. <TypeTerm
  119. articleId={onArticleChange ? articleId : currId}
  120. channelId={channelId}
  121. mode={mode}
  122. onArticleChange={(type: ArticleType, id: string, target: string) => {
  123. if (typeof onArticleChange !== "undefined") {
  124. onArticleChange(type, id, target);
  125. }
  126. }}
  127. />
  128. ) : type === "chapter" || type === "para" ? (
  129. <TypePali
  130. type={type}
  131. articleId={onArticleChange ? articleId : currId}
  132. channelId={channelId}
  133. mode={mode}
  134. book={book}
  135. para={para}
  136. focus={focus}
  137. onArticleChange={(
  138. type: ArticleType,
  139. id: string,
  140. target: string,
  141. param?: ISearchParams[]
  142. ) => {
  143. if (typeof onArticleChange !== "undefined") {
  144. onArticleChange(type, id, target, param);
  145. }
  146. }}
  147. onLoad={(data: IArticleDataResponse) => {
  148. if (typeof onLoad !== "undefined") {
  149. onLoad(data);
  150. }
  151. }}
  152. onTitle={(value: string) => {
  153. if (typeof onTitle !== "undefined") {
  154. onTitle(value);
  155. }
  156. }}
  157. />
  158. ) : type === "series" ? (
  159. <TypeSeries
  160. articleId={onArticleChange ? articleId : currId}
  161. channelId={channelId}
  162. onArticleChange={(
  163. type: ArticleType,
  164. id: string,
  165. target: string,
  166. param: ISearchParams[]
  167. ) => {
  168. if (typeof onArticleChange !== "undefined") {
  169. onArticleChange(type, id, target, param);
  170. }
  171. }}
  172. />
  173. ) : type === "page" ? (
  174. <TypePage
  175. articleId={onArticleChange ? articleId : currId}
  176. channelId={channelId}
  177. focus={focus}
  178. mode={mode}
  179. onArticleChange={(type: ArticleType, id: string, target: string) => {
  180. if (typeof onArticleChange !== "undefined") {
  181. onArticleChange(type, id, target);
  182. } else {
  183. if (target === "_blank") {
  184. let url = `/article/page/${id}?mode=${mode}`;
  185. if (channelId) {
  186. url += `&channel=${channelId}`;
  187. }
  188. window.open(fullUrl(url), "_blank");
  189. } else {
  190. setCurrId(id);
  191. }
  192. }
  193. }}
  194. />
  195. ) : type === "cs-para" ? (
  196. <TypeCSPara
  197. articleId={onArticleChange ? articleId : currId}
  198. channelId={channelId}
  199. mode={mode}
  200. onArticleChange={(type: ArticleType, id: string, target: string) => {
  201. if (typeof onArticleChange !== "undefined") {
  202. onArticleChange(type, id, target);
  203. }
  204. }}
  205. />
  206. ) : type === "textbook" ? (
  207. <TypeCourse
  208. type={type}
  209. articleId={onArticleChange ? articleId : currId}
  210. channelId={channelId}
  211. courseId={courseId}
  212. mode={mode}
  213. onArticleChange={onArticleChange}
  214. />
  215. ) : type === "task" ? (
  216. <TypeTask articleId={articleId} />
  217. ) : (
  218. <></>
  219. )}
  220. </div>
  221. );
  222. };
  223. export default ArticleWidget;