VideoTpl.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { useEffect, useState } from "react";
  2. import { useIntl } from "react-intl";
  3. import {
  4. Button,
  5. Divider,
  6. Input,
  7. Modal,
  8. Select,
  9. Space,
  10. Tabs,
  11. Typography,
  12. } from "antd";
  13. import { FolderOpenOutlined } from "@ant-design/icons";
  14. import type { TDisplayStyle } from "../Article";
  15. import { VideoCtl } from "../Video";
  16. import AttachmentDialog from "../../attachment/AttachmentDialog";
  17. import type { IAttachmentRequest } from "../../../api/Attachments";
  18. const { TextArea } = Input;
  19. const { Paragraph } = Typography;
  20. interface IWidget {
  21. url?: string;
  22. title?: string;
  23. style?: TDisplayStyle;
  24. onSelect?: Function;
  25. onCancel?: Function;
  26. }
  27. const VideoTplWidget = ({ url, title, style = "modal" }: IWidget) => {
  28. const intl = useIntl(); //i18n
  29. const [currTitle, setCurrTitle] = useState(title);
  30. const [styleText, setStyleText] = useState(style);
  31. const [urlText, setUrlText] = useState(url);
  32. const [tplText, setTplText] = useState("");
  33. useEffect(() => {
  34. setCurrTitle(title);
  35. }, [title]);
  36. useEffect(() => {
  37. let tplText = `{{v|\n`;
  38. tplText += `url=${urlText}|\n`;
  39. tplText += `title=${currTitle}|\n`;
  40. tplText += `style=${styleText}`;
  41. tplText += "}}";
  42. setTplText(tplText);
  43. }, [currTitle, styleText, urlText]);
  44. return (
  45. <>
  46. <Space orientation="vertical" style={{ width: 500 }}>
  47. <AttachmentDialog
  48. trigger={<Button icon={<FolderOpenOutlined />}>网盘</Button>}
  49. onSelect={(value: IAttachmentRequest) => {
  50. console.debug("VideoTpl onSelect", value);
  51. setCurrTitle(value.title);
  52. setUrlText(value.url);
  53. }}
  54. />
  55. <Space style={{ width: 500 }}>
  56. {"标题:"}
  57. <Input
  58. width={400}
  59. value={currTitle}
  60. placeholder="Title"
  61. onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
  62. setCurrTitle(event.target.value);
  63. }}
  64. />
  65. </Space>
  66. <Space style={{ width: 500 }}>
  67. {"Url"}
  68. <Space>
  69. <Input
  70. defaultValue={url}
  71. width={400}
  72. value={urlText}
  73. placeholder="Url"
  74. onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
  75. setUrlText(event.target.value);
  76. }}
  77. />
  78. </Space>
  79. </Space>
  80. <Space>
  81. {"显示为:"}
  82. <Select
  83. defaultValue={style}
  84. style={{ width: 120 }}
  85. onChange={(value: string) => {
  86. console.log(`selected ${value}`);
  87. setStyleText(value as TDisplayStyle);
  88. }}
  89. options={["window", "modal", "card", "toggle", "link"].map(
  90. (item) => {
  91. return { value: item, label: item };
  92. }
  93. )}
  94. />
  95. </Space>
  96. <Tabs
  97. size="small"
  98. defaultActiveKey="preview"
  99. items={[
  100. {
  101. label: intl.formatMessage({
  102. id: "buttons.preview",
  103. }),
  104. key: "preview",
  105. children: (
  106. <VideoCtl url={urlText} title={currTitle} style={styleText} />
  107. ),
  108. },
  109. {
  110. label: `Code`,
  111. key: "code",
  112. children: (
  113. <TextArea
  114. value={tplText}
  115. rows={4}
  116. placeholder="maxLength is 6"
  117. maxLength={6}
  118. />
  119. ),
  120. },
  121. ]}
  122. />
  123. <Divider></Divider>
  124. <Paragraph copyable={{ text: tplText }}>复制到剪贴板</Paragraph>
  125. </Space>
  126. </>
  127. );
  128. };
  129. interface IModalWidget {
  130. url?: string;
  131. title?: string;
  132. style?: TDisplayStyle;
  133. trigger?: JSX.Element;
  134. }
  135. export const VideoTplModal = ({
  136. url,
  137. title,
  138. style = "modal",
  139. trigger,
  140. }: IModalWidget) => {
  141. const [isModalOpen, setIsModalOpen] = useState(false);
  142. const showModal = () => {
  143. setIsModalOpen(true);
  144. };
  145. const handleOk = () => {
  146. setIsModalOpen(false);
  147. };
  148. const handleCancel = () => {
  149. setIsModalOpen(false);
  150. };
  151. return (
  152. <>
  153. <span onClick={showModal}>{trigger}</span>
  154. <Modal
  155. width={"80%"}
  156. title="生成模版"
  157. open={isModalOpen}
  158. onOk={handleOk}
  159. onCancel={handleCancel}
  160. >
  161. <VideoTplWidget url={url} title={title} style={style} />
  162. </Modal>
  163. </>
  164. );
  165. };
  166. export default VideoTplWidget;