| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- import { useEffect, useState } from "react";
- import { useIntl } from "react-intl";
- import {
- Dropdown,
- Tabs,
- Divider,
- Button,
- Switch,
- Rate,
- Space,
- Tooltip,
- } from "antd";
- import {
- SaveOutlined,
- VerticalAlignBottomOutlined,
- VerticalAlignTopOutlined,
- } from "@ant-design/icons";
- import type { IWbw, IWbwAttachment, IWbwField, TFieldName } from "./WbwWord";
- import WbwDetailBasic from "./WbwDetailBasic";
- import WbwDetailBookMark from "./WbwDetailBookMark";
- import WbwDetailNote from "./WbwDetailNote";
- import WbwDetailAdvance from "./WbwDetailAdvance";
- import { LockIcon, UnLockIcon } from "../../../assets/icon";
- import type { IAttachmentRequest } from "../../../api/Attachments";
- import WbwDetailAttachment from "./WbwDetailAttachment";
- import { useAppSelector } from "../../../hooks";
- import { currentUser } from "../../../reducers/current-user";
- import DiscussionButton from "../../discussion/DiscussionButton";
- import { courseUser } from "../../../reducers/course-user";
- import { tempSet } from "../../../reducers/setting";
- import { PopPlacement } from "./WbwPali";
- import store from "../../../store";
- import TagSelectButton from "../../tag/TagSelectButton";
- import type { ITagMapData } from "../../../api/Tag";
- interface IWidget {
- data: IWbw;
- visible?: boolean;
- popIsTop?: boolean;
- readonly?: boolean;
- onClose?: Function;
- onSave?: Function;
- onAttachmentSelectOpen?: Function;
- onPopTopChange?: Function;
- onTagCreate?: Function;
- }
- const WbwDetailWidget = ({
- data,
- visible = true,
- popIsTop = false,
- readonly = false,
- onClose,
- onSave,
- onAttachmentSelectOpen,
- onPopTopChange,
- onTagCreate,
- }: IWidget) => {
- const intl = useIntl();
- const [currWbwData, setCurrWbwData] = useState<IWbw>(
- JSON.parse(JSON.stringify(data))
- );
- const [tabKey, setTabKey] = useState<string>("basic");
- const currUser = useAppSelector(currentUser);
- useEffect(() => {
- console.debug("input data", data);
- setCurrWbwData(JSON.parse(JSON.stringify(data)));
- }, [data]);
- function fieldChanged(field: TFieldName, value: string) {
- console.log("field", field, "value", value);
- const origin = JSON.parse(JSON.stringify(currWbwData));
- switch (field) {
- case "note":
- origin.note = { value: value, status: 7 };
- break;
- case "bookMarkColor":
- origin.bookMarkColor = { value: parseInt(value), status: 7 };
- break;
- case "bookMarkText":
- origin.bookMarkText = { value: value, status: 7 };
- break;
- case "word":
- origin.word = { value: value, status: 7 };
- break;
- case "real":
- origin.real = { value: value, status: 7 };
- break;
- case "meaning":
- origin.meaning = { value: value, status: 7 };
- break;
- case "factors":
- origin.factors = { value: value, status: 7 };
- break;
- case "factorMeaning":
- origin.factorMeaning = { value: value, status: 7 };
- break;
- case "parent":
- origin.parent = { value: value, status: 7 };
- break;
- case "parent2":
- origin.parent2 = { value: value, status: 7 };
- break;
- case "grammar2":
- origin.grammar2 = { value: value, status: 7 };
- break;
- case "case":
- const arrCase = value.split("#");
- origin.case = { value: value, status: 7 };
- origin.type = { value: arrCase[0] ? arrCase[0] : "", status: 7 };
- origin.grammar = { value: arrCase[1] ? arrCase[1] : "", status: 7 };
- break;
- case "relation":
- origin.relation = { value: value, status: 7 };
- break;
- case "confidence":
- origin.confidence = parseFloat(value);
- break;
- case "locked":
- origin.locked = JSON.parse(value);
- break;
- case "attachments":
- //mData.attachments = value;
- break;
- default:
- break;
- }
- console.debug("origin", origin);
- setCurrWbwData(origin);
- }
- const userInCourse = useAppSelector(courseUser);
- if (userInCourse && userInCourse.role === "student") {
- }
- return (
- <div
- className="wbw_detail"
- style={{
- minWidth: 450,
- }}
- >
- <Tabs
- size="small"
- type="card"
- tabBarExtraContent={
- <Space>
- <Tooltip
- title={popIsTop ? "底端弹窗" : "顶端弹窗"}
- getTooltipContainer={(_node: HTMLElement) =>
- document.getElementsByClassName("wbw_detail")[0] as HTMLElement
- }
- >
- <Button
- type="text"
- icon={
- popIsTop ? (
- <VerticalAlignBottomOutlined />
- ) : (
- <VerticalAlignTopOutlined />
- )
- }
- onClick={() => {
- store.dispatch(
- tempSet({
- key: PopPlacement,
- value: !popIsTop,
- })
- );
- if (typeof onPopTopChange !== "undefined") {
- //onPopTopChange(popIsTop);
- }
- }}
- />
- </Tooltip>
- <TagSelectButton
- selectorTitle={data.word.value}
- resType="wbw"
- resId={data.uid}
- onOpen={() => {
- if (typeof onClose !== "undefined") {
- onClose();
- }
- }}
- onCreate={(tags: ITagMapData[]) => {
- if (typeof onTagCreate !== "undefined") {
- onTagCreate(tags);
- }
- }}
- />
- <DiscussionButton
- initCount={data.hasComment ? 1 : 0}
- hideCount
- resId={data.uid}
- resType="wbw"
- />
- </Space>
- }
- onChange={(activeKey: string) => {
- setTabKey(activeKey);
- }}
- items={[
- {
- label: intl.formatMessage({ id: "buttons.basic" }),
- key: "basic",
- children: (
- <WbwDetailBasic
- visible={visible && tabKey === "basic"}
- data={currWbwData}
- readonly={readonly}
- onChange={(e: IWbwField) => {
- console.debug("WbwDetailBasic onchange", e);
- fieldChanged(e.field, e.value);
- }}
- onRelationAdd={onClose}
- />
- ),
- },
- {
- label: intl.formatMessage({ id: "buttons.bookmark" }),
- key: "bookmark",
- children: (
- <div style={{ minHeight: 270 }}>
- <WbwDetailBookMark
- data={data}
- onChange={(e: IWbwField) => {
- fieldChanged(e.field, e.value);
- }}
- />
- </div>
- ),
- },
- {
- label: intl.formatMessage({ id: "buttons.note" }),
- key: "note",
- children: (
- <div style={{ minHeight: 270 }}>
- <WbwDetailNote
- data={data}
- onChange={(e: IWbwField) => {
- fieldChanged(e.field, e.value);
- }}
- />
- </div>
- ),
- },
- {
- label: intl.formatMessage({ id: "buttons.spell" }),
- key: "spell",
- children: (
- <div style={{ minHeight: 270 }}>
- <WbwDetailAdvance
- data={currWbwData}
- onChange={(e: IWbwField) => {
- fieldChanged(e.field, e.value);
- }}
- />
- </div>
- ),
- },
- {
- label: intl.formatMessage({ id: "buttons.attachments" }),
- key: "attachments",
- children: (
- <div style={{ minHeight: 270 }}>
- <WbwDetailAttachment
- data={currWbwData}
- onUpload={(fileList: IAttachmentRequest[]) => {
- const mData = JSON.parse(JSON.stringify(currWbwData));
- mData.attachments = fileList.map((item) => {
- return {
- id: item.id,
- title: item.title,
- size: item.size ? item.size : 0,
- content_type: item.content_type,
- };
- });
- setCurrWbwData(mData);
- }}
- onDialogOpen={(open: boolean) => {
- if (typeof onAttachmentSelectOpen !== "undefined") {
- onAttachmentSelectOpen(open);
- }
- }}
- onChange={(value: IWbwAttachment[]) => {
- const mData = JSON.parse(JSON.stringify(currWbwData));
- mData.attachments = value;
- setCurrWbwData(mData);
- //fieldChanged(e.field, e.value);
- }}
- />
- </div>
- ),
- },
- ]}
- />
- <Divider style={{ margin: "4px 0" }}></Divider>
- <div style={{ display: "flex", justifyContent: "space-between" }}>
- <Switch
- checkedChildren={<LockIcon />}
- unCheckedChildren={<UnLockIcon />}
- />
- <div>
- {"信心指数"}
- <Rate
- defaultValue={data.confidence * 5}
- onChange={(value: number) => {
- fieldChanged("confidence", (value / 5).toString());
- }}
- />
- </div>
- </div>
- <Divider style={{ margin: "4px 0" }}></Divider>
- <div style={{ display: "flex", justifyContent: "space-between" }}>
- <div>
- <Button
- danger
- onClick={() => {
- if (typeof onClose !== "undefined") {
- onClose();
- }
- }}
- >
- {intl.formatMessage({ id: "buttons.cancel" })}
- </Button>
- </div>
- <Dropdown.Button
- disabled={readonly}
- style={{ width: "unset" }}
- type="primary"
- menu={{
- items: [
- {
- key: "user-dict-public",
- label: intl.formatMessage({ id: "buttons.save.publish" }),
- disabled: currUser?.roles?.includes("basic"),
- },
- {
- key: "user-dict-private",
- label: intl.formatMessage({ id: "buttons.save.my.dict" }),
- },
- ],
- onClick: (e) => {
- if (typeof onSave !== "undefined") {
- //保存并发布
- if (e.key === "user-dict-public") {
- onSave(currWbwData, true, true);
- } else {
- onSave(currWbwData, true, false);
- }
- }
- },
- }}
- onClick={() => {
- if (typeof onSave !== "undefined") {
- onSave(currWbwData, false);
- }
- }}
- >
- <SaveOutlined />
- {intl.formatMessage({ id: "buttons.save" })}
- </Dropdown.Button>
- </div>
- </div>
- );
- };
- export default WbwDetailWidget;
|