mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 13:36:48 +00:00
ce37e8e97c
* Rich formatting and colours for notes Asked for in discussion #1629: notes should carry formatting, links and colour, so a warning reads differently from a recommendation. Notes have rendered Markdown for a long time. The problem was that the only way to write it was a bare textarea, so unless you already knew the syntax there was nothing to tell you it existed. Both note fields get a formatting bar - bold, italic, strikethrough, code, link, lists, quote - which edits Markdown rather than replacing it with a rich-text model, so the stored value stays plain text that the PDF, the plugin API and every existing note keep reading unchanged. Links now open in their own tab. A bare anchor navigates the planner away, which loses the open day and the map, and without rel=noopener the opened page can navigate this one - notes are content other trip members wrote. Day notes get a colour from a short shared palette. It tints the card, the border and the icon circle, and it reaches the PDF as the spine and rule rather than a fill. The dialog is two columns now, with a live preview of the card beside the picker, because a row of swatches says nothing about what a tinted note looks like next to a place. On the phone the thirty-two icon grid was most of the screen, so it is folded behind the chosen icon, which doubles as the colour preview. The detail field there got the same formatting bar and the same 2000 character ceiling as the desktop one - they write the same column. * Fix the tests that pinned the old note caps, and rework the note actions The e2e suite builds its own day_notes table by hand rather than going through schema.ts, so it never got the new column and every note insert came back 500. Three more tests wrote down the 250 character body cap and the old create() arity. While in there: the edit and delete buttons on a note card were a 10px icon in a 14px box, side by side, and they took width away from the text on every note whether you were hovering or not. They are stacked in a capsule now, 30px targets with their own hover states, floating over the right edge of the card only while the pointer is on it. The text keeps the full width the rest of the time. * Centre the day badge when there is no weather to stack under it The badge is pinned to the top of the row because with weather it is a tall stack that has to start there. Without a location there is no weather block, so all that is left is a 26px circle floating above the day title. * Render the place subtitle in the places list, and drop the form hint The row in the places sidebar printed the description raw while the same line in the day plan renders it, so a place written with the formatting bar read as _underscores_ in one list and italics in the other. Still clamped to one line - it is a list entry, not the inspector. The "Markdown supported" line under the notes field goes: the bar above it already says so.
114 lines
4.2 KiB
TypeScript
114 lines
4.2 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest'
|
|
import { screen, waitFor } from '@testing-library/react'
|
|
import { render } from '../../../tests/helpers/render'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { useRef, useState } from 'react'
|
|
import NoteFormatToolbar from './NoteFormatToolbar'
|
|
|
|
/**
|
|
* The toolbar edits somebody else's controlled field, so every test drives it
|
|
* through a real textarea rather than asserting on a callback in isolation —
|
|
* the selection handling is the part that breaks, and it only exists in the DOM.
|
|
*/
|
|
function Harness({ initial = '', onValue }: { initial?: string; onValue?: (v: string) => void }) {
|
|
const ref = useRef<HTMLTextAreaElement | null>(null)
|
|
const [value, setValue] = useState(initial)
|
|
return (
|
|
<>
|
|
<NoteFormatToolbar textareaRef={ref} onChange={v => { setValue(v); onValue?.(v) }} />
|
|
<textarea ref={ref} aria-label="body" value={value} onChange={e => setValue(e.target.value)} />
|
|
</>
|
|
)
|
|
}
|
|
|
|
const field = () => screen.getByLabelText('body') as HTMLTextAreaElement
|
|
const select = (from: number, to: number) => {
|
|
const ta = field()
|
|
ta.focus()
|
|
ta.setSelectionRange(from, to)
|
|
}
|
|
|
|
describe('NoteFormatToolbar', () => {
|
|
it('FE-NOTEBAR-001: wraps the selection and leaves the selection on the text, not the markers', async () => {
|
|
const user = userEvent.setup()
|
|
render(<Harness initial="closes at 5pm" />)
|
|
select(0, 6)
|
|
|
|
await user.click(screen.getByRole('button', { name: /bold/i }))
|
|
|
|
expect(field().value).toBe('**closes** at 5pm')
|
|
// "closes", not "**closes**" — a second format nests instead of wrapping syntax.
|
|
// The selection is restored in a rAF, after the field holds the new text.
|
|
await waitFor(() => expect(field().value.slice(field().selectionStart, field().selectionEnd)).toBe('closes'))
|
|
})
|
|
|
|
it('FE-NOTEBAR-002: an empty selection inserts the markers and puts the caret between them', async () => {
|
|
const user = userEvent.setup()
|
|
render(<Harness initial="note" />)
|
|
select(4, 4)
|
|
|
|
await user.click(screen.getByRole('button', { name: /italic/i }))
|
|
|
|
expect(field().value).toBe('note__')
|
|
await waitFor(() => expect(field().selectionStart).toBe(5))
|
|
expect(field().selectionEnd).toBe(5)
|
|
})
|
|
|
|
it('FE-NOTEBAR-003: a list prefix applies to the line the caret is on, not the whole field', async () => {
|
|
const user = userEvent.setup()
|
|
render(<Harness initial={'first\nsecond'} />)
|
|
select(8, 8) // inside "second"
|
|
|
|
await user.click(screen.getByRole('button', { name: /bulleted list/i }))
|
|
|
|
expect(field().value).toBe('first\n- second')
|
|
})
|
|
|
|
it('FE-NOTEBAR-004: pressing the same line button again removes the prefix', async () => {
|
|
const user = userEvent.setup()
|
|
render(<Harness initial="- already a list" />)
|
|
select(5, 5)
|
|
|
|
await user.click(screen.getByRole('button', { name: /bulleted list/i }))
|
|
|
|
expect(field().value).toBe('already a list')
|
|
})
|
|
|
|
it('FE-NOTEBAR-005: the link button leaves a placeholder URL to type over', async () => {
|
|
const user = userEvent.setup()
|
|
render(<Harness initial="opening hours" />)
|
|
select(0, 13)
|
|
|
|
await user.click(screen.getByRole('button', { name: /^link$/i }))
|
|
|
|
expect(field().value).toBe('[opening hours](https://)')
|
|
})
|
|
|
|
it('FE-NOTEBAR-006: the buttons do not take focus away from the field', async () => {
|
|
const user = userEvent.setup()
|
|
render(<Harness initial="text" />)
|
|
select(0, 4)
|
|
|
|
await user.click(screen.getByRole('button', { name: /bold/i }))
|
|
|
|
// If the button had taken focus, the selection would be gone and the wrap
|
|
// would have landed at position 0 instead of around the word.
|
|
expect(field().value).toBe('**text**')
|
|
await waitFor(() => expect(document.activeElement).toBe(field()))
|
|
})
|
|
|
|
it('FE-NOTEBAR-007: does nothing when the field is not mounted', async () => {
|
|
const user = userEvent.setup()
|
|
const onChange = vi.fn()
|
|
function Detached() {
|
|
const ref = useRef<HTMLTextAreaElement | null>(null)
|
|
return <NoteFormatToolbar textareaRef={ref} onChange={onChange} />
|
|
}
|
|
render(<Detached />)
|
|
|
|
await user.click(screen.getByRole('button', { name: /bold/i }))
|
|
|
|
expect(onChange).not.toHaveBeenCalled()
|
|
})
|
|
})
|