1
1
'use client' ;
2
2
3
- import React , { useCallback , useEffect , useState } from 'react' ;
3
+ import React , { useCallback , useState } from 'react' ;
4
4
5
5
import type { DropdownMenuProps } from '@radix-ui/react-dropdown-menu' ;
6
6
7
7
import { cn } from '@udecode/cn' ;
8
- import { insertNodes } from '@udecode/plate-common' ;
9
- import { focusEditor , useEditorRef } from '@udecode/plate-common /react' ;
8
+ import { insertNodes , isUrl } from '@udecode/plate-common' ;
9
+ import { useEditorRef } from '@udecode/plate-core /react' ;
10
10
import {
11
11
AudioPlugin ,
12
12
FilePlugin ,
@@ -20,6 +20,7 @@ import {
20
20
ImageIcon ,
21
21
LinkIcon ,
22
22
} from 'lucide-react' ;
23
+ import { toast } from 'sonner' ;
23
24
import { useFilePicker } from 'use-file-picker' ;
24
25
25
26
import {
@@ -104,21 +105,16 @@ export function MediaToolbarButton({
104
105
const [ url , setUrl ] = useState ( '' ) ;
105
106
106
107
const embedMedia = useCallback ( ( ) => {
108
+ if ( ! isUrl ( url ) ) return toast . error ( 'Invalid URL' ) ;
109
+
107
110
setDialogOpen ( false ) ;
108
111
insertNodes ( editor , {
109
112
children : [ { text : '' } ] ,
110
113
name : nodeType === FilePlugin . key ? url . split ( '/' ) . pop ( ) : undefined ,
111
114
type : nodeType ,
112
115
url,
113
116
} ) ;
114
- } , [ editor , nodeType , url ] ) ;
115
-
116
- useEffect ( ( ) => {
117
- if ( ! dialogOpen ) {
118
- focusEditor ( editor ) ;
119
- setUrl ( '' ) ;
120
- }
121
- } , [ dialogOpen , editor ] ) ;
117
+ } , [ url , editor , nodeType ] ) ;
122
118
123
119
return (
124
120
< >
@@ -166,7 +162,13 @@ export function MediaToolbarButton({
166
162
</ DropdownMenuContent >
167
163
</ DropdownMenu >
168
164
169
- < AlertDialog open = { dialogOpen } onOpenChange = { setDialogOpen } >
165
+ < AlertDialog
166
+ open = { dialogOpen }
167
+ onOpenChange = { ( value ) => {
168
+ setDialogOpen ( value ) ;
169
+ setUrl ( '' ) ;
170
+ } }
171
+ >
170
172
< AlertDialogContent >
171
173
< AlertDialogHeader >
172
174
< AlertDialogTitle > { currentConfig . title } </ AlertDialogTitle >
@@ -192,7 +194,14 @@ export function MediaToolbarButton({
192
194
</ AlertDialogHeader >
193
195
< AlertDialogFooter >
194
196
< AlertDialogCancel > Cancel</ AlertDialogCancel >
195
- < AlertDialogAction onClick = { embedMedia } > Accept</ AlertDialogAction >
197
+ < AlertDialogAction
198
+ onClick = { ( e ) => {
199
+ e . preventDefault ( ) ;
200
+ embedMedia ( ) ;
201
+ } }
202
+ >
203
+ Accept
204
+ </ AlertDialogAction >
196
205
</ AlertDialogFooter >
197
206
</ AlertDialogContent >
198
207
</ AlertDialog >
0 commit comments