/** * A Wikipedia article shown as a tree, so the keys that move through nodes move through it: the * lead is its text, its sections are rows (in a pane a section opens as the next pane; on the * column's article page it opens in place), its links are Tab stops, and a disambiguation page * lists its senses as rows. It is never stored in the library: it comes from /api/wiki/article, * one request per article, cached by the server. * * Article panes look different on purpose (sans-serif, a tinted ground, a W at the head) so it is * always clear which text is Wikipedia's and which is the reader's, and every view of article * text ends with its attribution. */ import { h, type ComponentChildren } from "preact"; import { useContext, useEffect, useRef, useState } from "preact/hooks"; import type { NodeId } from "../core/model"; import { wikiKey } from "../core/ref"; import { clip, type Range } from "../core/text"; import { PLAYER_ALLOW, PLAYER_SANDBOX, embedSrc } from "../core/video"; import type { Block } from "./articleText"; import { Attribution, Backlinks, PaneCtx, useForkMark } from "./Links"; import { NodeBody, NoteView, type VNote } from "./Node"; import { sameAux, store, type WRow } from "./store"; import { article, articleText, known, revisionUrl, sectionsUnder, sensesOf, summary, type Article } from "./wiki"; import { webKey } from "./web"; type Para = Extract; /** What drawing a piece of article text needs besides its blocks (a web page's too: Web.tsx). */ export interface Ctx { text: string; notes: VNote[]; sel: { grain: string; range: Range } | null; /** The focused link's words, when the selection is exactly a link. */ link: Range | null; /** In a pane with forks: the mark for a link whose article is open as one of them. */ fork: ((...keys: string[]) => string) | null; } /** * A piece of article text (the lead or a section) as text the keys select in: every paragraph is * drawn from the same characters sentences() and words() count (articleText.ts), so a selection, * an annotation's words and a link are ranges into it, as in a node's body. Annotations are drawn * as the library's notes are (a dotted underline; the note inline, or after its paragraph), in the * library's own face. Tables and preformatted blocks are shown as they came. */ function ArticleTextView({ host, row, sec, html, article, lang }: { host: string; row: WRow; sec: number; html: string; article: Article; lang: string }) { const at = articleText(html); const a = store.aux; const here = a?.kind === "wiki" && a.host === host && sameAux({ ...a, grain: undefined, range: undefined }, { kind: "wiki", host, row }); const sel = here && a.grain && a.range ? { grain: a.grain, range: a.range } : null; const link = sel && at.links.some((l) => l.range[0] === sel.range[0] && l.range[1] === sel.range[1]) ? sel.range : null; const snap = store.snap; const notes: VNote[] = []; for (const [id, p] of store.annotations(article, lang).at) { const n = snap.nodes[id]; if (p.sec === sec) notes.push({ key: id, id, form: n.form ?? "note", ranges: [p.range], open: store.open.has(id), lens: n.lens }); } const key = wikiKey(lang, article.title); const fork = useForkMark(); for (const p of store.pending.values()) if (p.art?.key === key && p.art.sec === sec && p.range) notes.push({ key: p.key, form: p.form ?? "note", ranges: [p.range], open: true, pending: p, lens: p.lens }); return (
); } export function Blocks({ bs, c }: { bs: Block[]; c: Ctx }) { return ( <> {bs.map((b, i) => b.k === "para" ? ( ) : b.k === "raw" ? (
) : b.tag === "figure" && b.attrs.some(([k, v]) => k === "class" && v === "video") ? ( ) : ( h(b.tag, { key: i, ...Object.fromEntries(b.attrs) }, ) ), )} ); } type El = Extract; /** A video box's shape: the nearest of a few common ones to the video's own (16:9 when unknown), as a class, since the text may carry no style attribute. */ const SHAPES: [string, number][] = [["r21-9", 21 / 9], ["r16-9", 16 / 9], ["r3-2", 3 / 2], ["r4-3", 4 / 3], ["r1-1", 1], ["r3-4", 3 / 4], ["r9-16", 9 / 16]]; function shape(size: { width?: string; height?: string }): string { const r = size.width && size.height ? Number(size.width) / Number(size.height) : 16 / 9; return SHAPES.reduce((best, s) => (Math.abs(Math.log(s[1] / r)) < Math.abs(Math.log(best[1] / r)) ? s : best))[0]; } /** * A video's facade (src/core/video.ts): its still and a play mark, its caption under them, and on * Wikipedia a link to its File page, which credits it. Nothing plays or loads from elsewhere until * the click; then a