@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
@@ -1,8 +1,18 @@
1
1
  import { describe, expect, it } from 'vitest';
2
2
  import {
3
+ addColumnAfter,
3
4
  type BoardColumn,
5
+ chapterProgress,
4
6
  columnOfPosition,
7
+ columnsAfterApply,
8
+ columnsAfterInsert,
5
9
  columnsAfterMove,
10
+ columnsAfterMoveMany,
11
+ columnsAfterRemove,
12
+ deleteColumnAt,
13
+ insertPageColumn,
14
+ moveColumnBlock,
15
+ orderAfterColumnMove,
6
16
  reconcileColumns,
7
17
  sanitizeStructure,
8
18
  } from './structure.ts';
@@ -99,3 +109,242 @@ describe('sanitizeStructure', () => {
99
109
  });
100
110
  });
101
111
  });
112
+
113
+ describe('addColumnAfter', () => {
114
+ it('puts an empty chapter right after the one it was asked for', () => {
115
+ const next = addColumnAfter(deck(), 0, 'New');
116
+ expect(next.map((c) => c.title)).toEqual(['Cover', 'New', 'Chapter 1', 'Chapter 2']);
117
+ expect(next[1].size).toBe(0);
118
+ });
119
+
120
+ it('leaves the page count alone', () => {
121
+ const before = deck();
122
+ const next = addColumnAfter(before, 2, 'New');
123
+ expect(next.reduce((sum, c) => sum + c.size, 0)).toBe(16);
124
+ expect(before).toHaveLength(3);
125
+ });
126
+ });
127
+
128
+ describe('deleteColumnAt', () => {
129
+ it('folds the pages of a removed chapter into the one before it', () => {
130
+ expect(deleteColumnAt(deck(), 1)?.map((c) => c.size)).toEqual([8, 8]);
131
+ });
132
+
133
+ it('folds its pages into the next chapter when it is the first one', () => {
134
+ const next = deleteColumnAt(deck(), 0);
135
+ expect(next?.map((c) => c.size)).toEqual([8, 8]);
136
+ expect(next?.[0].title).toBe('Chapter 1');
137
+ });
138
+
139
+ it('refuses to remove the only chapter left', () => {
140
+ expect(deleteColumnAt([{ title: 'All', size: 4 }], 0)).toBeNull();
141
+ expect(deleteColumnAt(deck(), 9)).toBeNull();
142
+ });
143
+ });
144
+
145
+ describe('moving a whole chapter', () => {
146
+ it('carries its pages with it and renumbers the deck', () => {
147
+ const columns = [
148
+ { title: 'A', size: 2 },
149
+ { title: 'B', size: 3 },
150
+ { title: 'C', size: 2 },
151
+ ];
152
+ expect(moveColumnBlock(columns, 0, 2)?.map((c) => c.title)).toEqual(['B', 'C', 'A']);
153
+ expect(orderAfterColumnMove(columns, 0, 2)).toEqual([2, 3, 4, 5, 6, 0, 1]);
154
+ expect(orderAfterColumnMove(columns, 2, 0)).toEqual([5, 6, 0, 1, 2, 3, 4]);
155
+ });
156
+
157
+ it('is a permutation of every page, losing none', () => {
158
+ const order = orderAfterColumnMove(deck(), 1, 0);
159
+ expect([...order].sort((a, b) => a - b)).toEqual([...Array(16).keys()]);
160
+ });
161
+
162
+ it('does nothing when the chapter stays where it is', () => {
163
+ expect(moveColumnBlock(deck(), 1, 1)).toBeNull();
164
+ expect(orderAfterColumnMove(deck(), 1, 1)).toEqual([...Array(16).keys()]);
165
+ });
166
+ });
167
+
168
+ describe('columnsAfterMoveMany', () => {
169
+ it('moves several pages across one boundary at once', () => {
170
+ const columns = [
171
+ { title: 'A', size: 3 },
172
+ { title: 'B', size: 3 },
173
+ ];
174
+ expect(columnsAfterMoveMany(columns, [0, 1], 4).map((c) => c.size)).toEqual([1, 5]);
175
+ });
176
+
177
+ it('leaves a column unchanged when it both gives and takes a page', () => {
178
+ const columns = [
179
+ { title: 'A', size: 3 },
180
+ { title: 'B', size: 3 },
181
+ ];
182
+ expect(columnsAfterMoveMany(columns, [1], 4).map((c) => c.size)).toEqual([2, 4]);
183
+ expect(columnsAfterMoveMany(columns, [2], 4).map((c) => c.size)).toEqual([2, 4]);
184
+ });
185
+
186
+ it('takes pages from two chapters into a third', () => {
187
+ const columns = [
188
+ { title: 'A', size: 2 },
189
+ { title: 'B', size: 2 },
190
+ { title: 'C', size: 2 },
191
+ ];
192
+ expect(columnsAfterMoveMany(columns, [0, 3], 5).map((c) => c.size)).toEqual([1, 1, 4]);
193
+ });
194
+ });
195
+
196
+ describe('insertPageColumn', () => {
197
+ it('drops a card into a chapter of its own between two chapters', () => {
198
+ const columns = [
199
+ { title: 'A', size: 2 },
200
+ { title: 'B', size: 3 },
201
+ ];
202
+ const result = insertPageColumn(columns, 3, 1, 'New');
203
+ expect(result?.columns.map((c) => [c.title, c.size])).toEqual([
204
+ ['A', 2],
205
+ ['New', 1],
206
+ ['B', 2],
207
+ ]);
208
+ expect(result?.order).toEqual([0, 1, 3, 2, 4]);
209
+ });
210
+
211
+ it('removes the chapter its only card came from', () => {
212
+ const columns = [
213
+ { title: 'A', size: 1 },
214
+ { title: 'B', size: 2 },
215
+ ];
216
+ const result = insertPageColumn(columns, 0, 1, 'New');
217
+ expect(result?.columns.map((c) => [c.title, c.size])).toEqual([
218
+ ['New', 1],
219
+ ['B', 2],
220
+ ]);
221
+ expect(result?.order).toEqual([0, 1, 2]);
222
+ });
223
+
224
+ it('moves a card backwards through the deck as well as forwards', () => {
225
+ const columns = [
226
+ { title: 'A', size: 2 },
227
+ { title: 'B', size: 2 },
228
+ { title: 'C', size: 2 },
229
+ ];
230
+ const result = insertPageColumn(columns, 5, 1, 'New');
231
+ expect(result?.columns.map((c) => [c.title, c.size])).toEqual([
232
+ ['A', 2],
233
+ ['New', 1],
234
+ ['B', 2],
235
+ ['C', 1],
236
+ ]);
237
+ expect(result?.order).toEqual([0, 1, 5, 2, 3, 4]);
238
+ });
239
+
240
+ it('refuses a boundary that is not between two chapters', () => {
241
+ const columns = [
242
+ { title: 'A', size: 2 },
243
+ { title: 'B', size: 2 },
244
+ ];
245
+ expect(insertPageColumn(columns, 0, 0, 'New')).toBeNull();
246
+ expect(insertPageColumn(columns, 0, 2, 'New')).toBeNull();
247
+ expect(insertPageColumn(columns, 99, 1, 'New')).toBeNull();
248
+ });
249
+ });
250
+
251
+ describe('column bookkeeping for batch edits', () => {
252
+ const columns = [
253
+ { title: 'A', size: 2 },
254
+ { title: 'B', size: 3 },
255
+ { title: 'C', size: 2 },
256
+ ];
257
+
258
+ it('takes one page off the chapter that held each removed page', () => {
259
+ expect(columnsAfterRemove(columns, [0, 4, 5]).map((c) => c.size)).toEqual([1, 2, 1]);
260
+ });
261
+
262
+ it('empties a chapter whose pages all went, rather than borrowing a neighbour', () => {
263
+ expect(columnsAfterRemove(columns, [0, 1]).map((c) => c.size)).toEqual([0, 3, 2]);
264
+ });
265
+
266
+ it('adds each new page to the chapter it was added beside', () => {
267
+ expect(columnsAfterInsert(columns, [0, 6]).map((c) => c.size)).toEqual([3, 3, 3]);
268
+ });
269
+
270
+ it('cannot put an emptied chapter back from positions alone, which is why undo replays the saved list', () => {
271
+ const removed = columnsAfterRemove(columns, [0, 1]);
272
+ expect(columnsAfterInsert(removed, [0, 1])).not.toEqual(columns);
273
+ });
274
+ });
275
+
276
+ describe('columnsAfterApply', () => {
277
+ const before = [
278
+ { title: 'Opening', keys: ['a', 'b'] },
279
+ { title: 'Middle', keys: ['c'] },
280
+ { title: 'End', keys: ['d'] },
281
+ ];
282
+
283
+ it('keeps a page the apply created right after another page in that page’s chapter', () => {
284
+ expect(columnsAfterApply(before, ['a', 'a2', 'b', 'c', 'd'])).toEqual([
285
+ { title: 'Opening', size: 3 },
286
+ { title: 'Middle', size: 1 },
287
+ { title: 'End', size: 1 },
288
+ ]);
289
+ });
290
+
291
+ it('takes a merged page out of the chapter that held it', () => {
292
+ expect(columnsAfterApply(before, ['a', 'c', 'd'])).toEqual([
293
+ { title: 'Opening', size: 1 },
294
+ { title: 'Middle', size: 1 },
295
+ { title: 'End', size: 1 },
296
+ ]);
297
+ });
298
+
299
+ it('gives a page added at the very front the chapter of the page behind it', () => {
300
+ expect(columnsAfterApply(before, ['new', 'a', 'b', 'c', 'd']).map((c) => c.size)).toEqual([
301
+ 3, 1, 1,
302
+ ]);
303
+ });
304
+
305
+ it('keeps a chapter that lost every page, with its title', () => {
306
+ expect(columnsAfterApply(before, ['a', 'b', 'd'])).toEqual([
307
+ { title: 'Opening', size: 2 },
308
+ { title: 'Middle', size: 0 },
309
+ { title: 'End', size: 1 },
310
+ ]);
311
+ });
312
+
313
+ it('sends every new page to the chapter named, even one that is still empty', () => {
314
+ const columns = [...before.slice(0, 2), { title: 'End', keys: [] }];
315
+ expect(columnsAfterApply(columns, ['a', 'b', 'c', 'n1', 'n2'], 2)).toEqual([
316
+ { title: 'Opening', size: 2 },
317
+ { title: 'Middle', size: 1 },
318
+ { title: 'End', size: 2 },
319
+ ]);
320
+ });
321
+
322
+ it('falls back to the deck’s new length when no page can be matched', () => {
323
+ expect(columnsAfterApply(before, ['x', 'y'])).toEqual([{ title: 'Opening', size: 2 }]);
324
+ });
325
+ });
326
+
327
+ describe('chapterProgress', () => {
328
+ it('counts an empty chapter as nothing at all', () => {
329
+ expect(chapterProgress({}, [])).toEqual({ done: 0, review: 0, total: 0 });
330
+ });
331
+
332
+ it('counts every page when the chapter is finished', () => {
333
+ const cards = { A: { status: 'done' }, B: { status: 'done' } } as const;
334
+ expect(chapterProgress(cards, ['A', 'B'])).toEqual({ done: 2, review: 0, total: 2 });
335
+ });
336
+
337
+ it('separates what is done from what is waiting to be looked over', () => {
338
+ const cards = {
339
+ A: { status: 'done' },
340
+ B: { status: 'review' },
341
+ C: { status: 'doing' },
342
+ } as const;
343
+ expect(chapterProgress(cards, ['A', 'B', 'C'])).toEqual({ done: 1, review: 1, total: 3 });
344
+ });
345
+
346
+ it('does not count a page with no status as done', () => {
347
+ const cards = { A: { status: 'done' }, B: { memo: 'still open' } } as const;
348
+ expect(chapterProgress(cards, ['A', 'B', 'C'])).toEqual({ done: 1, review: 0, total: 3 });
349
+ });
350
+ });
@@ -1,3 +1,5 @@
1
+ import type { BoardCards } from './cards';
2
+
1
3
  /**
2
4
  * The board's own structure, stored outside the deck: a deck file never learns
3
5
  * that a board exists. Columns hold a count rather than page ids, so reordering
@@ -47,6 +49,55 @@ export function reconcileColumns(columns: BoardColumn[], pageCount: number): Boa
47
49
  return kept;
48
50
  }
49
51
 
52
+ /**
53
+ * The structure after an apply that added or removed pages. A page still in the
54
+ * deck keeps its chapter; a page the apply created joins the chapter of the page
55
+ * in front of it, or of the page behind it when nothing in front survives. A
56
+ * chapter left without pages keeps its title, and when no page can be matched
57
+ * at all the deck's new length is shared out the way a re-read of the deck does.
58
+ */
59
+ export function columnsAfterApply(
60
+ before: { title: string; keys: string[] }[],
61
+ afterKeys: string[],
62
+ newPagesTo?: number,
63
+ ): BoardColumn[] {
64
+ const at = new Map(afterKeys.map((key, index) => [key, index]));
65
+ const owner = new Array<number>(afterKeys.length).fill(-1);
66
+ const sizes = before.map(() => 0);
67
+ before.forEach((column, c) => {
68
+ for (const key of column.keys) {
69
+ const index = at.get(key);
70
+ if (index === undefined || owner[index] !== -1) continue;
71
+ owner[index] = c;
72
+ sizes[c] += 1;
73
+ }
74
+ });
75
+
76
+ if (!owner.some((value) => value !== -1)) {
77
+ return reconcileColumns(
78
+ before.map((column) => ({ title: column.title, size: column.keys.length })),
79
+ afterKeys.length,
80
+ );
81
+ }
82
+
83
+ const into = (index: number): number => {
84
+ for (let i = index - 1; i >= 0; i--) if (owner[i] !== -1) return owner[i];
85
+ for (let i = index + 1; i < owner.length; i++) if (owner[i] !== -1) return owner[i];
86
+ return 0;
87
+ };
88
+
89
+ owner.forEach((value, index) => {
90
+ if (value !== -1) return;
91
+ const column =
92
+ newPagesTo !== undefined && newPagesTo >= 0 && newPagesTo < before.length
93
+ ? newPagesTo
94
+ : into(index);
95
+ sizes[column] += 1;
96
+ });
97
+
98
+ return before.map((column, i) => ({ title: column.title, size: sizes[i] }));
99
+ }
100
+
50
101
  /** Column index that owns a position, and where each column starts. */
51
102
  export function columnStarts(columns: BoardColumn[]): number[] {
52
103
  const starts: number[] = [];
@@ -66,6 +117,25 @@ export function columnOfPosition(columns: BoardColumn[], position: number): numb
66
117
  return 0;
67
118
  }
68
119
 
120
+ export type ChapterProgress = {
121
+ done: number;
122
+ /** Pages waiting to be looked over: they are not finished either. */
123
+ review: number;
124
+ total: number;
125
+ };
126
+
127
+ /** How far along a chapter is; a page with no status counts as unfinished. */
128
+ export function chapterProgress(cards: BoardCards, keys: string[]): ChapterProgress {
129
+ let done = 0;
130
+ let review = 0;
131
+ for (const key of keys) {
132
+ const status = cards[key]?.status;
133
+ if (status === 'done') done += 1;
134
+ else if (status === 'review') review += 1;
135
+ }
136
+ return { done, review, total: keys.length };
137
+ }
138
+
69
139
  /**
70
140
  * Moving a card across a column boundary hands one page from one column to the
71
141
  * other. Nothing else about the structure moves — a chapter divider is an
@@ -80,3 +150,142 @@ export function columnsAfterMove(columns: BoardColumn[], from: number, to: numbe
80
150
  next[target].size += 1;
81
151
  return next;
82
152
  }
153
+
154
+ /**
155
+ * Moving several cards at once sends each one across the same boundary, so a
156
+ * column that both gives and takes a card keeps its length.
157
+ */
158
+ export function columnsAfterMoveMany(
159
+ columns: BoardColumn[],
160
+ froms: number[],
161
+ to: number,
162
+ ): BoardColumn[] {
163
+ if (froms.length === 0) return columns;
164
+ const target = columnOfPosition(columns, to);
165
+ const next = columns.map((column) => ({ ...column }));
166
+ for (const from of froms) next[columnOfPosition(columns, from)].size -= 1;
167
+ next[target].size += froms.length;
168
+ return next;
169
+ }
170
+
171
+ /** The structure left after pages at `indices` are removed, one per page. */
172
+ export function columnsAfterRemove(columns: BoardColumn[], indices: number[]): BoardColumn[] {
173
+ const owners = indices.map((index) => columnOfPosition(columns, index));
174
+ return columns.map((column, i) => {
175
+ const take = owners.filter((owner) => owner === i).length;
176
+ return take === 0 ? column : { ...column, size: Math.max(0, column.size - take) };
177
+ });
178
+ }
179
+
180
+ /**
181
+ * The structure left after a page is added at each index. An added page joins
182
+ * the chapter the page it was added beside belongs to.
183
+ */
184
+ export function columnsAfterInsert(columns: BoardColumn[], indices: number[]): BoardColumn[] {
185
+ const owners = indices.map((index) => columnOfPosition(columns, index));
186
+ return columns.map((column, i) => {
187
+ const give = owners.filter((owner) => owner === i).length;
188
+ return give === 0 ? column : { ...column, size: column.size + give };
189
+ });
190
+ }
191
+
192
+ /** A new chapter sits after `index`, empty until something is put in it. */
193
+ export function addColumnAfter(
194
+ columns: BoardColumn[],
195
+ index: number,
196
+ title: string,
197
+ size = 0,
198
+ ): BoardColumn[] {
199
+ const next = columns.map((column) => ({ ...column }));
200
+ next.splice(index + 1, 0, { title, size });
201
+ return next;
202
+ }
203
+
204
+ export function renameColumnAt(
205
+ columns: BoardColumn[],
206
+ index: number,
207
+ title: string,
208
+ ): BoardColumn[] {
209
+ if (!columns[index]) return columns;
210
+ return columns.map((column, i) => (i === index ? { ...column, title } : column));
211
+ }
212
+
213
+ /**
214
+ * Removing a chapter folds its pages into a neighbour rather than deleting
215
+ * them: the previous chapter when there is one, the next otherwise. The last
216
+ * chapter standing cannot be removed, because every page needs a chapter.
217
+ */
218
+ export function deleteColumnAt(columns: BoardColumn[], index: number): BoardColumn[] | null {
219
+ if (columns.length <= 1 || !columns[index]) return null;
220
+ const next = columns.map((column) => ({ ...column }));
221
+ const [removed] = next.splice(index, 1);
222
+ const into = index === 0 ? 0 : index - 1;
223
+ next[into].size += removed.size;
224
+ return next;
225
+ }
226
+
227
+ /** Chapters in a new order; their pages travel with them. */
228
+ export function moveColumnBlock(
229
+ columns: BoardColumn[],
230
+ from: number,
231
+ to: number,
232
+ ): BoardColumn[] | null {
233
+ if (from === to || !columns[from] || !columns[to]) return null;
234
+ const next = columns.map((column) => ({ ...column }));
235
+ const [moved] = next.splice(from, 1);
236
+ next.splice(to, 0, moved);
237
+ return next;
238
+ }
239
+
240
+ /** Deck positions regrouped by the new chapter order; `order[new] = old`. */
241
+ export function orderAfterColumnMove(columns: BoardColumn[], from: number, to: number): number[] {
242
+ const moved = moveColumnBlock(columns, from, to);
243
+ if (!moved) return identityOrder(columns);
244
+ const blocks: number[][] = [];
245
+ let at = 0;
246
+ for (const column of columns) {
247
+ blocks.push(Array.from({ length: column.size }, (_, i) => at + i));
248
+ at += column.size;
249
+ }
250
+ const [block] = blocks.splice(from, 1);
251
+ blocks.splice(to, 0, block);
252
+ return blocks.flat();
253
+ }
254
+
255
+ function identityOrder(columns: BoardColumn[]): number[] {
256
+ const total = columns.reduce((sum, column) => sum + column.size, 0);
257
+ return Array.from({ length: total }, (_, i) => i);
258
+ }
259
+
260
+ /**
261
+ * A card dropped into the gap between two chapters starts a chapter of its own
262
+ * there, holding nothing but that card. The chapter it came from loses it, and
263
+ * a chapter left with no pages goes away.
264
+ */
265
+ export function insertPageColumn(
266
+ columns: BoardColumn[],
267
+ from: number,
268
+ boundary: number,
269
+ title: string,
270
+ ): { columns: BoardColumn[]; order: number[] } | null {
271
+ const total = columns.reduce((sum, column) => sum + column.size, 0);
272
+ if (from < 0 || from >= total) return null;
273
+ if (boundary <= 0 || boundary >= columns.length) return null;
274
+ const owner = columnOfPosition(columns, from);
275
+
276
+ const kept = columns.flatMap((column, i) => {
277
+ const size = i === owner ? column.size - 1 : column.size;
278
+ return i === owner && size === 0 ? [] : [{ title: column.title, size, original: i }];
279
+ });
280
+ const at = kept.filter((column) => column.original < boundary).length;
281
+ const next = [
282
+ ...kept.slice(0, at).map(({ title: t, size }) => ({ title: t, size })),
283
+ { title, size: 1 },
284
+ ...kept.slice(at).map(({ title: t, size }) => ({ title: t, size })),
285
+ ];
286
+
287
+ const insertAt = next.slice(0, at).reduce((sum, column) => sum + column.size, 0);
288
+ const order = identityOrder(columns).filter((position) => position !== from);
289
+ order.splice(insertAt, 0, from);
290
+ return { columns: next, order };
291
+ }
@@ -0,0 +1,113 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Block } from '../blocks/classify';
3
+ import {
4
+ estimateSeconds,
5
+ formatDuration,
6
+ sanitizeTargetMinutes,
7
+ speakingSeconds,
8
+ } from './timing.ts';
9
+
10
+ const block = (kind: Block['kind'], text = ''): Block => ({
11
+ id: `${kind}-${text}`,
12
+ kind,
13
+ text,
14
+ loc: null,
15
+ box: { x: 0, y: 0, w: 0, h: 0 },
16
+ });
17
+
18
+ /** A run of Chinese characters, one second per four of them. */
19
+ const han = (count: number) => '字'.repeat(count);
20
+ const words = (count: number) => Array.from({ length: count }, (_, i) => `word${i}`).join(' ');
21
+
22
+ describe('estimateSeconds', () => {
23
+ it('reads Chinese by the character: 240 a minute', () => {
24
+ expect(estimateSeconds({ blocks: null, notes: han(240) })).toEqual({
25
+ seconds: 60,
26
+ basis: 'notes',
27
+ });
28
+ });
29
+
30
+ it('reads English by the word: 150 a minute', () => {
31
+ expect(estimateSeconds({ blocks: null, notes: words(150) })).toEqual({
32
+ seconds: 60,
33
+ basis: 'notes',
34
+ });
35
+ });
36
+
37
+ it('adds up a page that mixes both', () => {
38
+ // Half a minute of Chinese plus half a minute of English.
39
+ expect(speakingSeconds(`${han(120)} ${words(75)}`)).toBeCloseTo(60, 5);
40
+ });
41
+
42
+ it('gives a page with only a chart the minimum', () => {
43
+ expect(estimateSeconds({ blocks: [block('chart')] })).toEqual({
44
+ seconds: 15,
45
+ basis: 'page',
46
+ });
47
+ });
48
+
49
+ it('prefers the speaker notes over the words on the page', () => {
50
+ const timing = estimateSeconds({ notes: han(240), blocks: [block('text', words(300))] });
51
+ expect(timing).toEqual({ seconds: 60, basis: 'notes' });
52
+ });
53
+
54
+ it('never goes under fifteen seconds', () => {
55
+ expect(estimateSeconds({ blocks: null, notes: 'Hi' }).seconds).toBe(15);
56
+ expect(estimateSeconds({ blocks: [block('text', han(4))] }).seconds).toBe(15);
57
+ });
58
+
59
+ it('rounds to five seconds', () => {
60
+ // 224 characters is 56 s, 232 is 58 s.
61
+ expect(estimateSeconds({ blocks: null, notes: han(224) }).seconds).toBe(55);
62
+ expect(estimateSeconds({ blocks: null, notes: han(232) }).seconds).toBe(60);
63
+ });
64
+
65
+ it('adds five seconds for each picture when the page is the source', () => {
66
+ // 120 characters is 30 s, plus two pictures.
67
+ const timing = estimateSeconds({
68
+ blocks: [block('text', han(120)), block('image'), block('chart')],
69
+ });
70
+ expect(timing).toEqual({ seconds: 40, basis: 'page' });
71
+ });
72
+
73
+ it('cannot tell how long a page whose blocks were not read takes', () => {
74
+ expect(estimateSeconds({ blocks: null })).toEqual({ seconds: 15, basis: 'none' });
75
+ });
76
+
77
+ it('falls back to the page when the notes are blank', () => {
78
+ expect(estimateSeconds({ notes: ' ', blocks: [block('text', han(120))] })).toEqual({
79
+ seconds: 30,
80
+ basis: 'page',
81
+ });
82
+ });
83
+ });
84
+
85
+ describe('formatDuration', () => {
86
+ it('writes seconds, minutes and seconds, and whole minutes', () => {
87
+ expect(formatDuration(45, 'en')).toBe('45s');
88
+ expect(formatDuration(80, 'en')).toBe('1m 20s');
89
+ expect(formatDuration(720, 'en')).toBe('12 min');
90
+ });
91
+
92
+ it('writes the same in Chinese', () => {
93
+ expect(formatDuration(45, 'zh-CN')).toBe('45 秒');
94
+ expect(formatDuration(80, 'zh-CN')).toBe('1 分 20 秒');
95
+ expect(formatDuration(720, 'zh-CN')).toBe('12 分钟');
96
+ });
97
+ });
98
+
99
+ describe('sanitizeTargetMinutes', () => {
100
+ it('keeps a whole number of minutes within an hour and a half', () => {
101
+ expect(sanitizeTargetMinutes(12)).toBe(12);
102
+ expect(sanitizeTargetMinutes('7')).toBe(7);
103
+ expect(sanitizeTargetMinutes(180)).toBe(180);
104
+ });
105
+
106
+ it('refuses what cannot be a target', () => {
107
+ expect(sanitizeTargetMinutes(0)).toBeNull();
108
+ expect(sanitizeTargetMinutes(181)).toBeNull();
109
+ expect(sanitizeTargetMinutes('')).toBeNull();
110
+ expect(sanitizeTargetMinutes('soon')).toBeNull();
111
+ expect(sanitizeTargetMinutes(null)).toBeNull();
112
+ });
113
+ });