"use client" import type { Editor } from "@tiptap/react" import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger, } from "@/components/ui/context-menu" import { openDocsGraphicDrawEditor } from "@/lib/drive/docs-graphic-draw-bridge" import { DOCS_GRAPHIC_WRAP_LABELS, parseGraphicAttrs, type DocsGraphicWrap, } from "@/lib/drive/docs-graphic-types" export function DocsGraphicContextMenu({ editor, children, onCrop, onOpenOptions, onReplaceImage, }: { editor: Editor children: React.ReactNode onCrop?: () => void onOpenOptions?: () => void onReplaceImage?: () => void }) { const active = editor.isActive("docsGraphic") || editor.isActive("docsInlineGraphic") if (!active) return <>{children} const name = editor.isActive("docsInlineGraphic") ? "docsInlineGraphic" : "docsGraphic" const attrs = parseGraphicAttrs(editor.getAttributes(name) as Record) const isImage = attrs.graphicType === "image" const applyWrap = (wrap: DocsGraphicWrap) => { editor.chain().setDocsGraphicWrap(wrap).run() } const downloadImage = () => { if (!attrs.src) return const link = document.createElement("a") link.href = attrs.src link.download = "image" link.click() } return ( {children} event.preventDefault()} > document.execCommand("cut")} > Couper document.execCommand("copy")} > Copier document.execCommand("paste")} > Coller editor.chain().focus().deleteSelection().run()} > Supprimer {isImage ? ( <> Remplacer l'image… Recadrer Options image… { const alt = window.prompt("Texte alternatif", attrs.alt) if (alt != null) editor.chain().focus().updateDocsGraphic({ alt }).run() }} > Texte alternatif… Télécharger l'image ) : attrs.graphicType === "draw" ? ( openDocsGraphicDrawEditor(editor)}> Modifier le dessin… ) : attrs.graphicType === "shape" ? ( <> { const fill = window.prompt("Couleur de remplissage", attrs.fill) if (fill) editor.chain().focus().updateDocsGraphic({ fill }).run() }} > Modifier le remplissage… { const stroke = window.prompt("Couleur du contour", attrs.stroke) if (stroke) editor.chain().focus().updateDocsGraphic({ stroke }).run() }} > Modifier le contour… ) : null} Habillage texte event.preventDefault()}> {(Object.keys(DOCS_GRAPHIC_WRAP_LABELS) as DocsGraphicWrap[]).map((wrap) => ( applyWrap(wrap)}> {DOCS_GRAPHIC_WRAP_LABELS[wrap]} ))} editor.chain().focus().bringDocsGraphicForward().run()}> Avancer editor.chain().focus().sendDocsGraphicBackward().run()}> Reculer ) }