import { BlockSchema, DefaultBlockSchema, DefaultInlineContentSchema, DefaultStyleSchema, InlineContentSchema, StyleSchema, filenameFromURL, } from "@blocknote/core"; import { ChangeEvent, KeyboardEvent, useCallback, useState } from "react"; import { useComponentsContext } from "../../../editor/ComponentsContext.js"; import { useBlockNoteEditor } from "../../../hooks/useBlockNoteEditor.js"; import { useDictionary } from "../../../i18n/dictionary.js"; import { FilePanelProps } from "../FilePanelProps.js"; export const EmbedTab = < B extends BlockSchema = DefaultBlockSchema, I extends InlineContentSchema = DefaultInlineContentSchema, S extends StyleSchema = DefaultStyleSchema, >( props: FilePanelProps, ) => { const Components = useComponentsContext()!; const dict = useDictionary(); const editor = useBlockNoteEditor(); const block = editor.getBlock(props.blockId)!; const [currentURL, setCurrentURL] = useState(""); const handleURLChange = useCallback( (event: ChangeEvent) => { setCurrentURL(event.currentTarget.value); }, [], ); const handleURLEnter = useCallback( (event: KeyboardEvent) => { if (event.key === "Enter" && !event.nativeEvent.isComposing) { event.preventDefault(); editor.updateBlock(block.id, { props: { name: filenameFromURL(currentURL), url: currentURL, } as any, }); } }, [editor, block.id, currentURL], ); const handleURLClick = useCallback(() => { editor.updateBlock(block.id, { props: { name: filenameFromURL(currentURL), url: currentURL, } as any, }); }, [editor, block.id, currentURL]); return ( {dict.file_panel.embed.embed_button[block.type] || dict.file_panel.embed.embed_button["file"]} ); };