Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Nov 17, 2024
1 parent 450429a commit a12690a
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions apps/www/src/registry/default/plate-ui/media-toolbar-button.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -20,6 +20,7 @@ import {
ImageIcon,
LinkIcon,
} from 'lucide-react';
import { toast } from 'sonner';
import { useFilePicker } from 'use-file-picker';

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

const embedMedia = useCallback(() => {
if (!isUrl(url)) return toast.error('Invalid URL');

setDialogOpen(false);
insertNodes(editor, {
children: [{ text: '' }],
name: nodeType === FilePlugin.key ? url.split('/').pop() : undefined,
type: nodeType,
url,
});
}, [editor, nodeType, url]);

useEffect(() => {
if (!dialogOpen) {
focusEditor(editor);
setUrl('');
}
}, [dialogOpen, editor]);
}, [url, editor, nodeType]);

return (
<>
Expand Down Expand Up @@ -166,7 +162,13 @@ export function MediaToolbarButton({
</DropdownMenuContent>
</DropdownMenu>

<AlertDialog open={dialogOpen} onOpenChange={setDialogOpen}>
<AlertDialog
open={dialogOpen}
onOpenChange={(value) => {
setDialogOpen(value);
setUrl('');
}}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{currentConfig.title}</AlertDialogTitle>
Expand All @@ -192,7 +194,14 @@ export function MediaToolbarButton({
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction onClick={embedMedia}>Accept</AlertDialogAction>
<AlertDialogAction
onClick={(e) => {
e.preventDefault();
embedMedia();
}}
>
Accept
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
Expand Down

0 comments on commit a12690a

Please sign in to comment.