SentCellEditable.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { useState } from "react";
  2. import { useIntl } from "react-intl";
  3. import { Button, message, Typography } from "antd";
  4. import { SaveOutlined } from "@ant-design/icons";
  5. import TextArea from "antd/lib/input/TextArea";
  6. import { post, put } from "../../../request";
  7. import {
  8. ISentencePrRequest,
  9. ISentencePrResponse,
  10. ISentenceRequest,
  11. ISentenceResponse,
  12. } from "../../api/Corpus";
  13. import { ISentence } from "../SentEdit";
  14. const { Text } = Typography;
  15. interface ISentCellEditable {
  16. data: ISentence;
  17. onDataChange?: Function;
  18. onClose?: Function;
  19. onCreate?: Function;
  20. isPr?: boolean;
  21. }
  22. const Widget = ({
  23. data,
  24. onDataChange,
  25. onClose,
  26. onCreate,
  27. isPr = false,
  28. }: ISentCellEditable) => {
  29. const intl = useIntl();
  30. const [value, setValue] = useState(data.content);
  31. const [saving, setSaving] = useState<boolean>(false);
  32. const savePr = () => {
  33. setSaving(true);
  34. post<ISentencePrRequest, ISentencePrResponse>(`/v2/sentpr`, {
  35. book: data.book,
  36. para: data.para,
  37. begin: data.wordStart,
  38. end: data.wordEnd,
  39. channel: data.channel.id,
  40. text: value,
  41. })
  42. .then((json) => {
  43. console.log(json);
  44. setSaving(false);
  45. if (json.ok) {
  46. message.success(intl.formatMessage({ id: "flashes.success" }));
  47. if (typeof onCreate !== "undefined") {
  48. onCreate();
  49. }
  50. } else {
  51. message.error(json.message);
  52. }
  53. })
  54. .catch((e) => {
  55. setSaving(false);
  56. console.error("catch", e);
  57. message.error(e.message);
  58. });
  59. };
  60. const save = () => {
  61. setSaving(true);
  62. put<ISentenceRequest, ISentenceResponse>(
  63. `/v2/sentence/${data.book}_${data.para}_${data.wordStart}_${data.wordEnd}_${data.channel.id}`,
  64. {
  65. book: data.book,
  66. para: data.para,
  67. wordStart: data.wordStart,
  68. wordEnd: data.wordEnd,
  69. channel: data.channel.id,
  70. content: value,
  71. }
  72. )
  73. .then((json) => {
  74. console.log(json);
  75. setSaving(false);
  76. if (json.ok) {
  77. message.success(intl.formatMessage({ id: "flashes.success" }));
  78. if (typeof onDataChange !== "undefined") {
  79. const newData: ISentence = {
  80. content: json.data.content,
  81. html: json.data.html,
  82. book: json.data.book,
  83. para: json.data.paragraph,
  84. wordStart: json.data.word_start,
  85. wordEnd: json.data.word_end,
  86. editor: json.data.editor,
  87. channel: json.data.channel,
  88. updateAt: json.data.updated_at,
  89. };
  90. onDataChange(newData);
  91. }
  92. } else {
  93. message.error(json.message);
  94. }
  95. })
  96. .catch((e) => {
  97. setSaving(false);
  98. console.error("catch", e);
  99. message.error(e.message);
  100. });
  101. };
  102. return (
  103. <div>
  104. <TextArea
  105. value={value}
  106. onChange={(e) => setValue(e.target.value)}
  107. placeholder="请输入"
  108. autoSize={{ minRows: 3, maxRows: 5 }}
  109. />
  110. <div style={{ display: "flex", justifyContent: "space-between" }}>
  111. <div>
  112. <span>
  113. <Text keyboard>esc</Text>=
  114. <Button
  115. size="small"
  116. type="link"
  117. onClick={(e) => {
  118. if (typeof onClose !== "undefined") {
  119. onClose(e);
  120. }
  121. }}
  122. >
  123. {intl.formatMessage({ id: "buttons.cancel" })}
  124. </Button>
  125. </span>
  126. <span>
  127. <Text keyboard>enter</Text>=
  128. <Button size="small" type="link">
  129. new line
  130. </Button>
  131. </span>
  132. </div>
  133. <div>
  134. <Text keyboard>Ctrl/⌘</Text>➕<Text keyboard>enter</Text>=
  135. <Button
  136. size="small"
  137. type="primary"
  138. icon={<SaveOutlined />}
  139. loading={saving}
  140. onClick={() => (isPr ? savePr() : save())}
  141. >
  142. {intl.formatMessage({ id: "buttons.save" })}
  143. </Button>
  144. </div>
  145. </div>
  146. </div>
  147. );
  148. };
  149. export default Widget;