WbwDetailFm.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { useIntl } from "react-intl";
  2. import { Button, Dropdown, Input, Space, Tooltip } from "antd";
  3. import { useEffect, useState } from "react";
  4. import {
  5. MoreOutlined,
  6. PlusOutlined,
  7. EditOutlined,
  8. CheckOutlined,
  9. SearchOutlined,
  10. CloseOutlined,
  11. } from "@ant-design/icons";
  12. import { useAppSelector } from "../../../hooks";
  13. import { inlineDict as _inlineDict } from "../../../reducers/inline-dict";
  14. import store from "../../../store";
  15. import { lookup } from "../../../reducers/command";
  16. import { openPanel } from "../../../reducers/right-panel";
  17. import { ItemType } from "antd/lib/menu/hooks/useItems";
  18. import { MergeIcon } from "../../../assets/icon";
  19. interface IWFMI {
  20. pali: string;
  21. meaning?: string;
  22. readonly?: boolean;
  23. onChange?: Function;
  24. }
  25. const WbwFactorMeaningItem = ({
  26. pali,
  27. readonly = false,
  28. meaning = "",
  29. onChange,
  30. }: IWFMI) => {
  31. const intl = useIntl();
  32. console.debug("WbwFactorMeaningItem meaning", meaning);
  33. const defaultMenu: ItemType[] = [
  34. {
  35. key: "_lookup",
  36. label: (
  37. <Space>
  38. <SearchOutlined />
  39. {intl.formatMessage({
  40. id: "buttons.lookup",
  41. })}
  42. </Space>
  43. ),
  44. },
  45. {
  46. key: "_edit",
  47. label: (
  48. <Space>
  49. <EditOutlined />
  50. {intl.formatMessage({
  51. id: "buttons.edit",
  52. })}
  53. </Space>
  54. ),
  55. },
  56. { key: pali, label: pali },
  57. ];
  58. const [items, setItems] = useState<ItemType[]>(defaultMenu);
  59. const [input, setInput] = useState<string>();
  60. const [editable, setEditable] = useState(false);
  61. const inlineDict = useAppSelector(_inlineDict);
  62. useEffect(() => {
  63. if (inlineDict.wordIndex.includes(pali)) {
  64. const result = inlineDict.wordList.filter((word) => word.word === pali);
  65. //查重
  66. //TODO 加入信心指数并排序
  67. const myMap = new Map<string, number>();
  68. const meanings: string[] = [];
  69. for (const it of result) {
  70. if (typeof it.mean === "string") {
  71. for (const meaning of it.mean.split("$")) {
  72. if (meaning !== "") {
  73. myMap.set(meaning, 1);
  74. }
  75. }
  76. }
  77. }
  78. myMap.forEach((_value, key, _map) => {
  79. meanings.push(key);
  80. });
  81. const menu = meanings.map((item) => {
  82. return { key: item, label: item };
  83. });
  84. setItems([...defaultMenu, ...menu]);
  85. }
  86. }, [pali, inlineDict]);
  87. const inputOk = () => {
  88. setEditable(false);
  89. if (typeof onChange !== "undefined") {
  90. onChange(input);
  91. }
  92. };
  93. const inputCancel = () => {
  94. setEditable(false);
  95. setInput(meaning);
  96. };
  97. const meaningInner = editable ? (
  98. <Input
  99. defaultValue={meaning}
  100. size="small"
  101. addonAfter={
  102. <>
  103. <CheckOutlined
  104. style={{ cursor: "pointer", marginRight: 4 }}
  105. onClick={() => inputOk()}
  106. />
  107. <CloseOutlined
  108. style={{ cursor: "pointer" }}
  109. onClick={() => inputCancel()}
  110. />
  111. </>
  112. }
  113. placeholder="Basic usage"
  114. style={{ width: 160 }}
  115. onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
  116. setInput(event.target.value);
  117. }}
  118. onPressEnter={(_event: React.KeyboardEvent<HTMLInputElement>) => {
  119. inputOk();
  120. }}
  121. onKeyDown={(event: React.KeyboardEvent<HTMLInputElement>) => {
  122. if (event.key === "Escape") {
  123. setEditable(false);
  124. }
  125. }}
  126. />
  127. ) : (
  128. <Button
  129. disabled={readonly}
  130. key={1}
  131. size="small"
  132. type="text"
  133. icon={meaning === "" ? <MoreOutlined /> : undefined}
  134. onClick={() => {
  135. setEditable(true);
  136. }}
  137. >
  138. {meaning}
  139. </Button>
  140. );
  141. return editable || readonly ? (
  142. meaningInner
  143. ) : (
  144. <Dropdown
  145. menu={{
  146. items: [
  147. ...items.filter((_value, index) => index <= 5),
  148. {
  149. key: "more",
  150. label: intl.formatMessage({ id: "buttons.more" }),
  151. disabled: items.length <= 5,
  152. children: items.filter((_value, index) => index > 5),
  153. },
  154. ],
  155. onClick: (e) => {
  156. switch (e.key) {
  157. case "_lookup":
  158. store.dispatch(lookup(pali));
  159. store.dispatch(openPanel("dict"));
  160. break;
  161. case "_edit":
  162. setEditable(true);
  163. break;
  164. default:
  165. if (typeof onChange !== "undefined") {
  166. onChange(e.key);
  167. }
  168. break;
  169. }
  170. },
  171. }}
  172. placement="bottomLeft"
  173. trigger={["hover"]}
  174. >
  175. {meaningInner}
  176. </Dropdown>
  177. );
  178. };
  179. const resizeArray = (input: string[], factors: string[]) => {
  180. const newFm = factors.map((_item, index) => {
  181. if (index < input.length) {
  182. return input[index];
  183. } else {
  184. return "";
  185. }
  186. });
  187. return newFm;
  188. };
  189. interface IWidget {
  190. factors?: string[];
  191. value?: string[];
  192. readonly?: boolean;
  193. onChange?: Function;
  194. onJoin?: Function;
  195. }
  196. const WbwDetailFmWidget = ({
  197. factors = [],
  198. value = [],
  199. readonly = false,
  200. onChange,
  201. onJoin,
  202. }: IWidget) => {
  203. console.debug("WbwDetailFmWidget render");
  204. const [factorInputEnable, setFactorInputEnable] = useState(false);
  205. const currValue = resizeArray(value, factors);
  206. const combine = (input: string): string => {
  207. let meaning = "";
  208. input
  209. .split("-")
  210. .forEach((value: string, index: number, _array: string[]) => {
  211. if (index === 0) {
  212. meaning += value;
  213. } else {
  214. if (value.includes("~")) {
  215. meaning = value.replace("~", meaning);
  216. } else {
  217. meaning += value;
  218. }
  219. }
  220. });
  221. console.debug("combine", meaning);
  222. return meaning;
  223. };
  224. return (
  225. <div className="wbw_word_item" style={{ width: "100%" }}>
  226. <div style={{ display: "flex", width: "100%" }}>
  227. <Input
  228. key="input"
  229. allowClear
  230. hidden={!factorInputEnable}
  231. value={currValue.join("+")}
  232. placeholder="请输入"
  233. onChange={(e) => {
  234. console.log(e.target.value);
  235. const newData = resizeArray(e.target.value.split("+"), factors);
  236. if (typeof onChange !== "undefined") {
  237. onChange(newData);
  238. }
  239. }}
  240. />
  241. {factorInputEnable ? (
  242. <Button
  243. key="input-button"
  244. type="text"
  245. icon={<CheckOutlined />}
  246. onClick={() => setFactorInputEnable(false)}
  247. />
  248. ) : undefined}
  249. </div>
  250. {!factorInputEnable ? (
  251. <Space size={0} key="space">
  252. {currValue.map((item, index) => {
  253. const fm = item.split("-");
  254. return (
  255. <span key={index} style={{ display: "flex" }}>
  256. {factors[index]?.split("-").map((item1, index1) => {
  257. return (
  258. <WbwFactorMeaningItem
  259. readonly={readonly}
  260. key={index1}
  261. pali={item1}
  262. meaning={fm[index1]}
  263. onChange={(value: string) => {
  264. const newData = [...currValue];
  265. const currFm = resizeArray(
  266. currValue[index].split("-"),
  267. factors[index].split("-")
  268. );
  269. currFm.forEach(
  270. (_value3: string, index3: number, array: string[]) => {
  271. if (index3 === index1) {
  272. array[index3] = value;
  273. }
  274. }
  275. );
  276. newData[index] = currFm.join("-");
  277. if (typeof onChange !== "undefined") {
  278. onChange(newData);
  279. }
  280. }}
  281. />
  282. );
  283. })}
  284. {index < currValue.length - 1 ? (
  285. <PlusOutlined disabled={readonly} key={`icon-${index}`} />
  286. ) : (
  287. <>
  288. <Tooltip title="在文本框中编辑">
  289. <Button
  290. disabled={readonly}
  291. key="EditOutlined"
  292. size="small"
  293. type="text"
  294. icon={<EditOutlined />}
  295. onClick={() => setFactorInputEnable(true)}
  296. />
  297. </Tooltip>
  298. <Tooltip title="合并后替换含义">
  299. <Button
  300. disabled={readonly}
  301. key="CheckOutlined"
  302. size="small"
  303. type="text"
  304. icon={<MergeIcon />}
  305. onClick={() => {
  306. if (typeof onJoin !== "undefined") {
  307. const newMeaning = currValue
  308. .map((item) => {
  309. return item
  310. .replaceAll("[[", "/*")
  311. .replaceAll("]]", "*/");
  312. })
  313. .filter((value) => !value.includes("["))
  314. .map((item) => {
  315. return item
  316. .replaceAll("/*", "[[")
  317. .replaceAll("*/", "]]");
  318. })
  319. .map((item) => {
  320. return combine(item);
  321. })
  322. .join("");
  323. onJoin(newMeaning);
  324. }
  325. }}
  326. />
  327. </Tooltip>
  328. </>
  329. )}
  330. </span>
  331. );
  332. })}
  333. </Space>
  334. ) : undefined}
  335. </div>
  336. );
  337. };
  338. export default WbwDetailFmWidget;