Skip to content

Commit a12690a

Browse files
committed
fix
1 parent 450429a commit a12690a

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

apps/www/src/registry/default/plate-ui/media-toolbar-button.tsx

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use client';
22

3-
import React, { useCallback, useEffect, useState } from 'react';
3+
import React, { useCallback, useState } from 'react';
44

55
import type { DropdownMenuProps } from '@radix-ui/react-dropdown-menu';
66

77
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';
1010
import {
1111
AudioPlugin,
1212
FilePlugin,
@@ -20,6 +20,7 @@ import {
2020
ImageIcon,
2121
LinkIcon,
2222
} from 'lucide-react';
23+
import { toast } from 'sonner';
2324
import { useFilePicker } from 'use-file-picker';
2425

2526
import {
@@ -104,21 +105,16 @@ export function MediaToolbarButton({
104105
const [url, setUrl] = useState('');
105106

106107
const embedMedia = useCallback(() => {
108+
if (!isUrl(url)) return toast.error('Invalid URL');
109+
107110
setDialogOpen(false);
108111
insertNodes(editor, {
109112
children: [{ text: '' }],
110113
name: nodeType === FilePlugin.key ? url.split('/').pop() : undefined,
111114
type: nodeType,
112115
url,
113116
});
114-
}, [editor, nodeType, url]);
115-
116-
useEffect(() => {
117-
if (!dialogOpen) {
118-
focusEditor(editor);
119-
setUrl('');
120-
}
121-
}, [dialogOpen, editor]);
117+
}, [url, editor, nodeType]);
122118

123119
return (
124120
<>
@@ -166,7 +162,13 @@ export function MediaToolbarButton({
166162
</DropdownMenuContent>
167163
</DropdownMenu>
168164

169-
<AlertDialog open={dialogOpen} onOpenChange={setDialogOpen}>
165+
<AlertDialog
166+
open={dialogOpen}
167+
onOpenChange={(value) => {
168+
setDialogOpen(value);
169+
setUrl('');
170+
}}
171+
>
170172
<AlertDialogContent>
171173
<AlertDialogHeader>
172174
<AlertDialogTitle>{currentConfig.title}</AlertDialogTitle>
@@ -192,7 +194,14 @@ export function MediaToolbarButton({
192194
</AlertDialogHeader>
193195
<AlertDialogFooter>
194196
<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>
196205
</AlertDialogFooter>
197206
</AlertDialogContent>
198207
</AlertDialog>

0 commit comments

Comments
 (0)