SentTab.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import { useEffect, useState } from "react";
  2. import { Badge, Space, Tabs, Typography } from "antd";
  3. import {
  4. TranslationOutlined,
  5. CloseOutlined,
  6. BlockOutlined,
  7. } from "@ant-design/icons";
  8. import SentTabButton from "./SentTabButton";
  9. import SentCanRead from "./SentCanRead";
  10. import SentSim from "./SentSim";
  11. import { useIntl } from "react-intl";
  12. import TocPath, { ITocPathNode } from "../../corpus/TocPath";
  13. import { IWbw } from "../Wbw/WbwWord";
  14. import RelaGraphic from "../Wbw/RelaGraphic";
  15. import SentMenu from "./SentMenu";
  16. import { ArticleMode } from "../../article/Article";
  17. import { IResNumber } from "../SentEdit";
  18. const { Text } = Typography;
  19. interface IWidget {
  20. id: string;
  21. book: number;
  22. para: number;
  23. wordStart: number;
  24. wordEnd: number;
  25. channelsId?: string[];
  26. path?: ITocPathNode[];
  27. layout?: "row" | "column";
  28. tranNum?: number;
  29. nissayaNum?: number;
  30. commNum?: number;
  31. originNum: number;
  32. simNum?: number;
  33. wbwData?: IWbw[];
  34. magicDictLoading?: boolean;
  35. compact?: boolean;
  36. mode?: ArticleMode;
  37. loadedRes?: IResNumber;
  38. onMagicDict?: Function;
  39. onCompact?: Function;
  40. onModeChange?: Function;
  41. }
  42. const SentTabWidget = ({
  43. id,
  44. book,
  45. para,
  46. wordStart,
  47. wordEnd,
  48. channelsId,
  49. path,
  50. tranNum = 0,
  51. nissayaNum = 0,
  52. commNum = 0,
  53. originNum,
  54. simNum = 0,
  55. wbwData,
  56. magicDictLoading = false,
  57. compact = false,
  58. mode,
  59. loadedRes,
  60. onMagicDict,
  61. onCompact,
  62. onModeChange,
  63. }: IWidget) => {
  64. const intl = useIntl();
  65. const [isCompact, setIsCompact] = useState(compact);
  66. const [hover, setHover] = useState(false);
  67. const [currKey, setCurrKey] = useState("close");
  68. const [currTranNum, setCurrTranNum] = useState(tranNum);
  69. const [currNissayaNum, setCurrNissayaNum] = useState(nissayaNum);
  70. const [currCommNum, setCurrCommNum] = useState(commNum);
  71. console.log("SentTabWidget render");
  72. useEffect(() => setIsCompact(compact), [compact]);
  73. const mPath = path
  74. ? [
  75. ...path,
  76. { book: book, paragraph: para, title: para.toString(), level: 100 },
  77. ]
  78. : [];
  79. if (typeof id === "undefined") {
  80. return <></>;
  81. }
  82. const sentId = id.split("_");
  83. const sId = sentId[0].split("-");
  84. const tabButtonStyle: React.CSSProperties | undefined = compact
  85. ? { visibility: hover || currKey !== "close" ? "visible" : "hidden" }
  86. : undefined;
  87. return (
  88. <Tabs
  89. onMouseEnter={() => setHover(true)}
  90. onMouseLeave={() => setHover(false)}
  91. activeKey={currKey}
  92. onChange={(activeKey: string) => {
  93. setCurrKey(activeKey);
  94. }}
  95. style={
  96. isCompact
  97. ? {
  98. position: currKey === "close" ? "absolute" : "unset",
  99. marginTop: -32,
  100. width: "100%",
  101. marginRight: 10,
  102. backgroundColor:
  103. hover || currKey !== "close" ? "#80808030" : "unset",
  104. }
  105. : {
  106. padding: "0 8px",
  107. backgroundColor: "#80808030",
  108. }
  109. }
  110. tabBarStyle={{ marginBottom: 0 }}
  111. size="small"
  112. tabBarGutter={0}
  113. tabBarExtraContent={
  114. <Space>
  115. <TocPath
  116. link="none"
  117. data={mPath}
  118. trigger={path ? path.length > 0 ? path[0].title : <></> : <></>}
  119. />
  120. <Text copyable={{ text: `{{${sentId[0]}}}` }}>{sentId[0]}</Text>
  121. <SentMenu
  122. book={book}
  123. para={para}
  124. loading={magicDictLoading}
  125. mode={mode}
  126. onMagicDict={(type: string) => {
  127. if (typeof onMagicDict !== "undefined") {
  128. onMagicDict(type);
  129. }
  130. }}
  131. onMenuClick={(key: string) => {
  132. switch (key) {
  133. case "compact" || "normal":
  134. if (typeof onCompact !== "undefined") {
  135. setIsCompact(true);
  136. onCompact(true);
  137. }
  138. break;
  139. case "normal":
  140. if (typeof onCompact !== "undefined") {
  141. setIsCompact(false);
  142. onCompact(false);
  143. }
  144. break;
  145. case "origin-edit":
  146. if (typeof onModeChange !== "undefined") {
  147. onModeChange("edit");
  148. }
  149. break;
  150. case "origin-wbw":
  151. if (typeof onModeChange !== "undefined") {
  152. onModeChange("wbw");
  153. }
  154. break;
  155. default:
  156. break;
  157. }
  158. }}
  159. />
  160. </Space>
  161. }
  162. items={[
  163. {
  164. label: (
  165. <span style={tabButtonStyle}>
  166. <Badge size="small" count={0}>
  167. <CloseOutlined />
  168. </Badge>
  169. </span>
  170. ),
  171. key: "close",
  172. children: <></>,
  173. },
  174. {
  175. label: (
  176. <SentTabButton
  177. style={tabButtonStyle}
  178. icon={<TranslationOutlined />}
  179. type="translation"
  180. sentId={id}
  181. count={
  182. currTranNum
  183. ? currTranNum -
  184. (loadedRes?.translation ? loadedRes.translation : 0)
  185. : undefined
  186. }
  187. title={intl.formatMessage({
  188. id: "channel.type.translation.label",
  189. })}
  190. />
  191. ),
  192. key: "translation",
  193. children: (
  194. <SentCanRead
  195. book={parseInt(sId[0])}
  196. para={parseInt(sId[1])}
  197. wordStart={parseInt(sId[2])}
  198. wordEnd={parseInt(sId[3])}
  199. type="translation"
  200. channelsId={channelsId}
  201. onCreate={() => setCurrTranNum((origin) => origin + 1)}
  202. />
  203. ),
  204. },
  205. {
  206. label: (
  207. <SentTabButton
  208. style={tabButtonStyle}
  209. icon={<CloseOutlined />}
  210. type="nissaya"
  211. sentId={id}
  212. count={
  213. currNissayaNum
  214. ? currNissayaNum -
  215. (loadedRes?.nissaya ? loadedRes.nissaya : 0)
  216. : undefined
  217. }
  218. title={intl.formatMessage({
  219. id: "channel.type.nissaya.label",
  220. })}
  221. />
  222. ),
  223. key: "nissaya",
  224. children: (
  225. <SentCanRead
  226. book={parseInt(sId[0])}
  227. para={parseInt(sId[1])}
  228. wordStart={parseInt(sId[2])}
  229. wordEnd={parseInt(sId[3])}
  230. type="nissaya"
  231. channelsId={channelsId}
  232. onCreate={() => setCurrNissayaNum((origin) => origin + 1)}
  233. />
  234. ),
  235. },
  236. {
  237. label: (
  238. <SentTabButton
  239. style={tabButtonStyle}
  240. icon={<TranslationOutlined />}
  241. type="commentary"
  242. sentId={id}
  243. count={
  244. currCommNum
  245. ? currCommNum -
  246. (loadedRes?.commentary ? loadedRes.commentary : 0)
  247. : undefined
  248. }
  249. title={intl.formatMessage({
  250. id: "channel.type.commentary.label",
  251. })}
  252. />
  253. ),
  254. key: "commentary",
  255. children: (
  256. <SentCanRead
  257. book={parseInt(sId[0])}
  258. para={parseInt(sId[1])}
  259. wordStart={parseInt(sId[2])}
  260. wordEnd={parseInt(sId[3])}
  261. type="commentary"
  262. channelsId={channelsId}
  263. onCreate={() => setCurrCommNum((origin) => origin + 1)}
  264. />
  265. ),
  266. },
  267. /*{
  268. label: (
  269. <SentTabButton
  270. icon={<BlockOutlined />}
  271. type="original"
  272. sentId={id}
  273. count={originNum}
  274. title={intl.formatMessage({
  275. id: "channel.type.original.label",
  276. })}
  277. />
  278. ),
  279. key: "original",
  280. children: (
  281. <SentCanRead
  282. book={parseInt(sId[0])}
  283. para={parseInt(sId[1])}
  284. wordStart={parseInt(sId[2])}
  285. wordEnd={parseInt(sId[3])}
  286. type="original"
  287. />
  288. ),
  289. },*/
  290. {
  291. label: (
  292. <SentTabButton
  293. style={tabButtonStyle}
  294. icon={<BlockOutlined />}
  295. type="original"
  296. sentId={id}
  297. count={simNum}
  298. title={intl.formatMessage({
  299. id: "buttons.sim",
  300. })}
  301. />
  302. ),
  303. key: "sim",
  304. children: (
  305. <SentSim
  306. book={parseInt(sId[0])}
  307. para={parseInt(sId[1])}
  308. wordStart={parseInt(sId[2])}
  309. wordEnd={parseInt(sId[3])}
  310. channelsId={channelsId}
  311. limit={5}
  312. />
  313. ),
  314. },
  315. {
  316. label: <span style={tabButtonStyle}>{"关系图"}</span>,
  317. key: "relation-graphic",
  318. children: <RelaGraphic wbwData={wbwData} />,
  319. },
  320. ]}
  321. />
  322. );
  323. };
  324. export default SentTabWidget;