Reading mode: every page and section reads as text
The reader (ReaderView.tsx) now reads an icon-only link's label, puts a card's link on its first line or picture when it has no heading, gives each item of a row of facts or links its own line, keeps line breaks, skips what CSS hides, turns a form into one line saying it can't be used there, shows empty table cells as "-", keeps section and heading ids so #links land, and opens file links as files. What it can't read draws its own text version (useReadingMode()): the hero, pathfinder, front page history, event lists (every event, past ones too), org lists and groups map (every region and group, linked), history timeline (every year open), schedules (every day, fold-outs included), people tiles (everyone, with details) and videos. Also: org names are linked headings, heading levels fixed where cards repeated their section's, Team pages lose the doubled "Members", Giving links the treasurer's email and names Church Center. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c79910ef8d
commit
27e5fbd690
24 changed files with 788 additions and 180 deletions
|
|
@ -14,26 +14,46 @@
|
|||
h1 to h6 headings, at their level
|
||||
p, li, td, quote... paragraphs, with their links
|
||||
a links (a card that is one link gives its
|
||||
heading the link)
|
||||
heading the link, or with none its first
|
||||
line or picture); an icon-only link reads
|
||||
its aria-label
|
||||
br a line break
|
||||
a part drawn on its a line of its own: a row of facts or link
|
||||
own line buttons reads one per line
|
||||
form one line saying it can't be used here
|
||||
img with alt text pictures; one with empty alt is decoration
|
||||
(a card's faded background) and is left out
|
||||
a button that opens its words, as a line: a person's tile, a
|
||||
something schedule row (aria-expanded)
|
||||
What's dropped: other buttons, form fields, navigation lists, video and
|
||||
map frames, drawings, and anything hidden from screen readers
|
||||
(aria-hidden, the copies a carousel draws for its turns).
|
||||
map frames, drawings, anything hidden from screen readers
|
||||
(aria-hidden, the copies a carousel draws for its turns) and
|
||||
anything CSS hides (display: none, eg. a print-only footer).
|
||||
|
||||
Ids on sections and headings are kept, so #links still land.
|
||||
|
||||
What this can't do well (a carousel showing one card, tabs,
|
||||
details opened by a click, a count waiting to be scrolled to) the
|
||||
component draws as text itself when useReadingMode() is on.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { useEffect, useState, type ReactNode, type RefObject } from 'react'
|
||||
import { Fragment, useEffect, useState, type ReactNode, type RefObject } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
|
||||
import { PAGE_LINKS } from '../navConfig.ts'
|
||||
|
||||
type Inline = string | { href: string; children: Inline[] }
|
||||
type Block =
|
||||
|
||||
/* A <br> in the page, kept as a line break (an address, a poem). */
|
||||
const BREAK = '
'
|
||||
/* `ids` are the page's own ids on a section or heading (a section
|
||||
and its heading can both have one), kept so a #link (the side
|
||||
nav, a "Jump to" button) still lands. */
|
||||
type Block = { ids?: string[] } & (
|
||||
| { kind: 'heading'; level: number; content: Inline[] }
|
||||
| { kind: 'text'; content: Inline[] }
|
||||
| { kind: 'image'; src: string; alt: string }
|
||||
| { kind: 'image'; src: string; alt: string; href?: string }
|
||||
)
|
||||
|
||||
const SKIP = new Set(['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'NAV', 'IFRAME', 'VIDEO', 'AUDIO', 'CANVAS', 'DIALOG', 'svg'])
|
||||
const HEADINGS = new Set(['H1', 'H2', 'H3', 'H4', 'H5', 'H6'])
|
||||
|
|
@ -48,7 +68,16 @@ const skipped = (el: Element) =>
|
|||
(SKIP.has(el.tagName) && !opens(el)) ||
|
||||
el.getAttribute('aria-hidden') === 'true' ||
|
||||
el.hasAttribute('hidden') ||
|
||||
['button', 'switch', 'tab', 'slider', 'presentation', 'none'].includes(el.getAttribute('role') ?? '')
|
||||
['button', 'switch', 'tab', 'slider', 'presentation', 'none'].includes(el.getAttribute('role') ?? '') ||
|
||||
// Hidden by CSS: a print-only footer, a phone-only copy.
|
||||
getComputedStyle(el).display === 'none'
|
||||
|
||||
/* Drawn on a line of its own (a flex row's items, a block link): a
|
||||
row of facts or link buttons reads one per line, not run together. */
|
||||
const ownLine = (el: Element) => {
|
||||
const display = getComputedStyle(el).display
|
||||
return !display.startsWith('inline') && display !== 'contents'
|
||||
}
|
||||
|
||||
/* A link's address as the app routes it: a path for this site, so
|
||||
it opens without reloading; the full address for anywhere else. */
|
||||
|
|
@ -66,9 +95,12 @@ function hrefOf(a: HTMLAnchorElement): string | null {
|
|||
function inline(node: Node): Inline[] {
|
||||
if (node.nodeType === Node.TEXT_NODE) return [node.textContent ?? '']
|
||||
if (!(node instanceof Element) || skipped(node) || node.tagName === 'IMG') return []
|
||||
if (node.tagName === 'BR') return [' ']
|
||||
const children = [...node.childNodes].flatMap(inline)
|
||||
if (node.tagName === 'BR') return [BREAK]
|
||||
let children = [...node.childNodes].flatMap(inline)
|
||||
const href = node instanceof HTMLAnchorElement ? hrefOf(node) : null
|
||||
// An icon-only link (an arrow) names itself in aria-label.
|
||||
const label = node.getAttribute('aria-label')
|
||||
if (href && label && !textOf(children)) children = [label]
|
||||
const parts: Inline[] = href ? [{ href, children }] : children
|
||||
// A part the page draws on its own line (a span set to block, a
|
||||
// flex row's items) has no space in its text: give it one.
|
||||
|
|
@ -82,13 +114,20 @@ const isBlock = (el: Element) => opens(el) || el.matches(BLOCK_SELECTOR) || el.q
|
|||
|
||||
function extract(root: Element): Block[] {
|
||||
const blocks: Block[] = []
|
||||
// Ids waiting for the next block written, which takes them.
|
||||
let pendingIds: string[] = []
|
||||
const push = (block: Block) => {
|
||||
blocks.push(pendingIds.length ? { ...block, ids: pendingIds } : block)
|
||||
pendingIds = []
|
||||
}
|
||||
const text = (content: Inline[]) => {
|
||||
if (textOf(content)) blocks.push({ kind: 'text', content })
|
||||
if (textOf(content)) push({ kind: 'text', content })
|
||||
}
|
||||
|
||||
function walk(el: Element, link: string | null) {
|
||||
// The root is the hidden copy of the page itself: read inside it.
|
||||
if (el !== root && skipped(el)) return
|
||||
if (el.id && el !== root) pendingIds.push(el.id)
|
||||
if (opens(el)) {
|
||||
// Each part on its own line in the page (a name, a title) joins
|
||||
// with a comma.
|
||||
|
|
@ -98,10 +137,14 @@ function extract(root: Element): Block[] {
|
|||
text([parts.join(', ')])
|
||||
return
|
||||
}
|
||||
if (el.tagName === 'FORM') {
|
||||
text(['This form can’t be filled in while reading mode is on. Turn it off (the accessibility button) to use it.'])
|
||||
return
|
||||
}
|
||||
if (HEADINGS.has(el.tagName)) {
|
||||
const content = [...el.childNodes].flatMap(inline)
|
||||
if (textOf(content)) {
|
||||
blocks.push({ kind: 'heading', level: Number(el.tagName[1]), content: link ? [{ href: link, children: content }] : content })
|
||||
push({ kind: 'heading', level: Number(el.tagName[1]), content: link ? [{ href: link, children: content }] : content })
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
@ -109,7 +152,7 @@ function extract(root: Element): Block[] {
|
|||
const img = el as HTMLImageElement
|
||||
const alt = img.getAttribute('alt')?.trim()
|
||||
const src = img.currentSrc || img.getAttribute('src')
|
||||
if (alt && src) blocks.push({ kind: 'image', src, alt })
|
||||
if (alt && src) push({ kind: 'image', src, alt, ...(link ? { href: link } : {}) })
|
||||
return
|
||||
}
|
||||
if (TEXT_BLOCKS.has(el.tagName) && !el.querySelector('p,li,h1,h2,h3,h4,h5,h6,img')) {
|
||||
|
|
@ -117,19 +160,26 @@ function extract(root: Element): Block[] {
|
|||
return
|
||||
}
|
||||
if (el.tagName === 'TR') {
|
||||
const cells = [...el.children].map((cell) => [...cell.childNodes].flatMap(inline)).filter((c) => textOf(c))
|
||||
const cells = [...el.children].map((cell) => [...cell.childNodes].flatMap(inline)).map((c) => (textOf(c) ? c : ['-']))
|
||||
text(cells.flatMap((cell, i) => (i ? [' · ', ...cell] : cell)))
|
||||
return
|
||||
}
|
||||
// A link around a whole card: its parts, with the link on the first
|
||||
// heading (or, with none, kept as a line of its own).
|
||||
// heading; with none, on its aria-label as a line of its own, or
|
||||
// else on its first line of text.
|
||||
const href = el instanceof HTMLAnchorElement ? hrefOf(el) : null
|
||||
if (href && isBlock(el)) {
|
||||
const before = blocks.length
|
||||
for (const child of el.children) walk(child, href)
|
||||
if (!blocks.slice(before).some((b) => b.kind === 'heading')) {
|
||||
const own = blocks.slice(before)
|
||||
if (!own.some((b) => b.kind === 'heading')) {
|
||||
const label = el.getAttribute('aria-label')
|
||||
const first = own.findIndex((b) => b.kind === 'text')
|
||||
if (label) blocks.splice(before, 0, { kind: 'text', content: [{ href, children: [label] }] })
|
||||
else if (first >= 0) {
|
||||
const block = blocks[before + first] as Extract<Block, { kind: 'text' }>
|
||||
blocks[before + first] = { ...block, content: [{ href, children: block.content }] }
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
@ -142,6 +192,10 @@ function extract(root: Element): Block[] {
|
|||
text(run)
|
||||
run = []
|
||||
walk(child, link)
|
||||
} else if (child instanceof Element && !skipped(child) && ownLine(child)) {
|
||||
text(run)
|
||||
run = []
|
||||
text(inline(child))
|
||||
} else {
|
||||
run.push(...inline(child))
|
||||
}
|
||||
|
|
@ -159,20 +213,31 @@ function extract(root: Element): Block[] {
|
|||
|
||||
function render(parts: Inline[]): ReactNode[] {
|
||||
return parts.map((part, i) => {
|
||||
if (typeof part === 'string') return part
|
||||
const children = render(part.children)
|
||||
return part.href.startsWith('/') ? (
|
||||
<Link key={i} to={part.href}>
|
||||
{children}
|
||||
</Link>
|
||||
) : (
|
||||
<a key={i} href={part.href}>
|
||||
{children}
|
||||
</a>
|
||||
if (typeof part === 'string') {
|
||||
if (!part.includes(BREAK)) return part
|
||||
return part.split(BREAK).map((line, j) => (
|
||||
<Fragment key={`${i}-${j}`}>
|
||||
{j > 0 && <br />}
|
||||
{line}
|
||||
</Fragment>
|
||||
))
|
||||
}
|
||||
return (
|
||||
<Anchor key={i} href={part.href}>
|
||||
{render(part.children)}
|
||||
</Anchor>
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/* A page on this site opens without reloading; a file (an upload,
|
||||
the API) or anywhere else is a plain link. */
|
||||
const isFile = (href: string) => /^\/(uploads|api)\//.test(href)
|
||||
|
||||
function Anchor({ href, children }: { href: string; children: ReactNode }) {
|
||||
return href.startsWith('/') && !isFile(href) ? <Link to={href}>{children}</Link> : <a href={href}>{children}</a>
|
||||
}
|
||||
|
||||
export default function ReaderView({ source }: { source: RefObject<HTMLElement | null> }) {
|
||||
const [blocks, setBlocks] = useState<Block[]>([])
|
||||
|
||||
|
|
@ -207,10 +272,37 @@ export default function ReaderView({ source }: { source: RefObject<HTMLElement |
|
|||
))}
|
||||
</p>
|
||||
{blocks.map((block, i) => {
|
||||
if (block.kind === 'image') return <img key={i} src={block.src} alt={block.alt} loading="lazy" />
|
||||
if (block.kind === 'text') return <p key={i}>{render(block.content)}</p>
|
||||
const [id, ...more] = block.ids ?? []
|
||||
const anchors = more.map((other) => <span key={other} id={other} />)
|
||||
if (block.kind === 'image') {
|
||||
return (
|
||||
<Fragment key={i}>
|
||||
{anchors}
|
||||
{block.href ? (
|
||||
<Anchor href={block.href}>
|
||||
<img id={id} src={block.src} alt={block.alt} loading="lazy" />
|
||||
</Anchor>
|
||||
) : (
|
||||
<img id={id} src={block.src} alt={block.alt} loading="lazy" />
|
||||
)}
|
||||
</Fragment>
|
||||
)
|
||||
}
|
||||
if (block.kind === 'text') {
|
||||
return (
|
||||
<p key={i} id={id}>
|
||||
{anchors}
|
||||
{render(block.content)}
|
||||
</p>
|
||||
)
|
||||
}
|
||||
const Heading = `h${block.level}` as 'h1'
|
||||
return <Heading key={i}>{render(block.content)}</Heading>
|
||||
return (
|
||||
<Heading key={i} id={id}>
|
||||
{anchors}
|
||||
{render(block.content)}
|
||||
</Heading>
|
||||
)
|
||||
})}
|
||||
</article>
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue