@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.
- package/README.md +14 -4
- package/dist/{build-Nl7EeJ6x.js → build-RlcGG-Ve.js} +1 -1
- package/dist/cli/bin.js +4 -4
- package/dist/{config-CpC0nM3r.js → config-BJT87wE9.js} +4756 -2012
- package/dist/{dev-BB3THd5I.js → dev-DR-j_xda.js} +2 -2
- package/dist/{preview-BGjgTg7H.js → preview-BTAhTb5B.js} +1 -1
- package/dist/{update-DsxZBtwv.js → update-CMRxtTc9.js} +18 -8
- package/dist/vite/index.js +1 -1
- package/package.json +2 -2
- package/src/agent/types.ts +14 -2
- package/src/app/components/agent/agent-panel.tsx +17 -5
- package/src/app/components/agent/connect-view.tsx +1 -1
- package/src/app/components/agent/provider-logo.tsx +10 -1
- package/src/app/lemonriver/agent/provider-marks.tsx +53 -0
- package/src/app/lemonriver/agent-providers/section.tsx +75 -0
- package/src/app/lemonriver/agent-providers/use-providers.ts +61 -0
- package/src/app/lemonriver/board/blocks/block-panel.tsx +666 -0
- package/src/app/lemonriver/board/blocks/cache.ts +28 -0
- package/src/app/lemonriver/board/blocks/change-list.tsx +189 -0
- package/src/app/lemonriver/board/blocks/classify.test.ts +209 -0
- package/src/app/lemonriver/board/blocks/classify.ts +152 -0
- package/src/app/lemonriver/board/blocks/snapshot.ts +116 -0
- package/src/app/lemonriver/board/board-canvas.tsx +627 -104
- package/src/app/lemonriver/board/board-page.tsx +1702 -91
- package/src/app/lemonriver/board/board.css +58 -7
- package/src/app/lemonriver/board/layout.test.ts +84 -0
- package/src/app/lemonriver/board/layout.ts +112 -11
- package/src/app/lemonriver/board/model/agent-tasks.test.ts +200 -0
- package/src/app/lemonriver/board/model/agent-tasks.ts +223 -0
- package/src/app/lemonriver/board/model/apply-prompt.test.ts +121 -0
- package/src/app/lemonriver/board/model/apply-prompt.ts +115 -0
- package/src/app/lemonriver/board/model/apply.ts +71 -0
- package/src/app/lemonriver/board/model/cards.test.ts +95 -2
- package/src/app/lemonriver/board/model/cards.ts +88 -20
- package/src/app/lemonriver/board/model/changes.test.ts +244 -0
- package/src/app/lemonriver/board/model/changes.ts +268 -0
- package/src/app/lemonriver/board/model/filter.test.ts +50 -0
- package/src/app/lemonriver/board/model/filter.ts +42 -0
- package/src/app/lemonriver/board/model/history.test.ts +71 -0
- package/src/app/lemonriver/board/model/history.ts +51 -0
- package/src/app/lemonriver/board/model/restructure.test.ts +205 -0
- package/src/app/lemonriver/board/model/restructure.ts +169 -0
- package/src/app/lemonriver/board/model/self-check.test.ts +329 -0
- package/src/app/lemonriver/board/model/self-check.ts +489 -0
- package/src/app/lemonriver/board/model/structure.test.ts +249 -0
- package/src/app/lemonriver/board/model/structure.ts +209 -0
- package/src/app/lemonriver/board/model/timing.test.ts +113 -0
- package/src/app/lemonriver/board/model/timing.ts +117 -0
- package/src/app/lemonriver/board/page-node.tsx +258 -55
- package/src/app/lemonriver/board/self-check-jump.ts +23 -0
- package/src/app/lemonriver/board/self-check-panel.tsx +272 -0
- package/src/app/lemonriver/board/self-check-runner.tsx +150 -0
- package/src/app/lemonriver/board/status-style.ts +25 -0
- package/src/app/lemonriver/board/use-board-apply.ts +206 -0
- package/src/app/lemonriver/board/use-board-cards.ts +108 -5
- package/src/app/lemonriver/board/use-board-changes.ts +108 -0
- package/src/app/lemonriver/board/use-board-columns.ts +37 -56
- package/src/app/lemonriver/board/use-board-comments.ts +36 -0
- package/src/app/lemonriver/board/use-board-restructures.ts +107 -0
- package/src/app/lemonriver/board/use-history.ts +86 -0
- package/src/app/lemonriver/board/use-page-order.ts +65 -38
- package/src/app/lemonriver/board/use-self-check.ts +117 -0
- package/src/app/lemonriver/board/use-target-minutes.ts +47 -0
- package/src/app/lemonriver/board/views/block-readings.tsx +46 -0
- package/src/app/lemonriver/board/views/list-view.tsx +356 -0
- package/src/app/lemonriver/board/views/progress.tsx +32 -0
- package/src/app/lemonriver/board/views/timeline-view.tsx +341 -0
- package/src/app/lemonriver/chrome/filter-pill.tsx +30 -0
- package/src/app/lemonriver/chrome/sidebar-extras.tsx +12 -1
- package/src/app/lemonriver/features.test.ts +5 -0
- package/src/app/lemonriver/features.ts +2 -1
- package/src/app/lemonriver/home/deck-card.tsx +10 -0
- package/src/app/lemonriver/home/home-page.tsx +55 -6
- package/src/app/lemonriver/home/use-deck-times.ts +4 -1
- package/src/app/lemonriver/outline/build-team.tsx +42 -0
- package/src/app/lemonriver/outline/describe-page.tsx +128 -27
- package/src/app/lemonriver/outline/draft.ts +38 -0
- package/src/app/lemonriver/outline/model.test.ts +118 -0
- package/src/app/lemonriver/outline/model.ts +92 -7
- package/src/app/lemonriver/outline/outline-page.tsx +90 -7
- package/src/app/lemonriver/outline/pick-style.tsx +282 -0
- package/src/app/lemonriver/routes.test.tsx +17 -5
- package/src/app/lemonriver/routes.tsx +28 -0
- package/src/app/lemonriver/settings/settings-page.tsx +3 -0
- package/src/app/lemonriver/slide-extensions.tsx +14 -2
- package/src/app/lemonriver/templates/client.ts +60 -0
- package/src/app/lemonriver/templates/demo-page.tsx +79 -0
- package/src/app/lemonriver/templates/knobs.test.ts +62 -0
- package/src/app/lemonriver/templates/knobs.ts +62 -0
- package/src/app/lemonriver/templates/library-page.tsx +292 -0
- package/src/app/lemonriver/templates/model.test.ts +175 -0
- package/src/app/lemonriver/templates/model.ts +339 -0
- package/src/app/lemonriver/templates/preview-content.test.ts +117 -0
- package/src/app/lemonriver/templates/preview-content.ts +149 -0
- package/src/app/lemonriver/templates/recommend.test.ts +71 -0
- package/src/app/lemonriver/templates/recommend.ts +72 -0
- package/src/app/lemonriver/templates/use-library.ts +58 -0
- package/src/app/lemonriver/templates/virtual.d.ts +28 -0
- package/src/app/lemonriver/text/en.ts +228 -0
- package/src/app/lemonriver/text/es.ts +220 -0
- package/src/app/lemonriver/text/ja.ts +220 -0
- package/src/app/lemonriver/text/types.ts +243 -0
- package/src/app/lemonriver/text/zh-cn.ts +218 -0
- package/src/app/lemonriver/text/zh-tw.ts +218 -0
- package/src/app/lemonriver/theme/lemon-icon.tsx +6 -0
- package/src/app/lemonriver/theme/pages.test.ts +4 -0
- package/src/app/lemonriver/theme/pages.ts +6 -1
- package/src/app/lemonriver/theme/theme-menu.tsx +3 -2
- package/src/app/lemonriver/theme/use-theme.tsx +2 -2
- package/src/app/lemonriver/update/prompt.ts +1 -1
- package/src/app/lib/agent/use-agent.ts +4 -3
- package/src/app/routes/slide.tsx +1 -1
- package/src/app/assets/open-slide.svg +0 -39
- package/src/app/favicon.ico +0 -0
- package/src/app/favicon.svg +0 -39
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { X } from 'lucide-react';
|
|
2
|
+
import { useMemo } from 'react';
|
|
3
|
+
import { Button } from '@/components/ui/button';
|
|
4
|
+
import { format, plural } from '@/lib/use-locale';
|
|
5
|
+
import { cn } from '@/lib/utils';
|
|
6
|
+
import { useLrText } from '../../text';
|
|
7
|
+
import { type Placement, staleChanges } from '../model/changes';
|
|
8
|
+
import type { Restructure } from '../model/restructure';
|
|
9
|
+
import type { Block } from './classify';
|
|
10
|
+
|
|
11
|
+
export type ChangeLine = { key: string; text: string; stale: boolean; remove: () => void };
|
|
12
|
+
|
|
13
|
+
export type ChangeApply = {
|
|
14
|
+
onApply: () => void;
|
|
15
|
+
disabled: boolean;
|
|
16
|
+
/** Why the button cannot be pressed, or null when it can. */
|
|
17
|
+
hint: string | null;
|
|
18
|
+
starting: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** The change list as sentences, one line per page's worth of block moves. */
|
|
22
|
+
export function ChangeList({
|
|
23
|
+
changes,
|
|
24
|
+
restructures,
|
|
25
|
+
pageKeys,
|
|
26
|
+
readOf,
|
|
27
|
+
onRemove,
|
|
28
|
+
onRemoveRestructure,
|
|
29
|
+
readOnly,
|
|
30
|
+
apply,
|
|
31
|
+
}: {
|
|
32
|
+
changes: Placement[];
|
|
33
|
+
restructures: Restructure[];
|
|
34
|
+
pageKeys: string[];
|
|
35
|
+
readOf: (page: string) => Block[] | null;
|
|
36
|
+
onRemove: (indices: number[]) => void;
|
|
37
|
+
onRemoveRestructure: (index: number) => void;
|
|
38
|
+
readOnly: boolean;
|
|
39
|
+
apply: ChangeApply;
|
|
40
|
+
}) {
|
|
41
|
+
const lr = useLrText();
|
|
42
|
+
const stale = useMemo(() => staleChanges(changes, readOf), [changes, readOf]);
|
|
43
|
+
|
|
44
|
+
const lines = useMemo<ChangeLine[]>(() => {
|
|
45
|
+
const pageNumber = (key: string) => pageKeys.indexOf(key) + 1;
|
|
46
|
+
const lines: ChangeLine[] = [];
|
|
47
|
+
const reorderAt = new Map<string, { line: ChangeLine; indices: number[] }>();
|
|
48
|
+
|
|
49
|
+
changes.forEach((item, index) => {
|
|
50
|
+
const from = pageNumber(item.origin.page);
|
|
51
|
+
const label = item.block.text || lr.board.blockKinds[item.block.kind];
|
|
52
|
+
const isStale = stale.has(index);
|
|
53
|
+
|
|
54
|
+
if (item.to === 'parked') {
|
|
55
|
+
if (from > 0) {
|
|
56
|
+
lines.push({
|
|
57
|
+
key: `p${index}`,
|
|
58
|
+
text: format(lr.board.changeToParking, { from, block: label }),
|
|
59
|
+
stale: isStale,
|
|
60
|
+
remove: () => onRemove([index]),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const to = pageNumber(item.to.page);
|
|
67
|
+
if (to <= 0) return;
|
|
68
|
+
|
|
69
|
+
if (item.fromParking) {
|
|
70
|
+
lines.push({
|
|
71
|
+
key: `f${index}`,
|
|
72
|
+
text: format(lr.board.changeFromParking, { block: label, to }),
|
|
73
|
+
stale: isStale,
|
|
74
|
+
remove: () => onRemove([index]),
|
|
75
|
+
});
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (item.to.page === item.origin.page) {
|
|
80
|
+
const existing = reorderAt.get(item.origin.page);
|
|
81
|
+
if (existing) {
|
|
82
|
+
// Every block reordered on this page is undone by the same line.
|
|
83
|
+
existing.indices.push(index);
|
|
84
|
+
existing.line.stale = existing.line.stale || isStale;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const indices = [index];
|
|
88
|
+
const line: ChangeLine = {
|
|
89
|
+
key: `r:${item.origin.page}`,
|
|
90
|
+
text: format(lr.board.changeReorder, { n: from }),
|
|
91
|
+
stale: isStale,
|
|
92
|
+
remove: () => onRemove(indices),
|
|
93
|
+
};
|
|
94
|
+
reorderAt.set(item.origin.page, { line, indices });
|
|
95
|
+
lines.push(line);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
lines.push({
|
|
100
|
+
key: `t${index}`,
|
|
101
|
+
text: format(lr.board.changeToPage, { from, block: label, to }),
|
|
102
|
+
stale: isStale,
|
|
103
|
+
remove: () => onRemove([index]),
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
restructures.forEach((entry, index) => {
|
|
108
|
+
if (entry.kind === 'split') {
|
|
109
|
+
const n = pageNumber(entry.page);
|
|
110
|
+
if (n <= 0) return;
|
|
111
|
+
lines.push({
|
|
112
|
+
key: `s${index}`,
|
|
113
|
+
text: format(plural(entry.blocks.length, lr.board.changeSplit), {
|
|
114
|
+
n,
|
|
115
|
+
k: entry.blocks.length,
|
|
116
|
+
}),
|
|
117
|
+
stale: false,
|
|
118
|
+
remove: () => onRemoveRestructure(index),
|
|
119
|
+
});
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const a = pageNumber(entry.pages[0]);
|
|
123
|
+
const b = pageNumber(entry.pages[1]);
|
|
124
|
+
if (a <= 0 || b <= 0) return;
|
|
125
|
+
lines.push({
|
|
126
|
+
key: `m${index}`,
|
|
127
|
+
text: format(lr.board.changeMerge, { a: Math.min(a, b), b: Math.max(a, b) }),
|
|
128
|
+
stale: false,
|
|
129
|
+
remove: () => onRemoveRestructure(index),
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return lines;
|
|
134
|
+
}, [changes, restructures, pageKeys, lr.board, stale, onRemove, onRemoveRestructure]);
|
|
135
|
+
|
|
136
|
+
if (lines.length === 0) return null;
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<div data-lr-change-list>
|
|
140
|
+
<ul className="max-h-[360px] overflow-y-auto py-1">
|
|
141
|
+
{lines.map((line) => (
|
|
142
|
+
<li
|
|
143
|
+
key={line.key}
|
|
144
|
+
className={cn(
|
|
145
|
+
'flex items-start gap-2 px-3 py-1.5 text-[12.5px]',
|
|
146
|
+
line.stale ? 'text-destructive' : 'text-foreground',
|
|
147
|
+
)}
|
|
148
|
+
>
|
|
149
|
+
<span className="min-w-0 flex-1">
|
|
150
|
+
<span className="line-clamp-2">{line.text}</span>
|
|
151
|
+
{line.stale && (
|
|
152
|
+
<span className="mt-0.5 block text-[11.5px] text-destructive/80">
|
|
153
|
+
{lr.board.changeStale}
|
|
154
|
+
</span>
|
|
155
|
+
)}
|
|
156
|
+
</span>
|
|
157
|
+
<button
|
|
158
|
+
type="button"
|
|
159
|
+
aria-label={lr.board.changeRemove}
|
|
160
|
+
title={lr.board.changeRemove}
|
|
161
|
+
disabled={readOnly}
|
|
162
|
+
onClick={line.remove}
|
|
163
|
+
className="mt-0.5 shrink-0 rounded p-0.5 text-muted-foreground outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/40 disabled:pointer-events-none disabled:opacity-30"
|
|
164
|
+
>
|
|
165
|
+
<X className="size-3.5" />
|
|
166
|
+
</button>
|
|
167
|
+
</li>
|
|
168
|
+
))}
|
|
169
|
+
</ul>
|
|
170
|
+
<div className="border-t border-hairline px-3 py-2">
|
|
171
|
+
<Button
|
|
172
|
+
type="button"
|
|
173
|
+
size="sm"
|
|
174
|
+
className="w-full"
|
|
175
|
+
data-lr-change-apply
|
|
176
|
+
disabled={readOnly || apply.disabled || apply.starting}
|
|
177
|
+
onClick={apply.onApply}
|
|
178
|
+
>
|
|
179
|
+
{lr.board.apply}
|
|
180
|
+
</Button>
|
|
181
|
+
{apply.hint && (
|
|
182
|
+
<p className="mt-1.5 text-[11.5px] text-muted-foreground" data-lr-apply-hint>
|
|
183
|
+
{apply.hint}
|
|
184
|
+
</p>
|
|
185
|
+
)}
|
|
186
|
+
</div>
|
|
187
|
+
</div>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { readBlocks, type SnapNode } from './classify.ts';
|
|
3
|
+
|
|
4
|
+
let counter = 0;
|
|
5
|
+
|
|
6
|
+
function node(tag: string, extra: Partial<SnapNode> = {}, children: SnapNode[] = []): SnapNode {
|
|
7
|
+
counter += 1;
|
|
8
|
+
return {
|
|
9
|
+
tag,
|
|
10
|
+
text: '',
|
|
11
|
+
fullText: extra.fullText ?? extra.text ?? '',
|
|
12
|
+
fontSize: 32,
|
|
13
|
+
lineHeight: 40,
|
|
14
|
+
fontWeight: 400,
|
|
15
|
+
color: '',
|
|
16
|
+
background: '',
|
|
17
|
+
box: { x: 0, y: counter * 10, w: 300, h: 40 },
|
|
18
|
+
clip: null,
|
|
19
|
+
lines: [],
|
|
20
|
+
loc: null,
|
|
21
|
+
placeholder: false,
|
|
22
|
+
step: false,
|
|
23
|
+
children,
|
|
24
|
+
...extra,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function page(...children: SnapNode[]): SnapNode {
|
|
29
|
+
return node('div', { box: { x: 0, y: 0, w: 1920, h: 1080 } }, children);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function blocksOf(...children: SnapNode[]) {
|
|
33
|
+
const result = readBlocks(page(...children));
|
|
34
|
+
if ('whole' in result) throw new Error(`expected blocks, got ${result.whole}`);
|
|
35
|
+
return result.blocks;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('readBlocks kinds', () => {
|
|
39
|
+
it('reads a heading as a title', () => {
|
|
40
|
+
const [block] = blocksOf(node('h1', { text: 'Alpha page one' }));
|
|
41
|
+
expect(block.kind).toBe('title');
|
|
42
|
+
expect(block.text).toBe('Alpha page one');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('reads a large text element as a title and a plain one as text', () => {
|
|
46
|
+
const blocks = blocksOf(
|
|
47
|
+
node('div', { text: 'Big', fontSize: 60 }),
|
|
48
|
+
node('div', { text: 'Small', fontSize: 32 }),
|
|
49
|
+
);
|
|
50
|
+
expect(blocks.map((b) => b.kind)).toEqual(['title', 'text']);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('reads images and placeholders as images', () => {
|
|
54
|
+
const blocks = blocksOf(node('img'), node('div', { placeholder: true }));
|
|
55
|
+
expect(blocks.map((b) => b.kind)).toEqual(['image', 'image']);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('reads a big svg as a chart but ignores a small one', () => {
|
|
59
|
+
const blocks = blocksOf(
|
|
60
|
+
node('svg', { box: { x: 0, y: 100, w: 400, h: 300 } }),
|
|
61
|
+
node('svg', { box: { x: 500, y: 100, w: 20, h: 20 } }),
|
|
62
|
+
);
|
|
63
|
+
expect(blocks.map((b) => b.kind)).toEqual(['chart']);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('reads each list item as a point and never the list itself', () => {
|
|
67
|
+
const blocks = blocksOf(
|
|
68
|
+
node('ul', {}, [node('li', { text: 'One' }), node('li', { text: 'Two' })]),
|
|
69
|
+
);
|
|
70
|
+
expect(blocks.map((b) => b.kind)).toEqual(['point', 'point']);
|
|
71
|
+
expect(blocks.map((b) => b.text)).toEqual(['One', 'Two']);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('reads a quote', () => {
|
|
75
|
+
const [block] = blocksOf(node('blockquote', { text: 'Stay hungry' }));
|
|
76
|
+
expect(block.kind).toBe('quote');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('reads a large figure as number and a small one as text', () => {
|
|
80
|
+
const blocks = blocksOf(
|
|
81
|
+
node('div', { text: '$40.0M', fontSize: 50 }),
|
|
82
|
+
node('div', { text: '$40.0M', fontSize: 24 }),
|
|
83
|
+
);
|
|
84
|
+
expect(blocks.map((b) => b.kind)).toEqual(['number', 'text']);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('reads a heading-sized figure as number and heading-sized words as title', () => {
|
|
88
|
+
const blocks = blocksOf(
|
|
89
|
+
node('div', { text: '$40.0M', fontSize: 96 }),
|
|
90
|
+
node('div', { text: 'Revenue', fontSize: 96 }),
|
|
91
|
+
);
|
|
92
|
+
expect(blocks.map((b) => b.kind)).toEqual(['number', 'title']);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('reads an h2 that happens to hold a figure as a title', () => {
|
|
96
|
+
const [block] = blocksOf(node('h2', { text: '$40.0M', fontSize: 96 }));
|
|
97
|
+
expect(block.kind).toBe('title');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('reads each element that carries its own text as its own block', () => {
|
|
101
|
+
const blocks = blocksOf(
|
|
102
|
+
node('div', {}, [node('span', { text: 'First' }), node('span', { text: 'second' })]),
|
|
103
|
+
);
|
|
104
|
+
expect(blocks.map((b) => b.text)).toEqual(['First', 'second']);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('keeps the words an inline element carries', () => {
|
|
108
|
+
const [block] = blocksOf(
|
|
109
|
+
node(
|
|
110
|
+
'p',
|
|
111
|
+
{
|
|
112
|
+
text: 'How we get from $28.6M to ',
|
|
113
|
+
fullText: 'How we get from $28.6M to $40.0M ARR, and who does what.',
|
|
114
|
+
},
|
|
115
|
+
[node('span', { text: '$40.0M ARR' })],
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
expect(block.kind).toBe('text');
|
|
119
|
+
expect(block.text).toBe('How we get from $28.6M to $40.0M ARR, and who does what.');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('reads a heading split by line breaks as one run of words', () => {
|
|
123
|
+
const [block] = blocksOf(
|
|
124
|
+
node('h1', { text: 'The FY27', fullText: 'The FY27 operating plan.' }),
|
|
125
|
+
);
|
|
126
|
+
expect(block.kind).toBe('title');
|
|
127
|
+
expect(block.text).toBe('The FY27 operating plan.');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('gives an image or a chart no words of its own', () => {
|
|
131
|
+
const blocks = blocksOf(node('img'), node('svg', { box: { x: 0, y: 100, w: 400, h: 300 } }));
|
|
132
|
+
expect(blocks.map((b) => b.text)).toEqual(['', '']);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('leaves small type on the page', () => {
|
|
136
|
+
const blocks = blocksOf(
|
|
137
|
+
node('div', { text: 'Fieldwork', fontSize: 12 }),
|
|
138
|
+
node('div', { text: '01 / 13', fontSize: 12 }),
|
|
139
|
+
node('div', { text: 'Margins held', fontSize: 32 }),
|
|
140
|
+
);
|
|
141
|
+
expect(blocks.map((b) => b.text)).toEqual(['Margins held']);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('does not count small type toward the forty', () => {
|
|
145
|
+
const small = Array.from({ length: 40 }, (_, i) =>
|
|
146
|
+
node('div', { text: `label ${i}`, fontSize: 11 }),
|
|
147
|
+
);
|
|
148
|
+
const big = Array.from({ length: 40 }, (_, i) => node('div', { text: `point ${i}` }));
|
|
149
|
+
expect(blocksOf(...small, ...big)).toHaveLength(40);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('readBlocks structure', () => {
|
|
154
|
+
it('does not read the inside of a block again', () => {
|
|
155
|
+
const blocks = blocksOf(node('h1', { text: 'Title' }, [node('span', { text: 'Inner' })]));
|
|
156
|
+
expect(blocks).toHaveLength(1);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('reads top to bottom, then left to right', () => {
|
|
160
|
+
const blocks = blocksOf(
|
|
161
|
+
node('div', { text: 'bottom-left', box: { x: 0, y: 400, w: 100, h: 40 } }),
|
|
162
|
+
node('div', { text: 'right', box: { x: 600, y: 100, w: 100, h: 40 } }),
|
|
163
|
+
node('div', { text: 'left', box: { x: 0, y: 105, w: 100, h: 40 } }),
|
|
164
|
+
);
|
|
165
|
+
expect(blocks.map((b) => b.text)).toEqual(['left', 'right', 'bottom-left']);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('uses the loc as the id and numbers repeats', () => {
|
|
169
|
+
const blocks = blocksOf(
|
|
170
|
+
node('div', { text: 'a', loc: '1:1' }),
|
|
171
|
+
node('div', { text: 'b', loc: '1:1' }),
|
|
172
|
+
node('div', { text: 'c', loc: '2:1' }),
|
|
173
|
+
);
|
|
174
|
+
expect(blocks.map((b) => b.id)).toEqual(['1:1', '1:1#2', '2:1']);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('numbers blocks that have no loc', () => {
|
|
178
|
+
const blocks = blocksOf(node('div', { text: 'a' }), node('div', { text: 'b' }));
|
|
179
|
+
expect(blocks.map((b) => b.id)).toEqual(['n0', 'n1']);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('squashes whitespace and clips to 80 characters', () => {
|
|
183
|
+
const long = 'word '.repeat(40);
|
|
184
|
+
const [block] = blocksOf(node('div', { text: long }));
|
|
185
|
+
expect(block.text.length).toBe(80);
|
|
186
|
+
expect(block.text).not.toContain(' ');
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('readBlocks whole-page reasons', () => {
|
|
191
|
+
it('refuses a page with stepped reveals', () => {
|
|
192
|
+
expect(readBlocks(page(node('div', { text: 'x', step: true })))).toEqual({
|
|
193
|
+
whole: 'steps',
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('refuses a page with nothing readable', () => {
|
|
198
|
+
expect(readBlocks(page(node('div')))).toEqual({ whole: 'empty' });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('refuses a page with more than 40 blocks', () => {
|
|
202
|
+
const many = Array.from({ length: 41 }, (_, i) => node('div', { text: `item ${i}` }));
|
|
203
|
+
expect(readBlocks(page(...many))).toEqual({ whole: 'tooMany' });
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('refuses a page whose tree could not be copied', () => {
|
|
207
|
+
expect(readBlocks(null)).toEqual({ whole: 'tooLarge' });
|
|
208
|
+
});
|
|
209
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/** A page's DOM copied into plain objects, in canvas units. */
|
|
2
|
+
export type SnapBox = { x: number; y: number; w: number; h: number };
|
|
3
|
+
export type SnapNode = {
|
|
4
|
+
tag: string;
|
|
5
|
+
/** The element's own text, before its children. */
|
|
6
|
+
text: string;
|
|
7
|
+
/** Everything it shows, in document order; a `<br>` becomes a space. */
|
|
8
|
+
fullText: string;
|
|
9
|
+
fontSize: number;
|
|
10
|
+
/** The used line height in px, or 0 when the browser reports none. */
|
|
11
|
+
lineHeight: number;
|
|
12
|
+
fontWeight: number;
|
|
13
|
+
/** Computed colour and background as `#rrggbb`, or '' when it has no hex. */
|
|
14
|
+
color: string;
|
|
15
|
+
background: string;
|
|
16
|
+
box: SnapBox;
|
|
17
|
+
/** What a clipping ancestor leaves of this element, or null when nothing clips it. */
|
|
18
|
+
clip: SnapBox | null;
|
|
19
|
+
/** Where the words actually fall, one box per line; empty when it shows none. */
|
|
20
|
+
lines: SnapBox[];
|
|
21
|
+
loc: string | null;
|
|
22
|
+
placeholder: boolean;
|
|
23
|
+
step: boolean;
|
|
24
|
+
children: SnapNode[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type BlockKind = 'title' | 'point' | 'text' | 'image' | 'chart' | 'quote' | 'number';
|
|
28
|
+
|
|
29
|
+
/** The part of a box a clipping ancestor leaves on screen, or null when none of it shows. */
|
|
30
|
+
export function visibleBox(box: SnapBox, clip: SnapBox | null): SnapBox | null {
|
|
31
|
+
if (!clip) return box;
|
|
32
|
+
const x = Math.max(box.x, clip.x);
|
|
33
|
+
const y = Math.max(box.y, clip.y);
|
|
34
|
+
const right = Math.min(box.x + box.w, clip.x + clip.w);
|
|
35
|
+
const bottom = Math.min(box.y + box.h, clip.y + clip.h);
|
|
36
|
+
if (right <= x || bottom <= y) return null;
|
|
37
|
+
return { x, y, w: right - x, h: bottom - y };
|
|
38
|
+
}
|
|
39
|
+
export type Block = {
|
|
40
|
+
id: string;
|
|
41
|
+
kind: BlockKind;
|
|
42
|
+
text: string;
|
|
43
|
+
loc: string | null;
|
|
44
|
+
box: SnapBox;
|
|
45
|
+
};
|
|
46
|
+
export type WholeReason = 'steps' | 'empty' | 'tooMany' | 'tooLarge';
|
|
47
|
+
|
|
48
|
+
const TEXT_MAX = 80;
|
|
49
|
+
const MAX_BLOCKS = 40;
|
|
50
|
+
const ROW_TOLERANCE = 24;
|
|
51
|
+
/** Below this, a run of text is a caption, a page number or an axis label. */
|
|
52
|
+
const SMALL_FONT = 22;
|
|
53
|
+
|
|
54
|
+
/** Kinds whose text is what the page says in words. */
|
|
55
|
+
const WORDY = new Set<BlockKind>(['title', 'point', 'text', 'quote']);
|
|
56
|
+
|
|
57
|
+
function squash(text: string): string {
|
|
58
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function clip(text: string): string {
|
|
62
|
+
return squash(text).slice(0, TEXT_MAX);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const NUMBER_RE = /^[+\-−]?[$€£¥]?\d[\d.,]*\s?(M|K|B|万|亿|[xX])?$/;
|
|
66
|
+
|
|
67
|
+
export function nodeKind(node: SnapNode): BlockKind | null {
|
|
68
|
+
const tag = node.tag.toLowerCase();
|
|
69
|
+
if (tag === 'img' || node.placeholder) return 'image';
|
|
70
|
+
if ((tag === 'svg' || tag === 'canvas') && node.box.w >= 200 && node.box.h >= 120) return 'chart';
|
|
71
|
+
if (tag === 'blockquote' || tag === 'q') return 'quote';
|
|
72
|
+
if (tag === 'li') return 'point';
|
|
73
|
+
const words = squash(node.fullText);
|
|
74
|
+
if (!words) return null;
|
|
75
|
+
if (tag === 'h1' || tag === 'h2' || tag === 'h3') return 'title';
|
|
76
|
+
// A big figure is a number even when it is as large as a heading.
|
|
77
|
+
if (node.fontSize >= 48 && NUMBER_RE.test(words)) return 'number';
|
|
78
|
+
if (node.fontSize >= 56) return 'title';
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The kind the reader files this node under, plain paragraphs included. */
|
|
83
|
+
export function blockKindOf(node: SnapNode): BlockKind | null {
|
|
84
|
+
const kind = nodeKind(node);
|
|
85
|
+
if (kind) return kind;
|
|
86
|
+
return squash(node.text) ? 'text' : null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function hasStep(node: SnapNode): boolean {
|
|
90
|
+
return node.step || node.children.some(hasStep);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
type Collected = { kind: BlockKind; text: string; loc: string | null; box: SnapBox };
|
|
94
|
+
|
|
95
|
+
function collect(node: SnapNode, out: Collected[]): void {
|
|
96
|
+
const kind = nodeKind(node);
|
|
97
|
+
if (kind) {
|
|
98
|
+
// Small type travels with its page: nobody moves a page number or an axis
|
|
99
|
+
// label on its own, and a dense page would otherwise read as forty blocks.
|
|
100
|
+
if (WORDY.has(kind) && node.fontSize < SMALL_FONT) return;
|
|
101
|
+
out.push({
|
|
102
|
+
kind,
|
|
103
|
+
text: WORDY.has(kind) ? clip(node.fullText) : '',
|
|
104
|
+
loc: node.loc,
|
|
105
|
+
box: node.box,
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
// An element with text of its own is a paragraph of its own, and that text is
|
|
110
|
+
// all of it, inline emphasis included.
|
|
111
|
+
if (squash(node.text)) {
|
|
112
|
+
if (node.fontSize < SMALL_FONT) return;
|
|
113
|
+
out.push({ kind: 'text', text: clip(node.fullText), loc: node.loc, box: node.box });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
for (const child of node.children) collect(child, out);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function inReadingOrder(blocks: Collected[]): Collected[] {
|
|
120
|
+
return [...blocks].sort((a, b) => {
|
|
121
|
+
const sameRow = Math.abs(a.box.y - b.box.y) < ROW_TOLERANCE;
|
|
122
|
+
if (sameRow) return a.box.x - b.box.x;
|
|
123
|
+
return a.box.y - b.box.y;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function withIds(blocks: Collected[]): Block[] {
|
|
128
|
+
const seen = new Map<string, number>();
|
|
129
|
+
return blocks.map((block, index) => {
|
|
130
|
+
let id: string;
|
|
131
|
+
if (block.loc) {
|
|
132
|
+
const n = (seen.get(block.loc) ?? 0) + 1;
|
|
133
|
+
seen.set(block.loc, n);
|
|
134
|
+
id = n === 1 ? block.loc : `${block.loc}#${n}`;
|
|
135
|
+
} else {
|
|
136
|
+
id = `n${index}`;
|
|
137
|
+
}
|
|
138
|
+
return { id, kind: block.kind, text: block.text, loc: block.loc, box: block.box };
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function readBlocks(tree: SnapNode | null): { blocks: Block[] } | { whole: WholeReason } {
|
|
143
|
+
if (!tree) return { whole: 'tooLarge' };
|
|
144
|
+
if (hasStep(tree)) return { whole: 'steps' };
|
|
145
|
+
|
|
146
|
+
const collected: Collected[] = [];
|
|
147
|
+
collect(tree, collected);
|
|
148
|
+
if (collected.length === 0) return { whole: 'empty' };
|
|
149
|
+
if (collected.length > MAX_BLOCKS) return { whole: 'tooMany' };
|
|
150
|
+
|
|
151
|
+
return { blocks: withIds(inReadingOrder(collected)) };
|
|
152
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { toHex } from '../model/self-check.ts';
|
|
2
|
+
import { type SnapBox, type SnapNode, visibleBox } from './classify.ts';
|
|
3
|
+
|
|
4
|
+
const MAX_ELEMENTS = 2000;
|
|
5
|
+
const CLIPPING = /^(hidden|clip|scroll|auto)$/;
|
|
6
|
+
|
|
7
|
+
/** An ancestor that clips everything away; its descendants stay in the tree but show nothing. */
|
|
8
|
+
const CLIPPED_AWAY: SnapBox = { x: 0, y: 0, w: 0, h: 0 };
|
|
9
|
+
|
|
10
|
+
function directText(el: Element): string {
|
|
11
|
+
let text = '';
|
|
12
|
+
for (const child of el.childNodes) {
|
|
13
|
+
if (child.nodeType === Node.TEXT_NODE) text += child.nodeValue ?? '';
|
|
14
|
+
}
|
|
15
|
+
return text;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Every word the element shows, in document order, with a line break standing
|
|
20
|
+
* in for a space. Inline elements like `strong` or a highlight carry words of
|
|
21
|
+
* their own, and a block has to keep them.
|
|
22
|
+
*/
|
|
23
|
+
function allText(el: Element): string {
|
|
24
|
+
let text = '';
|
|
25
|
+
const walk = (parent: Node) => {
|
|
26
|
+
for (const child of parent.childNodes) {
|
|
27
|
+
if (child.nodeType === Node.TEXT_NODE) {
|
|
28
|
+
text += child.nodeValue ?? '';
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (child.nodeType !== Node.ELEMENT_NODE) continue;
|
|
32
|
+
const element = child as Element;
|
|
33
|
+
if (element.tagName === 'BR') text += ' ';
|
|
34
|
+
else walk(element);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
walk(el);
|
|
38
|
+
return text;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Copies the page's DOM into plain objects in canvas units. Runs in the browser
|
|
43
|
+
* only: every figure comes from layout, so nothing here can be tested off-DOM.
|
|
44
|
+
*/
|
|
45
|
+
export function snapshotPage(canvas: HTMLElement, scale: number): SnapNode | null {
|
|
46
|
+
if (!(scale > 0)) return null;
|
|
47
|
+
const origin = canvas.getBoundingClientRect();
|
|
48
|
+
let count = 0;
|
|
49
|
+
|
|
50
|
+
const visit = (el: Element, clip: SnapBox | null): SnapNode | { tooLarge: true } | null => {
|
|
51
|
+
count += 1;
|
|
52
|
+
if (count > MAX_ELEMENTS) return { tooLarge: true };
|
|
53
|
+
|
|
54
|
+
const style = window.getComputedStyle(el);
|
|
55
|
+
if (style.display === 'none' || style.visibility === 'hidden') return null;
|
|
56
|
+
|
|
57
|
+
const rect = el.getBoundingClientRect();
|
|
58
|
+
if (rect.width === 0 || rect.height === 0) return null;
|
|
59
|
+
|
|
60
|
+
const box: SnapBox = {
|
|
61
|
+
x: (rect.left - origin.left) / scale,
|
|
62
|
+
y: (rect.top - origin.top) / scale,
|
|
63
|
+
w: rect.width / scale,
|
|
64
|
+
h: rect.height / scale,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const clips =
|
|
68
|
+
CLIPPING.test(style.overflowX) || CLIPPING.test(style.overflowY)
|
|
69
|
+
? (visibleBox(box, clip) ?? CLIPPED_AWAY)
|
|
70
|
+
: clip;
|
|
71
|
+
|
|
72
|
+
const lines: SnapBox[] = [];
|
|
73
|
+
if (directText(el) !== '') {
|
|
74
|
+
const range = document.createRange();
|
|
75
|
+
range.selectNodeContents(el);
|
|
76
|
+
for (const rect of Array.from(range.getClientRects())) {
|
|
77
|
+
if (rect.width === 0 || rect.height === 0) continue;
|
|
78
|
+
lines.push({
|
|
79
|
+
x: (rect.left - origin.left) / scale,
|
|
80
|
+
y: (rect.top - origin.top) / scale,
|
|
81
|
+
w: rect.width / scale,
|
|
82
|
+
h: rect.height / scale,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const children: SnapNode[] = [];
|
|
88
|
+
for (const child of el.children) {
|
|
89
|
+
const copied = visit(child, clips);
|
|
90
|
+
if (copied && 'tooLarge' in copied) return copied;
|
|
91
|
+
if (copied) children.push(copied);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
tag: el.tagName.toLowerCase(),
|
|
96
|
+
text: directText(el),
|
|
97
|
+
fullText: allText(el),
|
|
98
|
+
fontSize: Number.parseFloat(style.fontSize) || 0,
|
|
99
|
+
lineHeight: Number.parseFloat(style.lineHeight) || 0,
|
|
100
|
+
fontWeight: Number.parseInt(style.fontWeight, 10) || 400,
|
|
101
|
+
color: toHex(style.color),
|
|
102
|
+
background: toHex(style.backgroundColor),
|
|
103
|
+
box,
|
|
104
|
+
clip,
|
|
105
|
+
lines,
|
|
106
|
+
loc: el.getAttribute('data-slide-loc'),
|
|
107
|
+
placeholder: el.hasAttribute('data-slide-placeholder'),
|
|
108
|
+
step: el.hasAttribute('data-osd-step'),
|
|
109
|
+
children,
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const root = visit(canvas, null);
|
|
114
|
+
if (!root || 'tooLarge' in root) return null;
|
|
115
|
+
return root;
|
|
116
|
+
}
|