@telepath-computer/television 1.5.10 → 1.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 (130) hide show
  1. package/dist/THIRD-PARTY-NOTICES.txt +701 -0
  2. package/dist/cli.cjs +9035 -596
  3. package/dist/onboarding/tv-guide/welcome.html +14 -4
  4. package/dist/skills/television/SKILL.md +39 -33
  5. package/dist/skills/television/resources.md +4 -4
  6. package/dist/skills/television/templates.md +77 -0
  7. package/dist/skills/television/theming.md +31 -17
  8. package/dist/templates/calendar/README.md +17 -0
  9. package/dist/{skills/tv-tasks → templates/calendar}/THIRD-PARTY-NOTICES.txt +22 -22
  10. package/dist/templates/calendar/calendar-attendees.js +131 -0
  11. package/dist/templates/calendar/calendar-event.js +22 -0
  12. package/dist/templates/calendar/calendar-motion.js +273 -0
  13. package/dist/templates/calendar/calendar-page.js +403 -0
  14. package/dist/templates/calendar/calendar-time-axis.js +32 -0
  15. package/dist/templates/calendar/calendar-week.js +711 -0
  16. package/dist/templates/calendar/data.json +3 -0
  17. package/dist/templates/calendar/icon.svg +1 -0
  18. package/dist/templates/calendar/index.html +27 -0
  19. package/dist/templates/calendar/manifest.json +148 -0
  20. package/dist/templates/calendar/styles.css +771 -0
  21. package/dist/templates/checklist/README.md +45 -0
  22. package/dist/templates/checklist/THIRD-PARTY-NOTICES.txt +29 -0
  23. package/dist/templates/checklist/data.json +1 -0
  24. package/dist/templates/checklist/icon.svg +8 -0
  25. package/dist/templates/checklist/icons.css +61 -0
  26. package/dist/templates/checklist/index.html +47 -0
  27. package/dist/templates/checklist/manifest.json +49 -0
  28. package/dist/templates/checklist/model.d.ts +30 -0
  29. package/dist/templates/checklist/model.js +60 -0
  30. package/dist/templates/checklist/page.js +425 -0
  31. package/dist/templates/checklist/styles.css +206 -0
  32. package/dist/templates/document/README.md +55 -0
  33. package/dist/templates/document/THIRD-PARTY-NOTICES.txt +1080 -0
  34. package/dist/templates/document/data.json +3 -0
  35. package/dist/templates/document/editor.css +1 -0
  36. package/dist/templates/document/editor.js +37 -0
  37. package/dist/templates/document/icon.svg +6 -0
  38. package/dist/templates/document/index.html +29 -0
  39. package/dist/templates/document/manifest.json +22 -0
  40. package/dist/templates/document/page.js +190 -0
  41. package/dist/templates/document/styles.css +76 -0
  42. package/dist/templates/map/README.md +85 -0
  43. package/dist/templates/map/THIRD-PARTY-NOTICES.txt +84 -0
  44. package/dist/templates/map/data.json +1 -0
  45. package/dist/templates/map/icon.svg +8 -0
  46. package/dist/templates/map/icons.svg +43 -0
  47. package/dist/templates/map/index.html +95 -0
  48. package/dist/templates/map/manifest.json +227 -0
  49. package/dist/templates/map/map-model.js +18 -0
  50. package/dist/templates/map/map.css +202 -0
  51. package/dist/templates/map/map.js +728 -0
  52. package/dist/templates/map/vendor/LICENSE +26 -0
  53. package/dist/templates/map/vendor/README.md +11 -0
  54. package/dist/templates/map/vendor/leaflet.css +661 -0
  55. package/dist/templates/map/vendor/leaflet.js +6 -0
  56. package/dist/templates/map/vendor/lucide/LICENSE +43 -0
  57. package/dist/templates/map/vendor/lucide/README.md +46 -0
  58. package/dist/templates/map/vendor/lucide/icons.js +832 -0
  59. package/dist/templates/map/vendor/lucide/svg/airport.svg +15 -0
  60. package/dist/templates/map/vendor/lucide/svg/bar.svg +17 -0
  61. package/dist/templates/map/vendor/lucide/svg/beach.svg +17 -0
  62. package/dist/templates/map/vendor/lucide/svg/bridge.svg +20 -0
  63. package/dist/templates/map/vendor/lucide/svg/bus.svg +21 -0
  64. package/dist/templates/map/vendor/lucide/svg/church.svg +19 -0
  65. package/dist/templates/map/vendor/lucide/svg/coffee.svg +18 -0
  66. package/dist/templates/map/vendor/lucide/svg/ferry.svg +19 -0
  67. package/dist/templates/map/vendor/lucide/svg/flag.svg +15 -0
  68. package/dist/templates/map/vendor/lucide/svg/heart.svg +15 -0
  69. package/dist/templates/map/vendor/lucide/svg/home.svg +16 -0
  70. package/dist/templates/map/vendor/lucide/svg/hospital.svg +19 -0
  71. package/dist/templates/map/vendor/lucide/svg/hotel.svg +24 -0
  72. package/dist/templates/map/vendor/lucide/svg/info.svg +17 -0
  73. package/dist/templates/map/vendor/lucide/svg/landmark.svg +20 -0
  74. package/dist/templates/map/vendor/lucide/svg/mountain.svg +15 -0
  75. package/dist/templates/map/vendor/lucide/svg/museum.svg +20 -0
  76. package/dist/templates/map/vendor/lucide/svg/music.svg +17 -0
  77. package/dist/templates/map/vendor/lucide/svg/park.svg +18 -0
  78. package/dist/templates/map/vendor/lucide/svg/parking.svg +16 -0
  79. package/dist/templates/map/vendor/lucide/svg/pin.svg +16 -0
  80. package/dist/templates/map/vendor/lucide/svg/restaurant.svg +17 -0
  81. package/dist/templates/map/vendor/lucide/svg/school.svg +20 -0
  82. package/dist/templates/map/vendor/lucide/svg/shop.svg +17 -0
  83. package/dist/templates/map/vendor/lucide/svg/sports.svg +19 -0
  84. package/dist/templates/map/vendor/lucide/svg/star.svg +15 -0
  85. package/dist/templates/map/vendor/lucide/svg/theatre.svg +23 -0
  86. package/dist/templates/map/vendor/lucide/svg/train.svg +20 -0
  87. package/dist/templates/map/vendor/lucide/svg/viewpoint.svg +20 -0
  88. package/dist/templates/map/vendor/lucide/svg/work.svg +18 -0
  89. package/dist/templates/sticky-note/Caveat-Medium.woff2 +0 -0
  90. package/dist/templates/sticky-note/README.md +47 -0
  91. package/dist/templates/sticky-note/THIRD-PARTY-NOTICES.txt +127 -0
  92. package/dist/templates/sticky-note/data.json +1 -0
  93. package/dist/templates/sticky-note/icon.svg +16 -0
  94. package/dist/templates/sticky-note/icons.css +61 -0
  95. package/dist/templates/sticky-note/index.html +31 -0
  96. package/dist/templates/sticky-note/manifest.json +27 -0
  97. package/dist/templates/sticky-note/model.d.ts +33 -0
  98. package/dist/templates/sticky-note/model.js +152 -0
  99. package/dist/templates/sticky-note/page.js +84 -0
  100. package/dist/templates/sticky-note/styles.css +140 -0
  101. package/dist/templates/table/README.md +107 -0
  102. package/dist/templates/table/data.json +4 -0
  103. package/dist/templates/table/icon.svg +6 -0
  104. package/dist/templates/table/index.html +15 -0
  105. package/dist/templates/table/manifest.json +70 -0
  106. package/dist/templates/table/model.d.ts +164 -0
  107. package/dist/templates/table/model.js +395 -0
  108. package/dist/templates/table/page.js +1045 -0
  109. package/dist/templates/table/selection-model.d.ts +67 -0
  110. package/dist/templates/table/selection-model.js +163 -0
  111. package/dist/templates/table/styles.css +522 -0
  112. package/dist/views/markdown/THIRD-PARTY-NOTICES.txt +408 -0
  113. package/dist/views/markdown/index.html +21 -21
  114. package/dist/web/assets/main-CRSNodQt.js +864 -0
  115. package/dist/web/assets/main-DnvQkpg4.css +1 -0
  116. package/dist/web/index.html +2 -2
  117. package/package.json +1 -1
  118. package/dist/skills/tv-calendar/SKILL.md +0 -100
  119. package/dist/skills/tv-calendar/calendar.css +0 -1
  120. package/dist/skills/tv-calendar/calendar.js +0 -400
  121. package/dist/skills/tv-table/SKILL.md +0 -95
  122. package/dist/skills/tv-tasks/SKILL.md +0 -269
  123. package/dist/skills/tv-tasks/task.css +0 -180
  124. package/dist/skills/tv-tasks/task.js +0 -304
  125. package/dist/web/assets/calendar-DToAnkzj.png +0 -0
  126. package/dist/web/assets/main-CekDfJPz.js +0 -848
  127. package/dist/web/assets/main-CunC2_Nx.css +0 -1
  128. package/dist/web/assets/markdown-DdDrJuVK.png +0 -0
  129. package/dist/web/assets/table-oeG9uAN1.png +0 -0
  130. package/dist/web/assets/tasks-D4xqXFPl.png +0 -0
@@ -0,0 +1,395 @@
1
+ /* Pure store, display, ordering and write-planning rules for the Table page. */
2
+
3
+ export const MINIMUM_AXIS_SIZE = 4;
4
+ export const AXIS_SIZES = {
5
+ columns: { field: "width", defaultValue: 160, minimum: 64, maximum: 640 },
6
+ rows: { field: "height", defaultValue: 38, minimum: 28, maximum: 240 },
7
+ };
8
+
9
+ function isObject(value) {
10
+ return value !== null && typeof value === "object" && !Array.isArray(value);
11
+ }
12
+
13
+ function isDisplayedEntry(value) {
14
+ return isObject(value) && Number.isFinite(value.order);
15
+ }
16
+
17
+ /** Orders keys the same way JSON store child events do. */
18
+ export function compareStoreKeys(left, right) {
19
+ const leftIndex = /^(?:0|[1-9][0-9]*)$/.test(left);
20
+ const rightIndex = /^(?:0|[1-9][0-9]*)$/.test(right);
21
+ if (leftIndex !== rightIndex) return leftIndex ? -1 : 1;
22
+ if (leftIndex && left.length !== right.length) return left.length - right.length;
23
+ return left < right ? -1 : left > right ? 1 : 0;
24
+ }
25
+
26
+ function orderedEntries(axis) {
27
+ return Object.entries(axis)
28
+ .filter(([, entry]) => isDisplayedEntry(entry))
29
+ .sort(([leftKey, left], [rightKey, right]) => left.order - right.order || compareStoreKeys(leftKey, rightKey));
30
+ }
31
+
32
+ function cellText(value) {
33
+ if (typeof value === "string") return value === "" ? null : value;
34
+ if (typeof value === "number" && Number.isFinite(value)) return JSON.stringify(value);
35
+ if (typeof value === "boolean") return JSON.stringify(value);
36
+ return null;
37
+ }
38
+
39
+ function clampSize(value, rules) {
40
+ return Math.max(rules.minimum, Math.min(value, rules.maximum));
41
+ }
42
+
43
+ function storedSize(value, rules) {
44
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) return rules.defaultValue;
45
+ return clampSize(value, rules);
46
+ }
47
+
48
+ /** Reads a store value into the tolerant display model, or returns null when the root is unreadable. */
49
+ export function readTable(value) {
50
+ const root = value === undefined ? {} : value;
51
+ if (!isObject(root)) return null;
52
+ if (Object.hasOwn(root, "columns") && !isObject(root.columns)) return null;
53
+ if (Object.hasOwn(root, "rows") && !isObject(root.rows)) return null;
54
+
55
+ const columnAxis = root.columns ?? {};
56
+ const rowAxis = root.rows ?? {};
57
+ const columns = orderedEntries(columnAxis).map(([key, column]) => ({
58
+ key,
59
+ order: column.order,
60
+ width: storedSize(column.width, AXIS_SIZES.columns),
61
+ }));
62
+ const displayedColumnKeys = new Set(columns.map(column => column.key));
63
+ const rows = orderedEntries(rowAxis).map(([key, row]) => {
64
+ const sourceCells = isObject(row.cells) ? row.cells : {};
65
+ const cells = Object.fromEntries(Object.entries(sourceCells).flatMap(([columnKey, value]) => {
66
+ const text = cellText(value);
67
+ return displayedColumnKeys.has(columnKey) && text !== null ? [[columnKey, text]] : [];
68
+ }));
69
+ return { key, order: row.order, height: storedSize(row.height, AXIS_SIZES.rows), cells };
70
+ });
71
+ return { columns, rows, headerRow: root.headerRow === true };
72
+ }
73
+
74
+ /** Adds stable stored identities and enough count-based drawn identities to reach four positions. */
75
+ export function displayAxis(entries) {
76
+ const stored = entries.map(entry => ({ ...entry, kind: "stored", id: `stored:${entry.key}` }));
77
+ const count = stored.length;
78
+ const drawn = Array.from({ length: Math.max(0, MINIMUM_AXIS_SIZE - count) }, (_, offset) => {
79
+ const index = count + offset;
80
+ return { kind: "drawn", id: `drawn:${count}:${index}`, index };
81
+ });
82
+ return [...stored, ...drawn];
83
+ }
84
+
85
+ function selectedBounds(entries, selectedKeys) {
86
+ if (selectedKeys.length === 0) return entries.length === 0 ? { start: 0, end: -1 } : null;
87
+ const positions = selectedKeys.map(key => entries.findIndex(([entryKey]) => entryKey === key));
88
+ if (positions.some(position => position < 0) || new Set(positions).size !== positions.length) return null;
89
+ positions.sort((left, right) => left - right);
90
+ if (positions.at(-1) - positions[0] + 1 !== positions.length) return null;
91
+ return { start: positions[0], end: positions.at(-1) };
92
+ }
93
+
94
+ function strictOrder(before, after) {
95
+ if (before === undefined && after === undefined) return 0;
96
+ const sum = before + after;
97
+ let candidate = before === undefined ? after - 1
98
+ : after === undefined ? before + 1
99
+ : Number.isFinite(sum) ? sum / 2 : before / 2 + after / 2;
100
+ if (before === undefined && !(candidate < after)) {
101
+ candidate = after > 0 ? after / 2 : after / 2 - Number.MAX_VALUE / 2;
102
+ } else if (after === undefined && !(candidate > before)) {
103
+ candidate = before < 0 ? before / 2 : before / 2 + Number.MAX_VALUE / 2;
104
+ }
105
+ return Number.isFinite(candidate)
106
+ && (before === undefined || candidate > before)
107
+ && (after === undefined || candidate < after) ? candidate : null;
108
+ }
109
+
110
+ /** Chooses a strict order beside a contiguous stored selection, or null when the axis needs renumbering. */
111
+ export function insertionOrder(entries, selectedKeys, side) {
112
+ const ordered = [...entries].sort((left, right) => left.order - right.order || compareStoreKeys(left.key, right.key));
113
+ const pairs = ordered.map(entry => [entry.key, entry]);
114
+ const bounds = selectedBounds(pairs, selectedKeys);
115
+ if (bounds === null) return undefined;
116
+ const index = side === "before" ? bounds.start : bounds.end + 1;
117
+ return strictOrder(ordered[index - 1]?.order, ordered[index]?.order);
118
+ }
119
+
120
+ /** Chooses consecutive strict orders after an axis, or null when its end must be renumbered. */
121
+ export function ordersAtEnd(entries, count) {
122
+ if (count === 0) return [];
123
+ const ordered = [...entries].sort((left, right) => left.order - right.order || compareStoreKeys(left.key, right.key));
124
+ const result = [];
125
+ let previous = ordered.at(-1)?.order;
126
+ for (let index = 0; index < count; index += 1) {
127
+ const next = strictOrder(previous, undefined);
128
+ if (next === null) return null;
129
+ result.push(next);
130
+ previous = next;
131
+ }
132
+ return result;
133
+ }
134
+
135
+ /** Creates a retry-safe callback that inserts or appends entries while renumbering one current axis. */
136
+ export function createAxisRenumberUpdate(request) {
137
+ return (current) => {
138
+ const axis = current === undefined ? {} : current;
139
+ if (!isObject(axis)) return undefined;
140
+ if (request.entries.some(entry => Object.hasOwn(axis, entry.key))) return undefined;
141
+ const displayed = orderedEntries(axis);
142
+ let insertionIndex = displayed.length;
143
+ if (request.mode === "insert") {
144
+ const bounds = selectedBounds(displayed, request.selectedKeys);
145
+ if (bounds === null) return undefined;
146
+ insertionIndex = request.side === "before" ? bounds.start : bounds.end + 1;
147
+ }
148
+
149
+ const sequence = [...displayed];
150
+ sequence.splice(insertionIndex, 0, ...request.entries.map(entry => [entry.key, entry.value]));
151
+ const result = { ...axis };
152
+ for (const [order, [key, entry]] of sequence.entries()) result[key] = { ...entry, order };
153
+ return result;
154
+ };
155
+ }
156
+
157
+ function planningState(value) {
158
+ const model = readTable(value);
159
+ if (model === null) return null;
160
+ const root = value === undefined ? {} : value;
161
+ return {
162
+ model,
163
+ rows: isObject(root.rows) ? root.rows : {},
164
+ };
165
+ }
166
+
167
+ function set(path, value) {
168
+ return { kind: "set", path, value };
169
+ }
170
+
171
+ function remove(path) {
172
+ return { kind: "remove", path };
173
+ }
174
+
175
+ function operations(operations) {
176
+ return { kind: "operations", operations };
177
+ }
178
+
179
+ function noWrite(reason) {
180
+ return { kind: "no-write", reason };
181
+ }
182
+
183
+ function renumber(axis, request, extraOperations = []) {
184
+ return { kind: "renumber", axis, request, operations: extraOperations };
185
+ }
186
+
187
+ function newAxisEntries(axis, entries, keys, additions = {}) {
188
+ const values = keys.map(key => ({
189
+ key,
190
+ value: axis === "rows" ? { cells: {}, ...additions[key] } : { ...additions[key] },
191
+ }));
192
+ const orders = ordersAtEnd(entries, keys.length);
193
+ if (orders === null) return { kind: "renumber", request: { mode: "append", entries: values } };
194
+ return {
195
+ kind: "ordered",
196
+ entries: values.map((entry, index) => ({
197
+ key: entry.key,
198
+ value: { order: orders[index], ...entry.value },
199
+ })),
200
+ };
201
+ }
202
+
203
+ function neededKeys(supplied, count) {
204
+ if (count === 0) return [];
205
+ return Array.isArray(supplied) && supplied.length >= count ? supplied.slice(0, count) : null;
206
+ }
207
+
208
+ /** Plans a cell commit; null text means that editing was cancelled. */
209
+ export function planCellCommit(value, target, openedText, text, keys) {
210
+ if (text === null) return noWrite("cancelled");
211
+ if (text === openedText) return noWrite("unchanged");
212
+ const state = planningState(value);
213
+ if (state === null) return noWrite("unreadable");
214
+ const columns = displayAxis(state.model.columns);
215
+ const rows = displayAxis(state.model.rows);
216
+ const column = columns[target.columnIndex];
217
+ const row = rows[target.rowIndex];
218
+ if (column === undefined || row === undefined) return noWrite("invalid-target");
219
+ const empty = text.trim() === "";
220
+ if (empty) {
221
+ if (column.kind === "drawn" || row.kind === "drawn") return noWrite("empty-drawn");
222
+ if (openedText === "") return noWrite("unchanged");
223
+ return operations([remove(["rows", row.key, "cells", column.key])]);
224
+ }
225
+
226
+ const columnCount = column.kind === "drawn" ? target.columnIndex - state.model.columns.length + 1 : 0;
227
+ const rowCount = row.kind === "drawn" ? target.rowIndex - state.model.rows.length + 1 : 0;
228
+ const columnKeys = neededKeys(keys.columns, columnCount);
229
+ const rowKeys = neededKeys(keys.rows, rowCount);
230
+ if (columnKeys === null || rowKeys === null) return noWrite("missing-keys");
231
+ if (new Set(columnKeys).size !== columnKeys.length || new Set(rowKeys).size !== rowKeys.length) {
232
+ return noWrite("invalid-keys");
233
+ }
234
+ const columnKey = column.kind === "stored" ? column.key : columnKeys.at(-1);
235
+ const rowKey = row.kind === "stored" ? row.key : rowKeys.at(-1);
236
+ const addedColumns = newAxisEntries("columns", state.model.columns, columnKeys);
237
+ const addedRows = newAxisEntries("rows", state.model.rows, rowKeys, row.kind === "drawn"
238
+ ? { [rowKey]: { cells: { [columnKey]: text } } } : {});
239
+ const writes = [];
240
+ if (addedColumns.kind === "ordered") {
241
+ writes.push(...addedColumns.entries.map(entry => set(["columns", entry.key], entry.value)));
242
+ }
243
+ if (addedRows.kind === "ordered") {
244
+ writes.push(...addedRows.entries.map(entry => set(["rows", entry.key], entry.value)));
245
+ }
246
+ if (row.kind === "stored") writes.push(set(["rows", rowKey, "cells", columnKey], text));
247
+
248
+ if (addedRows.kind === "renumber") {
249
+ if (addedColumns.kind === "renumber") return noWrite("multiple-renumbering-required");
250
+ return renumber("rows", addedRows.request, writes);
251
+ }
252
+ if (addedColumns.kind === "renumber") return renumber("columns", addedColumns.request, writes);
253
+ return operations(writes);
254
+ }
255
+
256
+ /** Plans showing or hiding the header row. */
257
+ export function planSetHeader(headerRow) {
258
+ return operations([set(["headerRow"], headerRow)]);
259
+ }
260
+
261
+ /** Plans one stored or drawn row-height or column-width change. */
262
+ export function planResize(value, axis, targetIndex, openedSize, committedSize, keys) {
263
+ const rules = AXIS_SIZES[axis];
264
+ if (!Number.isFinite(committedSize)) return noWrite("invalid-size");
265
+ const size = clampSize(committedSize, rules);
266
+ if (size === openedSize) return noWrite("unchanged");
267
+ const state = planningState(value);
268
+ if (state === null) return noWrite("unreadable");
269
+ const entries = state.model[axis];
270
+ const target = displayAxis(entries)[targetIndex];
271
+ if (target === undefined) return noWrite("invalid-target");
272
+ if (target.kind === "stored") return operations([set([axis, target.key, rules.field], size)]);
273
+
274
+ const count = targetIndex - entries.length + 1;
275
+ const suppliedKeys = neededKeys(keys, count);
276
+ if (suppliedKeys === null) return noWrite("missing-keys");
277
+ if (new Set(suppliedKeys).size !== suppliedKeys.length) return noWrite("invalid-keys");
278
+ const targetKey = suppliedKeys.at(-1);
279
+ const added = newAxisEntries(axis, entries, suppliedKeys, { [targetKey]: { [rules.field]: size } });
280
+ if (added.kind === "renumber") return renumber(axis, added.request);
281
+ return operations(added.entries.map(entry => set([axis, entry.key], entry.value)));
282
+ }
283
+
284
+ /** Plans adding one row or column after storing every drawn position on that axis. */
285
+ export function planAdd(value, axis, keys) {
286
+ const state = planningState(value);
287
+ if (state === null) return noWrite("unreadable");
288
+ const entries = state.model[axis];
289
+ const count = Math.max(0, MINIMUM_AXIS_SIZE - entries.length) + 1;
290
+ const newKeys = neededKeys(keys, count);
291
+ if (newKeys === null) return noWrite("missing-keys");
292
+ if (new Set(newKeys).size !== newKeys.length) return noWrite("invalid-keys");
293
+ const added = newAxisEntries(axis, entries, newKeys);
294
+ if (added.kind === "renumber") return renumber(axis, added.request);
295
+ return operations(added.entries.map(entry => set([axis, entry.key], entry.value)));
296
+ }
297
+
298
+ /** Plans inserting one row or column and only the drawn positions before it. */
299
+ export function planInsert(value, axis, selection, side, keys) {
300
+ const state = planningState(value);
301
+ if (state === null) return noWrite("unreadable");
302
+ const entries = state.model[axis];
303
+ const displayed = displayAxis(entries);
304
+ if (selection.start < 0 || selection.end < selection.start || selection.end >= displayed.length) {
305
+ return noWrite("invalid-selection");
306
+ }
307
+ const insertionIndex = side === "before" ? selection.start : selection.end + 1;
308
+ const drawnBefore = Math.max(0, insertionIndex - entries.length);
309
+ const newKeys = neededKeys(keys, drawnBefore + 1);
310
+ if (newKeys === null) return noWrite("missing-keys");
311
+ if (new Set(newKeys).size !== newKeys.length) return noWrite("invalid-keys");
312
+ const drawnKeys = newKeys.slice(0, drawnBefore);
313
+ const newKey = newKeys.at(-1);
314
+ const storedSelectionKeys = displayed.slice(selection.start, selection.end + 1)
315
+ .filter(item => item.kind === "stored").map(item => item.key);
316
+
317
+ if (insertionIndex >= entries.length) {
318
+ const added = newAxisEntries(axis, entries, [...drawnKeys, newKey]);
319
+ if (added.kind === "renumber") {
320
+ return renumber(axis, storedSelectionKeys.length === 0 ? added.request : {
321
+ mode: "insert",
322
+ selectedKeys: storedSelectionKeys,
323
+ side,
324
+ entries: added.request.entries,
325
+ });
326
+ }
327
+ return operations(added.entries.map(entry => set([axis, entry.key], entry.value)));
328
+ }
329
+
330
+ const order = insertionOrder(entries, storedSelectionKeys, side);
331
+ if (order === undefined) return noWrite("invalid-selection");
332
+ if (order === null) {
333
+ const valueForEntry = axis === "rows" ? { cells: {} } : {};
334
+ return renumber(axis, {
335
+ mode: "insert",
336
+ selectedKeys: storedSelectionKeys,
337
+ side,
338
+ entries: [{ key: newKey, value: valueForEntry }],
339
+ });
340
+ }
341
+ const valueForEntry = axis === "rows" ? { order, cells: {} } : { order };
342
+ return operations([set([axis, newKey], valueForEntry)]);
343
+ }
344
+
345
+ /** Plans removing the stored entries in a displayed row or column range. */
346
+ export function planDelete(value, axis, selection) {
347
+ const state = planningState(value);
348
+ if (state === null) return noWrite("unreadable");
349
+ const displayed = displayAxis(state.model[axis]);
350
+ if (selection.start < 0 || selection.end < selection.start || selection.end >= displayed.length) {
351
+ return noWrite("invalid-selection");
352
+ }
353
+ const writes = displayed.slice(selection.start, selection.end + 1)
354
+ .filter(item => item.kind === "stored")
355
+ .map(item => remove([axis, item.key]));
356
+ return writes.length === 0 ? noWrite("nothing-to-delete") : operations(writes);
357
+ }
358
+
359
+ function nonEmptyCell(state, rowKey, columnKey) {
360
+ const cells = state.rows[rowKey]?.cells;
361
+ return isObject(cells) && Object.hasOwn(cells, columnKey) && cellText(cells[columnKey]) !== null;
362
+ }
363
+
364
+ /** Plans clearing selected non-empty cells without removing structure. */
365
+ export function planClear(value, selection) {
366
+ const state = planningState(value);
367
+ if (state === null) return noWrite("unreadable");
368
+ const rows = displayAxis(state.model.rows);
369
+ const columns = displayAxis(state.model.columns);
370
+ if (selection.kind === "cell"
371
+ && (rows[selection.rowIndex] === undefined || columns[selection.columnIndex] === undefined)) {
372
+ return noWrite("invalid-target");
373
+ }
374
+ if (selection.kind === "rows" || selection.kind === "columns") {
375
+ const axis = selection.kind === "rows" ? rows : columns;
376
+ if (selection.start < 0 || selection.end < selection.start || selection.end >= axis.length) {
377
+ return noWrite("invalid-selection");
378
+ }
379
+ }
380
+
381
+ const selectedRows = selection.kind === "cell" ? rows.slice(selection.rowIndex, selection.rowIndex + 1)
382
+ : selection.kind === "rows" ? rows.slice(selection.start, selection.end + 1) : rows;
383
+ const selectedColumns = selection.kind === "cell" ? columns.slice(selection.columnIndex, selection.columnIndex + 1)
384
+ : selection.kind === "columns" ? columns.slice(selection.start, selection.end + 1) : columns;
385
+ const writes = [];
386
+ for (const row of selectedRows) {
387
+ if (row.kind !== "stored") continue;
388
+ for (const column of selectedColumns) {
389
+ if (column.kind === "stored" && nonEmptyCell(state, row.key, column.key)) {
390
+ writes.push(remove(["rows", row.key, "cells", column.key]));
391
+ }
392
+ }
393
+ }
394
+ return writes.length === 0 ? noWrite("nothing-to-clear") : operations(writes);
395
+ }