import { BubbleMenu as BaseBubbleMenu } from "@tiptap/react/menus"; import { findParentNode, posToDOMRect, useEditorState } from "@tiptap/react"; import { useCallback } from "react"; import { Node as PMNode } from "@tiptap/pm/model"; import { EditorMenuProps, ShouldShowProps, } from "@/features/editor/components/table/types/types.ts"; import { ActionIcon, Tooltip } from "@mantine/core"; import { IconDownload, IconTrash, } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import { getFileUrl } from "@/lib/config.ts"; import classes from "../common/toolbar-menu.module.css"; export function AudioMenu({ editor }: EditorMenuProps) { const { t } = useTranslation(); const editorState = useEditorState({ editor, selector: (ctx) => { if (!ctx.editor) { return null; } const audioAttrs = ctx.editor.getAttributes("audio"); return { isAudio: ctx.editor.isActive("audio"), src: audioAttrs?.src || null, }; }, }); const shouldShow = useCallback( ({ state }: ShouldShowProps) => { if (!state) { return false; } return editor.isActive("audio") && editor.getAttributes("audio").src; }, [editor], ); const getReferencedVirtualElement = useCallback(() => { if (!editor) return; const { selection } = editor.state; const predicate = (node: PMNode) => node.type.name === "audio"; const parent = findParentNode(predicate)(selection); if (parent) { const dom = editor.view.nodeDOM(parent?.pos) as HTMLElement; const domRect = dom.getBoundingClientRect(); return { getBoundingClientRect: () => domRect, getClientRects: () => [domRect], }; } const domRect = posToDOMRect(editor.view, selection.from, selection.to); return { getBoundingClientRect: () => domRect, getClientRects: () => [domRect], }; }, [editor]); const handleDownload = useCallback(() => { if (!editorState?.src) return; const url = getFileUrl(editorState.src); const a = document.createElement("a"); a.href = url; a.download = ""; a.click(); }, [editorState?.src]); const handleDelete = useCallback(() => { editor.commands.deleteSelection(); }, [editor]); return (
); } export default AudioMenu;