| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- import { Button, Dropdown, type MenuProps, message, notification } from "antd"
- import { useNavigate, useSearchParams } from "react-router";
- import { fullUrl } from "../../utils";
- import { useIntl } from "react-intl";
- import { addToCart } from "./SentEdit/SentCart";
- import { scrollToTop } from "../../pages/library/article/show";
- import store from "../../store";
- import { modeChange } from "../../reducers/article-mode";
- interface IWidgetParaHandleCtl {
- book: number;
- para: number;
- mode?: string;
- channels?: string[];
- sentences: string[];
- onTranslate?: Function;
- }
- export const ParaHandleCtl = ({
- book,
- para,
- ___mode = "read",
- ___channels,
- sentences,
- onTranslate,
- }: IWidgetParaHandleCtl) => {
- const navigate = useNavigate();
- const [searchParams] = useSearchParams();
- const intl = useIntl();
- const items: MenuProps["items"] = [
- {
- key: "solo",
- label: intl.formatMessage({
- id: "labels.curr.paragraph.only",
- }),
- },
- {
- key: "solo-in-tab",
- label: intl.formatMessage({
- id: "labels.curr.paragraph.open",
- }),
- },
- {
- key: "ai-translate",
- label: intl.formatMessage({
- id: "buttons.ai.translate",
- }),
- },
- {
- type: "divider",
- },
- {
- key: "mode",
- label: intl.formatMessage({
- id: "buttons.set.display.mode",
- }),
- children: [
- {
- key: "mode-translate",
- label: intl.formatMessage({
- id: "buttons.translate",
- }),
- },
- {
- key: "mode-wbw",
- label: intl.formatMessage({
- id: "buttons.wbw",
- }),
- },
- ],
- },
- {
- type: "divider",
- },
- {
- key: "copy-sent",
- label: intl.formatMessage({
- id: "labels.curr.paragraph.copy.tpl",
- }),
- },
- {
- key: "cart-sent",
- label: intl.formatMessage({
- id: "labels.curr.paragraph.cart.tpl",
- }),
- },
- {
- key: "quote-link-tpl",
- label: intl.formatMessage({
- id: "labels.curr.paragraph.copy.quote.link.tpl",
- }),
- children: [
- {
- key: "quote-link-tpl-c",
- label: intl.formatMessage({
- id: "labels.page.number.type.c",
- }),
- },
- {
- key: "quote-link-tpl-m",
- label: intl.formatMessage({
- id: "labels.page.number.type.M",
- }),
- },
- {
- key: "quote-link-tpl-p",
- label: intl.formatMessage({
- id: "labels.page.number.type.P",
- }),
- },
- {
- key: "quote-link-tpl-t",
- label: intl.formatMessage({
- id: "labels.page.number.type.T",
- }),
- },
- ],
- },
- ];
- const copyToClipboard = (text: string) => {
- navigator.clipboard.writeText(text).then(() => {
- message.success("链接地址已经拷贝到剪贴板");
- });
- };
- const onClick: MenuProps["onClick"] = (e) => {
- /**
- * TODO 临时的解决方案。以后应该从传参获取其他参数,然后reducer 通知更新。
- * 因为如果是Article组件被嵌入其他页面。不能直接更新浏览器,而是应该更新Article组件内部
- */
- let url = `/article/para/${book}-${para}?book=${book}&par=${para}`;
- const param: string[] = [];
- searchParams.forEach((value: unknown, key: unknown) => {
- if (key !== "book" && key !== "par") {
- param.push(`${key}=${value}`);
- }
- });
- if (param.length > 0) {
- url += "&" + param.join("&");
- }
- switch (e.key) {
- case "solo":
- navigate(url);
- scrollToTop();
- break;
- case "solo-in-tab":
- window.open(fullUrl(url), "_blank");
- break;
- case "mode-translate":
- store.dispatch(modeChange({ mode: "edit", id: `${book}-${para}` }));
- break;
- case "ai-translate":
- if (typeof onTranslate !== "undefined") {
- onTranslate();
- }
- break;
- case "mode-wbw":
- store.dispatch(modeChange({ mode: "wbw", id: `${book}-${para}` }));
- break;
- case "copy-sent":
- copyToClipboard(sentences.map((item) => `{{${item}}}`).join(""));
- break;
- case "cart-sent":
- const cartData = sentences.map((item) => {
- return { id: `{{${item}}}`, text: `{{${item}}}` };
- });
- addToCart(cartData);
- notification.success({
- message: cartData.length + "个句子已经添加到Cart",
- });
- break;
- case "quote-link-tpl-c":
- copyToClipboard(`{{ql|type=c|book=${book}|para=${para}}}`);
- break;
- case "quote-link-tpl-m":
- copyToClipboard(`{{ql|type=m|book=${book}|para=${para}}}`);
- break;
- case "quote-link-tpl-p":
- copyToClipboard(`{{ql|type=p|book=${book}|para=${para}}}`);
- break;
- case "quote-link-tpl-t":
- copyToClipboard(`{{ql|type=t|book=${book}|para=${para}}}`);
- break;
- default:
- break;
- }
- };
- return (
- <div>
- <Dropdown
- menu={{ items, onClick }}
- placement="bottomLeft"
- trigger={["click"]}
- >
- <Button size="small" type="text">
- {para}
- </Button>
- </Dropdown>
- </div>
- );
- };
- interface IWidget {
- props: string;
- }
- const Widget = ({ props }: IWidget) => {
- const prop = JSON.parse(atob(props)) as IWidgetParaHandleCtl;
- return (
- <>
- <ParaHandleCtl {...prop} />
- </>
- );
- };
- export default Widget;
|