|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import cn from 'clsx'; |
| 4 | +import { Check, Copy, WrapText } from 'lucide-react'; |
| 5 | +import { type FC, type HTMLAttributes, type MouseEvent, type ReactNode, useEffect, useState } from 'react'; |
| 6 | +import type { ButtonProps } from 'soybean-react-ui'; |
| 7 | +import { Button } from 'soybean-react-ui'; |
| 8 | + |
| 9 | +/* -------------------- ToggleWordWrapButton -------------------- */ |
| 10 | +function toggleWordWrap() { |
| 11 | + const htmlDataset = document.documentElement.dataset; |
| 12 | + if ('nextraWordWrap' in htmlDataset) delete htmlDataset.nextraWordWrap; |
| 13 | + else htmlDataset.nextraWordWrap = ''; |
| 14 | +} |
| 15 | + |
| 16 | +export const ToggleWordWrapButton: FC<{ children?: ReactNode }> = ({ children }) => ( |
| 17 | + <Button |
| 18 | + className="flex items-center gap-1 rounded-md border border-border/50 bg-transparent hover:bg-muted" |
| 19 | + size="sm" |
| 20 | + title="切换自动换行" |
| 21 | + variant="outline" |
| 22 | + onClick={toggleWordWrap} |
| 23 | + > |
| 24 | + {children ?? <WrapText size={16} />} |
| 25 | + </Button> |
| 26 | +); |
| 27 | + |
| 28 | +/* -------------------- Pre 组件 -------------------- */ |
| 29 | +export interface PreProps extends HTMLAttributes<HTMLPreElement> { |
| 30 | + 'data-copy'?: ''; |
| 31 | + 'data-filename'?: string; |
| 32 | + 'data-language'?: string; |
| 33 | + 'data-word-wrap'?: ''; |
| 34 | + icon?: ReactNode; |
| 35 | +} |
| 36 | + |
| 37 | +export const Pre: FC<PreProps> = rest => { |
| 38 | + const { |
| 39 | + children, |
| 40 | + className, |
| 41 | + 'data-copy': copy, |
| 42 | + 'data-filename': filename, |
| 43 | + 'data-language': _lang, |
| 44 | + 'data-word-wrap': hasWordWrap, |
| 45 | + icon, |
| 46 | + ...props |
| 47 | + } = rest; |
| 48 | + |
| 49 | + const copyButton = copy === '' && <CopyToClipboard />; |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="code-block relative my-6 w-full"> |
| 53 | + {/* 文件名栏 */} |
| 54 | + {filename && ( |
| 55 | + <div |
| 56 | + className={cn( |
| 57 | + 'flex items-center justify-between gap-2 rounded-t-md border border-border/50 border-b-0', |
| 58 | + 'bg-gray-100 dark:bg-neutral-900 px-4 py-2 text-xs text-gray-700 dark:text-gray-200' |
| 59 | + )} |
| 60 | + > |
| 61 | + <div className="flex items-center gap-2 min-w-0"> |
| 62 | + {icon} |
| 63 | + <span className="truncate">{filename}</span> |
| 64 | + </div> |
| 65 | + {copyButton} |
| 66 | + </div> |
| 67 | + )} |
| 68 | + |
| 69 | + {/* 代码区 */} |
| 70 | + <div |
| 71 | + className={cn( |
| 72 | + 'relative group rounded-b-md border border-border/50 bg-white dark:bg-black', |
| 73 | + filename ? 'rounded-t-none' : 'rounded-md' |
| 74 | + )} |
| 75 | + > |
| 76 | + {/* hover 按钮区 */} |
| 77 | + <div |
| 78 | + className={cn( |
| 79 | + 'absolute right-3 flex gap-2 transition-opacity', |
| 80 | + 'opacity-0 group-hover:opacity-100 focus-within:opacity-100', |
| 81 | + filename ? 'top-3' : 'top-3' |
| 82 | + )} |
| 83 | + > |
| 84 | + {hasWordWrap === '' && <ToggleWordWrapButton />} |
| 85 | + {!filename && copyButton} |
| 86 | + </div> |
| 87 | + |
| 88 | + <pre |
| 89 | + className={cn( |
| 90 | + 'overflow-x-auto p-4 text-sm leading-relaxed subpixel-antialiased not-prose', |
| 91 | + 'bg-transparent text-foreground dark:text-foreground/90', |
| 92 | + 'font-mono', |
| 93 | + className |
| 94 | + )} |
| 95 | + {...props} |
| 96 | + > |
| 97 | + {children} |
| 98 | + </pre> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +}; |
0 commit comments