diff --git a/client/src/components/shared/NoteFormatToolbar.test.tsx b/client/src/components/shared/NoteFormatToolbar.test.tsx index a9e7fb3d7..c27b41771 100644 --- a/client/src/components/shared/NoteFormatToolbar.test.tsx +++ b/client/src/components/shared/NoteFormatToolbar.test.tsx @@ -74,6 +74,32 @@ describe('NoteFormatToolbar', () => { expect(field().value).toBe('already a list') }) + it('FE-NOTEBAR-010: pressing the numbered list button on each new line counts up instead of repeating 1.', async () => { + const user = userEvent.setup() + render() + const numbered = screen.getByRole('button', { name: /numbered list/i }) + select(0, 0) + + for (const [i, item] of ['Breakfast', 'Lunch', 'Dinner'].entries()) { + if (i > 0) await user.keyboard('{Enter}') + await user.click(numbered) + await waitFor(() => expect(field().selectionStart).toBe(field().value.length)) + await user.keyboard(item) + } + + expect(field().value).toBe('1. Breakfast\n2. Lunch\n3. Dinner') + }) + + it('FE-NOTEBAR-011: a multi-line selection gets a marker on every line', async () => { + const user = userEvent.setup() + render() + select(0, 22) + + await user.click(screen.getByRole('button', { name: /numbered list/i })) + + expect(field().value).toBe('1. Breakfast\n2. Lunch\n3. Dinner') + }) + it('FE-NOTEBAR-005: the link button leaves a placeholder URL to type over', async () => { const user = userEvent.setup() render() diff --git a/client/src/components/shared/NoteFormatToolbar.tsx b/client/src/components/shared/NoteFormatToolbar.tsx index a6fb78314..d01c7017c 100644 --- a/client/src/components/shared/NoteFormatToolbar.tsx +++ b/client/src/components/shared/NoteFormatToolbar.tsx @@ -2,10 +2,11 @@ import { useRef } from 'react' import { Bold, Italic, Strikethrough, List, ListOrdered, Link2, Quote, Code } from 'lucide-react' import { useTranslation } from '../../i18n' import { Tooltip } from './Tooltip' +import { toggleLineMarker, type LineMarker } from './noteLineMarker' type FormatAction = | { type: 'wrap'; before: string; after: string } - | { type: 'line'; prefix: string } + | { type: 'line'; marker: LineMarker } interface Props { /** The field being formatted. Must be mounted before a button is pressed. */ @@ -26,9 +27,9 @@ const ACTIONS: Array<{ icon: typeof Bold; labelKey: string; action: FormatAction { icon: Strikethrough, labelKey: 'notes.format.strike', action: { type: 'wrap', before: '~~', after: '~~' } }, { icon: Code, labelKey: 'notes.format.code', action: { type: 'wrap', before: '`', after: '`' } }, { icon: Link2, labelKey: 'notes.format.link', action: { type: 'wrap', before: '[', after: '](https://)' } }, - { icon: List, labelKey: 'notes.format.list', action: { type: 'line', prefix: '- ' } }, - { icon: ListOrdered, labelKey: 'notes.format.orderedList', action: { type: 'line', prefix: '1. ' } }, - { icon: Quote, labelKey: 'notes.format.quote', action: { type: 'line', prefix: '> ' } }, + { icon: List, labelKey: 'notes.format.list', action: { type: 'line', marker: 'bullet' } }, + { icon: ListOrdered, labelKey: 'notes.format.orderedList', action: { type: 'line', marker: 'ordered' } }, + { icon: Quote, labelKey: 'notes.format.quote', action: { type: 'line', marker: 'quote' } }, ] /** @@ -68,13 +69,9 @@ export default function NoteFormatToolbar({ textareaRef, onChange, compact, vari ? [start + action.before.length, end + action.before.length] : [start + action.before.length, start + action.before.length] } else { - const lineStart = value.lastIndexOf('\n', start - 1) + 1 - const alreadyPrefixed = value.startsWith(action.prefix, lineStart) - next = alreadyPrefixed - ? value.slice(0, lineStart) + value.slice(lineStart + action.prefix.length) - : value.slice(0, lineStart) + action.prefix + value.slice(lineStart) - const shift = alreadyPrefixed ? -action.prefix.length : action.prefix.length - caret = [Math.max(lineStart, start + shift), Math.max(lineStart, end + shift)] + const result = toggleLineMarker(value, start, end, action.marker) + next = result.value + caret = result.selection } pendingSelection.current = caret diff --git a/client/src/components/shared/noteLineMarker.test.ts b/client/src/components/shared/noteLineMarker.test.ts new file mode 100644 index 000000000..952f0cb2d --- /dev/null +++ b/client/src/components/shared/noteLineMarker.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, it } from 'vitest'; +import { toggleLineMarker } from './noteLineMarker'; + +describe('toggleLineMarker', () => { + it('FE-NOTELINE-001: numbers a new item after the one above instead of starting at 1 again', () => { + const value = '1. Breakfast\nLunch'; + expect(toggleLineMarker(value, value.length, value.length, 'ordered').value).toBe('1. Breakfast\n2. Lunch'); + }); + + it('FE-NOTELINE-002: keeps counting from whatever number the item above has', () => { + const value = '4. Museum\nDinner'; + expect(toggleLineMarker(value, value.length, value.length, 'ordered').value).toBe('4. Museum\n5. Dinner'); + }); + + it('FE-NOTELINE-003: starts at 1 when the line above is no numbered item', () => { + expect(toggleLineMarker('Meals\nBreakfast', 8, 8, 'ordered').value).toBe('Meals\n1. Breakfast'); + expect(toggleLineMarker('Breakfast', 0, 0, 'ordered').value).toBe('1. Breakfast'); + }); + + it('FE-NOTELINE-004: numbers every selected line in order and skips the blank ones', () => { + const value = 'Breakfast\nLunch\n\nDinner'; + const result = toggleLineMarker(value, 0, value.length, 'ordered'); + expect(result.value).toBe('1. Breakfast\n2. Lunch\n\n3. Dinner'); + expect(result.selection).toEqual([0, result.value.length]); + }); + + it('FE-NOTELINE-005: renumbers a selection that mixes numbered and plain lines', () => { + const value = '1. Breakfast\n1. Lunch\nDinner'; + expect(toggleLineMarker(value, 0, value.length, 'ordered').value).toBe('1. Breakfast\n2. Lunch\n3. Dinner'); + }); + + it('FE-NOTELINE-006: removes the numbers when every selected line already has one', () => { + const value = '1. Breakfast\n2. Lunch'; + expect(toggleLineMarker(value, 0, value.length, 'ordered').value).toBe('Breakfast\nLunch'); + }); + + it('FE-NOTELINE-007: removes the number from a single item, whatever the number is', () => { + const result = toggleLineMarker('1. a\n12. Dinner', 10, 10, 'ordered'); + expect(result.value).toBe('1. a\nDinner'); + expect(result.selection).toEqual([6, 6]); + }); + + it('FE-NOTELINE-008: bullets and quotes apply to each selected line, without doubling an existing marker', () => { + const value = 'one\n- two\nthree'; + expect(toggleLineMarker(value, 0, value.length, 'bullet').value).toBe('- one\n- two\n- three'); + expect(toggleLineMarker('one\ntwo', 0, 7, 'quote').value).toBe('> one\n> two'); + }); + + it('FE-NOTELINE-009: a selection ending right after a newline leaves the next line alone', () => { + const value = 'one\ntwo\nthree'; + expect(toggleLineMarker(value, 0, 8, 'bullet').value).toBe('- one\n- two\nthree'); + }); + + it('FE-NOTELINE-010: the caret moves with the text on a single line', () => { + const value = 'first\nsecond'; + expect(toggleLineMarker(value, 8, 8, 'bullet')).toEqual({ value: 'first\n- second', selection: [10, 10] }); + }); + + it('FE-NOTELINE-011: an empty line still gets the marker, so a list can be started on it', () => { + expect(toggleLineMarker('', 0, 0, 'ordered')).toEqual({ value: '1. ', selection: [3, 3] }); + expect(toggleLineMarker('1. Breakfast\n', 13, 13, 'ordered').value).toBe('1. Breakfast\n2. '); + }); +}); diff --git a/client/src/components/shared/noteLineMarker.ts b/client/src/components/shared/noteLineMarker.ts new file mode 100644 index 000000000..85bcd577b --- /dev/null +++ b/client/src/components/shared/noteLineMarker.ts @@ -0,0 +1,64 @@ +export type LineMarker = 'bullet' | 'ordered' | 'quote'; + +const PATTERN: Record = { + bullet: /^- /, + ordered: /^\d+\. /, + quote: /^> /, +}; + +const FIXED_PREFIX: Record, string> = { + bullet: '- ', + quote: '> ', +}; + +export interface LineMarkerResult { + value: string; + selection: [number, number]; +} + +/** The number an ordered item gets when it is added right below `lineStart`'s previous line. */ +function nextNumberAt(value: string, lineStart: number): number { + if (lineStart === 0) return 1; + const prevStart = value.lastIndexOf('\n', lineStart - 2) + 1; + const match = /^(\d+)\. /.exec(value.slice(prevStart, lineStart - 1)); + return match ? Number(match[1]) + 1 : 1; +} + +/** + * Adds or removes a line marker (bullet, number, quote) on every line the + * selection touches. + * + * Numbers continue from the item above, so pressing the button on each new + * line writes 1. 2. 3. instead of 1. 1. 1. Markdown renders both the same, + * but the field is what people read while they write. A selection over several + * lines is formatted line by line and skips the blank ones in between. + */ +export function toggleLineMarker(value: string, start: number, end: number, marker: LineMarker): LineMarkerResult { + const blockStart = value.lastIndexOf('\n', start - 1) + 1; + // A selection that ends right after a newline stops on the line above it. + const last = end > start && value[end - 1] === '\n' ? end - 1 : end; + const newline = value.indexOf('\n', last); + const blockEnd = newline === -1 ? value.length : newline; + + const lines = value.slice(blockStart, blockEnd).split('\n'); + const multi = lines.length > 1; + const formats = (line: string) => !multi || line.trim() !== ''; + const pattern = PATTERN[marker]; + const remove = lines.some(formats) && lines.filter(formats).every((line) => pattern.test(line)); + + let number = nextNumberAt(value, blockStart); + const next = lines.map((line) => { + if (!formats(line)) return line; + if (remove) return line.replace(pattern, ''); + if (marker === 'ordered') return `${number++}. ${line.replace(pattern, '')}`; + return pattern.test(line) ? line : FIXED_PREFIX[marker] + line; + }); + + const block = next.join('\n'); + const result = value.slice(0, blockStart) + block + value.slice(blockEnd); + + if (multi) return { value: result, selection: [blockStart, blockStart + block.length] }; + // One line: the caret moves with the text, never in front of the line. + const shift = block.length - (blockEnd - blockStart); + return { value: result, selection: [Math.max(blockStart, start + shift), Math.max(blockStart, end + shift)] }; +}