'use client' import type { Editor } from '@tiptap/react' import { useTiptapEditor } from '@admin/hooks/content-editor/use-tiptap-editor' import { findNodePosition } from '@admin/utils/editor/find-node-position' import { getSelectedBlockNodes } from '@admin/utils/editor/get-selected-block-nodes' import { isNodeInSchema } from '@admin/utils/editor/is-node-in-schema' import { isNodeTypeSelected } from '@admin/utils/editor/is-node-type-selected' import { isValidPosition } from '@admin/utils/editor/is-valid-position' import { selectionWithinConvertibleTypes } from '@admin/utils/editor/selection-within-convertible-types' import { NodeSelection, TextSelection } from '@tiptap/pm/state' import { SquareCodeIcon as CodeBlockIcon } from 'lucide-react' import { useCallback, useEffect, useState } from 'react' export const CODE_BLOCK_SHORTCUT_KEY = 'mod+alt+c' export interface UseCodeBlockConfig { editor?: Editor | null hideWhenUnavailable?: boolean onToggled?: () => void } export function canToggle(editor: Editor | null, turnInto: boolean = true): boolean { if (!editor || !editor.isEditable) return false if (!isNodeInSchema('codeBlock', editor) || isNodeTypeSelected(editor, ['image'])) return false if (!turnInto) { return editor.can().toggleNode('codeBlock', 'paragraph') } if ( !selectionWithinConvertibleTypes(editor, [ 'paragraph', 'heading', 'bulletList', 'orderedList', 'taskList', 'blockquote', 'codeBlock' ]) ) return false return editor.can().toggleNode('codeBlock', 'paragraph') || editor.can().clearNodes() } export function toggleCodeBlock(editor: Editor | null): boolean { if (!editor || !editor.isEditable) return false if (!canToggle(editor)) return false try { const view = editor.view let state = view.state let tr = state.tr const blocks = getSelectedBlockNodes(editor) const isPossibleToTurnInto = selectionWithinConvertibleTypes(editor, [ 'paragraph', 'heading', 'bulletList', 'orderedList', 'taskList', 'blockquote', 'codeBlock' ]) && blocks.length === 1 if ( (state.selection.empty || state.selection instanceof TextSelection) && isPossibleToTurnInto ) { const pos = findNodePosition({ editor, node: state.selection.$anchor.node(1) })?.pos if (!isValidPosition(pos)) return false tr = tr.setSelection(NodeSelection.create(state.doc, pos)) view.dispatch(tr) state = view.state } const selection = state.selection let chain = editor.chain().focus() if (selection instanceof NodeSelection) { const firstChild = selection.node.firstChild?.firstChild const lastChild = selection.node.lastChild?.lastChild const from = firstChild ? selection.from + firstChild.nodeSize : selection.from + 1 const to = lastChild ? selection.to - lastChild.nodeSize : selection.to - 1 const resolvedFrom = state.doc.resolve(from) const resolvedTo = state.doc.resolve(to) chain = chain.setTextSelection(TextSelection.between(resolvedFrom, resolvedTo)).clearNodes() } const toggle = editor.isActive('codeBlock') ? chain.setNode('paragraph') : chain.toggleNode('codeBlock', 'paragraph') toggle.run() editor.chain().focus().selectTextblockEnd().run() return true } catch { return false } } export function shouldShowButton(props: { editor: Editor | null hideWhenUnavailable: boolean }): boolean { const { editor, hideWhenUnavailable } = props if (!editor || !editor.isEditable) return false if (!hideWhenUnavailable) { return true } if (!isNodeInSchema('codeBlock', editor)) return false if (!editor.isActive('code')) { return canToggle(editor) } return true } export function useCodeBlock(config?: UseCodeBlockConfig) { const { editor: providedEditor, hideWhenUnavailable = false, onToggled } = config || {} const { editor } = useTiptapEditor(providedEditor) const [isVisible, setIsVisible] = useState(true) const canToggleState = canToggle(editor) const isActive = editor?.isActive('codeBlock') || false useEffect(() => { if (!editor) return const handleSelectionUpdate = () => { setIsVisible(shouldShowButton({ editor, hideWhenUnavailable })) } handleSelectionUpdate() editor.on('selectionUpdate', handleSelectionUpdate) return () => { editor.off('selectionUpdate', handleSelectionUpdate) } }, [editor, hideWhenUnavailable]) const handleToggle = useCallback(() => { if (!editor) return false const success = toggleCodeBlock(editor) if (success) { onToggled?.() } return success }, [editor, onToggled]) return { isVisible, isActive, handleToggle, canToggle: canToggleState, label: 'Code Block', shortcutKeys: CODE_BLOCK_SHORTCUT_KEY, Icon: CodeBlockIcon } }