Add a visible Markdown formatting bar to pages and bookmark notes
Shared component (components/markdown-toolbar.tsx): bold/italic/ strikethrough/inline code/link with URL preselected, H1-H3, quote, bullet/numbered/to-do line-prefix toggles across the selection, divider. Mounted above the page editor textarea and the notes editor in the bookmark detail sheet; the textarea stays the source of truth so autosave/conflict handling and the notes save flow are untouched, and the page slash menu remains as the keyboard path. Gitea issue #7.
This commit is contained in:
@@ -17,6 +17,7 @@ import { Textarea } from '@/components/ui/textarea'
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { MarkdownToolbar } from '@/components/markdown-toolbar'
|
||||
import { PageAutosave, PageConflictError, type PageSaveState } from '@/lib/page-autosave'
|
||||
import {
|
||||
PAGES_CHANGED, collectionEmbedMarkdown, descendantPageIds, internalPageId, nextUntitledTitle,
|
||||
@@ -822,6 +823,7 @@ export function PagesClient({ initialId }: { initialId: string | null }) {
|
||||
|
||||
{mode === 'edit' ? (
|
||||
<div className="relative">
|
||||
<MarkdownToolbar textareaRef={textareaRef} value={content} onChange={onContentChange} className="mb-2" />
|
||||
<Textarea
|
||||
ref={textareaRef}
|
||||
value={content}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||
import { Sheet, SheetContent, SheetHeader, SheetTitle } from '@/components/ui/sheet'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@@ -11,6 +11,7 @@ import { toast } from 'sonner'
|
||||
import type { Bookmark } from '@/components/app-shell'
|
||||
import ReactMarkdown from 'react-markdown'
|
||||
import remarkGfm from 'remark-gfm'
|
||||
import { MarkdownToolbar } from '@/components/markdown-toolbar'
|
||||
import { BookmarkExtras } from '@/components/bookmark-extras'
|
||||
import { fetchReference } from '@/lib/reference-data'
|
||||
import { format } from 'date-fns'
|
||||
@@ -22,6 +23,7 @@ interface Props {
|
||||
}
|
||||
|
||||
export function BookmarkDetailSheet({ bookmark, onClose, onUpdate }: Props) {
|
||||
const notesRef = useRef<HTMLTextAreaElement>(null)
|
||||
const [editing, setEditing] = useState(false)
|
||||
const [notes, setNotes] = useState('')
|
||||
const [kanban, setKanban] = useState('INBOX')
|
||||
@@ -261,7 +263,10 @@ export function BookmarkDetailSheet({ bookmark, onClose, onUpdate }: Props) {
|
||||
</Button>
|
||||
</div>
|
||||
{editing ? (
|
||||
<Textarea value={notes} onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => setNotes(e.target.value)} rows={6} placeholder="Markdown notes..." />
|
||||
<div className="space-y-2">
|
||||
<MarkdownToolbar textareaRef={notesRef} value={notes} onChange={setNotes} />
|
||||
<Textarea ref={notesRef} value={notes} onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => setNotes(e.target.value)} rows={6} placeholder="Markdown notes..." />
|
||||
</div>
|
||||
) : (
|
||||
<div className="prose prose-sm dark:prose-invert max-w-none rounded-lg bg-muted/50 p-3 min-h-[60px]">
|
||||
{notes ? <ReactMarkdown remarkPlugins={[remarkGfm]}>{notes}</ReactMarkdown> : <span className="text-muted-foreground text-xs">No notes</span>}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
'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<HTMLTextAreaElement | null>
|
||||
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<Sel | null>(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 (
|
||||
<div className={`flex flex-wrap items-center gap-0.5 rounded-md border bg-muted/40 p-1 ${className ?? ''}`} role="toolbar" aria-label="Markdown formatting">
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Bold" title="Bold" onMouseDown={(e) => e.preventDefault()} onClick={() => wrap('**', 'bold text')}><Bold className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Italic" title="Italic" onMouseDown={(e) => e.preventDefault()} onClick={() => wrap('*', 'italic text')}><Italic className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Strikethrough" title="Strikethrough" onMouseDown={(e) => e.preventDefault()} onClick={() => wrap('~~', 'struck text')}><Strikethrough className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Inline code" title="Inline code" onMouseDown={(e) => e.preventDefault()} onClick={() => wrap('`', 'code')}><Code className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Link" title="Link" onMouseDown={(e) => e.preventDefault()} onClick={link}><Link2 className="h-3.5 w-3.5" /></Button>
|
||||
<span className="mx-1 h-4 w-px bg-border" aria-hidden />
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Heading 1" title="Heading 1" onMouseDown={(e) => e.preventDefault()} onClick={() => linePrefix('# ')}><Heading1 className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Heading 2" title="Heading 2" onMouseDown={(e) => e.preventDefault()} onClick={() => linePrefix('## ')}><Heading2 className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Heading 3" title="Heading 3" onMouseDown={(e) => e.preventDefault()} onClick={() => linePrefix('### ')}><Heading3 className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Quote" title="Quote" onMouseDown={(e) => e.preventDefault()} onClick={() => linePrefix('> ')}><Quote className="h-3.5 w-3.5" /></Button>
|
||||
<span className="mx-1 h-4 w-px bg-border" aria-hidden />
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Bulleted list" title="Bulleted list" onMouseDown={(e) => e.preventDefault()} onClick={() => linePrefix('- ')}><List className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Numbered list" title="Numbered list" onMouseDown={(e) => e.preventDefault()} onClick={() => linePrefix('1. ')}><ListOrdered className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="To-do list" title="To-do list" onMouseDown={(e) => e.preventDefault()} onClick={() => linePrefix('- [ ] ')}><ListTodo className="h-3.5 w-3.5" /></Button>
|
||||
<Button type="button" variant="ghost" size="sm" className={btn} aria-label="Divider" title="Divider" onMouseDown={(e) => e.preventDefault()} onClick={() => insertBlock('---\n')}><Minus className="h-3.5 w-3.5" /></Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user