@yaks/tui 0.0.0-stage → 0.2.36
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/Frame.js +52 -0
- package/Frame.js.map +1 -0
- package/Frame.ts +92 -0
- package/Image.js +10 -0
- package/Image.js.map +1 -0
- package/Image.ts +22 -0
- package/LICENSE +202 -0
- package/README.md +524 -2
- package/RenderedCursor.js +103 -0
- package/RenderedCursor.js.map +1 -0
- package/RenderedCursor.ts +127 -0
- package/Scroll.js +88 -0
- package/Scroll.js.map +1 -0
- package/Scroll.ts +155 -0
- package/Textarea.js +186 -0
- package/Textarea.js.map +1 -0
- package/Textarea.ts +234 -0
- package/VirtualList.js +729 -0
- package/VirtualList.js.map +1 -0
- package/VirtualList.ts +838 -0
- package/_dist/Frame.d.ts +25 -0
- package/_dist/Frame.d.ts.map +1 -0
- package/_dist/Image.d.ts +13 -0
- package/_dist/Image.d.ts.map +1 -0
- package/_dist/RenderedCursor.d.ts +9 -0
- package/_dist/RenderedCursor.d.ts.map +1 -0
- package/_dist/Scroll.d.ts +30 -0
- package/_dist/Scroll.d.ts.map +1 -0
- package/_dist/Textarea.d.ts +26 -0
- package/_dist/Textarea.d.ts.map +1 -0
- package/_dist/VirtualList.d.ts +103 -0
- package/_dist/VirtualList.d.ts.map +1 -0
- package/_dist/guide.d.ts +21 -0
- package/_dist/guide.d.ts.map +1 -0
- package/_dist/history.d.ts +12 -0
- package/_dist/history.d.ts.map +1 -0
- package/_dist/input.d.ts +47 -0
- package/_dist/input.d.ts.map +1 -0
- package/_dist/interaction.d.ts +15 -0
- package/_dist/interaction.d.ts.map +1 -0
- package/_dist/keymap.d.ts +4 -0
- package/_dist/keymap.d.ts.map +1 -0
- package/_dist/mod.d.ts +42 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/mouse.d.ts +15 -0
- package/_dist/mouse.d.ts.map +1 -0
- package/_dist/paint.d.ts +86 -0
- package/_dist/paint.d.ts.map +1 -0
- package/_dist/print.d.ts +20 -0
- package/_dist/print.d.ts.map +1 -0
- package/_dist/run.d.ts +19 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/screen.d.ts +22 -0
- package/_dist/screen.d.ts.map +1 -0
- package/_dist/scrollbar.d.ts +7 -0
- package/_dist/scrollbar.d.ts.map +1 -0
- package/_dist/testing.d.ts +19 -0
- package/_dist/testing.d.ts.map +1 -0
- package/_dist/theme.d.ts +11 -0
- package/_dist/theme.d.ts.map +1 -0
- package/_dist/visual.d.ts +28 -0
- package/_dist/visual.d.ts.map +1 -0
- package/deno.json +1 -0
- package/graphics.js +157 -0
- package/graphics.js.map +1 -0
- package/graphics.ts +191 -0
- package/grid.js +38 -0
- package/grid.js.map +1 -0
- package/grid.ts +46 -0
- package/guide.js +144 -0
- package/guide.js.map +1 -0
- package/guide.ts +169 -0
- package/history.js +58 -0
- package/history.js.map +1 -0
- package/history.ts +59 -0
- package/input.js +356 -0
- package/input.js.map +1 -0
- package/input.ts +324 -0
- package/interaction.js +180 -0
- package/interaction.js.map +1 -0
- package/interaction.ts +185 -0
- package/keymap.js +20 -0
- package/keymap.js.map +1 -0
- package/keymap.ts +23 -0
- package/mod.js +39 -0
- package/mod.js.map +1 -0
- package/mod.ts +92 -0
- package/mouse.js +65 -0
- package/mouse.js.map +1 -0
- package/mouse.ts +98 -0
- package/package.json +49 -4
- package/paint.js +1010 -0
- package/paint.js.map +1 -0
- package/paint.ts +1063 -0
- package/print.js +31 -0
- package/print.js.map +1 -0
- package/print.ts +40 -0
- package/run.js +220 -0
- package/run.js.map +1 -0
- package/run.ts +237 -0
- package/screen.js +97 -0
- package/screen.js.map +1 -0
- package/screen.ts +139 -0
- package/scrollbar.js +30 -0
- package/scrollbar.js.map +1 -0
- package/scrollbar.ts +33 -0
- package/table.js +134 -0
- package/table.js.map +1 -0
- package/table.ts +152 -0
- package/testing.js +86 -0
- package/testing.js.map +1 -0
- package/testing.ts +95 -0
- package/textRows.js +38 -0
- package/textRows.js.map +1 -0
- package/textRows.ts +31 -0
- package/theme.js +50 -0
- package/theme.js.map +1 -0
- package/theme.ts +39 -0
- package/visual.js +195 -0
- package/visual.js.map +1 -0
- package/visual.ts +209 -0
package/VirtualList.js
ADDED
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
import { clampPoint, copyRendered, cursorLine, stepColumn } from "./RenderedCursor.js";
|
|
2
|
+
import { useTextSurface } from "./visual.js";
|
|
3
|
+
import { scrollbar as drawScrollbar } from "./scrollbar.js";
|
|
4
|
+
/** An identity-anchored, lazily measured viewport. No total-height pass. */ /* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h, useLayoutEffect, useRef } from "react";
|
|
5
|
+
import { render } from "@yaks/react/draw";
|
|
6
|
+
import { TElement, touch } from "@yaks/dom";
|
|
7
|
+
import { clip, lay, viewports } from "./paint.js";
|
|
8
|
+
import { terminalFocused, useKeys } from "./screen.js";
|
|
9
|
+
import { rekey, walk } from "@yaks/ux/anchor";
|
|
10
|
+
/** Also usable without Preact: measure is called only for visited items.
|
|
11
|
+
* Cache is bounded by item count; one very large item still costs its full size.
|
|
12
|
+
*/ export class VirtualWindow {
|
|
13
|
+
measure;
|
|
14
|
+
version;
|
|
15
|
+
capacity;
|
|
16
|
+
anchor;
|
|
17
|
+
cursor;
|
|
18
|
+
cursorStyle;
|
|
19
|
+
cursorVisible;
|
|
20
|
+
cursorStamp;
|
|
21
|
+
reader;
|
|
22
|
+
selected;
|
|
23
|
+
selectionVisible;
|
|
24
|
+
selectionStyle;
|
|
25
|
+
follow;
|
|
26
|
+
items;
|
|
27
|
+
indices;
|
|
28
|
+
retainedOrder;
|
|
29
|
+
cache;
|
|
30
|
+
// Retain numeric heights independently of the bounded text/layout cache.
|
|
31
|
+
// Only the current width/style is kept; no extra layout is performed.
|
|
32
|
+
heights;
|
|
33
|
+
heightWidth;
|
|
34
|
+
heightStyle;
|
|
35
|
+
heightItems;
|
|
36
|
+
heightOffset;
|
|
37
|
+
height;
|
|
38
|
+
movement;
|
|
39
|
+
start;
|
|
40
|
+
style;
|
|
41
|
+
revealedSelection;
|
|
42
|
+
revealedWidth;
|
|
43
|
+
revealedHeight;
|
|
44
|
+
range;
|
|
45
|
+
visibleEnd;
|
|
46
|
+
stats;
|
|
47
|
+
constructor(measure, version, follow = false, capacity = 256){
|
|
48
|
+
this.measure = measure;
|
|
49
|
+
this.version = version;
|
|
50
|
+
this.capacity = capacity;
|
|
51
|
+
this.cursorStyle = {
|
|
52
|
+
inverse: true
|
|
53
|
+
};
|
|
54
|
+
this.cursorVisible = true;
|
|
55
|
+
this.cursorStamp = '';
|
|
56
|
+
this.selectionVisible = true;
|
|
57
|
+
this.selectionStyle = {
|
|
58
|
+
bg: '#343f44'
|
|
59
|
+
};
|
|
60
|
+
this.items = [];
|
|
61
|
+
this.indices = new Map();
|
|
62
|
+
this.retainedOrder = [];
|
|
63
|
+
this.cache = new Map();
|
|
64
|
+
this.heights = new Map();
|
|
65
|
+
this.heightWidth = -1;
|
|
66
|
+
this.heightStyle = '';
|
|
67
|
+
this.heightOffset = -1;
|
|
68
|
+
this.height = 0;
|
|
69
|
+
this.movement = 0;
|
|
70
|
+
this.start = false;
|
|
71
|
+
this.style = '';
|
|
72
|
+
this.revealedWidth = 0;
|
|
73
|
+
this.revealedHeight = 0;
|
|
74
|
+
this.range = {
|
|
75
|
+
before: false,
|
|
76
|
+
after: false
|
|
77
|
+
};
|
|
78
|
+
this.visibleEnd = 0;
|
|
79
|
+
this.stats = {
|
|
80
|
+
measured: 0,
|
|
81
|
+
hits: 0
|
|
82
|
+
};
|
|
83
|
+
this.follow = follow;
|
|
84
|
+
}
|
|
85
|
+
update(items) {
|
|
86
|
+
if (items === this.items) return;
|
|
87
|
+
const was = this.items.map((item)=>item.id);
|
|
88
|
+
// Keep bounded ordering metadata across overlapping loaded pages for selection.
|
|
89
|
+
// Membership is by Set: a list of thousands updates in linear time.
|
|
90
|
+
const nextIds = items.map((item)=>item.id);
|
|
91
|
+
const next = new Set(nextIds);
|
|
92
|
+
const retained = new Set(this.retainedOrder);
|
|
93
|
+
const firstOverlap = nextIds.findIndex((id)=>retained.has(id));
|
|
94
|
+
if (firstOverlap < 0) this.retainedOrder = nextIds;
|
|
95
|
+
else {
|
|
96
|
+
const oldAt = this.retainedOrder.indexOf(nextIds[firstOverlap]);
|
|
97
|
+
this.retainedOrder = [
|
|
98
|
+
...new Set([
|
|
99
|
+
...this.retainedOrder.slice(0, oldAt),
|
|
100
|
+
...nextIds,
|
|
101
|
+
...this.retainedOrder.slice(oldAt).filter((id)=>!next.has(id))
|
|
102
|
+
])
|
|
103
|
+
];
|
|
104
|
+
if (this.retainedOrder.length > 512) {
|
|
105
|
+
this.retainedOrder = this.retainedOrder.slice(-512);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
this.items = items;
|
|
109
|
+
this.indices = new Map(items.map((item, i)=>[
|
|
110
|
+
item.id,
|
|
111
|
+
i
|
|
112
|
+
]));
|
|
113
|
+
// A removed anchor falls to its former successor (or its predecessor).
|
|
114
|
+
this.anchor = rekey(this.anchor, was, nextIds);
|
|
115
|
+
}
|
|
116
|
+
/** Item navigation uses only cached heights, never measures intervening history. */ selectionKey(key, selected) {
|
|
117
|
+
if (key.alt || key.shift) return undefined;
|
|
118
|
+
let average = this.cache.size ? [
|
|
119
|
+
...this.cache.values()
|
|
120
|
+
].reduce((n, item)=>n + item.lines.length, 0) / this.cache.size : 1;
|
|
121
|
+
let page = Math.max(1, Math.floor(this.height / average));
|
|
122
|
+
let half = Math.max(1, Math.floor(this.height / average / 2));
|
|
123
|
+
let delta = key.ctrl ? key.text == 'u' ? -half : key.text == 'd' ? half : undefined : key.name == 'up' ? -1 : key.name == 'down' ? 1 : key.name == 'pageup' ? -page : key.name == 'pagedown' ? page : undefined;
|
|
124
|
+
let index = this.indices.get(selected ?? this.anchor?.id ?? '') ?? -1;
|
|
125
|
+
if (key.name == 'home') {
|
|
126
|
+
index = 0;
|
|
127
|
+
this.follow = false;
|
|
128
|
+
} else if (key.name == 'end') {
|
|
129
|
+
index = this.items.length - 1;
|
|
130
|
+
this.follow = true;
|
|
131
|
+
} else if (delta !== undefined) index = index < 0 ? 0 : index + delta;
|
|
132
|
+
else return undefined;
|
|
133
|
+
return this.items[Math.max(0, Math.min(this.items.length - 1, index))]?.id;
|
|
134
|
+
}
|
|
135
|
+
key(key) {
|
|
136
|
+
let page = Math.max(1, this.height - 1);
|
|
137
|
+
let delta = key.name == 'up' ? -1 : key.name == 'down' ? 1 : key.name == 'wheelup' ? -3 : key.name == 'wheeldown' ? 3 : key.name == 'pageup' ? -page : key.name == 'pagedown' ? page : null;
|
|
138
|
+
if (key.ctrl && key.name == 'end') {
|
|
139
|
+
this.follow = true;
|
|
140
|
+
this.movement = 0;
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
if (key.ctrl && key.name == 'home') {
|
|
144
|
+
this.follow = false;
|
|
145
|
+
this.start = true;
|
|
146
|
+
this.movement = 0;
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
if (delta == null) return false;
|
|
150
|
+
// Wheel can bubble to an enclosing scroll region at a known boundary.
|
|
151
|
+
if (key.name == 'wheeldown' && this.follow) return false;
|
|
152
|
+
if (key.name == 'wheelup' && !this.follow && this.movement == 0 && this.anchor?.id == this.items[0]?.id && this.anchor?.offset == 0) return false;
|
|
153
|
+
if (delta < 0) this.follow = false;
|
|
154
|
+
this.movement += delta;
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
/** Move a rendered cursor; only visited items are measured. */ moveCursor(key, point) {
|
|
158
|
+
if (!this.reader) return;
|
|
159
|
+
let index = this.indices.get(point.id);
|
|
160
|
+
if (index == null) return;
|
|
161
|
+
let lines = this.reader(point.id), p = clampPoint(point, lines);
|
|
162
|
+
let row = p.row, col = p.col;
|
|
163
|
+
if (key.name == 'left' || key.name == 'right') {
|
|
164
|
+
col = stepColumn(lines[row], col, key.name == 'left' ? -1 : 1);
|
|
165
|
+
} else if (key.name == 'up') row--;
|
|
166
|
+
else if (key.name == 'down') row++;
|
|
167
|
+
else if (key.name == 'pageup' || key.name == 'pagedown') {
|
|
168
|
+
row += (key.name == 'pageup' ? -1 : 1) * Math.max(1, this.height - 1);
|
|
169
|
+
} else if (key.ctrl && (key.text == 'u' || key.text == 'd')) {
|
|
170
|
+
row += (key.text == 'u' ? -1 : 1) * Math.max(1, Math.floor(this.height / 2));
|
|
171
|
+
} else if (key.name == 'home' && !key.ctrl) col = 0;
|
|
172
|
+
else if (key.name == 'end' && !key.ctrl) {
|
|
173
|
+
col = lines[row].reduce((n, s)=>n + s.text.length, 0) - 1;
|
|
174
|
+
} else return;
|
|
175
|
+
while(row < 0 && index > 0){
|
|
176
|
+
index--;
|
|
177
|
+
lines = this.reader(this.items[index].id);
|
|
178
|
+
row += lines.length;
|
|
179
|
+
}
|
|
180
|
+
while(row >= lines.length && index < this.items.length - 1){
|
|
181
|
+
row -= lines.length;
|
|
182
|
+
index++;
|
|
183
|
+
lines = this.reader(this.items[index].id);
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
...point,
|
|
187
|
+
...clampPoint({
|
|
188
|
+
id: this.items[index].id,
|
|
189
|
+
row,
|
|
190
|
+
col
|
|
191
|
+
}, lines)
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
copyCursor(point) {
|
|
195
|
+
return copyRendered(point, this.retainedOrder, (id)=>this.reader?.(id) ?? this.cache.get(id)?.lines);
|
|
196
|
+
}
|
|
197
|
+
/** Exact measured rows plus the observed average for unmeasured items.
|
|
198
|
+
* The estimate is presentation only: it never changes the scroll anchor.
|
|
199
|
+
*/ position(width) {
|
|
200
|
+
const known = width == this.heightWidth && this.style == this.heightStyle ? [
|
|
201
|
+
...this.heights.values()
|
|
202
|
+
] : [];
|
|
203
|
+
const average = known.length ? known.reduce((sum, item)=>sum + item.rows, 0) / known.length : 1;
|
|
204
|
+
const counted = this.range.total != null && this.range.offset != null;
|
|
205
|
+
const offset = counted ? this.range.offset : 0;
|
|
206
|
+
const index = offset + (this.anchor ? this.indices.get(this.anchor.id) ?? 0 : 0);
|
|
207
|
+
let total = (counted ? this.range.total : this.items.length) * average;
|
|
208
|
+
let top = index * average + (this.anchor?.offset ?? 0);
|
|
209
|
+
for (const item of known){
|
|
210
|
+
if (!counted && (item.index < 0 || item.index >= this.items.length)) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const correction = item.rows - average;
|
|
214
|
+
total += correction;
|
|
215
|
+
if (item.index < index) top += correction;
|
|
216
|
+
}
|
|
217
|
+
// Older callers may only know whether neighbors exist. Keep their fallback
|
|
218
|
+
// without pretending that those booleans describe the full item count.
|
|
219
|
+
if (!counted) {
|
|
220
|
+
const unknown = Math.max(this.height, this.items.length * average);
|
|
221
|
+
if (this.range.before) {
|
|
222
|
+
top += unknown;
|
|
223
|
+
total += unknown;
|
|
224
|
+
}
|
|
225
|
+
if (this.range.after) total += unknown;
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
total,
|
|
229
|
+
top: Math.max(0, Math.min(top, Math.max(0, total - this.height))),
|
|
230
|
+
height: this.height,
|
|
231
|
+
bottom: this.follow && !this.range.after
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
layout(width, height, style = '') {
|
|
235
|
+
this.height = height;
|
|
236
|
+
this.style = style;
|
|
237
|
+
if (width != this.heightWidth || style != this.heightStyle) {
|
|
238
|
+
this.heights.clear();
|
|
239
|
+
this.heightWidth = width;
|
|
240
|
+
this.heightStyle = style;
|
|
241
|
+
}
|
|
242
|
+
const pageOffset = this.range.offset ?? 0;
|
|
243
|
+
if (this.items !== this.heightItems || pageOffset != this.heightOffset) {
|
|
244
|
+
this.heightItems = this.items;
|
|
245
|
+
this.heightOffset = pageOffset;
|
|
246
|
+
const complete = !this.range.before && !this.range.after;
|
|
247
|
+
// Loaded rows are authoritative for version and position; rows outside a
|
|
248
|
+
// partial page retain their last measurements until encountered again.
|
|
249
|
+
if (complete) {
|
|
250
|
+
for (const id of this.heights.keys()){
|
|
251
|
+
if (!this.indices.has(id)) this.heights.delete(id);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for(let i = 0; i < this.items.length; i++){
|
|
255
|
+
const item = this.items[i], known = this.heights.get(item.id);
|
|
256
|
+
if (known && known.version != this.version(item)) {
|
|
257
|
+
this.heights.delete(item.id);
|
|
258
|
+
} else if (known) known.index = pageOffset + i;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (!this.items.length || height <= 0 || width <= 0) return [];
|
|
262
|
+
let get = (i)=>{
|
|
263
|
+
let item = this.items[i], version = this.version(item);
|
|
264
|
+
let cached = this.cache.get(item.id);
|
|
265
|
+
if (!cached || cached.version != version || cached.width != width || cached.style != this.style) {
|
|
266
|
+
let lines = this.measure(item, width);
|
|
267
|
+
cached = {
|
|
268
|
+
version,
|
|
269
|
+
width,
|
|
270
|
+
style: this.style,
|
|
271
|
+
lines: lines.length ? lines : [
|
|
272
|
+
[]
|
|
273
|
+
]
|
|
274
|
+
};
|
|
275
|
+
this.stats.measured++;
|
|
276
|
+
} else this.stats.hits++;
|
|
277
|
+
this.heights.set(item.id, {
|
|
278
|
+
version,
|
|
279
|
+
rows: cached.lines.length,
|
|
280
|
+
index: pageOffset + i
|
|
281
|
+
});
|
|
282
|
+
this.cache.delete(item.id);
|
|
283
|
+
this.cache.set(item.id, cached);
|
|
284
|
+
while(this.cache.size > this.capacity){
|
|
285
|
+
this.cache.delete(this.cache.keys().next().value);
|
|
286
|
+
}
|
|
287
|
+
return cached.lines;
|
|
288
|
+
};
|
|
289
|
+
this.reader = (id)=>{
|
|
290
|
+
const index = this.indices.get(id);
|
|
291
|
+
return index == null ? undefined : get(index);
|
|
292
|
+
};
|
|
293
|
+
if (this.cursor && this.indices.has(this.cursor.id)) {
|
|
294
|
+
this.cursor = {
|
|
295
|
+
...this.cursor,
|
|
296
|
+
...clampPoint(this.cursor, get(this.indices.get(this.cursor.id)))
|
|
297
|
+
};
|
|
298
|
+
this.selected = this.cursor.id;
|
|
299
|
+
const stamp = JSON.stringify([
|
|
300
|
+
this.cursor.id,
|
|
301
|
+
this.cursor.row,
|
|
302
|
+
this.cursor.col,
|
|
303
|
+
width
|
|
304
|
+
]);
|
|
305
|
+
if (stamp != this.cursorStamp) {
|
|
306
|
+
this.cursorStamp = stamp;
|
|
307
|
+
const startIndex = this.anchor ? this.indices.get(this.anchor.id) : undefined;
|
|
308
|
+
let distance = -(this.anchor?.offset ?? 0);
|
|
309
|
+
let index = startIndex ?? this.indices.get(this.cursor.id);
|
|
310
|
+
const target = this.indices.get(this.cursor.id);
|
|
311
|
+
let visible = target >= index;
|
|
312
|
+
while(visible && index < target && distance < height){
|
|
313
|
+
distance += get(index++).length;
|
|
314
|
+
}
|
|
315
|
+
distance += this.cursor.row;
|
|
316
|
+
visible = visible && index == target && distance >= 0 && distance < height;
|
|
317
|
+
if (!visible) {
|
|
318
|
+
this.anchor = {
|
|
319
|
+
id: this.cursor.id,
|
|
320
|
+
offset: Math.max(0, this.cursor.row - height + 1)
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
this.revealedSelection = this.cursor.id;
|
|
324
|
+
this.revealedWidth = width;
|
|
325
|
+
this.revealedHeight = height;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
let i = this.anchor ? this.indices.get(this.anchor.id) ?? 0 : 0;
|
|
329
|
+
let offset = this.anchor?.offset ?? 0;
|
|
330
|
+
if (this.follow && !this.range.after) {
|
|
331
|
+
i = this.items.length - 1;
|
|
332
|
+
let remaining = height;
|
|
333
|
+
while(i > 0 && get(i).length < remaining){
|
|
334
|
+
remaining -= get(i).length;
|
|
335
|
+
i--;
|
|
336
|
+
}
|
|
337
|
+
offset = Math.max(0, get(i).length - remaining);
|
|
338
|
+
} else {
|
|
339
|
+
if (this.start) {
|
|
340
|
+
i = 0;
|
|
341
|
+
offset = 0;
|
|
342
|
+
this.start = false;
|
|
343
|
+
}
|
|
344
|
+
const walked = walk({
|
|
345
|
+
index: i,
|
|
346
|
+
offset
|
|
347
|
+
}, this.movement, (index)=>get(index).length, this.items.length);
|
|
348
|
+
i = walked.index;
|
|
349
|
+
offset = walked.offset;
|
|
350
|
+
}
|
|
351
|
+
// Probe only the existing viewport. Jump directly to a distant selection,
|
|
352
|
+
// then measure at most one screen backwards to reveal its trailing edge.
|
|
353
|
+
let selected = this.selected === undefined ? undefined : this.indices.get(this.selected);
|
|
354
|
+
const reveal = this.selected != this.revealedSelection || width != this.revealedWidth || height != this.revealedHeight;
|
|
355
|
+
this.revealedSelection = this.selected;
|
|
356
|
+
this.revealedWidth = width;
|
|
357
|
+
this.revealedHeight = height;
|
|
358
|
+
if (selected !== undefined && !this.follow && reveal) {
|
|
359
|
+
let end = i, remaining = height + offset, seen = false;
|
|
360
|
+
while(end < this.items.length && remaining > 0){
|
|
361
|
+
remaining -= get(end).length;
|
|
362
|
+
if (end == selected) {
|
|
363
|
+
seen = true;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
end++;
|
|
367
|
+
}
|
|
368
|
+
if (selected < i || selected == i && offset > 0) {
|
|
369
|
+
i = selected;
|
|
370
|
+
offset = 0;
|
|
371
|
+
this.follow = false;
|
|
372
|
+
} else if (!seen || remaining < 0) {
|
|
373
|
+
i = selected;
|
|
374
|
+
let room = height - get(i).length;
|
|
375
|
+
while(i > 0 && room > 0){
|
|
376
|
+
i--;
|
|
377
|
+
room -= get(i).length;
|
|
378
|
+
}
|
|
379
|
+
offset = Math.max(0, -room);
|
|
380
|
+
// Oversized entries show their beginning, not just their final lines.
|
|
381
|
+
if (i == selected) offset = 0;
|
|
382
|
+
this.follow = false;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
let exhausted = false;
|
|
386
|
+
let out = [], end = i, local = offset;
|
|
387
|
+
while(end < this.items.length && out.length < height){
|
|
388
|
+
let lines = get(end);
|
|
389
|
+
exhausted = end == this.items.length - 1 && lines.length - local <= height - out.length;
|
|
390
|
+
let visible = lines.slice(local, local + height - out.length);
|
|
391
|
+
if (this.cursor) {
|
|
392
|
+
visible = visible.map((line, row)=>cursorLine(line, this.items[end].id, row + local, this.cursor, this.retainedOrder, this.cursorVisible ? this.cursorStyle : {}));
|
|
393
|
+
}
|
|
394
|
+
out.push(...this.selectionVisible && end == selected ? visible.map((line)=>line.map((seg)=>({
|
|
395
|
+
...seg,
|
|
396
|
+
style: {
|
|
397
|
+
...seg.style,
|
|
398
|
+
...this.selectionStyle
|
|
399
|
+
}
|
|
400
|
+
}))) : visible);
|
|
401
|
+
local = 0;
|
|
402
|
+
end++;
|
|
403
|
+
}
|
|
404
|
+
// Scrolling down into the end snaps; an append while detached never does.
|
|
405
|
+
if (!this.range.after && !this.follow && this.movement > 0 && exhausted) {
|
|
406
|
+
this.follow = true;
|
|
407
|
+
this.movement = 0;
|
|
408
|
+
return this.layout(width, height, style);
|
|
409
|
+
}
|
|
410
|
+
this.visibleEnd = end;
|
|
411
|
+
this.movement = 0;
|
|
412
|
+
this.anchor = {
|
|
413
|
+
id: this.items[i].id,
|
|
414
|
+
offset
|
|
415
|
+
};
|
|
416
|
+
return out.concat(Array.from({
|
|
417
|
+
length: Math.max(0, height - out.length)
|
|
418
|
+
}, ()=>[]));
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/** The viewport owns a bounded line cache, not thousands of mounted components.
|
|
422
|
+
* Width/style changes invalidate lazily, so resize only measures visible items.
|
|
423
|
+
*/ export let VirtualList = ({ items, renderItem, textOf, version = JSON.stringify, follow = false, pending = false, value, onViewportChange, selected, cursor, onCursor, onYank, selectionVisible = true, selectionClass = 'List_Selected', onSelect, range, onRange, ...attrs })=>{
|
|
424
|
+
// The same range may be requested again only once the loaded ids change. If
|
|
425
|
+
// the caller returns the same page, it must not be asked again, or the list
|
|
426
|
+
// requests forever, stays pending, and drops every key.
|
|
427
|
+
let rangeRequest = useRef(undefined);
|
|
428
|
+
let rangeIds = useRef(undefined);
|
|
429
|
+
let ids = items.map((item)=>item.id).join('\n');
|
|
430
|
+
if (rangeIds.current !== ids) {
|
|
431
|
+
rangeRequest.current = undefined;
|
|
432
|
+
rangeIds.current = ids;
|
|
433
|
+
}
|
|
434
|
+
let requestRange = (request)=>{
|
|
435
|
+
if (!onRange) return;
|
|
436
|
+
let key = JSON.stringify(request);
|
|
437
|
+
if (rangeRequest.current === key) return;
|
|
438
|
+
rangeRequest.current = key;
|
|
439
|
+
queueMicrotask(()=>onRange(request));
|
|
440
|
+
};
|
|
441
|
+
let props = useRef({
|
|
442
|
+
renderItem,
|
|
443
|
+
version
|
|
444
|
+
});
|
|
445
|
+
props.current = {
|
|
446
|
+
renderItem,
|
|
447
|
+
version
|
|
448
|
+
};
|
|
449
|
+
let selectionWidth = useRef(80);
|
|
450
|
+
let selectedIndex = useRef(undefined);
|
|
451
|
+
let env = useRef(undefined);
|
|
452
|
+
let state = useRef(undefined);
|
|
453
|
+
let trees = useRef(new Map());
|
|
454
|
+
let clearTrees = ()=>{
|
|
455
|
+
for (let { root } of trees.current.values()){
|
|
456
|
+
render(null, root);
|
|
457
|
+
}
|
|
458
|
+
trees.current.clear();
|
|
459
|
+
};
|
|
460
|
+
useLayoutEffect(()=>clearTrees, []);
|
|
461
|
+
// Keep the last accepted viewport while its replacement is in flight.
|
|
462
|
+
// This is a paint cache, not a second source of item or scroll state.
|
|
463
|
+
let frame = useRef(undefined);
|
|
464
|
+
let identity = useRef(attrs.id);
|
|
465
|
+
if (identity.current != attrs.id) {
|
|
466
|
+
frame.current = undefined;
|
|
467
|
+
let old = [
|
|
468
|
+
...trees.current.values()
|
|
469
|
+
];
|
|
470
|
+
trees.current = new Map();
|
|
471
|
+
queueMicrotask(()=>{
|
|
472
|
+
for (let { root } of old)render(null, root);
|
|
473
|
+
});
|
|
474
|
+
state.current = undefined;
|
|
475
|
+
identity.current = attrs.id;
|
|
476
|
+
}
|
|
477
|
+
if (!state.current) {
|
|
478
|
+
state.current = new VirtualWindow((item, width)=>{
|
|
479
|
+
let version = props.current.version(item);
|
|
480
|
+
let cached = trees.current.get(item.id);
|
|
481
|
+
if (!cached || cached.version != version) {
|
|
482
|
+
if (cached) render(null, cached.root);
|
|
483
|
+
let root = new TElement('div');
|
|
484
|
+
render(/*#__PURE__*/ h('div', null, props.current.renderItem(item)), root);
|
|
485
|
+
cached = {
|
|
486
|
+
version,
|
|
487
|
+
root
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
trees.current.delete(item.id);
|
|
491
|
+
trees.current.set(item.id, cached);
|
|
492
|
+
while(trees.current.size > 256){
|
|
493
|
+
let key = trees.current.keys().next().value;
|
|
494
|
+
render(null, trees.current.get(key).root);
|
|
495
|
+
trees.current.delete(key);
|
|
496
|
+
}
|
|
497
|
+
let lines = lay(cached.root, env.current.style, width, null, {
|
|
498
|
+
sheet: env.current.sheet,
|
|
499
|
+
metrics: {}
|
|
500
|
+
});
|
|
501
|
+
return lines;
|
|
502
|
+
}, (item)=>props.current.version(item), follow);
|
|
503
|
+
}
|
|
504
|
+
if (value) {
|
|
505
|
+
state.current.anchor = value.anchor;
|
|
506
|
+
state.current.follow = value.follow;
|
|
507
|
+
}
|
|
508
|
+
let publish = ()=>{
|
|
509
|
+
if (pending) return;
|
|
510
|
+
let next = {
|
|
511
|
+
anchor: state.current.anchor,
|
|
512
|
+
follow: state.current.follow
|
|
513
|
+
};
|
|
514
|
+
if (next.follow != value?.follow || next.anchor?.id != value?.anchor?.id || next.anchor?.offset != value?.anchor?.offset) onViewportChange?.(next);
|
|
515
|
+
};
|
|
516
|
+
if (!pending) state.current.update(items);
|
|
517
|
+
useTextSurface({
|
|
518
|
+
id: attrs.id ?? 'list',
|
|
519
|
+
enabled: Boolean(textOf) && !onCursor,
|
|
520
|
+
snapshot: ()=>{
|
|
521
|
+
selectedIndex.current = items.findIndex((i)=>i.id == (selected ?? state.current.anchor?.id));
|
|
522
|
+
let item = items[selectedIndex.current] ?? items.at(-1);
|
|
523
|
+
return {
|
|
524
|
+
text: item && textOf ? textOf(item) : ''
|
|
525
|
+
};
|
|
526
|
+
},
|
|
527
|
+
width: ()=>selectionWidth.current,
|
|
528
|
+
adjacent: (delta)=>{
|
|
529
|
+
let index = Math.max(0, Math.min(items.length - 1, (selectedIndex.current ?? 0) + delta));
|
|
530
|
+
selectedIndex.current = index;
|
|
531
|
+
let item = items[index];
|
|
532
|
+
return {
|
|
533
|
+
text: item && textOf ? textOf(item) : ''
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
state.current.selectionVisible = selectionVisible;
|
|
538
|
+
state.current.range = range ?? {
|
|
539
|
+
before: false,
|
|
540
|
+
after: false
|
|
541
|
+
};
|
|
542
|
+
state.current.selected = selected;
|
|
543
|
+
state.current.cursor = cursor;
|
|
544
|
+
useKeys((key)=>{
|
|
545
|
+
if (pending) return false;
|
|
546
|
+
const cursor = state.current.cursor;
|
|
547
|
+
if (cursor && onCursor) {
|
|
548
|
+
const text = key.name == 'char' && !key.ctrl && !key.alt ? key.text : undefined;
|
|
549
|
+
if (text == 'v') {
|
|
550
|
+
const next = {
|
|
551
|
+
...cursor,
|
|
552
|
+
anchor: {
|
|
553
|
+
id: cursor.id,
|
|
554
|
+
row: cursor.row,
|
|
555
|
+
col: cursor.col
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
state.current.cursor = next;
|
|
559
|
+
onCursor(next);
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
if (key.name == 'escape') {
|
|
563
|
+
state.current.cursor = {
|
|
564
|
+
...cursor,
|
|
565
|
+
anchor: undefined
|
|
566
|
+
};
|
|
567
|
+
onCursor(state.current.cursor);
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
if (text == 'y' && cursor.anchor) {
|
|
571
|
+
try {
|
|
572
|
+
onYank?.(state.current.copyCursor(cursor));
|
|
573
|
+
state.current.cursor = {
|
|
574
|
+
...cursor,
|
|
575
|
+
anchor: undefined
|
|
576
|
+
};
|
|
577
|
+
onCursor(state.current.cursor);
|
|
578
|
+
} catch (error) {
|
|
579
|
+
onYank?.(String(error), true);
|
|
580
|
+
}
|
|
581
|
+
return true;
|
|
582
|
+
}
|
|
583
|
+
const down = key.name == 'down' || key.name == 'pagedown' || key.ctrl && key.text == 'd';
|
|
584
|
+
const up = key.name == 'up' || key.name == 'pageup' || key.ctrl && key.text == 'u';
|
|
585
|
+
const index = items.findIndex((item)=>item.id == cursor.id);
|
|
586
|
+
if (range && (down && range.after && index >= items.length - 2 || up && range.before && index < 2)) {
|
|
587
|
+
requestRange({
|
|
588
|
+
anchor: cursor.id
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
const moved = state.current.moveCursor(key, cursor);
|
|
592
|
+
if (moved) {
|
|
593
|
+
state.current.follow = false;
|
|
594
|
+
state.current.cursor = moved;
|
|
595
|
+
state.current.selected = moved.id;
|
|
596
|
+
onCursor(moved);
|
|
597
|
+
touch();
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
if (range && (key.name == 'home' || key.name == 'end') && (key.ctrl || onSelect)) {
|
|
602
|
+
let edge = key.name == 'home' ? 'start' : 'end';
|
|
603
|
+
if (edge == 'start' ? range.before : range.after) {
|
|
604
|
+
requestRange({
|
|
605
|
+
edge
|
|
606
|
+
});
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (range && selected) {
|
|
611
|
+
let index = items.findIndex((item)=>item.id == selected);
|
|
612
|
+
let up = key.name == 'up' || key.name == 'pageup' || key.ctrl && key.text == 'u';
|
|
613
|
+
let down = key.name == 'down' || key.name == 'pagedown' || key.ctrl && key.text == 'd';
|
|
614
|
+
if (up && range.before && index <= 1 || down && range.after && index >= items.length - 2) {
|
|
615
|
+
requestRange({
|
|
616
|
+
anchor: selected
|
|
617
|
+
});
|
|
618
|
+
return true;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (onSelect) {
|
|
622
|
+
let id = state.current.selectionKey(key, selected);
|
|
623
|
+
if (id !== undefined) {
|
|
624
|
+
// Ordinary selection detaches; End explicitly resumes following.
|
|
625
|
+
if (key.name != 'end') state.current.follow = false;
|
|
626
|
+
publish();
|
|
627
|
+
onSelect(id);
|
|
628
|
+
if (cursor && onCursor) {
|
|
629
|
+
onCursor({
|
|
630
|
+
id,
|
|
631
|
+
row: key.name == 'end' ? Number.MAX_SAFE_INTEGER : 0,
|
|
632
|
+
col: key.name == 'end' ? Number.MAX_SAFE_INTEGER : 0,
|
|
633
|
+
anchor: cursor.anchor
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
if (!state.current.key(key)) return false;
|
|
640
|
+
touch();
|
|
641
|
+
return true;
|
|
642
|
+
}, String(attrs.id));
|
|
643
|
+
useLayoutEffect(()=>{
|
|
644
|
+
touch();
|
|
645
|
+
}, [
|
|
646
|
+
items,
|
|
647
|
+
renderItem,
|
|
648
|
+
selected,
|
|
649
|
+
selectionVisible,
|
|
650
|
+
selectionClass,
|
|
651
|
+
cursor
|
|
652
|
+
]);
|
|
653
|
+
return /*#__PURE__*/ h('div', {
|
|
654
|
+
...attrs,
|
|
655
|
+
onWheel: (event)=>{
|
|
656
|
+
let { release, ctrl, alt, shift } = event.nativeEvent ?? event;
|
|
657
|
+
if (pending) return;
|
|
658
|
+
if (!event.deltaY || release || ctrl || alt || shift) return;
|
|
659
|
+
if (state.current.key({
|
|
660
|
+
name: event.deltaY < 0 ? 'wheelup' : 'wheeldown'
|
|
661
|
+
})) {
|
|
662
|
+
touch();
|
|
663
|
+
event.preventDefault();
|
|
664
|
+
event.stopPropagation();
|
|
665
|
+
}
|
|
666
|
+
},
|
|
667
|
+
ref: (el)=>{
|
|
668
|
+
if (!el) return;
|
|
669
|
+
viewports.set(el, (width, height, style, sheet)=>{
|
|
670
|
+
selectionWidth.current = width;
|
|
671
|
+
env.current = {
|
|
672
|
+
style,
|
|
673
|
+
sheet
|
|
674
|
+
};
|
|
675
|
+
state.current.cursorVisible = terminalFocused.value;
|
|
676
|
+
state.current.selectionStyle = sheet[selectionClass] ?? {
|
|
677
|
+
bg: '#343f44'
|
|
678
|
+
};
|
|
679
|
+
let inner = attrs.scrollbar && width >= 2 ? width - 1 : width;
|
|
680
|
+
if (pending) {
|
|
681
|
+
let saved = frame.current;
|
|
682
|
+
let lines = saved?.lines.slice(0, height).map((line)=>clip(line, inner)) ?? [
|
|
683
|
+
[
|
|
684
|
+
{
|
|
685
|
+
text: 'Loading…',
|
|
686
|
+
style: sheet.Muted ?? {}
|
|
687
|
+
}
|
|
688
|
+
]
|
|
689
|
+
];
|
|
690
|
+
// Never normalize/publish an unloaded window or move its anchor.
|
|
691
|
+
// Keep the existing scrollbar position as well as its content.
|
|
692
|
+
let position = saved ? {
|
|
693
|
+
...saved.position,
|
|
694
|
+
height
|
|
695
|
+
} : {
|
|
696
|
+
height,
|
|
697
|
+
total: height,
|
|
698
|
+
top: 0,
|
|
699
|
+
bottom: true
|
|
700
|
+
};
|
|
701
|
+
return attrs.scrollbar ? drawScrollbar(lines, width, position, sheet) : lines.slice(0, height);
|
|
702
|
+
}
|
|
703
|
+
let lines = state.current.layout(inner, height, JSON.stringify([
|
|
704
|
+
style,
|
|
705
|
+
sheet
|
|
706
|
+
]));
|
|
707
|
+
publish();
|
|
708
|
+
const normalized = state.current.cursor;
|
|
709
|
+
if (cursor && normalized && (selected !== normalized.id || cursor.row != normalized.row || cursor.col != normalized.col)) {
|
|
710
|
+
onCursor?.(normalized);
|
|
711
|
+
}
|
|
712
|
+
if (range && items.length) {
|
|
713
|
+
let at = items.findIndex((item)=>item.id == state.current.anchor?.id);
|
|
714
|
+
if (range.before && at < 8 || range.after && state.current.visibleEnd > items.length - 8) {
|
|
715
|
+
requestRange({
|
|
716
|
+
anchor: range.before && at < 8 ? state.current.anchor?.id : items[Math.min(items.length - 1, state.current.visibleEnd - 1)].id
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
frame.current = {
|
|
721
|
+
lines,
|
|
722
|
+
position: state.current.position(inner)
|
|
723
|
+
};
|
|
724
|
+
return attrs.scrollbar ? drawScrollbar(lines, width, frame.current.position, sheet) : lines;
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
};
|
|
729
|
+
//# sourceMappingURL=VirtualList.js.map
|