Annotate Wikipedia articles with the same keys as nodes
An article's text is drawn from its sanitised HTML as selectable text (articleText.ts): paragraphs, list items and quotes with their inline elements; a formula is one unit; tables and preformatted blocks are shown but carry no offsets. The selection ladder, Tab, Enter, e d y x c :, D and the rest go through the same store paths as on a node; a note on article words is a note (Enter steps into it; c, f, u, the editor and history work on it as on any other) and shows wherever the article is opened, in the library's own note style. Detached annotations are listed at the top of their section with a link to the old revision. Keys that only make sense on a node say why in the status line. Tab on a link now selects its words, so Esc widens before h leaves, as in a node. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
f63fc10dc0
commit
2c1ee048a4
18 files changed
+1134
-126
No files matched your search
+133
-26
@@ -9,32 +9,138 @@
|
||||
* always clear which text is Wikipedia's and which is the reader's, and every view of article
|
||||
* text ends with its attribution.
|
||||
*/
|
||||
import { useLayoutEffect, useRef } from "preact/hooks";
|
||||
import { clip } from "../core/text";
|
||||
import { h, type ComponentChildren } from "preact";
|
||||
import type { NodeId } from "../core/model";
|
||||
import { wikiKey } from "../core/ref";
|
||||
import { clip, type Range } from "../core/text";
|
||||
import type { Block } from "./articleText";
|
||||
import { Attribution, Backlinks } from "./Links";
|
||||
import { NodeBody, NoteView, type VNote } from "./Node";
|
||||
import { sameAux, store, type WRow } from "./store";
|
||||
import { article, cleanHtml, known, sectionsUnder, sensesOf, summary, type Article } from "./wiki";
|
||||
import { article, articleText, known, revisionUrl, sectionsUnder, sensesOf, summary, type Article } from "./wiki";
|
||||
|
||||
type Para = Extract<Block, { k: "para" }>;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Article HTML, cleaned twice (server and here) and put into the page. The focused link, when Tab
|
||||
* has picked one, is marked here rather than in the HTML, which never changes once set.
|
||||
* 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 WikiText({ html, link }: { html: string; link?: number }) {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
useLayoutEffect(() => {
|
||||
const el = ref.current;
|
||||
if (!el) return;
|
||||
el.querySelectorAll("a.wfocus").forEach((a) => a.classList.remove("wfocus"));
|
||||
if (link === undefined) return;
|
||||
const a = el.querySelectorAll("a[data-wiki]")[link];
|
||||
if (a) {
|
||||
a.classList.add("wfocus");
|
||||
(a as HTMLElement).scrollIntoView?.({ block: "nearest" });
|
||||
}
|
||||
});
|
||||
return <div class="wiki-text" ref={ref} dangerouslySetInnerHTML={{ __html: cleanHtml(html) }} />;
|
||||
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);
|
||||
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 (
|
||||
<div class="wiki-text" data-wtext={row.t === "sec" ? row.id : "head"}>
|
||||
<Blocks bs={at.blocks} c={{ text: at.text, notes, sel, link }} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Blocks({ bs, c }: { bs: Block[]; c: Ctx }) {
|
||||
return (
|
||||
<>
|
||||
{bs.map((b, i) =>
|
||||
b.k === "para" ? <APara key={i} b={b} c={c} /> : b.k === "raw" ? <div key={i} class="wraw" dangerouslySetInnerHTML={{ __html: b.html }} /> : h(b.tag, { key: i, ...Object.fromEntries(b.attrs) }, <Blocks bs={b.kids} c={c} />),
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** One paragraph, cut where its inline elements, annotations, the selection and the focused link begin and end; a formula is never cut. */
|
||||
function APara({ b, c }: { b: Para; c: Ctx }) {
|
||||
const clipTo = (r: Range): Range | null => (r[0] < b.e && r[1] > b.s ? [Math.max(r[0], b.s), Math.min(r[1], b.e)] : null);
|
||||
const cuts = new Set<number>([b.s, b.e]);
|
||||
for (const r of b.runs) cuts.add(r.s).add(r.e);
|
||||
const notes = c.notes.filter((n) => n.ranges.some((r) => clipTo(r)));
|
||||
for (const n of notes) for (const r of n.ranges.map(clipTo)) if (r) cuts.add(r[0]).add(r[1]);
|
||||
const sel = c.sel && clipTo(c.sel.range);
|
||||
if (sel) cuts.add(sel[0]).add(sel[1]);
|
||||
const maths = b.runs.filter((r) => r.math);
|
||||
const points = [...cuts].filter((x) => !maths.some((m) => x > m.s && x < m.e)).sort((x, y) => x - y);
|
||||
const meets = (r: Range, x: number, y: number, whole: boolean) => (whole ? r[0] < y && r[1] > x : r[0] <= x && r[1] >= y);
|
||||
const out: ComponentChildren[] = [];
|
||||
const after: ComponentChildren[] = [];
|
||||
for (let i = 0; i + 1 < points.length; i++) {
|
||||
const [x, y] = [points[i], points[i + 1]];
|
||||
const run = b.runs.find((r) => r.s <= x && x < r.e);
|
||||
if (run) {
|
||||
const whole = !!run.math;
|
||||
// Where notes overlap, the narrowest is the one these words open.
|
||||
const note = notes.filter((n) => n.ranges.some((r) => meets(r, x, y, whole))).sort((m, n) => m.ranges[0][1] - m.ranges[0][0] - (n.ranges[0][1] - n.ranges[0][0]))[0];
|
||||
const cls = [note && `anchor a-${note.form}`, note?.open && "on", note?.pending && "writing", sel && meets(sel, x, y, whole) && `sel ${c.sel!.grain}`].filter(Boolean).join(" ") || undefined;
|
||||
let el = run.math ? (
|
||||
<span key={x} class={cls ? `wmath ${cls}` : "wmath"} data-o={x} data-note={note?.id} dangerouslySetInnerHTML={{ __html: run.math }} />
|
||||
) : (
|
||||
<span key={x} class={cls} data-o={x} data-note={note?.id}>
|
||||
{c.text.slice(x, y)}
|
||||
</span>
|
||||
);
|
||||
for (const m of [...run.marks].reverse()) el = h(m.tag, { key: x, ...Object.fromEntries(m.attrs), class: m.tag === "a" && c.link && meets(c.link, x, y, false) ? "wfocus" : undefined }, el);
|
||||
out.push(el);
|
||||
}
|
||||
for (const n of notes) {
|
||||
if (!n.open || n.ranges[0][1] !== y) continue;
|
||||
const view = <NoteView key={`n${n.key}`} v={n} inNote />;
|
||||
if (n.form === "gloss") out.push(view);
|
||||
else after.push(view);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<>
|
||||
{b.p ? <p class="wpara">{out}</p> : <span class="wpara loose">{out}</span>}
|
||||
{after}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/** Annotations whose words are not in this revision: kept, shown at the top of their section (or of the article), with the words and the revision they were made on. */
|
||||
function Detached({ ids, article, lang }: { ids: NodeId[]; article: Article; lang: string }) {
|
||||
if (!ids.length) return null;
|
||||
const snap = store.snap;
|
||||
return (
|
||||
<div class="wdetached">
|
||||
{ids.map((id) => {
|
||||
const w = snap.nodes[id].wanchor!;
|
||||
return (
|
||||
<span key={id} class={`inset detached${store.focus.node === id && !store.aux ? " focused" : ""}`} data-node={id}>
|
||||
<span class="meta">
|
||||
detached: “{clip(w.q, 90)}” is not in this revision ·{" "}
|
||||
<a href={revisionUrl(article.title, w.rev, lang)} target="_blank" rel="noopener noreferrer">
|
||||
the words in revision {w.rev}
|
||||
</a>
|
||||
</span>
|
||||
<NodeBody id={id} inline inNote />
|
||||
</span>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** The detached annotations of a section (0: the lead, which also takes those whose section is gone). */
|
||||
const detachedIn = (article: Article, lang: string, sec: number) =>
|
||||
store.annotations(article, lang).detached.filter((d) => d.sec === sec || (sec === 0 && d.sec === null)).map((d) => d.id);
|
||||
|
||||
/** The W that marks Wikipedia's text, and the title. */
|
||||
function Head({ title, sub, focused }: { title: string; sub?: string; focused: boolean }) {
|
||||
return (
|
||||
@@ -59,8 +165,7 @@ export function WikiView({ host }: { host: string }) {
|
||||
const h = store.wikiHost(host);
|
||||
if (!h) return null;
|
||||
const a = store.aux;
|
||||
const at = (row: WRow) => !!a && a.kind === "wiki" && a.host === host && sameAux({ ...a, link: undefined }, { kind: "wiki", host, row });
|
||||
const linkAt = (row: WRow) => (at(row) && a?.kind === "wiki" ? a.link : undefined);
|
||||
const at = (row: WRow) => !!a && a.kind === "wiki" && a.host === host && !a.grain && sameAux(a, { kind: "wiki", host, row });
|
||||
const embedded = h.embedded;
|
||||
|
||||
// Under a node, folded: its summary only, until l unfolds the article.
|
||||
@@ -108,17 +213,18 @@ export function WikiView({ host }: { host: string }) {
|
||||
<section class={`wiki${embedded ? " embedded" : ""}`} data-host={host}>
|
||||
<Head title={v.title} sub={sec?.heading} focused={at({ t: "head" })} />
|
||||
{art.stale && <p class="wiki-wait">Wikipedia could not be reached; this is the copy kept from before.</p>}
|
||||
<WikiText html={sec ? sec.html : v.lead} link={linkAt({ t: "head" })} />
|
||||
<Detached ids={detachedIn(v, h.lang, h.sec)} article={v} lang={h.lang} />
|
||||
<ArticleTextView host={host} row={{ t: "head" }} sec={h.sec} html={sec ? sec.html : v.lead} article={v} lang={h.lang} />
|
||||
{v.disambiguation && !sec && <Senses title={v.title} lang={h.lang} at={at} />}
|
||||
<Rows article={v} parent={h.sec} nested={!!h.nested} at={at} linkAt={linkAt} showing={showing} />
|
||||
<Rows article={v} host={host} lang={h.lang} parent={h.sec} nested={!!h.nested} at={at} showing={showing} />
|
||||
<Mentions title={v.title} lang={h.lang} show={!!h.mentions} at={at} />
|
||||
<Attribution a={v.attribution} />
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
function Rows(props: { article: Article; parent: number; nested: boolean; at: (r: WRow) => boolean; linkAt: (r: WRow) => number | undefined; showing: (id: number) => boolean }) {
|
||||
const { article: v, parent, nested, at, linkAt, showing } = props;
|
||||
function Rows(props: { article: Article; host: string; lang: string; parent: number; nested: boolean; at: (r: WRow) => boolean; showing: (id: number) => boolean }) {
|
||||
const { article: v, host, lang, parent, nested, at, showing } = props;
|
||||
const kids = sectionsUnder(v, parent);
|
||||
if (!kids.length) return null;
|
||||
return (
|
||||
@@ -136,7 +242,8 @@ function Rows(props: { article: Article; parent: number; nested: boolean; at: (r
|
||||
</div>
|
||||
{open && (
|
||||
<div class="content">
|
||||
<WikiText html={x.html} link={linkAt(row)} />
|
||||
<Detached ids={detachedIn(v, lang, x.id)} article={v} lang={lang} />
|
||||
<ArticleTextView host={host} row={row} sec={x.id} html={x.html} article={v} lang={lang} />
|
||||
<Rows {...props} parent={x.id} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user