@bigpixelrocket/uiok 0.3.0 → 0.4.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 (121) hide show
  1. package/CHANGELOG.md +23 -1
  2. package/dist/dialog/ConfirmDialog.d.ts +7 -1
  3. package/dist/dialog/ConfirmDialog.js +2 -2
  4. package/dist/dialog/ProgressDialog.d.ts +7 -1
  5. package/dist/dialog/ProgressDialog.js +2 -2
  6. package/dist/dialog/TypedNameDialog.d.ts +8 -0
  7. package/dist/field/Check.d.ts +9 -2
  8. package/dist/field/Check.js +5 -4
  9. package/dist/field/Controls.d.ts +198 -0
  10. package/dist/field/Controls.js +94 -0
  11. package/dist/field/FieldLayout.d.ts +126 -0
  12. package/dist/field/FieldLayout.js +89 -0
  13. package/dist/field/index.d.ts +9 -6
  14. package/dist/field/index.js +9 -6
  15. package/dist/frame/Dock.js +1 -1
  16. package/dist/frame/SidebarResizer.js +1 -1
  17. package/dist/frame/Tree.d.ts +9 -2
  18. package/dist/frame/Tree.js +29 -6
  19. package/dist/labels/UiokLabels.d.ts +3 -0
  20. package/dist/labels/UiokLabels.js +3 -1
  21. package/dist/labels/index.d.ts +1 -0
  22. package/dist/labels/index.js +1 -0
  23. package/dist/labels/viewer.d.ts +193 -0
  24. package/dist/labels/viewer.js +124 -0
  25. package/dist/listing/PaneMessage.d.ts +28 -0
  26. package/dist/listing/PaneMessage.js +12 -0
  27. package/dist/listing/index.d.ts +4 -2
  28. package/dist/listing/index.js +4 -2
  29. package/dist/styles.css +3 -0
  30. package/dist/surface/Card.d.ts +18 -0
  31. package/dist/surface/Card.js +10 -0
  32. package/dist/surface/Notice.d.ts +45 -0
  33. package/dist/surface/Notice.js +27 -0
  34. package/dist/surface/index.d.ts +5 -3
  35. package/dist/surface/index.js +5 -3
  36. package/dist/themes/themes.generated.css +1 -0
  37. package/dist/themes/tokens.js +4 -1
  38. package/dist/viewer/AudioView.d.ts +13 -0
  39. package/dist/viewer/AudioView.js +25 -0
  40. package/dist/viewer/HexJump.logic.d.ts +16 -0
  41. package/dist/viewer/HexJump.logic.js +27 -0
  42. package/dist/viewer/HexRows.logic.d.ts +55 -0
  43. package/dist/viewer/HexRows.logic.js +84 -0
  44. package/dist/viewer/HexSelection.logic.d.ts +57 -0
  45. package/dist/viewer/HexSelection.logic.js +68 -0
  46. package/dist/viewer/HexView.d.ts +10 -0
  47. package/dist/viewer/HexView.js +291 -0
  48. package/dist/viewer/HexViewport.logic.d.ts +61 -0
  49. package/dist/viewer/HexViewport.logic.js +79 -0
  50. package/dist/viewer/ImageView.d.ts +19 -0
  51. package/dist/viewer/ImageView.js +230 -0
  52. package/dist/viewer/ImageZoom.logic.d.ts +65 -0
  53. package/dist/viewer/ImageZoom.logic.js +119 -0
  54. package/dist/viewer/LatestRead.logic.d.ts +27 -0
  55. package/dist/viewer/LatestRead.logic.js +55 -0
  56. package/dist/viewer/MediaControls.d.ts +25 -0
  57. package/dist/viewer/MediaControls.js +183 -0
  58. package/dist/viewer/MediaPlayer.logic.d.ts +53 -0
  59. package/dist/viewer/MediaPlayer.logic.js +68 -0
  60. package/dist/viewer/PdfView.d.ts +23 -0
  61. package/dist/viewer/PdfView.js +25 -0
  62. package/dist/viewer/QuietFocus.d.ts +19 -0
  63. package/dist/viewer/QuietFocus.js +27 -0
  64. package/dist/viewer/ScaledScroll.logic.d.ts +30 -0
  65. package/dist/viewer/ScaledScroll.logic.js +28 -0
  66. package/dist/viewer/ScaledScrollbar.d.ts +19 -0
  67. package/dist/viewer/ScaledScrollbar.js +71 -0
  68. package/dist/viewer/SyntaxHighlighter.d.ts +29 -0
  69. package/dist/viewer/SyntaxHighlighter.js +144 -0
  70. package/dist/viewer/SyntaxLanguage.logic.d.ts +110 -0
  71. package/dist/viewer/SyntaxLanguage.logic.js +283 -0
  72. package/dist/viewer/SyntaxService.d.ts +75 -0
  73. package/dist/viewer/SyntaxService.js +49 -0
  74. package/dist/viewer/SyntaxSession.logic.d.ts +78 -0
  75. package/dist/viewer/SyntaxSession.logic.js +221 -0
  76. package/dist/viewer/SyntaxWorker.d.ts +6 -0
  77. package/dist/viewer/SyntaxWorker.js +8 -0
  78. package/dist/viewer/TextEncoding.logic.d.ts +35 -0
  79. package/dist/viewer/TextEncoding.logic.js +58 -0
  80. package/dist/viewer/TextRows.logic.d.ts +86 -0
  81. package/dist/viewer/TextRows.logic.js +241 -0
  82. package/dist/viewer/TextSelection.logic.d.ts +97 -0
  83. package/dist/viewer/TextSelection.logic.js +139 -0
  84. package/dist/viewer/TextView.d.ts +13 -0
  85. package/dist/viewer/TextView.js +448 -0
  86. package/dist/viewer/TextViewport.logic.d.ts +70 -0
  87. package/dist/viewer/TextViewport.logic.js +116 -0
  88. package/dist/viewer/VideoView.d.ts +18 -0
  89. package/dist/viewer/VideoView.js +53 -0
  90. package/dist/viewer/ViewerClipboard.d.ts +30 -0
  91. package/dist/viewer/ViewerClipboard.js +70 -0
  92. package/dist/viewer/ViewerFrame.logic.d.ts +61 -0
  93. package/dist/viewer/ViewerFrame.logic.js +130 -0
  94. package/dist/viewer/ViewerKeys.d.ts +16 -0
  95. package/dist/viewer/ViewerKeys.js +14 -0
  96. package/dist/viewer/ViewerScope.d.ts +12 -0
  97. package/dist/viewer/ViewerScope.js +22 -0
  98. package/dist/viewer/ViewerViews.d.ts +28 -0
  99. package/dist/viewer/ViewerViews.js +22 -0
  100. package/dist/viewer/ViewerWindow.d.ts +53 -0
  101. package/dist/viewer/ViewerWindow.js +219 -0
  102. package/dist/viewer/index.d.ts +12 -0
  103. package/dist/viewer/index.js +12 -0
  104. package/dist/viewer/types.d.ts +151 -0
  105. package/dist/viewer/types.js +26 -0
  106. package/dist/viewer/useElementHeight.d.ts +6 -0
  107. package/dist/viewer/useElementHeight.js +22 -0
  108. package/dist/viewer/useElementSize.d.ts +7 -0
  109. package/dist/viewer/useElementSize.js +24 -0
  110. package/dist/viewer/useEscapeToClear.d.ts +8 -0
  111. package/dist/viewer/useEscapeToClear.js +26 -0
  112. package/dist/viewer/useHexDocument.d.ts +41 -0
  113. package/dist/viewer/useHexDocument.js +144 -0
  114. package/dist/viewer/useMediaElement.d.ts +10 -0
  115. package/dist/viewer/useMediaElement.js +24 -0
  116. package/dist/viewer/useSyntaxHighlight.d.ts +24 -0
  117. package/dist/viewer/useSyntaxHighlight.js +133 -0
  118. package/dist/viewer/useTextDocument.d.ts +66 -0
  119. package/dist/viewer/useTextDocument.js +245 -0
  120. package/dist/viewer/viewer.css +645 -0
  121. package/package.json +7 -1
@@ -0,0 +1,448 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
3
+ import { tooltip } from '../tooltip/Tooltip.logic.js';
4
+ import { useLabels } from '../labels/UiokProvider.js';
5
+ import { encodingLabel } from './TextEncoding.logic.js';
6
+ import { endQuietFocus, focusQuietly } from './QuietFocus.js';
7
+ import ScaledScrollbar from './ScaledScrollbar.js';
8
+ import { highlightPlan } from './SyntaxLanguage.logic.js';
9
+ import { isCollapsed, rowParts, selectedSpan, wordAt } from './TextSelection.logic.js';
10
+ import { screenRows as clampScreenRows } from './TextViewport.logic.js';
11
+ import { useElementHeight } from './useElementHeight.js';
12
+ import { useEscapeToClear } from './useEscapeToClear.js';
13
+ import { useSyntaxHighlight } from './useSyntaxHighlight.js';
14
+ import { useTextDocument } from './useTextDocument.js';
15
+ import { deliverCopy, useCopyTickets } from './ViewerClipboard.js';
16
+ import { isViewerEvent } from './ViewerScope.js';
17
+ /** One row's height in pixels; the stylesheet's row height and line height match it. */
18
+ const ROW_PX = 18;
19
+ /** Rows before the layout is measured (or where it never is). */
20
+ const FALLBACK_ROWS = 40;
21
+ /** How often a drag held past the top or bottom edge scrolls one row. */
22
+ const DRAG_SCROLL_MS = 50;
23
+ /** No highlighting until the first line is read: a script's `#!` line can still name a language. */
24
+ const NOT_YET = { kind: 'none' };
25
+ /** A token style as the page draws it. */
26
+ const cssFor = ({ color, italic, bold, strikethrough }) => ({
27
+ color: color ?? undefined,
28
+ fontStyle: italic ? 'italic' : undefined,
29
+ fontWeight: bold ? 'bold' : undefined,
30
+ textDecoration: strikethrough ? 'line-through' : undefined
31
+ });
32
+ /** One row: the gutter, then the text in its token styles with the selected part marked. */
33
+ function Row({ row, gutter, selection, tokens, styles }) {
34
+ const span = selection ? selectedSpan(selection, row.start, row.text.length) : null;
35
+ return (_jsxs("div", { className: "text-row", "data-kind": row.kind, "data-row": row.start, children: [_jsx("span", { "aria-hidden": "true", className: "text-gutter", children: gutter }), _jsx("span", { className: "text-content", children: rowParts(row.text.length, tokens, span).map((part) => (_jsx("span", { className: part.selected ? 'text-selected' : undefined, "data-char": part.from, style: part.style === null ? undefined : styles[part.style], children: row.text.slice(part.from, part.to) }, part.from))) })] }));
36
+ }
37
+ /**
38
+ * The text view: the file's text a screen at a time, scrolled by byte
39
+ * position like `less`, with a scaled scrollbar whose thumb maps to a byte,
40
+ * line numbers once the background index reaches the screen (byte offsets
41
+ * until then), a wrap toggle, a selection kept in file terms so it survives
42
+ * scrolling and copies in one bounded read, and syntax highlighting for a
43
+ * file in a known language, tokenized a screen at a time in a worker. A
44
+ * highlighted row selects, copies, wraps, and splits exactly as a plain one.
45
+ */
46
+ function TextView({ reader, info }) {
47
+ const labels = useLabels().viewer;
48
+ const takeCopyTicket = useCopyTickets();
49
+ const [wrap, setWrap] = useState(false);
50
+ const [selection, setSelection] = useState(null);
51
+ const [message, setMessage] = useState(null);
52
+ const clearSelection = useCallback(() => setSelection(null), []);
53
+ useEscapeToClear(selection !== null, clearSelection);
54
+ const [rowsRef, rowsHeight] = useElementHeight();
55
+ // Rows drawn include one the bottom edge may cut; the end of the file and
56
+ // a page count only the rows that fit whole.
57
+ const screenRows = clampScreenRows(rowsHeight === null ? FALLBACK_ROWS : Math.ceil(rowsHeight / ROW_PX));
58
+ const fullRows = clampScreenRows(rowsHeight === null ? FALLBACK_ROWS : Math.floor(rowsHeight / ROW_PX));
59
+ const view = useMemo(() => ({ screenRows, fullRows, wrap }), [screenRows, fullRows, wrap]);
60
+ const textDocument = useTextDocument(reader, info, view);
61
+ const { layout, firstLine, screen, go, scroll: scrollRows } = textDocument;
62
+ const plan = useMemo(() => firstLine === null
63
+ ? NOT_YET
64
+ : highlightPlan(info.name, info.sizeBytes, firstLine, info.content.kind), [firstLine, info.name, info.sizeBytes, info.content.kind]);
65
+ const syntax = useSyntaxHighlight(plan, info.generation, layout, screen);
66
+ const tokenStyles = useMemo(() => syntax.styles.map(cssFor), [syntax.styles]);
67
+ const rowsElement = useRef(null);
68
+ const wheelRemainder = useRef(0);
69
+ const drag = useRef(null);
70
+ const shown = useRef([]);
71
+ const contentStart = layout?.contentStart ?? 0;
72
+ const top = screen?.top.start ?? contentStart;
73
+ const end = screen?.end ?? info.sizeBytes;
74
+ const page = Math.max(1, fullRows - 1);
75
+ // A wrapped row can be taller than the box. The box scrolls within the
76
+ // top row until its end shows before the view moves on a row. A new top
77
+ // row shows from its start, or from its end when a step up brought it in,
78
+ // so stepping either way passes every part of it. A move longer than a
79
+ // page lands only on the screen where it ends.
80
+ const landing = useRef('start');
81
+ // Wrapped pages pressed while one is still landing, signed by direction;
82
+ // each goes on from where the one before it lands.
83
+ const queuedPages = useRef(0);
84
+ // The screen a wrapped page left from. The page is still landing while
85
+ // that screen shows and no read has failed; a new screen that kept the
86
+ // same top row (the file shrank) ends it as surely as one that moved.
87
+ const pagedFrom = useRef(null);
88
+ const pageLanding = () => typeof landing.current === 'object' &&
89
+ pagedFrom.current === screen &&
90
+ textDocument.failure === null;
91
+ const settled = screen?.settled ?? true;
92
+ // The landing below runs only when the top row or settling changes, and
93
+ // reads the screen it lands on from here; this runs first.
94
+ const landingScreen = useRef(screen);
95
+ useLayoutEffect(() => {
96
+ landingScreen.current = screen;
97
+ });
98
+ useLayoutEffect(() => {
99
+ const box = rowsElement.current;
100
+ if (box === null)
101
+ return;
102
+ const rowElements = [...box.querySelectorAll('.text-row')];
103
+ const first = rowElements[0];
104
+ const land = landing.current;
105
+ if (!settled || land === 'start' || first === undefined) {
106
+ box.scrollTop = 0;
107
+ if (settled)
108
+ landing.current = 'start';
109
+ return;
110
+ }
111
+ landing.current = 'start';
112
+ if (land === 'end') {
113
+ box.scrollTop = Math.max(0, first.offsetHeight - box.clientHeight);
114
+ return;
115
+ }
116
+ // The scroll position must lie within the top row: past it, the row
117
+ // that holds it becomes the top row, landing on the same point.
118
+ const target = rowElements.find((element) => element.dataset['row'] === String(land.row));
119
+ const y = (target?.offsetTop ?? 0) + land.offsetPx - land.atY;
120
+ const holder = rowElements.find((element) => element.offsetTop + element.offsetHeight > y);
121
+ if (target === undefined || y <= 0 || holder === undefined) {
122
+ box.scrollTop = 0;
123
+ }
124
+ else if (holder === first) {
125
+ box.scrollTop = y;
126
+ }
127
+ else {
128
+ landing.current = {
129
+ row: Number(holder.dataset['row']),
130
+ offsetPx: y - holder.offsetTop,
131
+ atY: 0
132
+ };
133
+ pagedFrom.current = landingScreen.current;
134
+ go({ anchor: Number(holder.dataset['row']), rows: 0 });
135
+ }
136
+ }, [go, screen?.top.start, settled, wrap]);
137
+ /** Scrolls `deltaPx` within a wrapped top row taller than the box; false when there is none left that way. */
138
+ const scrollWithinRow = (deltaPx) => {
139
+ const box = rowsElement.current;
140
+ if (!wrap || box === null || deltaPx === 0)
141
+ return false;
142
+ if (deltaPx < 0) {
143
+ if (box.scrollTop <= 0)
144
+ return false;
145
+ box.scrollTop = Math.max(0, box.scrollTop + deltaPx);
146
+ return true;
147
+ }
148
+ const first = box.querySelector('.text-row');
149
+ const hidden = (first?.offsetHeight ?? 0) - box.clientHeight - box.scrollTop;
150
+ if (hidden <= 0)
151
+ return false;
152
+ box.scrollTop += Math.min(hidden, deltaPx);
153
+ return true;
154
+ };
155
+ /** Moves `rows` rows; a top row a move up brings in shows from its end. */
156
+ const moveRows = (rows) => {
157
+ queuedPages.current = 0;
158
+ landing.current = wrap && rows < 0 ? 'end' : 'start';
159
+ scrollRows(rows);
160
+ };
161
+ /** Moves a row, or a line within a tall wrapped top row first. */
162
+ const step = (rows) => {
163
+ if (!scrollWithinRow(rows * ROW_PX))
164
+ moveRows(rows);
165
+ };
166
+ /**
167
+ * Moves a screen less one line. Unwrapped, that is a page of rows; wrapped,
168
+ * a row can hold many lines, so the view moves by the lines in view
169
+ * instead: the line a screen less one below the top comes to the top, or
170
+ * the line at the top goes to the bottom. A wrapped page measures the
171
+ * screen it starts from, so one pressed while another is still landing
172
+ * waits for it.
173
+ */
174
+ const pageBy = (direction) => {
175
+ const box = rowsElement.current;
176
+ if (!wrap || box === null || screen === null) {
177
+ moveRows(direction * page);
178
+ return;
179
+ }
180
+ if (pageLanding()) {
181
+ queuedPages.current += direction;
182
+ return;
183
+ }
184
+ if (typeof landing.current === 'object') {
185
+ // The page that was landing never will (its read failed, or the
186
+ // screen came back without moving): this one starts afresh.
187
+ landing.current = 'start';
188
+ queuedPages.current = 0;
189
+ }
190
+ const distance = Math.max(ROW_PX, box.clientHeight - ROW_PX);
191
+ const rowElements = [...box.querySelectorAll('.text-row')];
192
+ if (direction > 0) {
193
+ const y = box.scrollTop + distance;
194
+ const holder = rowElements.find((element) => element.offsetTop + element.offsetHeight > y);
195
+ // Nothing below the screen: the end of the file is in view.
196
+ if (holder === undefined)
197
+ return;
198
+ if (holder === rowElements[0]) {
199
+ box.scrollTop = y;
200
+ return;
201
+ }
202
+ landing.current = {
203
+ row: Number(holder.dataset['row']),
204
+ offsetPx: y - holder.offsetTop,
205
+ atY: 0
206
+ };
207
+ pagedFrom.current = screen;
208
+ go({ anchor: Number(holder.dataset['row']), rows: 0 });
209
+ return;
210
+ }
211
+ if (box.scrollTop >= distance || screen.top.start <= contentStart) {
212
+ box.scrollTop = Math.max(0, box.scrollTop - distance);
213
+ return;
214
+ }
215
+ // Bring in a page of rows above, each a line at least, then put the
216
+ // line at the top now at the bottom.
217
+ landing.current = { row: screen.top.start, offsetPx: box.scrollTop, atY: distance };
218
+ pagedFrom.current = screen;
219
+ go({ anchor: screen.top.start, rows: -page });
220
+ };
221
+ // Once a wrapped page has landed, the pages pressed meanwhile follow it.
222
+ useLayoutEffect(() => {
223
+ while (queuedPages.current !== 0 && !pageLanding() && settled) {
224
+ const direction = queuedPages.current > 0 ? 1 : -1;
225
+ queuedPages.current -= direction;
226
+ pageBy(direction);
227
+ }
228
+ });
229
+ /** Goes to `target` and shows its top row from the start, even when that row is already on top. */
230
+ const jump = useCallback((target) => {
231
+ queuedPages.current = 0;
232
+ landing.current = 'start';
233
+ if (rowsElement.current)
234
+ rowsElement.current.scrollTop = 0;
235
+ go(target);
236
+ }, [go]);
237
+ // Pointer handlers outlive a render (a drag scrolls on a timer), so they
238
+ // read the rows on screen, the wrap mode, and the scroll step through refs.
239
+ const stepRef = useRef(step);
240
+ const wrapRef = useRef(wrap);
241
+ useLayoutEffect(() => {
242
+ shown.current = screen?.rows ?? [];
243
+ stepRef.current = step;
244
+ wrapRef.current = wrap;
245
+ });
246
+ const toStart = useCallback(() => jump({ anchor: contentStart, rows: 0 }), [contentStart, jump]);
247
+ const toEnd = useCallback(() => jump({ anchor: end, rows: -page }), [jump, end, page]);
248
+ // Copy answers the Edit menu, whose Copy item copies natively in the page.
249
+ useEffect(() => {
250
+ const onCopy = (event) => {
251
+ // A copy the page makes itself (a field beside the viewer, nothing
252
+ // selected here) is the newest: a copy of ours still reading must
253
+ // not land on the clipboard after it.
254
+ if (selection === null || isCollapsed(selection) || !isViewerEvent(event)) {
255
+ takeCopyTicket();
256
+ return;
257
+ }
258
+ event.preventDefault();
259
+ deliverCopy(textDocument.copy(selection), event.clipboardData, setMessage, labels, takeCopyTicket());
260
+ };
261
+ window.addEventListener('copy', onCopy);
262
+ return () => window.removeEventListener('copy', onCopy);
263
+ }, [textDocument, selection, labels, takeCopyTicket]);
264
+ const selectAll = useCallback(() => {
265
+ setSelection({
266
+ anchor: { row: contentStart, kind: 'line', char: 0 },
267
+ focus: { row: end, kind: 'line', char: 0 }
268
+ });
269
+ setMessage(null);
270
+ }, [contentStart, end]);
271
+ const onKeyDown = (event) => {
272
+ const command = event.metaKey && !event.ctrlKey && !event.altKey;
273
+ const plain = !event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey;
274
+ let handled = true;
275
+ if (command && event.key === 'a')
276
+ selectAll();
277
+ else if (command && event.key === 'ArrowUp')
278
+ toStart();
279
+ else if (command && event.key === 'ArrowDown')
280
+ toEnd();
281
+ else if (!plain)
282
+ handled = false;
283
+ else if (event.key === 'ArrowDown')
284
+ step(1);
285
+ else if (event.key === 'ArrowUp')
286
+ step(-1);
287
+ else if (event.key === 'PageDown')
288
+ pageBy(1);
289
+ else if (event.key === 'PageUp')
290
+ pageBy(-1);
291
+ else if (event.key === 'Home')
292
+ toStart();
293
+ else if (event.key === 'End')
294
+ toEnd();
295
+ else
296
+ handled = false;
297
+ if (handled)
298
+ event.preventDefault();
299
+ };
300
+ const onWheel = (event) => {
301
+ const unit = event.deltaMode === 1 ? ROW_PX : event.deltaMode === 2 ? page * ROW_PX : 1;
302
+ if (scrollWithinRow(event.deltaY * unit))
303
+ return;
304
+ wheelRemainder.current += event.deltaY * unit;
305
+ const rows = Math.trunc(wheelRemainder.current / ROW_PX);
306
+ wheelRemainder.current -= rows * ROW_PX;
307
+ if (rows !== 0)
308
+ moveRows(rows);
309
+ };
310
+ /**
311
+ * The text point under a pointer. A pointer outside the rows box points at
312
+ * the text along the nearest edge, so a drag never selects rows the box
313
+ * hides (a wrapped row runs past the bottom); past the left edge that is
314
+ * the start of the line under it (the start of the row, unless the row
315
+ * wraps), past the right its end.
316
+ */
317
+ const pointAt = useCallback((pointerX, pointerY) => {
318
+ const box = rowsElement.current;
319
+ const rows = shown.current;
320
+ const last = rows.at(-1);
321
+ if (box === null || last === undefined)
322
+ return null;
323
+ const bounds = box.getBoundingClientRect();
324
+ let clientX = Math.max(bounds.left, Math.min(pointerX, bounds.right - 1));
325
+ const clientY = Math.max(bounds.top, Math.min(pointerY, bounds.bottom - 1));
326
+ let caret = document.caretPositionFromPoint?.(clientX, clientY) ?? null;
327
+ let node = caret?.offsetNode ?? null;
328
+ let element = node instanceof Element ? node : (node?.parentElement ?? null);
329
+ const rowElement = element?.closest('.text-row') ??
330
+ document.elementFromPoint(clientX, clientY)?.closest('.text-row') ??
331
+ null;
332
+ const row = rows.find((candidate) => String(candidate.start) === rowElement?.dataset['row']);
333
+ if (row === undefined)
334
+ return { row: last.start, kind: last.kind, char: last.text.length };
335
+ // Left of a wrapped row's text, the point is the start of the line the
336
+ // pointer is level with, found on the text's left edge.
337
+ const textLeft = Math.max(rowElement?.querySelector('.text-content')?.getBoundingClientRect().left ?? clientX, rowElement?.querySelector('.text-gutter')?.getBoundingClientRect().right ?? clientX);
338
+ if (wrapRef.current && clientX < textLeft) {
339
+ clientX = Math.ceil(textLeft);
340
+ caret = document.caretPositionFromPoint?.(clientX, clientY) ?? null;
341
+ node = caret?.offsetNode ?? null;
342
+ element = node instanceof Element ? node : (node?.parentElement ?? null);
343
+ }
344
+ const part = element?.closest('[data-char]');
345
+ if (caret !== null &&
346
+ part !== null &&
347
+ part !== undefined &&
348
+ node?.nodeType === Node.TEXT_NODE) {
349
+ return { row: row.start, kind: row.kind, char: Number(part.dataset['char']) + caret.offset };
350
+ }
351
+ const content = rowElement?.querySelector('.text-content')?.getBoundingClientRect();
352
+ const before = content === undefined || clientX < content.left;
353
+ return { row: row.start, kind: row.kind, char: before ? 0 : row.text.length };
354
+ }, []);
355
+ // The window opens on its text, so the keys scroll it without a click first.
356
+ // That focus is the window's own, so it draws no ring (QuietFocus.ts).
357
+ useEffect(() => {
358
+ focusQuietly(rowsElement.current);
359
+ }, []);
360
+ const endDrag = useCallback(() => {
361
+ if (drag.current?.timer)
362
+ clearInterval(drag.current.timer);
363
+ drag.current = null;
364
+ }, []);
365
+ useEffect(() => endDrag, [endDrag]);
366
+ const onMouseDown = (event) => {
367
+ if (event.button !== 0)
368
+ return;
369
+ // The page's own selection would cover only the rows in the DOM, so the
370
+ // view keeps its own and the native one never starts.
371
+ event.preventDefault();
372
+ rowsElement.current?.focus();
373
+ const point = pointAt(event.clientX, event.clientY);
374
+ if (point === null)
375
+ return;
376
+ setMessage(null);
377
+ // A double click selects the word under the pointer, within its row.
378
+ if (event.detail === 2) {
379
+ const text = shown.current.find((row) => row.start === point.row)?.text ?? '';
380
+ const word = wordAt(text, point.char);
381
+ setSelection({
382
+ anchor: { ...point, char: word.from },
383
+ focus: { ...point, char: word.to }
384
+ });
385
+ return;
386
+ }
387
+ setSelection((current) => event.shiftKey && current !== null
388
+ ? { anchor: current.anchor, focus: point }
389
+ : { anchor: point, focus: point });
390
+ const box = rowsElement.current?.getBoundingClientRect();
391
+ const state = {
392
+ x: event.clientX,
393
+ y: event.clientY,
394
+ timer: null
395
+ };
396
+ drag.current = state;
397
+ const extend = (clientX, clientY) => {
398
+ const focus = pointAt(clientX, clientY);
399
+ if (focus)
400
+ setSelection((current) => (current ? { anchor: current.anchor, focus } : null));
401
+ };
402
+ const onMove = (move) => {
403
+ state.x = move.clientX;
404
+ state.y = move.clientY;
405
+ extend(move.clientX, move.clientY);
406
+ const outside = box !== undefined && (move.clientY < box.top || move.clientY >= box.bottom);
407
+ if (outside && state.timer === null) {
408
+ state.timer = setInterval(() => {
409
+ const above = box !== undefined && state.y < box.top;
410
+ stepRef.current(above ? -1 : 1);
411
+ extend(state.x, state.y);
412
+ }, DRAG_SCROLL_MS);
413
+ }
414
+ else if (!outside && state.timer !== null) {
415
+ clearInterval(state.timer);
416
+ state.timer = null;
417
+ }
418
+ };
419
+ const onUp = () => {
420
+ window.removeEventListener('mousemove', onMove);
421
+ window.removeEventListener('mouseup', onUp);
422
+ endDrag();
423
+ };
424
+ window.addEventListener('mousemove', onMove);
425
+ window.addEventListener('mouseup', onUp);
426
+ };
427
+ const rows = screen?.rows ?? [];
428
+ const visibleBytes = rows.length > 0 ? (rows.at(-1)?.end ?? top) - top : 0;
429
+ if (textDocument.failure !== null && screen === null) {
430
+ return (_jsx("p", { className: "viewer-failure", role: "alert", children: textDocument.failure }));
431
+ }
432
+ // Wide enough for the largest byte offset as the gutter prints it, separators included.
433
+ const gutterDigits = labels.number(Math.max(1, info.sizeBytes)).length;
434
+ const position = screen === null
435
+ ? ''
436
+ : screen.topLine !== null
437
+ ? labels.text.line(screen.topLine)
438
+ : labels.text.counting(top);
439
+ return (_jsxs("div", { className: "text-view", children: [_jsxs("div", { className: "text-view-body viewer-rows-frame", children: [_jsxs("div", { "aria-label": labels.text.rows(info.name), "aria-readonly": "true", onBlur: endQuietFocus, className: wrap ? 'text-rows text-rows-wrap' : 'text-rows scroll-fade-x', onKeyDown: onKeyDown, onMouseDown: onMouseDown, onWheel: onWheel, ref: (element) => {
440
+ rowsElement.current = element;
441
+ rowsRef(element);
442
+ }, role: "textbox", style: { ['--text-gutter-width']: `${gutterDigits + 1}ch` }, tabIndex: 0, children: [textDocument.empty ? _jsx("p", { className: "text-empty", children: labels.empty }) : null, rows.map((row) => (_jsx(Row, { styles: tokenStyles, tokens: syntax.tokensFor(row.start), gutter: screen?.topLine !== null
443
+ ? row.line === null
444
+ ? ''
445
+ : labels.number(row.line)
446
+ : labels.number(row.start), row: row, selection: selection }, row.start)))] }), _jsx(ScaledScrollbar, { onDrag: (position) => jump({ anchor: contentStart + position, rows: 0 }), onPage: pageBy, position: top - contentStart, total: Math.max(0, end - contentStart), visible: visibleBytes })] }), _jsxs("footer", { className: "viewer-status text-status", children: [_jsx("span", { className: "text-position", children: position }), _jsx("span", { "aria-live": "polite", className: "text-message", children: message ?? textDocument.failure ?? '' }), syntax.status ? (_jsx("span", { className: "text-syntax", ...tooltip(syntax.status), children: syntax.status })) : null, layout ? _jsx("span", { className: "text-encoding", children: encodingLabel(layout, labels) }) : null, _jsxs("label", { className: "text-wrap", children: [_jsx("input", { checked: wrap, onChange: (event) => setWrap(event.target.checked), type: "checkbox" }), labels.text.wrap] })] })] }));
447
+ }
448
+ export default TextView;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * What the text view shows and what it must read to show it. A view target
3
+ * is a byte and a number of rows to move from the row holding it; resolving
4
+ * a target against a chunk of the file gives the screen's rows. Every target
5
+ * needs one bounded read at most, sized from the screen, never from the file.
6
+ */
7
+ import type { TextLayout } from './TextEncoding.logic.js';
8
+ import { type RowBounds, type RowStart, type TextChunk } from './TextRows.logic.js';
9
+ /**
10
+ * The most rows one screen shows; a taller window shows this many. It keeps
11
+ * a screen, a page either way, and the look back for a row start inside one
12
+ * read of `VIEWER_MAX_READ_BYTES`.
13
+ */
14
+ export declare const MAX_SCREEN_ROWS = 120;
15
+ /** Where to look: the row holding byte `anchor`, moved `rows` rows down (or up when negative). */
16
+ export interface ViewTarget {
17
+ readonly anchor: number;
18
+ readonly rows: number;
19
+ }
20
+ /** A byte range to read for a target. */
21
+ export interface ReadWindow {
22
+ readonly offset: number;
23
+ readonly length: number;
24
+ }
25
+ /**
26
+ * The screen: its first row, the rows from there, and the rows of the
27
+ * target's move still to go. A move of more than a page goes a page per
28
+ * screen, so `rest` carries the remainder to the next screen; it is 0 once
29
+ * the move is done or stopped at the start or end of the file.
30
+ */
31
+ export interface Viewport {
32
+ readonly top: RowStart;
33
+ readonly rows: readonly RowBounds[];
34
+ readonly rest: number;
35
+ }
36
+ /** A screen's row count, at least one and at most `MAX_SCREEN_ROWS`. */
37
+ export declare const screenRows: (rows: number) => number;
38
+ /**
39
+ * The one read for a target the view does not hold: what it needs, widened
40
+ * evenly to a full bounded read so the rows around it scroll without
41
+ * another. A read never exceeds `VIEWER_MAX_READ_BYTES`, whatever the file.
42
+ */
43
+ export declare function readWindowFor(target: ViewTarget, screen: number): ReadWindow;
44
+ /** Whether `chunk` holds everything `target` needs, so it resolves without a read. */
45
+ export declare function covers(chunk: TextChunk | null, target: ViewTarget, screen: number): boolean;
46
+ /**
47
+ * The screen `target` lands on. The top row is the row holding the anchor,
48
+ * moved by the target's rows, at most a page; past the end it is pulled back
49
+ * until at least `minimumRows` rows show (the rows that fit whole when lines
50
+ * do not wrap, one row when they do, since a wrapped row can fill the screen
51
+ * alone). Null for a file with no text.
52
+ */
53
+ export declare function resolveViewport(chunk: TextChunk, layout: TextLayout, target: ViewTarget, screen: number, minimumRows: number): Viewport | null;
54
+ /**
55
+ * The line number for each row: the first row's line is `firstLine` (the
56
+ * line holding its first byte); each later row that starts a line counts one
57
+ * more. A row that continues a line gets null.
58
+ */
59
+ export declare function numberRows(rows: readonly RowStart[], firstLine: number): Array<number | null>;
60
+ /** A known line: the line holding byte `offset`. */
61
+ export interface LineAnchor {
62
+ readonly offset: number;
63
+ readonly line: number;
64
+ }
65
+ /**
66
+ * The line holding byte `offset`, carried from a known line by counting the
67
+ * line breaks between them in `chunk`; null when the chunk does not hold
68
+ * both.
69
+ */
70
+ export declare function lineFromAnchor(chunk: TextChunk, layout: TextLayout, anchor: LineAnchor, offset: number): number | null;
@@ -0,0 +1,116 @@
1
+ /**
2
+ * What the text view shows and what it must read to show it. A view target
3
+ * is a byte and a number of rows to move from the row holding it; resolving
4
+ * a target against a chunk of the file gives the screen's rows. Every target
5
+ * needs one bounded read at most, sized from the screen, never from the file.
6
+ */
7
+ import { VIEWER_MAX_READ_BYTES } from './types.js';
8
+ import { countLineBreaks, LOOKBEHIND_BYTES, MAX_ROW_BYTES, rowAt, rowContaining, rowsBefore, rowsFrom } from './TextRows.logic.js';
9
+ /**
10
+ * The most rows one screen shows; a taller window shows this many. It keeps
11
+ * a screen, a page either way, and the look back for a row start inside one
12
+ * read of `VIEWER_MAX_READ_BYTES`.
13
+ */
14
+ export const MAX_SCREEN_ROWS = 120;
15
+ /** A screen's row count, at least one and at most `MAX_SCREEN_ROWS`. */
16
+ export const screenRows = (rows) => Math.min(MAX_SCREEN_ROWS, Math.max(1, Math.floor(rows)));
17
+ /** A move clamped to a page, so any target fits one read. */
18
+ const clampMove = (rows, screen) => Math.max(-screen, Math.min(screen, Math.trunc(rows)));
19
+ /**
20
+ * The bytes `target` needs on a screen of `screen` rows: back far enough to
21
+ * find the row holding the anchor, move up a page, and pull the last screen
22
+ * up to the end of the file; forward far enough to move down a page and fill
23
+ * the screen. Always within one read of `VIEWER_MAX_READ_BYTES`.
24
+ */
25
+ function neededWindow(target, screen) {
26
+ const rows = screenRows(screen);
27
+ const move = clampMove(target.rows, rows);
28
+ const before = LOOKBEHIND_BYTES + (Math.max(0, -move) + rows + 1) * MAX_ROW_BYTES;
29
+ const after = (Math.max(0, move) + rows + 1) * MAX_ROW_BYTES;
30
+ const offset = Math.max(0, target.anchor - before);
31
+ return { offset, length: Math.min(VIEWER_MAX_READ_BYTES, target.anchor + after - offset) };
32
+ }
33
+ /**
34
+ * The one read for a target the view does not hold: what it needs, widened
35
+ * evenly to a full bounded read so the rows around it scroll without
36
+ * another. A read never exceeds `VIEWER_MAX_READ_BYTES`, whatever the file.
37
+ */
38
+ export function readWindowFor(target, screen) {
39
+ const needed = neededWindow(target, screen);
40
+ const offset = Math.max(0, needed.offset - (VIEWER_MAX_READ_BYTES - needed.length) / 2);
41
+ return { offset: Math.floor(offset), length: VIEWER_MAX_READ_BYTES };
42
+ }
43
+ /** Whether `chunk` holds everything `target` needs, so it resolves without a read. */
44
+ export function covers(chunk, target, screen) {
45
+ if (chunk === null)
46
+ return false;
47
+ const window = neededWindow(target, screen);
48
+ return (chunk.offset <= window.offset &&
49
+ (chunk.eof || chunk.offset + chunk.bytes.length >= window.offset + window.length));
50
+ }
51
+ /**
52
+ * The screen `target` lands on. The top row is the row holding the anchor,
53
+ * moved by the target's rows, at most a page; past the end it is pulled back
54
+ * until at least `minimumRows` rows show (the rows that fit whole when lines
55
+ * do not wrap, one row when they do, since a wrapped row can fill the screen
56
+ * alone). Null for a file with no text.
57
+ */
58
+ export function resolveViewport(chunk, layout, target, screen, minimumRows) {
59
+ const rows = screenRows(screen);
60
+ const move = clampMove(target.rows, rows);
61
+ let top = rowContaining(chunk, layout, target.anchor);
62
+ let moved = 0;
63
+ if (top === null)
64
+ return null;
65
+ if (move > 0) {
66
+ for (; moved < move; moved += 1) {
67
+ const next = rowAt(chunk, layout, top)?.next;
68
+ if (next === null || next === undefined)
69
+ break;
70
+ top = next;
71
+ }
72
+ }
73
+ else if (move < 0) {
74
+ const before = rowsBefore(chunk, layout, top, -move);
75
+ top = before[0] ?? top;
76
+ moved = -before.length;
77
+ }
78
+ let shown = rowsFrom(chunk, layout, top, rows);
79
+ const wanted = Math.min(rows, Math.max(1, minimumRows));
80
+ if (shown.end && shown.rows.length < wanted) {
81
+ const back = rowsBefore(chunk, layout, top, wanted - shown.rows.length);
82
+ if (back[0] !== undefined) {
83
+ top = back[0];
84
+ shown = rowsFrom(chunk, layout, top, rows);
85
+ }
86
+ }
87
+ // A move cut short by the start or end of the file is done.
88
+ return { top, rows: shown.rows, rest: moved === move ? Math.trunc(target.rows) - move : 0 };
89
+ }
90
+ /**
91
+ * The line number for each row: the first row's line is `firstLine` (the
92
+ * line holding its first byte); each later row that starts a line counts one
93
+ * more. A row that continues a line gets null.
94
+ */
95
+ export function numberRows(rows, firstLine) {
96
+ let line = firstLine;
97
+ return rows.map((row, index) => {
98
+ if (index > 0 && row.kind === 'line')
99
+ line += 1;
100
+ return row.kind === 'line' ? line : null;
101
+ });
102
+ }
103
+ /**
104
+ * The line holding byte `offset`, carried from a known line by counting the
105
+ * line breaks between them in `chunk`; null when the chunk does not hold
106
+ * both.
107
+ */
108
+ export function lineFromAnchor(chunk, layout, anchor, offset) {
109
+ const end = chunk.offset + chunk.bytes.length;
110
+ const holds = (position) => position >= chunk.offset && position <= end;
111
+ if (!holds(anchor.offset) || !holds(offset))
112
+ return null;
113
+ return offset >= anchor.offset
114
+ ? anchor.line + countLineBreaks(chunk, layout, anchor.offset, offset)
115
+ : anchor.line - countLineBreaks(chunk, layout, offset, anchor.offset);
116
+ }
@@ -0,0 +1,18 @@
1
+ import { type ReactElement } from 'react';
2
+ import type { ViewerViewProps } from './ViewerViews.js';
3
+ /**
4
+ * The video view: Chromium plays the file straight from its media URL
5
+ * (`info.mediaUrl`) in a `<video>` fitted to the window with its
6
+ * aspect kept, under the player's control bar (`MediaControls`), and nothing
7
+ * here decodes or demuxes a format. Only the metadata loads when the window
8
+ * opens, and nothing plays until the user asks; a seek reads from the new
9
+ * position through a range request, so a video of many gigabytes costs no
10
+ * more to open than a short one. The bar gives the picture's pixel size
11
+ * once the metadata is in, and F shows the player full screen. A file whose
12
+ * picture Chromium cannot show but whose sound it plays shows the audio
13
+ * view's mark and says so, instead of an empty frame. A file Chromium cannot
14
+ * play at all (a container or codecs it lacks, a decode failure) hands the
15
+ * window to hex with a note.
16
+ */
17
+ declare function VideoView({ info, onFallback }: ViewerViewProps): ReactElement;
18
+ export default VideoView;