-
Notifications
You must be signed in to change notification settings - Fork 169
/
index.tsx
256 lines (237 loc) · 7.25 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import React, { useRef, useState, FunctionComponent, useEffect } from 'react'
import MUIRichTextEditor, { TMUIRichTextEditorRef } from '../..'
import {
Card, CardHeader, Avatar, CardMedia, CardContent,
Typography, IconButton, CardActions, Grid
} from '@mui/material'
import { makeStyles } from '@mui/styles'
import Popover from '@mui/material/Popover'
import TextField from '@mui/material/TextField'
import Button from '@mui/material/Button'
import WebAssetIcon from '@mui/icons-material/WebAsset'
import ShareIcon from '@mui/icons-material/Share'
import FavoriteIcon from '@mui/icons-material/Favorite'
import DoneIcon from '@mui/icons-material/Done'
import CloseIcon from '@mui/icons-material/Close'
type TMyCardData = {
title?: string
name?: string
date?: Date
text?: string
image?: string
}
type TAnchor = HTMLElement | null
interface IMyCardPopoverProps {
anchor: TAnchor
onSubmit: (data: TMyCardData, insert: boolean) => void
}
type TMyCardPopoverState = {
anchor: TAnchor
isCancelled: boolean
}
const cardPopverStyles = makeStyles({
root: {
padding: 10,
maxWidth: 350
},
textField: {
width: "100%"
}
})
const cardStyles = makeStyles({
root: {
maxWidth: 345
},
media: {
height: 0,
paddingTop: '56.25%'
},
avatar: {
backgroundColor: "tomato"
}
})
const save = (data: string) => {
console.log(data)
}
const MyCard: FunctionComponent<any> = (props) => {
const { blockProps } = props
const classes = cardStyles(props)
const handleLiked = () => {
alert("Favorited")
}
const handleShared = () => {
alert("Shared")
}
return (
<Card className={classes.root}>
<CardHeader
avatar={
<Avatar aria-label="name" className={classes.avatar}>
{blockProps.name && blockProps.name.substring(0, 1)}
</Avatar>
}
title={blockProps.title}
subheader={blockProps.date && blockProps.date.toLocaleDateString()}
/>
<CardMedia
className={classes.media}
image={blockProps.image || "default"}
title={blockProps.title}
/>
<CardContent>
<Typography variant="body2" color="textSecondary" component="p">
{blockProps.text}
</Typography>
</CardContent>
<CardActions disableSpacing>
<IconButton
aria-label="like card"
onClick={handleLiked}>
<FavoriteIcon />
</IconButton>
<IconButton
aria-label="share"
onClick={handleShared}
>
<ShareIcon />
</IconButton>
</CardActions>
</Card>
)
}
const MyCardPopover: FunctionComponent<IMyCardPopoverProps> = (props) => {
const classes = cardPopverStyles(props)
const [state, setState] = useState<TMyCardPopoverState>({
anchor: null,
isCancelled: false
})
const [data, setData] = useState<TMyCardData>({})
useEffect(() => {
setState({
anchor: props.anchor,
isCancelled: false
})
setData({
date: new Date()
})
}, [props.anchor])
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setData({
...data,
[event.target.name]: event.target.value
})
}
const textFieldProps = {
className: classes.textField,
onChange: handleChange,
InputLabelProps: {
shrink: true
}
}
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={6}>
<TextField
{...textFieldProps}
autoFocus={true}
label="Title"
name="title"
/>
</Grid>
<Grid item xs={6}>
<TextField
{...textFieldProps}
label="Name"
name="name"
/>
</Grid>
<Grid item xs={12}>
<TextField
{...textFieldProps}
label="Text"
name="text"
/>
</Grid>
<Grid item xs={12}>
<TextField
{...textFieldProps}
label="Image URL"
name="image"
/>
</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 AtomicCustomBlock: FunctionComponent = () => {
const ref = useRef<TMUIRichTextEditorRef>(null)
const [anchor, setAnchor] = useState<HTMLElement | null>(null)
return (
<>
<MyCardPopover
anchor={anchor}
onSubmit={(data, insert) => {
if (insert) {
ref.current?.insertAtomicBlockSync("my-card", data)
}
setAnchor(null)
}}
/>
<MUIRichTextEditor
label="Press the last icon in the toolbar to insert an atomic custom block...."
ref={ref}
onSave={save}
controls={["title", "bold", "underline", "save", "add-card"]}
customControls={[
{
name: "my-card",
type: "atomic",
atomicComponent: MyCard
},
{
name: "add-card",
icon: <WebAssetIcon />,
type: "callback",
onClick: (_editorState, _name, anchor) => {
setAnchor(anchor)
}
}
]}
/>
</>
)
}
export default AtomicCustomBlock