'use client'; import { cn } from 'cn'; import { DiffMatchPatch, DiffOp } from 'diff-match-patch-ts'; import { BracesIcon, Check, CheckIcon, CopyIcon } from 'lucide-react'; import { CodeBlockRules, type Element, ElementApi, NodeApi } from 'platejs'; import { CodeBlockPlugin, type Editor, EditorElement, type EditorElementProps, useEditor, useEditorReadOnly, useElement, } from 'platejs/react'; import * as React from 'react'; import { Button } from '@/components/ui/button'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from '@/components/ui/command'; import { type CodeBlockLanguageOption, codeBlockLanguages, codeSyntaxClassName, findCodeBlockLanguage, getCodeBlockLanguageLabel, matchesCodeBlockLanguageQuery, resolveCodeBlockLanguage, } from '@/registry/components/editor/code-block-languages'; import { FloatingPopover, FloatingPopoverContent, FloatingPopoverTrigger, } from '@/registry/components/editor/floating-popover'; /** What a wrapper such as `withDiagrams` adds to either code-block variant. */ export type CodeBlockAddon = Readonly<{ languageOptions?: readonly CodeBlockLanguageOption[]; toolbar?: React.ReactNode; wrapSource?: (source: React.ReactNode) => React.ReactNode; }>; export type CodeBlockElementProps = EditorElementProps< typeof CodeBlockPlugin > & { addon?: CodeBlockAddon; showLanguageLabel?: boolean; }; /** What `createCodeBlockPlugin` takes: `CodeBlockElement`, `CodeBlockCodeMirrorElement` or a wrapped one. */ export type CodeBlockComponent = ( props: CodeBlockElementProps ) => React.ReactElement; export function CodeBlockContainer({ addon, children, elementProps, languageOptions = codeBlockLanguages, showLanguageLabel = true, }: { children: React.ReactNode; elementProps: EditorElementProps; addon?: CodeBlockAddon; languageOptions?: readonly CodeBlockLanguageOption[]; showLanguageLabel?: boolean; }) { const { editor, element, attributes, plugin, ref, slots } = elementProps; const readOnly = useEditorReadOnly(); const addedOptions = addon?.languageOptions; const options = React.useMemo( () => addedOptions ? [ ...languageOptions, ...addedOptions.filter( (added) => !languageOptions.some((own) => own.value === added.value) ), ] : languageOptions, [addedOptions, languageOptions] ); return (
{addon?.wrapSource ? addon.wrapSource(children) : children}
{addon?.toolbar} {!readOnly && element.language === 'json' && ( )}
); } export function formatJsonCodeBlock(editor: Editor, element: Element) { if (editor.read.view.isReadOnly()) return; const blockPath = editor.read.nodes.path(element); if (!blockPath) return; const block = editor.read.nodes.get(blockPath)?.[0]; if ( block !== element || !ElementApi.isElement(block) || block.type !== editor.plugin(CodeBlockPlugin).schema.type || block.language !== 'json' || block.children.length !== 1 ) { return; } const [child] = block.children; if (!NodeApi.isText(child)) return; const { text } = child; let formatted: string; try { formatted = JSON.stringify(JSON.parse(text), null, 2); } catch { return; } if (formatted === text) return; const diff = new DiffMatchPatch().diff_main(text, formatted, false); const edits: Array<{ end: number; start: number; text: string }> = []; let oldOffset = 0; let start: number | null = null; let replacement = ''; for (const [operation, value] of diff) { if (operation === DiffOp.Equal) { if (start !== null) { edits.push({ start, end: oldOffset, text: replacement }); start = null; replacement = ''; } oldOffset += value.length; } else if (operation === DiffOp.Delete) { start ??= oldOffset; oldOffset += value.length; } else { start ??= oldOffset; replacement += value; } } if (start !== null) { edits.push({ start, end: oldOffset, text: replacement }); } editor.update((tx) => { for (const edit of edits.reverse()) { const at = { anchor: { path: [...blockPath, 0], offset: edit.start }, focus: { path: [...blockPath, 0], offset: edit.end }, }; if (edit.text) { tx.text.insert(edit.text, { at }); } else { tx.text.delete({ at }); } } }); } function CodeBlockCopyButton() { const editor = useEditor(); const element = useElement(CodeBlockPlugin); return ( { const path = editor.read.nodes.path(element); return path ? editor.read.text.string(path) : ''; }} /> ); } function CodeBlockCombobox({ languageOptions, showLanguageLabel, }: { languageOptions: readonly CodeBlockLanguageOption[]; showLanguageLabel: boolean; }) { const [open, setOpen] = React.useState(false); const readOnly = useEditorReadOnly(); const editor = useEditor(); const element = useElement(CodeBlockPlugin); const listId = React.useId(); const value = element.language || 'plaintext'; const activeValue = findCodeBlockLanguage(value, languageOptions)?.value; const [searchValue, setSearchValue] = React.useState(''); const items = React.useMemo( () => languageOptions.filter((language) => matchesCodeBlockLanguageQuery(language, searchValue) ), [searchValue, languageOptions] ); if (readOnly) { if (!showLanguageLabel) return null; return ( ); } return ( { setOpen(nextOpen); if (!nextOpen) setSearchValue(''); }} > { setSearchValue(innerValue); }} placeholder="Search language..." /> No language found. {items.map((language) => ( { const path = editor.read.nodes.path(element); if (!path) return; editor .plugin(CodeBlockPlugin) .update.set({ language: selected }, { at: path }); setSearchValue(''); setOpen(false); }} > {language.label} ))} ); } function CodeBlockLanguageLabel({ languageOptions, lang, }: { languageOptions: readonly CodeBlockLanguageOption[]; lang?: string | null; }) { const label = getCodeBlockLanguageLabel(lang, languageOptions); if (!label) return null; return ( {label} ); } function CopyButton({ value, ...props }: { value: (() => string) | string } & Omit< React.ComponentProps, 'value' >) { const [status, setStatus] = React.useState< 'idle' | 'copying' | 'copied' | 'failed' >('idle'); React.useEffect(() => { if (status !== 'copied') return undefined; const timeout = setTimeout(() => { setStatus('idle'); }, 2000); return () => { clearTimeout(timeout); }; }, [status]); return ( ); } export const createCodeBlockPlugin = ( component: CodeBlockComponent, options?: { normalizeLanguage?: (language: string) => string | undefined; } ) => { const normalizeLanguage = options?.normalizeLanguage ?? resolveCodeBlockLanguage; return CodeBlockPlugin.configure({ component, inputRules: [ CodeBlockRules.markdown({ normalizeLanguage, on: 'space', }), CodeBlockRules.markdown({ normalizeLanguage, on: 'break', }), ], shortcuts: { toggle: { keys: 'mod+alt+8' } }, }); };