ArticleTplMaker.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { useEffect, useState } from "react";
  2. import { Input, Modal, Select, Space, Typography } from "antd";
  3. import { TDisplayStyle } from "../template/Article";
  4. const { TextArea } = Input;
  5. const { Paragraph } = Typography;
  6. interface IWidget {
  7. type?: string;
  8. id?: string;
  9. title?: string;
  10. style?: TDisplayStyle;
  11. trigger?: JSX.Element;
  12. onSelect?: Function;
  13. onCancel?: Function;
  14. }
  15. const ArticleTplMakerWidget = ({
  16. type,
  17. id,
  18. title,
  19. style = "modal",
  20. trigger,
  21. onSelect,
  22. onCancel,
  23. }: IWidget) => {
  24. const [isModalOpen, setIsModalOpen] = useState(false);
  25. const [titleText, setTitleText] = useState(title);
  26. const [styleText, setStyleText] = useState(style);
  27. const [tplText, setTplText] = useState("");
  28. const ids = id?.split("_");
  29. const id1 = ids ? ids[0] : undefined;
  30. const channels = ids
  31. ? ids.length > 1
  32. ? ids?.slice(1)
  33. : undefined
  34. : undefined;
  35. const showModal = () => {
  36. setIsModalOpen(true);
  37. };
  38. const handleOk = () => {
  39. setIsModalOpen(false);
  40. };
  41. const handleCancel = () => {
  42. setIsModalOpen(false);
  43. };
  44. useEffect(() => {
  45. setTitleText(title);
  46. }, [title]);
  47. useEffect(() => {
  48. let tplText = `{{article|
  49. type=${type}|
  50. id=${id1}|
  51. title=${titleText}|
  52. style=${styleText}`;
  53. tplText += channels ? `channel=${channels}` : undefined;
  54. tplText += "}}";
  55. setTplText(tplText);
  56. }, [titleText, styleText, type, id1, channels]);
  57. return (
  58. <>
  59. <span onClick={showModal}>{trigger}</span>
  60. <Modal
  61. width={"80%"}
  62. title="生成模版"
  63. open={isModalOpen}
  64. onOk={handleOk}
  65. onCancel={handleCancel}
  66. >
  67. <Space direction="vertical" style={{ width: 500 }}>
  68. <Space style={{ width: 500 }}>
  69. {"标题:"}
  70. <Input
  71. width={400}
  72. value={titleText}
  73. placeholder="Basic usage"
  74. onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
  75. setTitleText(event.target.value);
  76. }}
  77. />
  78. </Space>
  79. <Space>
  80. {"显示为:"}
  81. <Select
  82. defaultValue={style}
  83. style={{ width: 120 }}
  84. onChange={(value: string) => {
  85. console.log(`selected ${value}`);
  86. setStyleText(value as TDisplayStyle);
  87. }}
  88. options={[
  89. { value: "modal", label: "对话框" },
  90. { value: "card", label: "卡片" },
  91. ]}
  92. />
  93. </Space>
  94. <div>
  95. <TextArea
  96. value={tplText}
  97. rows={4}
  98. placeholder="maxLength is 6"
  99. maxLength={6}
  100. />
  101. <Paragraph copyable={{ text: tplText }}>复制</Paragraph>
  102. </div>
  103. </Space>
  104. </Modal>
  105. </>
  106. );
  107. };
  108. export default ArticleTplMakerWidget;