import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js"; import { FilePanelExtension } from "../../../../extensions/FilePanel/FilePanel.js"; import { BlockConfig, BlockFromConfigNoChildren, } from "../../../../schema/index.js"; export const createAddFileButton = ( block: BlockFromConfigNoChildren, any, any>, editor: BlockNoteEditor, buttonIcon?: HTMLElement, ) => { const addFileButton = document.createElement("div"); addFileButton.className = "bn-add-file-button"; const addFileButtonIcon = document.createElement("div"); addFileButtonIcon.className = "bn-add-file-button-icon"; if (buttonIcon) { addFileButtonIcon.appendChild(buttonIcon); } else { addFileButtonIcon.innerHTML = ''; } addFileButton.appendChild(addFileButtonIcon); const addFileButtonText = document.createElement("p"); addFileButtonText.className = "bn-add-file-button-text"; addFileButtonText.innerHTML = block.type in editor.dictionary.file_blocks.add_button_text ? editor.dictionary.file_blocks.add_button_text[block.type] : editor.dictionary.file_blocks.add_button_text["file"]; addFileButton.appendChild(addFileButtonText); // Prevents focus from moving to the button. const addFileButtonMouseDownHandler = (event: MouseEvent) => { event.preventDefault(); event.stopPropagation(); }; // Opens the file toolbar. const addFileButtonClickHandler = () => { if (!editor.isEditable) { return; } editor.getExtension(FilePanelExtension)?.showMenu(block.id); }; addFileButton.addEventListener( "mousedown", addFileButtonMouseDownHandler, true, ); addFileButton.addEventListener("click", addFileButtonClickHandler, true); return { dom: addFileButton, destroy: () => { addFileButton.removeEventListener( "mousedown", addFileButtonMouseDownHandler, true, ); addFileButton.removeEventListener( "click", addFileButtonClickHandler, true, ); }, }; };