| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import { useEffect, useState } from "react";
- import { Input, Modal, Select, Space, Typography } from "antd";
- import { TDisplayStyle } from "../template/Article";
- const { TextArea } = Input;
- const { Paragraph } = Typography;
- interface IWidget {
- type?: string;
- id?: string;
- title?: string;
- style?: TDisplayStyle;
- trigger?: JSX.Element;
- onSelect?: Function;
- onCancel?: Function;
- }
- const ArticleTplMakerWidget = ({
- type,
- id,
- title,
- style = "modal",
- trigger,
- onSelect,
- onCancel,
- }: IWidget) => {
- const [isModalOpen, setIsModalOpen] = useState(false);
- const [titleText, setTitleText] = useState(title);
- const [styleText, setStyleText] = useState(style);
- const [tplText, setTplText] = useState("");
- const ids = id?.split("_");
- const id1 = ids ? ids[0] : undefined;
- const channels = ids
- ? ids.length > 1
- ? ids?.slice(1)
- : undefined
- : undefined;
- const showModal = () => {
- setIsModalOpen(true);
- };
- const handleOk = () => {
- setIsModalOpen(false);
- };
- const handleCancel = () => {
- setIsModalOpen(false);
- };
- useEffect(() => {
- setTitleText(title);
- }, [title]);
- useEffect(() => {
- let tplText = `{{article|
- type=${type}|
- id=${id1}|
- title=${titleText}|
- style=${styleText}`;
- tplText += channels ? `channel=${channels}` : undefined;
- tplText += "}}";
- setTplText(tplText);
- }, [titleText, styleText, type, id1, channels]);
- return (
- <>
- <span onClick={showModal}>{trigger}</span>
- <Modal
- width={"80%"}
- title="生成模版"
- open={isModalOpen}
- onOk={handleOk}
- onCancel={handleCancel}
- >
- <Space direction="vertical" style={{ width: 500 }}>
- <Space style={{ width: 500 }}>
- {"标题:"}
- <Input
- width={400}
- value={titleText}
- placeholder="Basic usage"
- onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
- setTitleText(event.target.value);
- }}
- />
- </Space>
- <Space>
- {"显示为:"}
- <Select
- defaultValue={style}
- style={{ width: 120 }}
- onChange={(value: string) => {
- console.log(`selected ${value}`);
- setStyleText(value as TDisplayStyle);
- }}
- options={[
- { value: "modal", label: "对话框" },
- { value: "card", label: "卡片" },
- ]}
- />
- </Space>
- <div>
- <TextArea
- value={tplText}
- rows={4}
- placeholder="maxLength is 6"
- maxLength={6}
- />
- <Paragraph copyable={{ text: tplText }}>复制</Paragraph>
- </div>
- </Space>
- </Modal>
- </>
- );
- };
- export default ArticleTplMakerWidget;
|