"use client"; import * as React from "react"; // --- Lib --- import { parseShortcutKeys } from "@/lib/tiptap-utils"; // --- Hooks --- import { useTiptapEditor } from "@/hooks/use-tiptap-editor"; import { Badge } from "@/components/shared/tiptap/tiptap-ui-primitive/badge"; // --- UI Primitives --- import type { ButtonProps } from "@/components/shared/tiptap/tiptap-ui-primitive/button"; import { Button } from "@/components/shared/tiptap/tiptap-ui-primitive/button"; // --- Tiptap UI --- import type { UseCodeBlockConfig } from "@/components/shared/tiptap/tiptap-ui/code-block-button"; import { CODE_BLOCK_SHORTCUT_KEY, useCodeBlock, } from "@/components/shared/tiptap/tiptap-ui/code-block-button"; export interface CodeBlockButtonProps extends Omit, UseCodeBlockConfig { /** * Optional text to display alongside the icon. */ text?: string; /** * Optional show shortcut keys in the button. * @default false */ showShortcut?: boolean; } export function CodeBlockShortcutBadge({ shortcutKeys = CODE_BLOCK_SHORTCUT_KEY, }: { shortcutKeys?: string; }) { return {parseShortcutKeys({ shortcutKeys })}; } /** * Button component for toggling code block in a Tiptap editor. * * For custom button implementations, use the `useCodeBlock` hook instead. */ export const CodeBlockButton = React.forwardRef< HTMLButtonElement, CodeBlockButtonProps >( ( { editor: providedEditor, text, hideWhenUnavailable = false, onToggled, showShortcut = false, onClick, children, ...buttonProps }, ref, ) => { const { editor } = useTiptapEditor(providedEditor); const { isVisible, canToggle, isActive, handleToggle, label, shortcutKeys, Icon, } = useCodeBlock({ editor, hideWhenUnavailable, onToggled, }); const handleClick = React.useCallback( (event: React.MouseEvent) => { onClick?.(event); if (event.defaultPrevented) return; handleToggle(); }, [handleToggle, onClick], ); if (!isVisible) { return null; } return ( ); }, ); CodeBlockButton.displayName = "CodeBlockButton";