-
Notifications
You must be signed in to change notification settings - Fork 169
/
index.tsx
212 lines (194 loc) · 6.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
import React, { useRef, useState, FunctionComponent, useEffect } from 'react'
import MUIRichTextEditor, { TMUIRichTextEditorRef, TAsyncAtomicBlockResponse } from '../..'
import { makeStyles } from '@mui/styles'
import Card from '@mui/material/Card'
import CardContent from '@mui/material/CardContent'
import Typography from '@mui/material/Typography'
import Grid from '@mui/material/Grid'
import Popover from '@mui/material/Popover'
import TextField from '@mui/material/TextField'
import Button from '@mui/material/Button'
import UpdateIcon from '@mui/icons-material/Update'
import DoneIcon from '@mui/icons-material/Done'
import CloseIcon from '@mui/icons-material/Close'
type TMyCardData = {
searchTerm?: 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
},
})
const getDataFromCloudService = (searchTerm: string) => {
return new Promise(resolve => {
console.log(`Searching for ${searchTerm}...`)
setTimeout(() => {
resolve({
title: "Data from cloud",
subtitle: `You searched: ${searchTerm}`,
text: "Some description from the cloud.",
})
}, 2000)
})
}
const downloadData = (searchTerm: string) => {
return new Promise<TAsyncAtomicBlockResponse>(async (resolve, reject) => {
const data = await getDataFromCloudService(searchTerm)
if (!data) { // for this example this will never be rejected
reject()
return
}
resolve({
data: data
})
})
}
const MyCard: FunctionComponent<any> = (props) => {
const { blockProps } = props
const classes = cardStyles(props)
return (
<Card className={classes.root}>
<CardContent>
<Typography gutterBottom variant="h5" component="h2">
{blockProps.title}
</Typography>
<Typography gutterBottom component="h2">
{blockProps.subtitle}
</Typography>
<Typography variant="body2" color="textSecondary" component="p">
{blockProps.text}
</Typography>
</CardContent>
</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
})
}, [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={12}>
<TextField
{...textFieldProps}
autoFocus={true}
label="Search term"
name="searchTerm"
placeholder="Type anything here..."
/>
</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 AsyncAtomicCustomBlock: FunctionComponent = () => {
const ref = useRef<TMUIRichTextEditorRef>(null)
const [anchor, setAnchor] = useState<HTMLElement | null>(null)
return (
<>
<MyCardPopover
anchor={anchor}
onSubmit={(data, insert) => {
if (insert && data.searchTerm) {
ref.current?.insertAtomicBlockAsync("my-card", downloadData(data.searchTerm), "Downloading data...")
}
setAnchor(null)
}}
/>
<MUIRichTextEditor
label="Press the last icon in the toolbar to insert an async atomic custom block..."
ref={ref}
controls={["title", "bold", "underline", "add-card"]}
customControls={[
{
name: "my-card",
type: "atomic",
atomicComponent: MyCard
},
{
name: "add-card",
icon: <UpdateIcon />,
type: "callback",
onClick: (_editorState, _name, anchor) => {
setAnchor(anchor)
}
}
]}
/>
</>
)
}
export default AsyncAtomicCustomBlock