'use client' // Shared Markdown formatting bar (Gitea-issue #7): a visible, always-there // toolbar for Markdown textareas — pages AND bookmark notes. The slash menu // in the page editor stays (keyboard path); this is the discoverable mouse // path david asked for ("the / menu may be missed"). // // The textarea remains the source of truth: every action rewrites the value // through the parent's onChange and restores focus/selection afterwards, so // autosave, conflict handling and the notes save flow are untouched. import { useEffect, useRef } from 'react' import { Button } from '@/components/ui/button' import { Bold, Code, Heading1, Heading2, Heading3, Italic, Link2, List, ListOrdered, ListTodo, Minus, Quote, Strikethrough, } from 'lucide-react' type Props = { textareaRef: React.RefObject value: string onChange: (next: string) => void className?: string } type Sel = [number, number] export function MarkdownToolbar({ textareaRef, value, onChange, className }: Props) { // Selection to restore once the rewritten value reaches the textarea. const pendingSel = useRef(null) useEffect(() => { const sel = pendingSel.current const ta = textareaRef.current if (!sel || !ta) return pendingSel.current = null ta.focus() ta.setSelectionRange(sel[0], sel[1]) }, [value, textareaRef]) const currentSel = (): Sel => { const ta = textareaRef.current if (ta) return [ta.selectionStart, ta.selectionEnd] return [value.length, value.length] } const commit = (next: string, sel: Sel) => { pendingSel.current = sel onChange(next) } // Wrap the selection (or an inserted placeholder) in a marker pair. const wrap = (marker: string, placeholder: string) => { const [s, e] = currentSel() const inner = value.slice(s, e) || placeholder const next = value.slice(0, s) + marker + inner + marker + value.slice(e) commit(next, [s + marker.length, s + marker.length + inner.length]) } // Toggle a line prefix on every line the selection touches. const linePrefix = (prefix: string) => { const [s, e] = currentSel() const start = value.lastIndexOf('\n', Math.max(0, s - 1)) + 1 const endIdx = value.indexOf('\n', e) const end = endIdx === -1 ? value.length : endIdx const block = value.slice(start, end) const lines = block.split('\n') const allHave = lines.every((l) => l.startsWith(prefix)) const nextBlock = lines .map((l) => (allHave ? l.slice(prefix.length) : (l.startsWith(prefix) ? l : prefix + l))) .join('\n') const next = value.slice(0, start) + nextBlock + value.slice(end) commit(next, [start, start + nextBlock.length]) } // Insert a standalone block at the caret with blank-line padding. const insertBlock = (snippet: string) => { const [s] = currentSel() const before = value.slice(0, s) const after = value.slice(s) const lead = before && !before.endsWith('\n') ? '\n\n' : '' const trail = after.startsWith('\n') ? '' : '\n' const next = before + lead + snippet + trail + after const at = before.length + lead.length + snippet.length commit(next, [at, at]) } const link = () => { const [s, e] = currentSel() const label = value.slice(s, e) || 'label' const snippet = `[${label}](https://)` const next = value.slice(0, s) + snippet + value.slice(e) // Select the URL part so typing replaces it. const urlStart = s + label.length + 3 commit(next, [urlStart, urlStart + 8]) } const btn = 'h-7 w-7 p-0 text-muted-foreground hover:text-foreground' return (
) }