Files
TREK/client/src/components/shared/MarkdownEditable.tsx
T
Maurice 8fd46c4c18 feat: quality of life sweep from the feature discussions
Small requests from the discussions, each built end to end: planner and
map (cycling mode, date first headings, clear a day, stay times per day,
off-route stops, map lock, compact unplanned markers, pending stays,
cable car, transit quick picks), places (location filter, category from
OSM, open in maps, geo: link, image from an attachment, enrichment on
file import, places near a pin, place name language, Amap first in
China), lists and costs (packed counter, inherited sharing, Markdown
to-dos, payment notes, bookings CSV, my plan PDF), sharing (unplanned
places, travel only, without photos), journey (photo zoom, drafts,
photo order, trip suggestion, day jump), atlas (region counter, region
bucket list, places per country), vacay half company holidays, studio
copy and paste plus the book's own layouts, and RESTORE_FROM_BACKUP for
a first start restore.
2026-09-30 23:00:12 +02:00

47 lines
1.7 KiB
TypeScript

import { useState, type CSSProperties, type ReactNode } from 'react'
import MarkdownText from './MarkdownText'
import { Tooltip } from './Tooltip'
interface EditorProps {
autoFocus: boolean
/** Holds the editor open while it has focus, so typing into an empty field does not flip it to text. */
onFocus: () => void
onBlur: () => void
}
/**
* A Markdown field that reads as rendered text and turns into its textarea on a
* click (#2558): a link in a to-do description is a link, not a line of syntax.
* An empty field, or one being written, is the editor straight away. A click that
* lands on a link follows the link and leaves the text alone.
*/
export default function MarkdownEditable({ value, canEdit, editLabel, renderEditor, className = '', style }: {
value: string
canEdit: boolean
/** The hint on the rendered text, which is what opens the editor. */
editLabel: string
renderEditor: (props: EditorProps) => ReactNode
className?: string
style?: CSSProperties
}) {
const [editing, setEditing] = useState(false)
if (canEdit && (editing || !value.trim())) {
return <>{renderEditor({ autoFocus: editing, onFocus: () => setEditing(true), onBlur: () => setEditing(false) })}</>
}
if (!canEdit) return <MarkdownText className={className}>{value}</MarkdownText>
return (
<Tooltip label={editLabel} placement="top">
<div
role="button"
tabIndex={0}
onClick={e => { if (!(e.target as HTMLElement).closest('a')) setEditing(true) }}
onKeyDown={e => { if (e.key === 'Enter' && e.target === e.currentTarget) { e.preventDefault(); setEditing(true) } }}
className={`cursor-text ${className}`}
style={style}
>
<MarkdownText>{value}</MarkdownText>
</div>
</Tooltip>
)
}