From 28d3fe8bb0a60402ed09ab3c208b577d0607324c Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 7 Mar 2021 16:48:51 +0800 Subject: ... --- .../app/views/timeline-common/TimelinePostEdit.tsx | 135 +++++++++++++++------ 1 file changed, 98 insertions(+), 37 deletions(-) (limited to 'FrontEnd/src') diff --git a/FrontEnd/src/app/views/timeline-common/TimelinePostEdit.tsx b/FrontEnd/src/app/views/timeline-common/TimelinePostEdit.tsx index 5bc5b166..efe5a6ff 100644 --- a/FrontEnd/src/app/views/timeline-common/TimelinePostEdit.tsx +++ b/FrontEnd/src/app/views/timeline-common/TimelinePostEdit.tsx @@ -18,12 +18,38 @@ import { base64 } from "@/http/common"; import BlobImage from "../common/BlobImage"; import LoadingButton from "../common/LoadingButton"; +interface TimelinePostEditTextProps { + text: string; + disabled: boolean; + onChange: (text: string) => void; + className?: string; + style?: React.CSSProperties; +} + +const TimelinePostEditText: React.FC = (props) => { + const { text, disabled, onChange, className, style } = props; + + return ( + { + onChange(event.target.value); + }} + className={className} + style={style} + /> + ); +}; + interface TimelinePostEditImageProps { onSelect: (file: File | null) => void; + disabled: boolean; } const TimelinePostEditImage: React.FC = (props) => { - const { onSelect } = props; + const { onSelect, disabled } = props; const { t } = useTranslation(); @@ -47,6 +73,7 @@ const TimelinePostEditImage: React.FC = (props) => { label={t("chooseImage")} onChange={onInputChange} accept="image/*" + disabled={disabled} className="mx-3 my-1 d-inline-block" /> {file != null && !error && ( @@ -78,15 +105,24 @@ const TimelinePostEdit: React.FC = (props) => { const { t } = useTranslation(); const [process, setProcess] = React.useState(false); - const [kind, setKind] = React.useState<"text" | "image">("text"); + + type PostKind = "text" | "markdown" | "image"; + + const [kind, setKind] = React.useState("text"); + const [text, setText] = React.useState(""); + const [markdown, setMarkdown] = React.useState(""); const [image, setImage] = React.useState(null); - const draftLocalStorageKey = `timeline.${timeline.name}.postDraft`; + const draftTextLocalStorageKey = `timeline.${timeline.name}.postDraft.text`; + const draftMarkdownLocalStorageKey = `timeline.${timeline.name}.postDraft.markdown`; React.useEffect(() => { - setText(window.localStorage.getItem(draftLocalStorageKey) ?? ""); - }, [draftLocalStorageKey]); + setText(window.localStorage.getItem(draftTextLocalStorageKey) ?? ""); + setMarkdown( + window.localStorage.getItem(draftMarkdownLocalStorageKey) ?? "" + ); + }, [draftTextLocalStorageKey, draftMarkdownLocalStorageKey]); const canSend = (kind === "text" && text.length !== 0) || @@ -102,9 +138,7 @@ const TimelinePostEdit: React.FC = (props) => { }; React.useEffect(() => { - if (onHeightChange) { - onHeightChange(containerRef.current.clientHeight); - } + notifyHeightChange(); return () => { if (onHeightChange) { onHeightChange(0); @@ -112,11 +146,6 @@ const TimelinePostEdit: React.FC = (props) => { }; }); - const toggleKind = React.useCallback(() => { - setKind((oldKind) => (oldKind === "text" ? "image" : "text")); - setImage(null); - }, []); - const onSend = async (): Promise => { setProcess(true); @@ -128,6 +157,12 @@ const TimelinePostEdit: React.FC = (props) => { data: await base64(new Blob([text])), }; break; + case "markdown": + requestData = { + contentType: "text/markdown", + data: await base64(new Blob([markdown])), + }; + break; case "image": if (image == null) { throw new UiLogicError( @@ -151,7 +186,10 @@ const TimelinePostEdit: React.FC = (props) => { (data) => { if (kind === "text") { setText(""); - window.localStorage.removeItem(draftLocalStorageKey); + window.localStorage.removeItem(draftTextLocalStorageKey); + } else if (kind === "markdown") { + setMarkdown(""); + window.localStorage.removeItem(draftMarkdownLocalStorageKey); } setProcess(false); setKind("text"); @@ -174,32 +212,55 @@ const TimelinePostEdit: React.FC = (props) => { > - {kind === "text" ? ( - ) => { - const value = event.currentTarget.value; - setText(value); - window.localStorage.setItem(draftLocalStorageKey, value); - }} - /> - ) : ( - - )} + {(() => { + if (kind === "text") { + return ( + { + setText(t); + window.localStorage.setItem(draftTextLocalStorageKey, t); + }} + /> + ); + } else if (kind === "image") { + return ( + + ); + } else if (kind === "markdown") { + return ( + { + setMarkdown(t); + window.localStorage.setItem( + draftMarkdownLocalStorageKey, + t + ); + }} + /> + ); + } + })()}
- + { + const { value } = event.currentTarget; + setKind(value as PostKind); + }} + > + + + +