diff --git a/src/components/CodeBlock.tsx b/src/components/CodeBlock.tsx index 1d785ce..9d7812a 100644 --- a/src/components/CodeBlock.tsx +++ b/src/components/CodeBlock.tsx @@ -1,5 +1,5 @@ import { useState, useCallback, type HTMLAttributes, type ReactElement } from 'react'; -import { Check, Copy, Hash } from 'lucide-react'; +import { Check, Copy, Hash, WrapText, AlignLeft } from 'lucide-react'; /** Extract the language from the nested element's className (e.g. "language-ts"). */ function extractLanguage(children: React.ReactNode): string | null { @@ -43,6 +43,7 @@ function formatLanguage(lang: string): string { * Wraps code blocks with a language label and a floating copy button. */ const LINE_NUMBER_KEY = 'pinchchat-line-numbers'; +const WRAP_KEY = 'pinchchat-code-wrap'; const LINE_THRESHOLD = 3; // Only show line numbers for blocks with more than this many lines export function CodeBlock(props: HTMLAttributes) { @@ -51,6 +52,10 @@ export function CodeBlock(props: HTMLAttributes) { const stored = localStorage.getItem(LINE_NUMBER_KEY); return stored === null ? true : stored === 'true'; }); + const [wordWrap, setWordWrap] = useState(() => { + const stored = localStorage.getItem(WRAP_KEY); + return stored === 'true'; + }); const language = extractLanguage(props.children); const code = (props.children as ReactElement<{ children?: string }> | undefined)?.props?.children; @@ -74,6 +79,14 @@ export function CodeBlock(props: HTMLAttributes) { }); }, []); + const toggleWrap = useCallback(() => { + setWordWrap(prev => { + const next = !prev; + localStorage.setItem(WRAP_KEY, String(next)); + return next; + }); + }, []); + const shouldShowNumbers = showLineNumbers && hasEnoughLines; return ( @@ -81,17 +94,27 @@ export function CodeBlock(props: HTMLAttributes) { {language && (
{formatLanguage(language)} - {hasEnoughLines && ( +
- )} + {hasEnoughLines && ( + + )} +
)} {shouldShowNumbers ? ( @@ -104,10 +127,10 @@ export function CodeBlock(props: HTMLAttributes) {
{i + 1}
))} -
+          
         
       ) : (
-        
+        
       )}