@lemonriver/core 0.4.0 → 0.5.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-CS1goZxt.js} +1 -1
- package/dist/cli/bin.js +4 -4
- package/dist/{config-CpC0nM3r.js → config-BpbcCbUv.js} +3673 -2065
- package/dist/{dev-BB3THd5I.js → dev-BpYnfgLL.js} +2 -2
- package/dist/{preview-BGjgTg7H.js → preview-HSzPPkSs.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 +204 -0
- package/src/app/lemonriver/board/blocks/classify.ts +125 -0
- package/src/app/lemonriver/board/blocks/snapshot.ts +86 -0
- package/src/app/lemonriver/board/board-canvas.tsx +627 -104
- package/src/app/lemonriver/board/board-page.tsx +1687 -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 +191 -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/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 +236 -55
- 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-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/outline/model.test.ts +39 -0
- package/src/app/lemonriver/outline/model.ts +12 -0
- package/src/app/lemonriver/outline/outline-page.tsx +34 -1
- package/src/app/lemonriver/settings/settings-page.tsx +3 -0
- package/src/app/lemonriver/slide-extensions.tsx +14 -2
- package/src/app/lemonriver/text/en.ts +171 -0
- package/src/app/lemonriver/text/es.ts +163 -0
- package/src/app/lemonriver/text/ja.ts +163 -0
- package/src/app/lemonriver/text/types.ts +170 -0
- package/src/app/lemonriver/text/zh-cn.ts +161 -0
- package/src/app/lemonriver/text/zh-tw.ts +161 -0
- package/src/app/lemonriver/theme/lemon-icon.tsx +6 -0
- package/src/app/lemonriver/theme/theme-menu.tsx +3 -2
- package/src/app/lemonriver/theme/use-theme.tsx +2 -2
- 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,169 @@
|
|
|
1
|
+
import type { BlockKind } from '../blocks/classify.ts';
|
|
2
|
+
|
|
3
|
+
/** A block the user picked to move onto a page of its own. */
|
|
4
|
+
export type SplitBlock = { id: string; kind: BlockKind; text: string };
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A page the board is about to break up or put together. Like a block move it
|
|
8
|
+
* is only written down here; the deck changes when the list is applied.
|
|
9
|
+
*
|
|
10
|
+
* A split keeps the words of its blocks beside their ids: the prompt that the
|
|
11
|
+
* server writes names them ("move the title …"), and only the browser can read
|
|
12
|
+
* them off the page.
|
|
13
|
+
*/
|
|
14
|
+
export type Restructure =
|
|
15
|
+
| {
|
|
16
|
+
kind: 'split';
|
|
17
|
+
page: string;
|
|
18
|
+
blocks: string[];
|
|
19
|
+
/** One per entry of `blocks`, or absent on a record from an older board. */
|
|
20
|
+
blockInfo?: SplitBlock[];
|
|
21
|
+
}
|
|
22
|
+
| { kind: 'merge'; pages: [string, string] };
|
|
23
|
+
|
|
24
|
+
const MAX_RESTRUCTURES = 50;
|
|
25
|
+
const MAX_BLOCKS = 40;
|
|
26
|
+
|
|
27
|
+
const pagesOf = (entry: Restructure): string[] =>
|
|
28
|
+
entry.kind === 'split' ? [entry.page] : entry.pages;
|
|
29
|
+
|
|
30
|
+
/** Whether a page is already spoken for by a record of the other kind. */
|
|
31
|
+
function overlapsOtherKind(list: Restructure[], entry: Restructure): boolean {
|
|
32
|
+
const pages = new Set(pagesOf(entry));
|
|
33
|
+
return list.some(
|
|
34
|
+
(item) => item.kind !== entry.kind && pagesOf(item).some((page) => pages.has(page)),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The blocks of `page` that go onto a new page after it. Rows can be dragged
|
|
40
|
+
* around after the fact, so the same page split again replaces the last one.
|
|
41
|
+
*/
|
|
42
|
+
export function addSplit(list: Restructure[], page: string, picked: SplitBlock[]): Restructure[] {
|
|
43
|
+
const seen = new Set<string>();
|
|
44
|
+
const kept = picked.filter((block) => block.id && !seen.has(block.id) && seen.add(block.id));
|
|
45
|
+
if (!page || kept.length === 0) return list;
|
|
46
|
+
const limited = kept.slice(0, MAX_BLOCKS);
|
|
47
|
+
const others = list.filter((item) => !pagesOf(item).includes(page));
|
|
48
|
+
return [
|
|
49
|
+
...others,
|
|
50
|
+
{
|
|
51
|
+
kind: 'split',
|
|
52
|
+
page,
|
|
53
|
+
blocks: limited.map((block) => block.id),
|
|
54
|
+
blockInfo: limited.map((block) => ({ id: block.id, kind: block.kind, text: block.text })),
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Two pages becoming one, at the place of the earlier of them. */
|
|
60
|
+
export function addMerge(list: Restructure[], a: string, b: string): Restructure[] {
|
|
61
|
+
if (!a || !b || a === b) return list;
|
|
62
|
+
const pages = new Set([a, b]);
|
|
63
|
+
const others = list.filter((item) => !pagesOf(item).some((page) => pages.has(page)));
|
|
64
|
+
return [...others, { kind: 'merge', pages: [a, b] }];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function removeRestructure(list: Restructure[], index: number): Restructure[] {
|
|
68
|
+
return list.filter((_, at) => at !== index);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** True when recording `entry` would take the place of what is already there. */
|
|
72
|
+
export function restructureReplaced(list: Restructure[], entry: Restructure): boolean {
|
|
73
|
+
return overlapsOtherKind(list, entry);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Every page a split or a merge touches, for the card's own count. */
|
|
77
|
+
export function restructuredPages(list: Restructure[]): Set<string> {
|
|
78
|
+
const pages = new Set<string>();
|
|
79
|
+
for (const entry of list) for (const page of pagesOf(entry)) pages.add(page);
|
|
80
|
+
return pages;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Follows a page operation the board made: keys are renamed the way cards are,
|
|
85
|
+
* and a split or merge whose page is gone is dropped.
|
|
86
|
+
*/
|
|
87
|
+
export function carryRestructures(
|
|
88
|
+
list: Restructure[],
|
|
89
|
+
before: string[],
|
|
90
|
+
after: string[],
|
|
91
|
+
origin: number[],
|
|
92
|
+
): Restructure[] {
|
|
93
|
+
const rename = new Map<string, string>();
|
|
94
|
+
const created = new Set<string>();
|
|
95
|
+
origin.forEach((from, i) => {
|
|
96
|
+
if (from >= 0 && from < before.length) rename.set(before[from], after[i]);
|
|
97
|
+
else created.add(after[i]);
|
|
98
|
+
});
|
|
99
|
+
const live = new Set(before);
|
|
100
|
+
const resolve = (key: string): string | null => {
|
|
101
|
+
const next = live.has(key) ? (rename.get(key) ?? null) : key;
|
|
102
|
+
return next !== null && !created.has(next) ? next : null;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const kept: Restructure[] = [];
|
|
106
|
+
for (const entry of list) {
|
|
107
|
+
if (entry.kind === 'split') {
|
|
108
|
+
const page = resolve(entry.page);
|
|
109
|
+
if (page !== null) kept.push({ ...entry, page });
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const [first, second] = entry.pages;
|
|
113
|
+
const a = resolve(first);
|
|
114
|
+
const b = resolve(second);
|
|
115
|
+
if (a !== null && b !== null) kept.push({ kind: 'merge', pages: [a, b] });
|
|
116
|
+
}
|
|
117
|
+
return kept;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const KINDS = new Set<BlockKind>(['title', 'point', 'text', 'image', 'chart', 'quote', 'number']);
|
|
121
|
+
|
|
122
|
+
function cleanSplit(raw: Record<string, unknown>): Restructure | null {
|
|
123
|
+
if (typeof raw.page !== 'string' || !raw.page) return null;
|
|
124
|
+
if (!Array.isArray(raw.blocks)) return null;
|
|
125
|
+
const source = Array.isArray(raw.blockInfo) ? raw.blockInfo : [];
|
|
126
|
+
const seen = new Set<string>();
|
|
127
|
+
const blocks: string[] = [];
|
|
128
|
+
const blockInfo: SplitBlock[] = [];
|
|
129
|
+
for (const [at, block] of raw.blocks.entries()) {
|
|
130
|
+
if (blocks.length >= MAX_BLOCKS) break;
|
|
131
|
+
if (typeof block !== 'string' || !block || seen.has(block)) continue;
|
|
132
|
+
seen.add(block);
|
|
133
|
+
blocks.push(block);
|
|
134
|
+
const info = (source[at] ?? null) as Record<string, unknown> | null;
|
|
135
|
+
const kind = info && typeof info.kind === 'string' ? (info.kind as BlockKind) : null;
|
|
136
|
+
const text = info && typeof info.text === 'string' ? info.text : '';
|
|
137
|
+
blockInfo.push({
|
|
138
|
+
id: block,
|
|
139
|
+
kind: kind && KINDS.has(kind) ? kind : 'text',
|
|
140
|
+
text: text.slice(0, 80),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
if (blocks.length === 0) return null;
|
|
144
|
+
return { kind: 'split', page: raw.page, blocks, blockInfo };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function cleanEntry(raw: unknown): Restructure | null {
|
|
148
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
149
|
+
const item = raw as Record<string, unknown>;
|
|
150
|
+
if (item.kind === 'split') return cleanSplit(item);
|
|
151
|
+
if (item.kind === 'merge') {
|
|
152
|
+
if (!Array.isArray(item.pages) || item.pages.length !== 2) return null;
|
|
153
|
+
const [a, b] = item.pages;
|
|
154
|
+
if (typeof a !== 'string' || typeof b !== 'string' || !a || !b || a === b) return null;
|
|
155
|
+
return { kind: 'merge', pages: [a, b] };
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function sanitizeRestructures(raw: unknown): Restructure[] {
|
|
161
|
+
if (!Array.isArray(raw)) return [];
|
|
162
|
+
const list: Restructure[] = [];
|
|
163
|
+
for (const value of raw) {
|
|
164
|
+
if (list.length >= MAX_RESTRUCTURES) break;
|
|
165
|
+
const entry = cleanEntry(value);
|
|
166
|
+
if (entry) list.push(entry);
|
|
167
|
+
}
|
|
168
|
+
return list;
|
|
169
|
+
}
|
|
@@ -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
|
+
}
|