From a12690aed3cfbe56ad211f5ad92bf1e97092daab Mon Sep 17 00:00:00 2001 From: Felix Feng Date: Sun, 17 Nov 2024 19:47:04 +0800 Subject: [PATCH] fix --- .../default/plate-ui/media-toolbar-button.tsx | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/apps/www/src/registry/default/plate-ui/media-toolbar-button.tsx b/apps/www/src/registry/default/plate-ui/media-toolbar-button.tsx index effda61402..0f62cd8a23 100644 --- a/apps/www/src/registry/default/plate-ui/media-toolbar-button.tsx +++ b/apps/www/src/registry/default/plate-ui/media-toolbar-button.tsx @@ -1,12 +1,12 @@ 'use client'; -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback, useState } from 'react'; import type { DropdownMenuProps } from '@radix-ui/react-dropdown-menu'; import { cn } from '@udecode/cn'; -import { insertNodes } from '@udecode/plate-common'; -import { focusEditor, useEditorRef } from '@udecode/plate-common/react'; +import { insertNodes, isUrl } from '@udecode/plate-common'; +import { useEditorRef } from '@udecode/plate-core/react'; import { AudioPlugin, FilePlugin, @@ -20,6 +20,7 @@ import { ImageIcon, LinkIcon, } from 'lucide-react'; +import { toast } from 'sonner'; import { useFilePicker } from 'use-file-picker'; import { @@ -104,6 +105,8 @@ export function MediaToolbarButton({ const [url, setUrl] = useState(''); const embedMedia = useCallback(() => { + if (!isUrl(url)) return toast.error('Invalid URL'); + setDialogOpen(false); insertNodes(editor, { children: [{ text: '' }], @@ -111,14 +114,7 @@ export function MediaToolbarButton({ type: nodeType, url, }); - }, [editor, nodeType, url]); - - useEffect(() => { - if (!dialogOpen) { - focusEditor(editor); - setUrl(''); - } - }, [dialogOpen, editor]); + }, [url, editor, nodeType]); return ( <> @@ -166,7 +162,13 @@ export function MediaToolbarButton({ - + { + setDialogOpen(value); + setUrl(''); + }} + > {currentConfig.title} @@ -192,7 +194,14 @@ export function MediaToolbarButton({ Cancel - Accept + { + e.preventDefault(); + embedMedia(); + }} + > + Accept +