-
Notifications
You must be signed in to change notification settings - Fork 162
/
index.tsx
204 lines (190 loc) · 6.4 KB
/
index.tsx
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import React, { useRef, useState, FunctionComponent, useEffect } from 'react'
import MUIRichTextEditor, { TMUIRichTextEditorRef, TAsyncAtomicBlockResponse } from '../..'
import Grid from '@mui/material/Grid'
import { makeStyles } from '@mui/styles'
import Popover from '@mui/material/Popover'
import TextField from '@mui/material/TextField'
import IconButton from '@mui/material/IconButton'
import Button from '@mui/material/Button'
import BackupIcon from '@mui/icons-material/Backup'
import DoneIcon from '@mui/icons-material/Done'
import CloseIcon from '@mui/icons-material/Close'
import AttachFileIcon from '@mui/icons-material/AttachFile'
interface IUploadImagePopoverProps {
anchor: TAnchor
onSubmit: (data: TUploadImageData, insert: boolean) => void
}
type TUploadImagePopoverState = {
anchor: TAnchor
isCancelled: boolean
}
type TUploadImageData = {
file?: File
}
type TAnchor = HTMLElement | null
const cardPopverStyles = makeStyles({
root: {
padding: 10,
maxWidth: 350
},
textField: {
width: "100%"
},
input: {
display: "none"
}
})
const uploadImageToServer = (file: File) => {
return new Promise(resolve => {
console.log(`Uploading image ${file.name} ...`)
setTimeout(() => {
console.log("Upload successful")
resolve(`https://return_uploaded_image_url/${file.name}`)
}, 2000)
})
}
const uploadImage = (file: File) => {
return new Promise<TAsyncAtomicBlockResponse>(async (resolve, reject) => {
const url = await uploadImageToServer(file)
if (!url) {
reject()
return
}
resolve({
data: {
url: url,
width: 300,
height: 200,
alignment: "left", // or "center", "right"
type: "image" // or "video"
}
})
})
}
const UploadImagePopover: FunctionComponent<IUploadImagePopoverProps> = (props) => {
const classes = cardPopverStyles(props)
const [state, setState] = useState<TUploadImagePopoverState>({
anchor: null,
isCancelled: false
})
const [data, setData] = useState<TUploadImageData>({})
useEffect(() => {
setState({
anchor: props.anchor,
isCancelled: false
})
setData({
file: undefined
})
}, [props.anchor])
return (
<Popover
anchorEl={state.anchor}
open={state.anchor !== null}
anchorOrigin={{
vertical: "bottom",
horizontal: "right"
}}
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
>
<Grid container spacing={1} className={classes.root}>
<Grid item xs={10}>
<TextField
className={classes.textField}
disabled
value={data.file?.name || ""}
placeholder="Click icon to attach image"
/>
</Grid>
<Grid item xs={2}>
<input
accept="image/*"
className={classes.input}
id="contained-button-file"
type="file"
onChange={(event) => {
setData({
...data,
file: event.target.files![0]
})
}}
/>
<label htmlFor="contained-button-file">
<IconButton color="primary" aria-label="upload image" component="span">
<AttachFileIcon />
</IconButton>
</label>
</Grid>
<Grid item container xs={12} justifyContent="flex-end">
<Button onClick={() => {
setState({
anchor: null,
isCancelled: true
})
}}
>
<CloseIcon />
</Button>
<Button onClick={() => {
setState({
anchor: null,
isCancelled: false
})
props.onSubmit(data, !state.isCancelled)
}}
>
<DoneIcon />
</Button>
</Grid>
</Grid>
</Popover>
)
}
const AsyncImageUpload: FunctionComponent = () => {
const ref = useRef<TMUIRichTextEditorRef>(null)
const [anchor, setAnchor] = useState<HTMLElement | null>(null)
const handleFileUpload = (file: File) => {
ref.current?.insertAtomicBlockAsync("IMAGE", uploadImage(file), "Uploading now...")
}
return (
<>
<UploadImagePopover
anchor={anchor}
onSubmit={(data, insert) => {
if (insert && data.file) {
handleFileUpload(data.file)
}
setAnchor(null)
}}
/>
<MUIRichTextEditor
label="Drop a file inside the editor or press the last icon in the toolbar to simulate uploading an image...."
ref={ref}
controls={["title", "bold", "underline", "media", "upload-image"]}
customControls={[
{
name: "upload-image",
icon: <BackupIcon />,
type: "callback",
onClick: (_editorState, _name, anchor) => {
setAnchor(anchor)
}
}
]}
draftEditorProps={{
handleDroppedFiles: (_selectionState, files) => {
if (files.length && (files[0] as File).name !== undefined) {
handleFileUpload(files[0] as File)
return "handled"
}
return "not-handled"
}
}}
/>
</>
)
}
export default AsyncImageUpload