WordCount.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { Descriptions, Modal } from "antd";
  2. import { LoadingOutlined } from "@ant-design/icons";
  3. import { useEffect, useState } from "react";
  4. import { get } from "../../request";
  5. import type { IArticleResponse } from "../../api/Article";
  6. interface IWidget {
  7. open?: boolean;
  8. articleId?: string;
  9. onClose?: Function;
  10. }
  11. const WordCount = ({ open = false, articleId, onClose }: IWidget) => {
  12. const [loading, setLoading] = useState(false);
  13. const [wordAll, setWordAll] = useState(0);
  14. useEffect(() => {
  15. if (!articleId) {
  16. return;
  17. }
  18. const url = `/v2/article/${articleId}?format=text&origin=true`;
  19. console.info("url", url);
  20. setLoading(true);
  21. get<IArticleResponse>(url)
  22. .then((json) => {
  23. console.info("api response", json);
  24. if (json.ok) {
  25. if (json.data.html) {
  26. setWordAll(json.data.html.length);
  27. }
  28. }
  29. })
  30. .finally(() => setLoading(false));
  31. }, [articleId]);
  32. return (
  33. <Modal
  34. destroyOnHidden={true}
  35. width={700}
  36. title={"字数统计"}
  37. open={open}
  38. footer={false}
  39. onOk={() => {
  40. if (typeof onClose !== "undefined") {
  41. onClose();
  42. }
  43. }}
  44. onCancel={() => {
  45. if (typeof onClose !== "undefined") {
  46. onClose();
  47. }
  48. }}
  49. >
  50. {loading ? (
  51. <LoadingOutlined />
  52. ) : (
  53. <Descriptions title="字数">
  54. <Descriptions.Item label="全部字符">{wordAll}</Descriptions.Item>
  55. </Descriptions>
  56. )}
  57. </Modal>
  58. );
  59. };
  60. export default WordCount;