Private
Public Access
Add a collapsible markdown formatting toolbar to the composer (#75)
Markdown support was already complete and documented, but nothing in the composer pointed at it -- unlike @, # and : (which advertise themselves when typed), formatting syntax was only discoverable via Help, buried in the account menu. The composer stays a plain markdown textarea; a new "Aa" button opens a toolbar that inserts the syntax for you: bold, italic, strikethrough, inline code, code block, link, quote, bulleted/numbered lists, H1-H3, sub/superscript. Collapsed by default, remembered across reloads and room switches. Clicking a button again removes the formatting (native undo doesn't track programmatic edits), and bold/italic/sub/strike toggles are run-length aware so "***x***" is handled correctly. The selection math is pure functions in lib/markdownFormat.ts, unit-tested. The "?" opens /help#formatting in a new tab so the draft isn't lost; HelpPage now scrolls to the hash once the guide has loaded, since the browser's own hash scroll fired before the async-fetched headings existed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c54c1bf97d
commit
6975ed4dc0
11 files changed
+682
-2
No files matched your search
@@ -105,6 +105,34 @@
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.composer-format-toggle {
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
flex: none;
|
||||
border-radius: var(--radius);
|
||||
background: transparent;
|
||||
color: var(--ds-muted);
|
||||
border: 1px solid var(--ds-border);
|
||||
font-size: 0.82rem;
|
||||
font-weight: 700;
|
||||
line-height: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.composer-format-toggle:hover:not(:disabled),
|
||||
.composer-format-toggle-open {
|
||||
color: var(--ds-text);
|
||||
border-color: var(--ds-accent);
|
||||
}
|
||||
|
||||
.composer-format-toggle:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.composer-attach-wrap {
|
||||
position: relative;
|
||||
flex: none;
|
||||
|
||||
@@ -15,10 +15,13 @@ import { getUploadLimit } from '../api/uploads'
|
||||
import { useCustomEmoji } from '../context/CustomEmojiContext'
|
||||
import { EMOJI_SHORTCODES, SHORTCODE_BY_GLYPH } from '../lib/emojiShortcodes'
|
||||
import { formatFileSize } from '../lib/fileSize'
|
||||
import { getFormattingToolbarOpen, setFormattingToolbarOpen } from '../lib/formattingToolbarPref'
|
||||
import { applyFormat, type FormatAction } from '../lib/markdownFormat'
|
||||
import { getRecentEmoji, recordEmojiUsed } from '../lib/recentEmoji'
|
||||
import type { MyRoomItem, RoomMember } from '../types'
|
||||
import { EmojiPicker } from './EmojiPicker'
|
||||
import { EmojiShortcodeAutocomplete, type EmojiShortcodeMatch } from './EmojiShortcodeAutocomplete'
|
||||
import { FormattingToolbar } from './FormattingToolbar'
|
||||
import { MentionAutocomplete } from './MentionAutocomplete'
|
||||
import { RoomReferenceAutocomplete } from './RoomReferenceAutocomplete'
|
||||
import './Composer.css'
|
||||
@@ -136,6 +139,7 @@ export function Composer({ roomId, roomName, isDm, members, rooms, disabled, arc
|
||||
const [emojiActiveIndex, setEmojiActiveIndex] = useState(0)
|
||||
const [dragActive, setDragActive] = useState(false)
|
||||
const [attachMenuOpen, setAttachMenuOpen] = useState(false)
|
||||
const [toolbarOpen, setToolbarOpen] = useState(getFormattingToolbarOpen)
|
||||
const { byShortcode: customEmojiByShortcode } = useCustomEmoji()
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null)
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
@@ -449,6 +453,29 @@ export function Composer({ roomId, roomName, isDm, members, rooms, disabled, arc
|
||||
})
|
||||
}
|
||||
|
||||
function toggleToolbar() {
|
||||
const next = !toolbarOpen
|
||||
setToolbarOpen(next)
|
||||
setFormattingToolbarOpen(next)
|
||||
}
|
||||
|
||||
// #75: the toolbar only ever edits the textarea's plain markdown text --
|
||||
// nothing about what gets sent changes.
|
||||
function applyFormatAction(action: FormatAction) {
|
||||
const el = textareaRef.current
|
||||
if (!el) return
|
||||
const result = applyFormat(
|
||||
{ value, start: el.selectionStart ?? value.length, end: el.selectionEnd ?? value.length },
|
||||
action,
|
||||
)
|
||||
setValue(result.value)
|
||||
requestAnimationFrame(() => {
|
||||
el.focus()
|
||||
el.setSelectionRange(result.start, result.end)
|
||||
autoGrow()
|
||||
})
|
||||
}
|
||||
|
||||
function insertEmoji(emoji: string) {
|
||||
const el = textareaRef.current
|
||||
setEmojiPickerOpen(false)
|
||||
@@ -517,6 +544,9 @@ export function Composer({ roomId, roomName, isDm, members, rooms, disabled, arc
|
||||
</div>
|
||||
)}
|
||||
{uploadError && <div className="composer-status composer-error">{uploadError}</div>}
|
||||
{toolbarOpen && (
|
||||
<FormattingToolbar id="composer-formatting-toolbar" disabled={isDisabled} onAction={applyFormatAction} />
|
||||
)}
|
||||
<div className="composer-box">
|
||||
<input
|
||||
ref={photoInputRef}
|
||||
@@ -589,6 +619,18 @@ export function Composer({ roomId, roomName, isDm, members, rooms, disabled, arc
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className={`composer-format-toggle${toolbarOpen ? ' composer-format-toggle-open' : ''}`}
|
||||
onClick={toggleToolbar}
|
||||
disabled={isDisabled}
|
||||
aria-label="Formatting options"
|
||||
aria-expanded={toolbarOpen}
|
||||
aria-controls="composer-formatting-toolbar"
|
||||
title="Formatting options"
|
||||
>
|
||||
Aa
|
||||
</button>
|
||||
<div className="composer-textarea-wrap">
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
.formatting-toolbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
/* Spacing, not divider lines, between groups -- a border on every group
|
||||
after the first leaves a dangling line at the start of the second row
|
||||
whenever the toolbar wraps on a narrow screen. */
|
||||
gap: 2px 14px;
|
||||
}
|
||||
|
||||
.formatting-toolbar-group {
|
||||
display: flex;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.formatting-toolbar-btn {
|
||||
min-width: 30px;
|
||||
height: 30px;
|
||||
padding: 0 6px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
color: var(--ds-muted);
|
||||
font-size: 0.82rem;
|
||||
line-height: 1;
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.formatting-toolbar-btn:hover:not(:disabled) {
|
||||
background: var(--ds-surface-2);
|
||||
color: var(--ds-text);
|
||||
}
|
||||
|
||||
.formatting-toolbar-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { FormatAction } from '../lib/markdownFormat'
|
||||
import { FormattingToolbar } from './FormattingToolbar'
|
||||
|
||||
afterEach(cleanup)
|
||||
|
||||
const EXPECTED: [string, FormatAction][] = [
|
||||
['Bold', { kind: 'inline', marker: '**' }],
|
||||
['Italic', { kind: 'inline', marker: '*' }],
|
||||
['Strikethrough', { kind: 'inline', marker: '~~' }],
|
||||
['Inline code', { kind: 'inline', marker: '`' }],
|
||||
['Code block', { kind: 'codeBlock' }],
|
||||
['Link', { kind: 'link' }],
|
||||
['Quote', { kind: 'prefix', prefix: 'quote' }],
|
||||
['Bulleted list', { kind: 'prefix', prefix: 'bullet' }],
|
||||
['Numbered list', { kind: 'prefix', prefix: 'ordered' }],
|
||||
['Heading 1', { kind: 'heading', level: 1 }],
|
||||
['Heading 2', { kind: 'heading', level: 2 }],
|
||||
['Heading 3', { kind: 'heading', level: 3 }],
|
||||
['Subscript', { kind: 'inline', marker: '~' }],
|
||||
['Superscript', { kind: 'inline', marker: '^' }],
|
||||
]
|
||||
|
||||
describe('FormattingToolbar', () => {
|
||||
it.each(EXPECTED)('%s button calls onAction with its action', (label, action) => {
|
||||
const onAction = vi.fn()
|
||||
render(<FormattingToolbar id="t" onAction={onAction} />)
|
||||
fireEvent.click(screen.getByRole('button', { name: label }))
|
||||
expect(onAction).toHaveBeenCalledTimes(1)
|
||||
expect(onAction).toHaveBeenCalledWith(action)
|
||||
})
|
||||
|
||||
it('disables every button when disabled, but leaves the help link usable', () => {
|
||||
render(<FormattingToolbar id="t" disabled onAction={vi.fn()} />)
|
||||
const buttons = screen.getAllByRole('button') as HTMLButtonElement[]
|
||||
expect(buttons).toHaveLength(EXPECTED.length)
|
||||
expect(buttons.every((b) => b.disabled)).toBe(true)
|
||||
expect(screen.getByRole('link', { name: 'Formatting help' })).toBeTruthy()
|
||||
})
|
||||
|
||||
it('opens help in a new tab so the draft in the composer is not lost', () => {
|
||||
render(<FormattingToolbar id="t" onAction={vi.fn()} />)
|
||||
const help = screen.getByRole('link', { name: 'Formatting help' })
|
||||
expect(help.getAttribute('href')).toBe('/help#formatting')
|
||||
expect(help.getAttribute('target')).toBe('_blank')
|
||||
expect(help.getAttribute('rel')).toContain('noopener')
|
||||
})
|
||||
|
||||
it('keeps focus in the textarea by cancelling mousedown on buttons', () => {
|
||||
render(<FormattingToolbar id="t" onAction={vi.fn()} />)
|
||||
// fireEvent returns false when the event's default was prevented.
|
||||
expect(fireEvent.mouseDown(screen.getByRole('button', { name: 'Bold' }))).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,104 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import type { FormatAction, InlineMarker } from '../lib/markdownFormat'
|
||||
import './FormattingToolbar.css'
|
||||
|
||||
interface FormattingToolbarProps {
|
||||
id: string
|
||||
disabled?: boolean
|
||||
onAction: (action: FormatAction) => void
|
||||
}
|
||||
|
||||
interface ToolbarButton {
|
||||
label: string
|
||||
// Shows the syntax the button inserts -- the toolbar doubles as the
|
||||
// "quick guide" for people who'd rather learn to type it themselves.
|
||||
title: string
|
||||
content: ReactNode
|
||||
action: FormatAction
|
||||
}
|
||||
|
||||
const inline = (marker: InlineMarker): FormatAction => ({ kind: 'inline', marker })
|
||||
|
||||
const GROUPS: ToolbarButton[][] = [
|
||||
[
|
||||
{ label: 'Bold', title: 'Bold: **text**', content: <b>B</b>, action: inline('**') },
|
||||
{ label: 'Italic', title: 'Italic: *text*', content: <i>I</i>, action: inline('*') },
|
||||
{ label: 'Strikethrough', title: 'Strikethrough: ~~text~~', content: <s>S</s>, action: inline('~~') },
|
||||
],
|
||||
[
|
||||
{ label: 'Inline code', title: 'Inline code: `code`', content: '</>', action: inline('`') },
|
||||
{ label: 'Code block', title: 'Code block: ``` on its own lines', content: '{ }', action: { kind: 'codeBlock' } },
|
||||
],
|
||||
[
|
||||
{
|
||||
label: 'Link',
|
||||
title: 'Link: [text](url)',
|
||||
content: (
|
||||
<svg width="15" height="15" viewBox="0 0 20 20" fill="none" aria-hidden="true">
|
||||
<path
|
||||
d="M8.5 11.5a3 3 0 0 0 4.2 0l2.6-2.6a3 3 0 0 0-4.2-4.2l-.9.9M11.5 8.5a3 3 0 0 0-4.2 0l-2.6 2.6a3 3 0 0 0 4.2 4.2l.9-.9"
|
||||
stroke="currentColor"
|
||||
strokeWidth="1.6"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
action: { kind: 'link' },
|
||||
},
|
||||
{ label: 'Quote', title: 'Quote: > text', content: '“', action: { kind: 'prefix', prefix: 'quote' } },
|
||||
],
|
||||
[
|
||||
{ label: 'Bulleted list', title: 'Bulleted list: - item', content: '•', action: { kind: 'prefix', prefix: 'bullet' } },
|
||||
{ label: 'Numbered list', title: 'Numbered list: 1. item', content: '1.', action: { kind: 'prefix', prefix: 'ordered' } },
|
||||
],
|
||||
[
|
||||
{ label: 'Heading 1', title: 'Heading 1: # text', content: 'H1', action: { kind: 'heading', level: 1 } },
|
||||
{ label: 'Heading 2', title: 'Heading 2: ## text', content: 'H2', action: { kind: 'heading', level: 2 } },
|
||||
{ label: 'Heading 3', title: 'Heading 3: ### text', content: 'H3', action: { kind: 'heading', level: 3 } },
|
||||
],
|
||||
[
|
||||
{ label: 'Subscript', title: 'Subscript: ~text~ (no spaces)', content: 'x₂', action: inline('~') },
|
||||
{ label: 'Superscript', title: 'Superscript: ^text^ (no spaces)', content: 'x²', action: inline('^') },
|
||||
],
|
||||
]
|
||||
|
||||
export function FormattingToolbar({ id, disabled, onAction }: FormattingToolbarProps) {
|
||||
return (
|
||||
<div id={id} className="formatting-toolbar" role="toolbar" aria-label="Formatting">
|
||||
{GROUPS.map((group) => (
|
||||
<div key={group[0].label} className="formatting-toolbar-group">
|
||||
{group.map((button) => (
|
||||
<button
|
||||
key={button.label}
|
||||
type="button"
|
||||
className="formatting-toolbar-btn"
|
||||
title={button.title}
|
||||
aria-label={button.label}
|
||||
disabled={disabled}
|
||||
// Keeps focus (and the selection) in the textarea -- a click
|
||||
// on a button would otherwise blur it, and on mobile dismiss
|
||||
// the keyboard.
|
||||
onMouseDown={(e) => e.preventDefault()}
|
||||
onClick={() => onAction(button.action)}
|
||||
>
|
||||
{button.content}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
{/* A new tab on purpose: the draft lives in Composer's own state, so
|
||||
navigating this one away would lose it. */}
|
||||
<a
|
||||
className="formatting-toolbar-btn formatting-toolbar-help"
|
||||
href="/help#formatting"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
title="Formatting help (opens in a new tab)"
|
||||
aria-label="Formatting help"
|
||||
>
|
||||
?
|
||||
</a>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user