@lemonriver/core 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +14 -4
  2. package/dist/{build-Nl7EeJ6x.js → build-RlcGG-Ve.js} +1 -1
  3. package/dist/cli/bin.js +4 -4
  4. package/dist/{config-CpC0nM3r.js → config-BJT87wE9.js} +4756 -2012
  5. package/dist/{dev-BB3THd5I.js → dev-DR-j_xda.js} +2 -2
  6. package/dist/{preview-BGjgTg7H.js → preview-BTAhTb5B.js} +1 -1
  7. package/dist/{update-DsxZBtwv.js → update-CMRxtTc9.js} +18 -8
  8. package/dist/vite/index.js +1 -1
  9. package/package.json +2 -2
  10. package/src/agent/types.ts +14 -2
  11. package/src/app/components/agent/agent-panel.tsx +17 -5
  12. package/src/app/components/agent/connect-view.tsx +1 -1
  13. package/src/app/components/agent/provider-logo.tsx +10 -1
  14. package/src/app/lemonriver/agent/provider-marks.tsx +53 -0
  15. package/src/app/lemonriver/agent-providers/section.tsx +75 -0
  16. package/src/app/lemonriver/agent-providers/use-providers.ts +61 -0
  17. package/src/app/lemonriver/board/blocks/block-panel.tsx +666 -0
  18. package/src/app/lemonriver/board/blocks/cache.ts +28 -0
  19. package/src/app/lemonriver/board/blocks/change-list.tsx +189 -0
  20. package/src/app/lemonriver/board/blocks/classify.test.ts +209 -0
  21. package/src/app/lemonriver/board/blocks/classify.ts +152 -0
  22. package/src/app/lemonriver/board/blocks/snapshot.ts +116 -0
  23. package/src/app/lemonriver/board/board-canvas.tsx +627 -104
  24. package/src/app/lemonriver/board/board-page.tsx +1702 -91
  25. package/src/app/lemonriver/board/board.css +58 -7
  26. package/src/app/lemonriver/board/layout.test.ts +84 -0
  27. package/src/app/lemonriver/board/layout.ts +112 -11
  28. package/src/app/lemonriver/board/model/agent-tasks.test.ts +200 -0
  29. package/src/app/lemonriver/board/model/agent-tasks.ts +223 -0
  30. package/src/app/lemonriver/board/model/apply-prompt.test.ts +121 -0
  31. package/src/app/lemonriver/board/model/apply-prompt.ts +115 -0
  32. package/src/app/lemonriver/board/model/apply.ts +71 -0
  33. package/src/app/lemonriver/board/model/cards.test.ts +95 -2
  34. package/src/app/lemonriver/board/model/cards.ts +88 -20
  35. package/src/app/lemonriver/board/model/changes.test.ts +244 -0
  36. package/src/app/lemonriver/board/model/changes.ts +268 -0
  37. package/src/app/lemonriver/board/model/filter.test.ts +50 -0
  38. package/src/app/lemonriver/board/model/filter.ts +42 -0
  39. package/src/app/lemonriver/board/model/history.test.ts +71 -0
  40. package/src/app/lemonriver/board/model/history.ts +51 -0
  41. package/src/app/lemonriver/board/model/restructure.test.ts +205 -0
  42. package/src/app/lemonriver/board/model/restructure.ts +169 -0
  43. package/src/app/lemonriver/board/model/self-check.test.ts +329 -0
  44. package/src/app/lemonriver/board/model/self-check.ts +489 -0
  45. package/src/app/lemonriver/board/model/structure.test.ts +249 -0
  46. package/src/app/lemonriver/board/model/structure.ts +209 -0
  47. package/src/app/lemonriver/board/model/timing.test.ts +113 -0
  48. package/src/app/lemonriver/board/model/timing.ts +117 -0
  49. package/src/app/lemonriver/board/page-node.tsx +258 -55
  50. package/src/app/lemonriver/board/self-check-jump.ts +23 -0
  51. package/src/app/lemonriver/board/self-check-panel.tsx +272 -0
  52. package/src/app/lemonriver/board/self-check-runner.tsx +150 -0
  53. package/src/app/lemonriver/board/status-style.ts +25 -0
  54. package/src/app/lemonriver/board/use-board-apply.ts +206 -0
  55. package/src/app/lemonriver/board/use-board-cards.ts +108 -5
  56. package/src/app/lemonriver/board/use-board-changes.ts +108 -0
  57. package/src/app/lemonriver/board/use-board-columns.ts +37 -56
  58. package/src/app/lemonriver/board/use-board-comments.ts +36 -0
  59. package/src/app/lemonriver/board/use-board-restructures.ts +107 -0
  60. package/src/app/lemonriver/board/use-history.ts +86 -0
  61. package/src/app/lemonriver/board/use-page-order.ts +65 -38
  62. package/src/app/lemonriver/board/use-self-check.ts +117 -0
  63. package/src/app/lemonriver/board/use-target-minutes.ts +47 -0
  64. package/src/app/lemonriver/board/views/block-readings.tsx +46 -0
  65. package/src/app/lemonriver/board/views/list-view.tsx +356 -0
  66. package/src/app/lemonriver/board/views/progress.tsx +32 -0
  67. package/src/app/lemonriver/board/views/timeline-view.tsx +341 -0
  68. package/src/app/lemonriver/chrome/filter-pill.tsx +30 -0
  69. package/src/app/lemonriver/chrome/sidebar-extras.tsx +12 -1
  70. package/src/app/lemonriver/features.test.ts +5 -0
  71. package/src/app/lemonriver/features.ts +2 -1
  72. package/src/app/lemonriver/home/deck-card.tsx +10 -0
  73. package/src/app/lemonriver/home/home-page.tsx +55 -6
  74. package/src/app/lemonriver/home/use-deck-times.ts +4 -1
  75. package/src/app/lemonriver/outline/build-team.tsx +42 -0
  76. package/src/app/lemonriver/outline/describe-page.tsx +128 -27
  77. package/src/app/lemonriver/outline/draft.ts +38 -0
  78. package/src/app/lemonriver/outline/model.test.ts +118 -0
  79. package/src/app/lemonriver/outline/model.ts +92 -7
  80. package/src/app/lemonriver/outline/outline-page.tsx +90 -7
  81. package/src/app/lemonriver/outline/pick-style.tsx +282 -0
  82. package/src/app/lemonriver/routes.test.tsx +17 -5
  83. package/src/app/lemonriver/routes.tsx +28 -0
  84. package/src/app/lemonriver/settings/settings-page.tsx +3 -0
  85. package/src/app/lemonriver/slide-extensions.tsx +14 -2
  86. package/src/app/lemonriver/templates/client.ts +60 -0
  87. package/src/app/lemonriver/templates/demo-page.tsx +79 -0
  88. package/src/app/lemonriver/templates/knobs.test.ts +62 -0
  89. package/src/app/lemonriver/templates/knobs.ts +62 -0
  90. package/src/app/lemonriver/templates/library-page.tsx +292 -0
  91. package/src/app/lemonriver/templates/model.test.ts +175 -0
  92. package/src/app/lemonriver/templates/model.ts +339 -0
  93. package/src/app/lemonriver/templates/preview-content.test.ts +117 -0
  94. package/src/app/lemonriver/templates/preview-content.ts +149 -0
  95. package/src/app/lemonriver/templates/recommend.test.ts +71 -0
  96. package/src/app/lemonriver/templates/recommend.ts +72 -0
  97. package/src/app/lemonriver/templates/use-library.ts +58 -0
  98. package/src/app/lemonriver/templates/virtual.d.ts +28 -0
  99. package/src/app/lemonriver/text/en.ts +228 -0
  100. package/src/app/lemonriver/text/es.ts +220 -0
  101. package/src/app/lemonriver/text/ja.ts +220 -0
  102. package/src/app/lemonriver/text/types.ts +243 -0
  103. package/src/app/lemonriver/text/zh-cn.ts +218 -0
  104. package/src/app/lemonriver/text/zh-tw.ts +218 -0
  105. package/src/app/lemonriver/theme/lemon-icon.tsx +6 -0
  106. package/src/app/lemonriver/theme/pages.test.ts +4 -0
  107. package/src/app/lemonriver/theme/pages.ts +6 -1
  108. package/src/app/lemonriver/theme/theme-menu.tsx +3 -2
  109. package/src/app/lemonriver/theme/use-theme.tsx +2 -2
  110. package/src/app/lemonriver/update/prompt.ts +1 -1
  111. package/src/app/lib/agent/use-agent.ts +4 -3
  112. package/src/app/routes/slide.tsx +1 -1
  113. package/src/app/assets/open-slide.svg +0 -39
  114. package/src/app/favicon.ico +0 -0
  115. package/src/app/favicon.svg +0 -39
@@ -0,0 +1,489 @@
1
+ import {
2
+ type Block,
3
+ blockKindOf,
4
+ readBlocks,
5
+ type SnapBox,
6
+ type SnapNode,
7
+ visibleBox,
8
+ } from '../blocks/classify.ts';
9
+
10
+ export const FINDING_KINDS = [
11
+ 'tiny',
12
+ 'small-body',
13
+ 'overflow',
14
+ 'overlap',
15
+ 'no-notes',
16
+ 'long-title',
17
+ 'accent',
18
+ ] as const;
19
+
20
+ export type FindingKind = (typeof FINDING_KINDS)[number];
21
+
22
+ /** Style choices a reader can disagree with; they never block a pass. */
23
+ export const HINT_KINDS: readonly FindingKind[] = ['long-title', 'accent'];
24
+
25
+ export type Finding = {
26
+ page: number;
27
+ kind: FindingKind;
28
+ /** One English line for the agent and the record. */
29
+ detail: string;
30
+ /** Every place on the page it was seen; one entry may hold many. */
31
+ boxes: SnapBox[];
32
+ /** How many places were merged into this entry. */
33
+ count: number;
34
+ /** Font size for the type findings, the smallest one it merged. */
35
+ size?: number;
36
+ /** The size of a hint: characters in a title, or blocks using the accent. */
37
+ measure?: number;
38
+ };
39
+
40
+ export type CheckedPage = {
41
+ /** 1-based page number as the deck counts pages. */
42
+ n: number;
43
+ tree: SnapNode | null;
44
+ notes: string;
45
+ title: string;
46
+ };
47
+
48
+ export type CheckOptions = {
49
+ accent?: string;
50
+ /** A template's own floor for small type, 16–20. */
51
+ minType?: number;
52
+ };
53
+
54
+ export const CANVAS = { width: 1920, height: 1080 } as const;
55
+ /** Under 20px type stops reading at presentation distance. */
56
+ export const TINY = 20;
57
+ /** Running parts may go down to 18 inside the header and footer bands. */
58
+ export const TINY_BAND = 18;
59
+ /** A header line ends by here. */
60
+ export const HEADER_BOTTOM = 110;
61
+ /** The footer band starts here; a source line at 948 sits inside it. */
62
+ export const FOOTER_TOP = 930;
63
+ /** Body paragraphs are set at 22px or more; the writing rules allow 22–28 for notes. */
64
+ export const BODY_MIN = 22;
65
+ /** Shorter runs are labels, axis ticks or source lines, not paragraphs. */
66
+ export const BODY_MIN_GLYPHS = 24;
67
+ export const OVERFLOW_TOLERANCE = 2;
68
+ /** Below this two boxes read as touching, not as overlapping. */
69
+ export const OVERLAP_MIN = 4;
70
+ /** At 120px and up a run is a watermark or a chapter number, not body text. */
71
+ export const GIANT = 120;
72
+ export const LONG_TITLE_CJK = 24;
73
+ export const LONG_TITLE_WORDS = 12;
74
+ /** Past three places on one page the accent stops meaning anything. */
75
+ export const MAX_ACCENT = 3;
76
+ export const MIN_TYPE_RANGE = { min: 16, max: 20 } as const;
77
+
78
+ const CJK = /[㐀-鿿豈-﫿]/g;
79
+ const WORD = /[\p{L}\p{N}][\p{L}\p{N}'’-]*/gu;
80
+ const GLYPH = /[\p{L}\p{N}]/gu;
81
+
82
+ /** `rgb()`/`rgba()` and `#rrggbb` become `#rrggbb`; anything else has no hex. */
83
+ export function toHex(color: string): string {
84
+ const value = color.trim().toLowerCase();
85
+ if (/^#[0-9a-f]{6}$/.test(value)) return value;
86
+ if (/^#[0-9a-f]{3}$/.test(value)) {
87
+ return `#${value[1]}${value[1]}${value[2]}${value[2]}${value[3]}${value[3]}`;
88
+ }
89
+ const match = /^rgba?\(\s*([\d.]+)[,\s]+([\d.]+)[,\s]+([\d.]+)(?:[,\s/]+([\d.]+%?))?\s*\)$/.exec(
90
+ value,
91
+ );
92
+ if (!match) return '';
93
+ const alpha = match[4];
94
+ if (alpha !== undefined) {
95
+ const value = alpha.endsWith('%') ? Number.parseFloat(alpha) / 100 : Number.parseFloat(alpha);
96
+ if (value === 0) return '';
97
+ }
98
+ const hex = (part: string) => Math.round(Number(part)).toString(16).padStart(2, '0');
99
+ return `#${hex(match[1])}${hex(match[2])}${hex(match[3])}`;
100
+ }
101
+
102
+ const squash = (text: string) => text.replace(/\s+/g, ' ').trim();
103
+ const ownText = (node: SnapNode) => squash(node.text) !== '';
104
+ const glyphCount = (text: string) => text.match(GLYPH)?.length ?? 0;
105
+
106
+ function overlaps(a: SnapBox, b: SnapBox): number {
107
+ const w = Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x);
108
+ const h = Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y);
109
+ if (w <= 0 || h <= 0) return 0;
110
+ return Math.min(w, h);
111
+ }
112
+
113
+ /** One box inside another is a run of text with a span in it, not an overlap. */
114
+ function wraps(outer: SnapBox, inner: SnapBox): boolean {
115
+ return (
116
+ inner.x >= outer.x - OVERLAP_MIN &&
117
+ inner.y >= outer.y - OVERLAP_MIN &&
118
+ inner.x + inner.w <= outer.x + outer.w + OVERLAP_MIN &&
119
+ inner.y + inner.h <= outer.y + outer.h + OVERLAP_MIN
120
+ );
121
+ }
122
+
123
+ function contains(parent: SnapNode, child: SnapNode): boolean {
124
+ return parent === child || parent.children.some((node) => contains(node, child));
125
+ }
126
+
127
+ function walk(node: SnapNode, visit: (node: SnapNode) => void): void {
128
+ visit(node);
129
+ for (const child of node.children) walk(child, visit);
130
+ }
131
+
132
+ const isPanel = (node: SnapNode) =>
133
+ node.box.w >= 200 && node.box.h >= 200 && node.background !== '';
134
+
135
+ /** Pictures and charts are read as content; a bare colour block is decoration. */
136
+ function isMedia(node: SnapNode): boolean {
137
+ const kind = blockKindOf(node);
138
+ return kind === 'image' || kind === 'chart';
139
+ }
140
+
141
+ /**
142
+ * What the reader sees of a run of words: one box per line, not the element's.
143
+ * Each line box keeps the font's leading, so it is trimmed to the glyphs it
144
+ * holds; two lines that only touch then stop reading as one over the other.
145
+ */
146
+ const linesOf = (node: SnapNode): SnapBox[] => {
147
+ const boxes = node.lines.length > 0 ? node.lines : [node.box];
148
+ const leading = node.lineHeight > node.fontSize ? (node.lineHeight - node.fontSize) / 2 : 0;
149
+ if (leading <= 0) return boxes;
150
+ return boxes.map((box) => {
151
+ const h = box.h - leading * 2;
152
+ return h > 0 ? { x: box.x, y: box.y + leading, w: box.w, h } : box;
153
+ });
154
+ };
155
+
156
+ const inBand = (box: SnapBox) => box.y + box.h <= HEADER_BOTTOM || box.y >= FOOTER_TOP;
157
+
158
+ /** Two parts of one block (a figure and its label) are not two overlapping runs. */
159
+ function sameBlock(blocks: readonly Block[], a: SnapNode, b: SnapNode): boolean {
160
+ return blocks.some((block) => wraps(block.box, a.box) && wraps(block.box, b.box));
161
+ }
162
+
163
+ /**
164
+ * One use per block: several elements of one block count once, and a block
165
+ * with nothing in it (a bare colour bar) counts on its own.
166
+ */
167
+ function accentUses(nodes: readonly SnapNode[], blocks: readonly Block[], accent: string): number {
168
+ const units = new Set<Block>();
169
+ let loose = 0;
170
+ for (const node of nodes) {
171
+ if (toHex(node.color) !== accent && toHex(node.background) !== accent) continue;
172
+ const block =
173
+ blocks.find((candidate) => wraps(candidate.box, node.box)) ??
174
+ blocks.find((candidate) => wraps(node.box, candidate.box));
175
+ if (block) units.add(block);
176
+ else loose += 1;
177
+ }
178
+ return units.size + loose;
179
+ }
180
+
181
+ export type CheckResult = { findings: Finding[]; unread: number[] };
182
+
183
+ type Draft = Omit<Finding, 'page'>;
184
+
185
+ function detailOf(kind: FindingKind, count: number, size: number, measure: number): string {
186
+ switch (kind) {
187
+ case 'tiny':
188
+ return count > 1 ? `${count} runs of type at ${size}px` : `type at ${size}px`;
189
+ case 'small-body':
190
+ return count > 1 ? `${count} body runs at ${size}px` : `body text at ${size}px`;
191
+ case 'overflow':
192
+ return count > 1 ? `${count} run off the page` : 'runs off the page';
193
+ case 'overlap':
194
+ return count > 1 ? `${count} overlaps` : 'text overlaps something';
195
+ case 'no-notes':
196
+ return 'no speaker notes';
197
+ case 'long-title':
198
+ return `title ${measure} long`;
199
+ default:
200
+ return `accent colour used ${measure} times`;
201
+ }
202
+ }
203
+
204
+ /** Two runs that meet at one spot report it twice; a reader counts it once. */
205
+ function distinctBoxes(boxes: readonly SnapBox[]): SnapBox[] {
206
+ const seen = new Set<string>();
207
+ return boxes.filter((box) => {
208
+ const key = `${Math.round(box.x)},${Math.round(box.y)},${Math.round(box.w)},${Math.round(box.h)}`;
209
+ if (seen.has(key)) return false;
210
+ seen.add(key);
211
+ return true;
212
+ });
213
+ }
214
+
215
+ /** One entry per kind on a page, with every place it was seen. */
216
+ function mergePage(page: number, drafts: readonly Draft[]): Finding[] {
217
+ const byKind = new Map<FindingKind, Draft[]>();
218
+ for (const draft of drafts) {
219
+ byKind.set(draft.kind, [...(byKind.get(draft.kind) ?? []), draft]);
220
+ }
221
+ return [...byKind].map(([kind, list]) => {
222
+ const hint = HINT_KINDS.includes(kind);
223
+ const boxes = hint ? list[0].boxes : distinctBoxes(list.flatMap((draft) => draft.boxes));
224
+ const count = hint ? 1 : Math.max(1, boxes.length);
225
+ const sizes = list.flatMap((draft) => (draft.size === undefined ? [] : [draft.size]));
226
+ const measures = list.flatMap((draft) => (draft.measure === undefined ? [] : [draft.measure]));
227
+ const size = sizes.length > 0 ? Math.min(...sizes) : 0;
228
+ const measure = measures.length > 0 ? Math.max(...measures) : 0;
229
+ const finding: Finding = {
230
+ page,
231
+ kind,
232
+ detail: hint ? list[0].detail : detailOf(kind, count, size, measure),
233
+ boxes,
234
+ count,
235
+ };
236
+ if (sizes.length > 0) finding.size = size;
237
+ if (measures.length > 0) finding.measure = measure;
238
+ return finding;
239
+ });
240
+ }
241
+
242
+ const isHint = (finding: Finding) => HINT_KINDS.includes(finding.kind);
243
+
244
+ /** What the badge and the header count: hints are style, not problems. */
245
+ export function problemsOf(findings: readonly Finding[]): Finding[] {
246
+ return findings.filter((finding) => !isHint(finding));
247
+ }
248
+
249
+ export function hintsOf(findings: readonly Finding[]): Finding[] {
250
+ return findings.filter(isHint);
251
+ }
252
+
253
+ /**
254
+ * What a finished page does that a person would have to fix. Pure: the caller
255
+ * hands it the page's snapshot, notes and title, so the same rules run on the
256
+ * server, in the board and in tests. A page whose tree could not be read is
257
+ * listed in `unread`; it is never counted as clean.
258
+ */
259
+ export function checkDeck(pages: readonly CheckedPage[], options: CheckOptions = {}): CheckResult {
260
+ const findings: Finding[] = [];
261
+ const unread: number[] = [];
262
+ const accent = options.accent ? toHex(options.accent) : '';
263
+ const minType =
264
+ options.minType !== undefined &&
265
+ Number.isInteger(options.minType) &&
266
+ options.minType >= MIN_TYPE_RANGE.min &&
267
+ options.minType <= MIN_TYPE_RANGE.max
268
+ ? options.minType
269
+ : TINY;
270
+
271
+ for (const page of pages) {
272
+ const drafts: Draft[] = [];
273
+ const add = (draft: Draft) => drafts.push(draft);
274
+
275
+ if (page.notes.trim() === '') {
276
+ add({
277
+ kind: 'no-notes',
278
+ detail: 'no speaker notes',
279
+ boxes: [],
280
+ count: 1,
281
+ });
282
+ }
283
+
284
+ const tree = page.tree;
285
+ if (!tree) {
286
+ unread.push(page.n);
287
+ findings.push(...mergePage(page.n, drafts));
288
+ continue;
289
+ }
290
+
291
+ const nodes: SnapNode[] = [];
292
+ walk(tree, (node) => nodes.push(node));
293
+ const textNodes = nodes.filter(ownText);
294
+ const panels = nodes.filter(isPanel);
295
+ const read = readBlocks(tree);
296
+ const blocks: Block[] = 'blocks' in read ? read.blocks : [];
297
+
298
+ for (const node of nodes) {
299
+ if (node.fontSize >= GIANT) continue;
300
+ if (!ownText(node) && !isMedia(node)) continue;
301
+ const box = visibleBox(node.box, node.clip);
302
+ if (!box) continue;
303
+ if (
304
+ box.x < -OVERFLOW_TOLERANCE ||
305
+ box.y < -OVERFLOW_TOLERANCE ||
306
+ box.x + box.w > CANVAS.width + OVERFLOW_TOLERANCE ||
307
+ box.y + box.h > CANVAS.height + OVERFLOW_TOLERANCE
308
+ ) {
309
+ add({ kind: 'overflow', detail: '', boxes: [node.box], count: 1 });
310
+ }
311
+ }
312
+
313
+ let titleSeen = false;
314
+ for (const node of textNodes) {
315
+ const kind = blockKindOf(node);
316
+ const size = Math.round(node.fontSize);
317
+ const band = inBand(node.box);
318
+ const floor = band ? Math.min(TINY_BAND, minType) : minType;
319
+
320
+ if (node.fontSize > 0 && node.fontSize < floor) {
321
+ add({ kind: 'tiny', detail: '', boxes: [node.box], count: 1, size });
322
+ } else if (
323
+ !band &&
324
+ node.fontSize > 0 &&
325
+ node.fontSize < BODY_MIN &&
326
+ (kind === 'text' || kind === 'point') &&
327
+ glyphCount(node.fullText) >= BODY_MIN_GLYPHS
328
+ ) {
329
+ add({ kind: 'small-body', detail: '', boxes: [node.box], count: 1, size });
330
+ }
331
+
332
+ if (!titleSeen && kind === 'title') {
333
+ titleSeen = true;
334
+ const flat = squash(node.fullText);
335
+ const cjk = flat.match(CJK)?.length ?? 0;
336
+ const english = cjk === 0;
337
+ const measure = english ? (flat.match(WORD)?.length ?? 0) : cjk;
338
+ const limit = english ? LONG_TITLE_WORDS : LONG_TITLE_CJK;
339
+ if (measure > limit) {
340
+ add({
341
+ kind: 'long-title',
342
+ detail: `title ${measure} ${english ? 'words' : 'characters'} long`,
343
+ boxes: [node.box],
344
+ count: 1,
345
+ measure,
346
+ });
347
+ }
348
+ }
349
+ }
350
+
351
+ for (let i = 0; i < textNodes.length; i += 1) {
352
+ const node = textNodes[i];
353
+ if (node.fontSize >= GIANT) continue;
354
+ const lines = linesOf(node);
355
+
356
+ const crossing = lines.filter((line) =>
357
+ panels.some(
358
+ (panel) =>
359
+ !contains(panel, node) &&
360
+ !wraps(panel.box, line) &&
361
+ overlaps(panel.box, line) > OVERLAP_MIN,
362
+ ),
363
+ );
364
+ if (crossing.length > 0) {
365
+ add({ kind: 'overlap', detail: '', boxes: crossing, count: 1 });
366
+ continue;
367
+ }
368
+
369
+ const other = textNodes.find(
370
+ (candidate, j) =>
371
+ j > i &&
372
+ candidate.fontSize < GIANT &&
373
+ !contains(node, candidate) &&
374
+ !contains(candidate, node) &&
375
+ !sameBlock(blocks, node, candidate) &&
376
+ !wraps(node.box, candidate.box) &&
377
+ !wraps(candidate.box, node.box) &&
378
+ linesOverlap(lines, linesOf(candidate)) > OVERLAP_MIN,
379
+ );
380
+ if (other) {
381
+ const otherLines = linesOf(other);
382
+ const hit = lines.filter((line) =>
383
+ otherLines.some((candidate) => overlaps(candidate, line) > OVERLAP_MIN),
384
+ );
385
+ add({ kind: 'overlap', detail: '', boxes: hit.length > 0 ? hit : [node.box], count: 1 });
386
+ }
387
+ }
388
+
389
+ if (accent) {
390
+ const used = accentUses(
391
+ nodes.filter((node) => !inBand(node.box)),
392
+ blocks,
393
+ accent,
394
+ );
395
+ if (used > MAX_ACCENT) {
396
+ add({
397
+ kind: 'accent',
398
+ detail: `accent colour used ${used} times`,
399
+ boxes: [],
400
+ count: 1,
401
+ measure: used,
402
+ });
403
+ }
404
+ }
405
+
406
+ findings.push(...mergePage(page.n, drafts));
407
+ }
408
+
409
+ return { findings, unread };
410
+ }
411
+
412
+ function linesOverlap(a: readonly SnapBox[], b: readonly SnapBox[]): number {
413
+ let most = 0;
414
+ for (const one of a) {
415
+ for (const other of b) most = Math.max(most, overlaps(one, other));
416
+ }
417
+ return most;
418
+ }
419
+
420
+ export type SelfCheck = { at: number; findings: Finding[]; unread: number[] };
421
+
422
+ const BOX_KEYS = ['x', 'y', 'w', 'h'] as const;
423
+
424
+ function readBox(raw: unknown): SnapBox | undefined {
425
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return undefined;
426
+ const record = raw as Record<string, unknown>;
427
+ const box = {} as SnapBox;
428
+ for (const key of BOX_KEYS) {
429
+ const value = record[key];
430
+ if (typeof value !== 'number' || !Number.isFinite(value)) return undefined;
431
+ box[key] = value;
432
+ }
433
+ return box;
434
+ }
435
+
436
+ function readCount(raw: unknown): number | undefined {
437
+ return typeof raw === 'number' && Number.isFinite(raw) ? Math.round(raw) : undefined;
438
+ }
439
+
440
+ export function sanitizeFindings(raw: unknown): Finding[] {
441
+ if (!Array.isArray(raw)) return [];
442
+ return raw.flatMap((entry) => {
443
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) return [];
444
+ const record = entry as Record<string, unknown>;
445
+ const page = record.page;
446
+ const kind = record.kind;
447
+ if (typeof page !== 'number' || !Number.isInteger(page) || page < 1 || page > 999) return [];
448
+ if (typeof kind !== 'string' || !(FINDING_KINDS as readonly string[]).includes(kind)) return [];
449
+ const boxes = Array.isArray(record.boxes)
450
+ ? record.boxes.flatMap((box) => readBox(box) ?? [])
451
+ : (() => {
452
+ const box = readBox(record.box);
453
+ return box ? [box] : [];
454
+ })();
455
+ const finding: Finding = {
456
+ page,
457
+ kind: kind as FindingKind,
458
+ detail: typeof record.detail === 'string' ? record.detail.slice(0, 160) : '',
459
+ boxes,
460
+ count: Math.max(1, readCount(record.count) ?? boxes.length ?? 1),
461
+ };
462
+ const size = readCount(record.size);
463
+ if (size !== undefined) finding.size = size;
464
+ const measure = readCount(record.measure);
465
+ if (measure !== undefined) finding.measure = measure;
466
+ return [finding];
467
+ });
468
+ }
469
+
470
+ function readPages(raw: unknown): number[] {
471
+ if (!Array.isArray(raw)) return [];
472
+ return raw.flatMap((value) =>
473
+ typeof value === 'number' && Number.isInteger(value) && value >= 1 && value <= 999
474
+ ? [value]
475
+ : [],
476
+ );
477
+ }
478
+
479
+ export function sanitizeSelfCheck(raw: unknown): SelfCheck | null {
480
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
481
+ const record = raw as Record<string, unknown>;
482
+ const at = record.at;
483
+ if (typeof at !== 'number' || !Number.isFinite(at)) return null;
484
+ return {
485
+ at,
486
+ findings: sanitizeFindings(record.findings),
487
+ unread: readPages(record.unread),
488
+ };
489
+ }