1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
import React from "react";
import { Form } from "react-bootstrap";
import { useTranslation } from "react-i18next";
import { Prompt } from "react-router";
import { getHttpTimelineClient, HttpTimelinePostInfo } from "@/http/timeline";
import TabPages from "../common/TabPages";
import TimelinePostBuilder from "@/services/TimelinePostBuilder";
export interface MarkdownPostEditProps {
timeline: string;
onPosted: (post: HttpTimelinePostInfo) => void;
onPostError: () => void;
onClose: () => void;
className?: string;
style?: React.CSSProperties;
}
const MarkdownPostEdit: React.FC<MarkdownPostEditProps> = ({
timeline: timelineName,
onPosted,
onClose,
onPostError,
className,
style,
}) => {
const { t } = useTranslation();
const [canLeave, setCanLeave] = React.useState<boolean>(true);
const [process, setProcess] = React.useState<boolean>(false);
const [text, _setText] = React.useState<string>("");
const [images, _setImages] = React.useState<{ file: File; url: string }[]>(
[]
);
const [previewHtml, _setPreviewHtml] = React.useState<string>("");
const _builder = React.useRef<TimelinePostBuilder | null>(null);
const getBuilder = (): TimelinePostBuilder => {
if (_builder.current == null) {
const builder = new TimelinePostBuilder(() => {
setCanLeave(builder.isEmpty);
_setText(builder.text);
_setImages(builder.images);
_setPreviewHtml(builder.renderHtml());
});
_builder.current = builder;
}
return _builder.current;
};
React.useEffect(() => {
return () => {
getBuilder().dispose();
};
}, []);
React.useEffect(() => {
window.onbeforeunload = () => {
if (!canLeave) {
return t("timeline.confirmLeave");
}
};
return () => {
window.onbeforeunload = null;
};
}, [canLeave, t]);
const send = async (): Promise<void> => {
setProcess(true);
try {
const dataList = await getBuilder().build();
const post = await getHttpTimelineClient().postPost(timelineName, {
dataList,
});
onPosted(post);
onClose();
} catch (e) {
setProcess(false);
onPostError();
}
};
return (
<>
<Prompt when={!canLeave} message={t("timeline.confirmLeave")} />
<TabPages
className={className}
style={style}
pageContainerClassName="py-2"
actions={
<>
<div className="flat-button text-danger mr-2" onClick={onClose}>
{t("operationDialog.cancel")}
</div>
<div className="flat-button text-primary" onClick={send}>
{t("timeline.send")}
</div>
</>
}
pages={[
{
id: "text",
tabText: "edit",
page: (
<Form.Control
as="textarea"
value={text}
disabled={process}
onChange={(event) => {
getBuilder().setMarkdownText(event.currentTarget.value);
}}
/>
),
},
{
id: "images",
tabText: "image",
page: (
<div className="timeline-markdown-post-edit-page">
{images.map((image) => (
<img
key={image.url}
src={image.url}
className="timeline-markdown-post-edit-image"
/>
))}
<Form.File
label={t("chooseImage")}
accept="image/*"
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
const { files } = event.currentTarget;
if (files != null && files.length !== 0) {
getBuilder().appendImage(files[0]);
}
}}
disabled={process}
/>
</div>
),
},
{
id: "preview",
tabText: "preview",
page: (
<div
className="markdown-container timeline-markdown-post-edit-page"
dangerouslySetInnerHTML={{ __html: previewHtml }}
/>
),
},
]}
/>
</>
);
};
export default MarkdownPostEdit;
|