MyCreate.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { Button, Col, Divider, Input, message, Row } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { useIntl } from "react-intl";
  4. import { SaveOutlined } from "@ant-design/icons";
  5. import WbwDetailBasic from "../template/Wbw/WbwDetailBasic";
  6. import WbwDetailNote from "../template/Wbw/WbwDetailNote";
  7. import { IWbw, IWbwField, TFieldName } from "../template/Wbw/WbwWord";
  8. import { get, post } from "../../request";
  9. import {
  10. IApiResponseDictList,
  11. IDictRequest,
  12. IDictResponse,
  13. IUserDictCreate,
  14. } from "../api/Dict";
  15. import { useAppSelector } from "../../hooks";
  16. import { add, updateIndex, wordIndex } from "../../reducers/inline-dict";
  17. import store from "../../store";
  18. import { get as getUiLang } from "../../locales";
  19. export const UserWbwPost = (data: IDictRequest[], view: string) => {
  20. let wordData: IDictRequest[] = data;
  21. data.forEach((value: IDictRequest) => {
  22. if (value.parent && value.type !== "") {
  23. if (!value.type?.includes("base") && value.type !== ".ind.") {
  24. let pFactors = "";
  25. let pFm;
  26. const orgFactors = value.factors?.split("+");
  27. if (
  28. orgFactors &&
  29. orgFactors.length > 0 &&
  30. orgFactors[orgFactors.length - 1].includes("[")
  31. ) {
  32. pFactors = orgFactors.slice(0, -1).join("+");
  33. pFm = value.factormean
  34. ?.split("+")
  35. .slice(0, orgFactors.length - 1)
  36. .join("+");
  37. }
  38. let grammar = value.grammar?.split("$").slice(0, 1).join("");
  39. if (value.type?.includes(".v")) {
  40. grammar = "";
  41. }
  42. wordData.push({
  43. word: value.parent,
  44. type: "." + value.type?.replaceAll(".", "") + ":base.",
  45. grammar: grammar,
  46. mean: value.mean,
  47. parent: value.parent2 ?? undefined,
  48. factors: pFactors,
  49. factormean: pFm,
  50. confidence: value.confidence,
  51. language: value.language,
  52. });
  53. }
  54. }
  55. if (value.factors && value.factors.split("+").length > 0) {
  56. const fm = value.factormean?.split("+");
  57. const factors: IDictRequest[] = [];
  58. value.factors.split("+").forEach((factor: string, index: number) => {
  59. const currWord = factor.replaceAll("-", "");
  60. console.debug("currWord", currWord);
  61. const meaning = fm ? fm[index].replaceAll("-", "") ?? null : null;
  62. if (meaning) {
  63. factors.push({
  64. word: currWord,
  65. type: ".part.",
  66. grammar: "",
  67. mean: meaning,
  68. confidence: value.confidence,
  69. language: value.language,
  70. });
  71. }
  72. const subFactorsMeaning: string[] = fm ? fm[index].split("-") : [];
  73. factor.split("-").forEach((subFactor, index1) => {
  74. if (subFactorsMeaning[index1] && subFactorsMeaning[index1] !== "") {
  75. factors.push({
  76. word: subFactor,
  77. type: ".part.",
  78. grammar: "",
  79. mean: subFactorsMeaning[index1],
  80. confidence: value.confidence,
  81. language: value.language,
  82. });
  83. }
  84. });
  85. });
  86. wordData = [...wordData, ...factors];
  87. }
  88. });
  89. return post<IUserDictCreate, IDictResponse>("/v2/userdict", {
  90. view: view,
  91. data: JSON.stringify(wordData),
  92. });
  93. };
  94. interface IWidget {
  95. word?: string;
  96. }
  97. const MyCreateWidget = ({ word }: IWidget) => {
  98. const intl = useIntl();
  99. const [wordSpell, setWordSpell] = useState(word);
  100. const [editWord, setEditWord] = useState<IWbw>({
  101. word: { value: word ? word : "", status: 7 },
  102. real: { value: word ? word : "", status: 7 },
  103. book: 0,
  104. para: 0,
  105. sn: [0],
  106. confidence: 100,
  107. });
  108. const [loading, setLoading] = useState(false);
  109. const inlineWordIndex = useAppSelector(wordIndex);
  110. useEffect(() => setWordSpell(word), [word]);
  111. useEffect(() => {
  112. //查询这个词在内存字典里是否有
  113. if (typeof wordSpell === "undefined") {
  114. return;
  115. }
  116. if (inlineWordIndex.includes(wordSpell)) {
  117. //已经有了,退出
  118. return;
  119. }
  120. get<IApiResponseDictList>(`/v2/wbwlookup?word=${wordSpell}`).then(
  121. (json) => {
  122. console.log("lookup ok", json.data.count);
  123. //存储到redux
  124. store.dispatch(add(json.data.rows));
  125. store.dispatch(updateIndex([wordSpell]));
  126. }
  127. );
  128. }, [inlineWordIndex, wordSpell]);
  129. function fieldChanged(field: TFieldName, value: string) {
  130. let mData: IWbw = JSON.parse(JSON.stringify(editWord));
  131. switch (field) {
  132. case "note":
  133. mData.note = { value: value, status: 7 };
  134. break;
  135. case "word":
  136. mData.word = { value: value, status: 7 };
  137. break;
  138. case "real":
  139. mData.real = { value: value, status: 7 };
  140. break;
  141. case "meaning":
  142. mData.meaning = { value: value, status: 7 };
  143. break;
  144. case "factors":
  145. mData.factors = { value: value, status: 7 };
  146. break;
  147. case "factorMeaning":
  148. mData.factorMeaning = { value: value, status: 7 };
  149. break;
  150. case "parent":
  151. mData.parent = { value: value, status: 7 };
  152. break;
  153. case "case":
  154. console.log("case", value);
  155. const _case = value.replaceAll("#", "$").split("$");
  156. const _type = _case[0];
  157. const _grammar = _case.slice(1).join("$");
  158. mData.type = { value: _type, status: 7 };
  159. mData.grammar = { value: _grammar, status: 7 };
  160. mData.case = { value: value, status: 7 };
  161. break;
  162. case "confidence":
  163. mData.confidence = parseFloat(value);
  164. break;
  165. default:
  166. break;
  167. }
  168. console.debug("field changed", mData);
  169. setEditWord(mData);
  170. }
  171. return (
  172. <div style={{ padding: "0 5px" }}>
  173. <Row>
  174. <Col
  175. span={4}
  176. style={{
  177. display: "inline-block",
  178. flexGrow: 0,
  179. overflow: "hidden",
  180. whiteSpace: "nowrap",
  181. textAlign: "right",
  182. verticalAlign: "middle",
  183. padding: 5,
  184. }}
  185. >
  186. 拼写
  187. </Col>
  188. <Col span={20}>
  189. <Input
  190. value={wordSpell}
  191. placeholder="Basic usage"
  192. onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
  193. console.debug("spell onChange", event.target.value);
  194. setWordSpell(event.target.value);
  195. fieldChanged("word", event.target.value);
  196. }}
  197. />
  198. </Col>
  199. </Row>
  200. <WbwDetailBasic
  201. data={editWord}
  202. showRelation={false}
  203. onChange={(e: IWbwField) => {
  204. console.log("WbwDetailBasic onchange", e);
  205. fieldChanged(e.field, e.value);
  206. }}
  207. />
  208. <Divider>{intl.formatMessage({ id: "buttons.note" })}</Divider>
  209. <WbwDetailNote
  210. data={editWord}
  211. onChange={(e: IWbwField) => {
  212. fieldChanged(e.field, e.value);
  213. }}
  214. />
  215. <Divider></Divider>
  216. <div
  217. style={{ display: "flex", justifyContent: "space-between", padding: 5 }}
  218. >
  219. <Button>重置</Button>
  220. <Button
  221. loading={loading}
  222. icon={<SaveOutlined />}
  223. onClick={() => {
  224. setLoading(true);
  225. const data: IDictRequest[] = [
  226. {
  227. word: editWord.word.value,
  228. type: editWord.type?.value,
  229. grammar: editWord.grammar?.value,
  230. mean: editWord.meaning?.value,
  231. parent: editWord.parent?.value,
  232. note: editWord.note?.value,
  233. factors: editWord.factors?.value,
  234. factormean: editWord.factorMeaning?.value,
  235. language: getUiLang(),
  236. confidence: 100,
  237. },
  238. ];
  239. UserWbwPost(data, "dict")
  240. .finally(() => {
  241. setLoading(false);
  242. })
  243. .then((json) => {
  244. if (json.ok) {
  245. message.success(
  246. intl.formatMessage({ id: "flashes.success" })
  247. );
  248. } else {
  249. message.error(json.message);
  250. }
  251. });
  252. }}
  253. type="primary"
  254. >
  255. {intl.formatMessage({ id: "buttons.save" })}
  256. </Button>
  257. </div>
  258. </div>
  259. );
  260. };
  261. export default MyCreateWidget;