From 4475f2a89c2f2adf3fcc68bad4054f9710199875 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 12 Mar 2021 17:46:27 +0800 Subject: ... --- FrontEnd/src/app/locales/en/translation.json | 2 + FrontEnd/src/app/locales/zh/translation.json | 2 + FrontEnd/src/app/services/TimelinePostBuilder.ts | 6 +- .../app/views/timeline-common/MarkdownPostEdit.tsx | 116 +++++++++++++++++++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 FrontEnd/src/app/views/timeline-common/MarkdownPostEdit.tsx (limited to 'FrontEnd/src') diff --git a/FrontEnd/src/app/locales/en/translation.json b/FrontEnd/src/app/locales/en/translation.json index 4002ee4f..5613668e 100644 --- a/FrontEnd/src/app/locales/en/translation.json +++ b/FrontEnd/src/app/locales/en/translation.json @@ -2,7 +2,9 @@ "welcome": "Welcome!", "search": "Search", "edit": "Edit", + "image": "Image", "done": "Done", + "preview": "Preview", "loadFailReload": "Load failed, <1>click here to reload.", "error": { "network": "Network error.", diff --git a/FrontEnd/src/app/locales/zh/translation.json b/FrontEnd/src/app/locales/zh/translation.json index 3f966d7c..c73f2876 100644 --- a/FrontEnd/src/app/locales/zh/translation.json +++ b/FrontEnd/src/app/locales/zh/translation.json @@ -2,7 +2,9 @@ "welcome": "欢迎!", "search": "搜索", "edit": "编辑", + "image": "图片", "done": "完成", + "preview": "预览", "loadFailReload": "加载失败,<1>点击重试。", "error": { "network": "网络错误。", diff --git a/FrontEnd/src/app/services/TimelinePostBuilder.ts b/FrontEnd/src/app/services/TimelinePostBuilder.ts index 8594fa49..f6c8f881 100644 --- a/FrontEnd/src/app/services/TimelinePostBuilder.ts +++ b/FrontEnd/src/app/services/TimelinePostBuilder.ts @@ -10,7 +10,7 @@ import { UiLogicError } from "@/common"; import { base64 } from "@/http/common"; import { HttpTimelinePostPostRequest } from "@/http/timeline"; -export class TimelinePostBuilder { +export default class TimelinePostBuilder { private _onChange: () => void; private _text = ""; private _images: { file: File; url: string }[] = []; @@ -86,6 +86,10 @@ export class TimelinePostBuilder { this._onChange(); } + get text(): string { + return this._text; + } + get images(): { file: File; url: string }[] { return this._images; } diff --git a/FrontEnd/src/app/views/timeline-common/MarkdownPostEdit.tsx b/FrontEnd/src/app/views/timeline-common/MarkdownPostEdit.tsx new file mode 100644 index 00000000..079344e1 --- /dev/null +++ b/FrontEnd/src/app/views/timeline-common/MarkdownPostEdit.tsx @@ -0,0 +1,116 @@ +import React from "react"; +import { Nav, Form } from "react-bootstrap"; +import { useTranslation } from "react-i18next"; + +import { getHttpTimelineClient, HttpTimelinePostInfo } from "@/http/timeline"; + +import TimelinePostBuilder from "@/services/TimelinePostBuilder"; + +export interface MarkdownPostEditProps { + timeline: string; + onPosted: (post: HttpTimelinePostInfo) => void; +} + +const MarkdownPostEdit: React.FC = ({ + timeline: timelineName, + onPosted, +}) => { + const { t } = useTranslation(); + + const [tab, setTab] = React.useState<"text" | "images" | "preview">("text"); + + const [process, setProcess] = React.useState(false); + + const [text, _setText] = React.useState(""); + const [images, _setImages] = React.useState<{ file: File; url: string }[]>( + [] + ); + const [previewHtml, _setPreviewHtml] = React.useState(""); + + const _builder = React.useRef(null); + + const getBuilder = (): TimelinePostBuilder => { + if (_builder.current == null) { + const builder = new TimelinePostBuilder(() => { + _setText(builder.text); + _setImages(builder.images); + _setPreviewHtml(builder.renderHtml()); + }); + _builder.current = builder; + } + return _builder.current; + }; + + React.useEffect(() => { + return () => { + getBuilder().dispose(); + }; + }, []); + + const send = async (): Promise => { + const dataList = await getBuilder().build(); + const post = await getHttpTimelineClient().postPost(timelineName, { + dataList, + }); + onPosted(post); + }; + + return ( +
+ +
+ {(() => { + if (tab === "text") { + return ( + { + getBuilder().setMarkdownText(event.currentTarget.value); + }} + /> + ); + } else if (tab === "images") { + return
; + } else { + return
; + } + })()} +
+
+ ); +}; + +export default MarkdownPostEdit; -- cgit v1.2.3