@bigpixelrocket/uiok 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/CHANGELOG.md +50 -1
  2. package/dist/button/TextLink.d.ts +19 -3
  3. package/dist/button/TextLink.js +4 -3
  4. package/dist/dialog/ConfirmDialog.d.ts +7 -1
  5. package/dist/dialog/ConfirmDialog.js +2 -2
  6. package/dist/dialog/ModalDialog.js +8 -2
  7. package/dist/dialog/ProgressDialog.d.ts +7 -1
  8. package/dist/dialog/ProgressDialog.js +2 -2
  9. package/dist/dialog/TypedNameDialog.d.ts +8 -0
  10. package/dist/field/Check.d.ts +9 -2
  11. package/dist/field/Check.js +5 -4
  12. package/dist/field/Controls.d.ts +198 -0
  13. package/dist/field/Controls.js +94 -0
  14. package/dist/field/FieldLayout.d.ts +126 -0
  15. package/dist/field/FieldLayout.js +89 -0
  16. package/dist/field/index.d.ts +9 -6
  17. package/dist/field/index.js +9 -6
  18. package/dist/frame/Dock.js +1 -1
  19. package/dist/frame/SidebarResizer.js +1 -1
  20. package/dist/frame/Tree.d.ts +17 -4
  21. package/dist/frame/Tree.js +34 -9
  22. package/dist/frame/WindowFrame.js +2 -1
  23. package/dist/frame/frame.css +26 -4
  24. package/dist/icon/ProductMark.d.ts +57 -0
  25. package/dist/icon/ProductMark.js +26 -0
  26. package/dist/icon/icon.css +53 -0
  27. package/dist/icon/index.d.ts +3 -1
  28. package/dist/icon/index.js +3 -1
  29. package/dist/labels/UiokLabels.d.ts +3 -0
  30. package/dist/labels/UiokLabels.js +3 -1
  31. package/dist/labels/index.d.ts +1 -0
  32. package/dist/labels/index.js +1 -0
  33. package/dist/labels/settings.d.ts +6 -0
  34. package/dist/labels/settings.js +1 -0
  35. package/dist/labels/viewer.d.ts +193 -0
  36. package/dist/labels/viewer.js +124 -0
  37. package/dist/listing/Listing.d.ts +60 -4
  38. package/dist/listing/Listing.js +39 -10
  39. package/dist/listing/ListingScope.d.ts +7 -0
  40. package/dist/listing/ListingScope.js +8 -0
  41. package/dist/listing/PaneMessage.d.ts +28 -0
  42. package/dist/listing/PaneMessage.js +12 -0
  43. package/dist/listing/Timeline.d.ts +51 -0
  44. package/dist/listing/Timeline.js +28 -0
  45. package/dist/listing/index.d.ts +7 -3
  46. package/dist/listing/index.js +6 -2
  47. package/dist/listing/listing.css +185 -22
  48. package/dist/log/Ansi.logic.d.ts +27 -0
  49. package/dist/log/Ansi.logic.js +203 -0
  50. package/dist/log/LogWell.d.ts +11 -2
  51. package/dist/log/LogWell.js +15 -3
  52. package/dist/log/index.d.ts +1 -0
  53. package/dist/log/index.js +1 -0
  54. package/dist/log/log.css +80 -0
  55. package/dist/menu/Menu.d.ts +89 -0
  56. package/dist/menu/Menu.js +143 -0
  57. package/dist/menu/Menu.logic.d.ts +56 -0
  58. package/dist/menu/Menu.logic.js +64 -0
  59. package/dist/menu/index.d.ts +5 -1
  60. package/dist/menu/index.js +5 -1
  61. package/dist/menu/menu.css +51 -0
  62. package/dist/palette/Palette.d.ts +3 -2
  63. package/dist/palette/Palette.js +8 -4
  64. package/dist/palette/PaletteRows.d.ts +129 -0
  65. package/dist/palette/PaletteRows.js +74 -0
  66. package/dist/palette/index.d.ts +5 -2
  67. package/dist/palette/index.js +5 -2
  68. package/dist/pane/PaneFrame.d.ts +22 -1
  69. package/dist/pane/PaneFrame.js +42 -5
  70. package/dist/pane/index.d.ts +1 -1
  71. package/dist/shell/SettingsDialog.d.ts +20 -2
  72. package/dist/shell/SettingsDialog.js +13 -3
  73. package/dist/shell/index.d.ts +1 -1
  74. package/dist/styles.css +4 -0
  75. package/dist/surface/Card.d.ts +18 -0
  76. package/dist/surface/Card.js +10 -0
  77. package/dist/surface/FactTable.d.ts +44 -0
  78. package/dist/surface/FactTable.js +33 -0
  79. package/dist/surface/Notice.d.ts +45 -0
  80. package/dist/surface/Notice.js +27 -0
  81. package/dist/surface/index.d.ts +7 -3
  82. package/dist/surface/index.js +7 -3
  83. package/dist/surface/surface.css +64 -0
  84. package/dist/tabs/PageTabs.d.ts +6 -0
  85. package/dist/tabs/PageTabs.js +1 -1
  86. package/dist/tabs/tabs.css +14 -0
  87. package/dist/terminal/TerminalBox.d.ts +30 -0
  88. package/dist/terminal/TerminalBox.js +17 -0
  89. package/dist/terminal/index.d.ts +3 -1
  90. package/dist/terminal/index.js +3 -1
  91. package/dist/terminal/terminal.css +29 -0
  92. package/dist/themes/themes.generated.css +21 -0
  93. package/dist/themes/tokens.d.ts +1 -0
  94. package/dist/themes/tokens.js +16 -1
  95. package/dist/viewer/AudioView.d.ts +13 -0
  96. package/dist/viewer/AudioView.js +25 -0
  97. package/dist/viewer/HexJump.logic.d.ts +16 -0
  98. package/dist/viewer/HexJump.logic.js +27 -0
  99. package/dist/viewer/HexRows.logic.d.ts +55 -0
  100. package/dist/viewer/HexRows.logic.js +84 -0
  101. package/dist/viewer/HexSelection.logic.d.ts +57 -0
  102. package/dist/viewer/HexSelection.logic.js +68 -0
  103. package/dist/viewer/HexView.d.ts +10 -0
  104. package/dist/viewer/HexView.js +291 -0
  105. package/dist/viewer/HexViewport.logic.d.ts +61 -0
  106. package/dist/viewer/HexViewport.logic.js +79 -0
  107. package/dist/viewer/ImageView.d.ts +19 -0
  108. package/dist/viewer/ImageView.js +230 -0
  109. package/dist/viewer/ImageZoom.logic.d.ts +65 -0
  110. package/dist/viewer/ImageZoom.logic.js +119 -0
  111. package/dist/viewer/LatestRead.logic.d.ts +27 -0
  112. package/dist/viewer/LatestRead.logic.js +55 -0
  113. package/dist/viewer/MediaControls.d.ts +25 -0
  114. package/dist/viewer/MediaControls.js +183 -0
  115. package/dist/viewer/MediaPlayer.logic.d.ts +53 -0
  116. package/dist/viewer/MediaPlayer.logic.js +68 -0
  117. package/dist/viewer/PdfView.d.ts +23 -0
  118. package/dist/viewer/PdfView.js +25 -0
  119. package/dist/viewer/QuietFocus.d.ts +19 -0
  120. package/dist/viewer/QuietFocus.js +27 -0
  121. package/dist/viewer/ScaledScroll.logic.d.ts +30 -0
  122. package/dist/viewer/ScaledScroll.logic.js +28 -0
  123. package/dist/viewer/ScaledScrollbar.d.ts +19 -0
  124. package/dist/viewer/ScaledScrollbar.js +71 -0
  125. package/dist/viewer/SyntaxHighlighter.d.ts +29 -0
  126. package/dist/viewer/SyntaxHighlighter.js +144 -0
  127. package/dist/viewer/SyntaxLanguage.logic.d.ts +110 -0
  128. package/dist/viewer/SyntaxLanguage.logic.js +283 -0
  129. package/dist/viewer/SyntaxService.d.ts +75 -0
  130. package/dist/viewer/SyntaxService.js +49 -0
  131. package/dist/viewer/SyntaxSession.logic.d.ts +78 -0
  132. package/dist/viewer/SyntaxSession.logic.js +221 -0
  133. package/dist/viewer/SyntaxWorker.d.ts +6 -0
  134. package/dist/viewer/SyntaxWorker.js +8 -0
  135. package/dist/viewer/TextEncoding.logic.d.ts +35 -0
  136. package/dist/viewer/TextEncoding.logic.js +58 -0
  137. package/dist/viewer/TextRows.logic.d.ts +86 -0
  138. package/dist/viewer/TextRows.logic.js +241 -0
  139. package/dist/viewer/TextSelection.logic.d.ts +97 -0
  140. package/dist/viewer/TextSelection.logic.js +139 -0
  141. package/dist/viewer/TextView.d.ts +13 -0
  142. package/dist/viewer/TextView.js +448 -0
  143. package/dist/viewer/TextViewport.logic.d.ts +70 -0
  144. package/dist/viewer/TextViewport.logic.js +116 -0
  145. package/dist/viewer/VideoView.d.ts +18 -0
  146. package/dist/viewer/VideoView.js +53 -0
  147. package/dist/viewer/ViewerClipboard.d.ts +30 -0
  148. package/dist/viewer/ViewerClipboard.js +70 -0
  149. package/dist/viewer/ViewerFrame.logic.d.ts +61 -0
  150. package/dist/viewer/ViewerFrame.logic.js +130 -0
  151. package/dist/viewer/ViewerKeys.d.ts +16 -0
  152. package/dist/viewer/ViewerKeys.js +14 -0
  153. package/dist/viewer/ViewerScope.d.ts +12 -0
  154. package/dist/viewer/ViewerScope.js +22 -0
  155. package/dist/viewer/ViewerViews.d.ts +28 -0
  156. package/dist/viewer/ViewerViews.js +22 -0
  157. package/dist/viewer/ViewerWindow.d.ts +53 -0
  158. package/dist/viewer/ViewerWindow.js +219 -0
  159. package/dist/viewer/index.d.ts +12 -0
  160. package/dist/viewer/index.js +12 -0
  161. package/dist/viewer/types.d.ts +151 -0
  162. package/dist/viewer/types.js +26 -0
  163. package/dist/viewer/useElementHeight.d.ts +6 -0
  164. package/dist/viewer/useElementHeight.js +22 -0
  165. package/dist/viewer/useElementSize.d.ts +7 -0
  166. package/dist/viewer/useElementSize.js +24 -0
  167. package/dist/viewer/useEscapeToClear.d.ts +8 -0
  168. package/dist/viewer/useEscapeToClear.js +26 -0
  169. package/dist/viewer/useHexDocument.d.ts +41 -0
  170. package/dist/viewer/useHexDocument.js +144 -0
  171. package/dist/viewer/useMediaElement.d.ts +10 -0
  172. package/dist/viewer/useMediaElement.js +24 -0
  173. package/dist/viewer/useSyntaxHighlight.d.ts +24 -0
  174. package/dist/viewer/useSyntaxHighlight.js +133 -0
  175. package/dist/viewer/useTextDocument.d.ts +66 -0
  176. package/dist/viewer/useTextDocument.js +245 -0
  177. package/dist/viewer/viewer.css +645 -0
  178. package/package.json +7 -1
@@ -0,0 +1,241 @@
1
+ /**
2
+ * The text view's row model, the way `less` reads a file: from any byte, back
3
+ * up to the start of its row, then read forward one screenful. Nothing here
4
+ * needs the whole file, only a chunk of bytes around the place in view.
5
+ *
6
+ * A row starts at every line start. A line longer than a segment is split
7
+ * into display segments, so a gigabyte with no line break is still drawn one
8
+ * bounded row at a time. Where a line breaks is a pure function of the bytes
9
+ * near the break, never of where the line began: a line starting at `L`
10
+ * breaks at the first multiple of `SEGMENT_BYTES` at least one segment past
11
+ * `L`, then at every multiple after it, each moved forward to the next
12
+ * character boundary, and never right before the line's own line break. So a
13
+ * row is found the same way scrolling down to it, scrolling up to it, or
14
+ * jumping into the middle of a line whose start lies a gigabyte back, and
15
+ * the reader never has to look further back than `LOOKBEHIND_BYTES`.
16
+ *
17
+ * Rows carry byte offsets and their decoded text, so the plain view draws
18
+ * them and syntax highlighting can tokenize the same rows later.
19
+ */
20
+ /** The longest stretch of one line drawn as one row before it is split. */
21
+ export const SEGMENT_BYTES = 1024;
22
+ /**
23
+ * The longest a row can be: the first row of a long line runs up to the
24
+ * second segment boundary past its start, plus a character's realignment and
25
+ * a line break kept on its row.
26
+ */
27
+ export const MAX_ROW_BYTES = 2 * SEGMENT_BYTES + 8;
28
+ /** How far back from a byte the reader may look to find the start of its row. */
29
+ export const LOOKBEHIND_BYTES = 3 * SEGMENT_BYTES;
30
+ const chunkEnd = (chunk) => chunk.offset + chunk.bytes.length;
31
+ const byteAt = (chunk, position) => chunk.bytes[position - chunk.offset];
32
+ /** Bytes per code unit: a line break is one unit. */
33
+ const unitBytes = ({ encoding }) => encoding === 'utf-16le' || encoding === 'utf-16be' ? 2 : 1;
34
+ /** Whether the code unit at `position` is `code` (an ASCII control such as 0A or 0D). */
35
+ function unitIs(chunk, layout, position, code) {
36
+ switch (layout.encoding) {
37
+ case 'utf-16le':
38
+ return (position % 2 === 0 &&
39
+ byteAt(chunk, position) === code &&
40
+ byteAt(chunk, position + 1) === 0x00);
41
+ case 'utf-16be':
42
+ return (position % 2 === 0 &&
43
+ byteAt(chunk, position) === 0x00 &&
44
+ byteAt(chunk, position + 1) === code);
45
+ default:
46
+ return byteAt(chunk, position) === code;
47
+ }
48
+ }
49
+ /** The unit holding the 0A byte found at `found`, or -1 when it is no line break. */
50
+ function breakUnitAt(chunk, layout, found) {
51
+ const unit = layout.encoding === 'utf-16be' ? found - 1 : found;
52
+ return unitIs(chunk, layout, unit, 0x0a) ? unit : -1;
53
+ }
54
+ /** The first line break starting in [from, to), or -1. Scans only those bytes. */
55
+ function findBreak(chunk, layout, from, to) {
56
+ // A UTF-16BE break's 0A byte sits one past its unit's start.
57
+ const limit = Math.min(to + unitBytes(layout) - 1, chunkEnd(chunk));
58
+ let search = Math.max(from, chunk.offset);
59
+ while (search < limit) {
60
+ const index = chunk.bytes.subarray(search - chunk.offset, limit - chunk.offset).indexOf(0x0a);
61
+ if (index === -1)
62
+ return -1;
63
+ const found = search + index;
64
+ const unit = breakUnitAt(chunk, layout, found);
65
+ if (unit >= from && unit < to)
66
+ return unit;
67
+ search = found + 1;
68
+ }
69
+ return -1;
70
+ }
71
+ /** The last line break starting in [from, to), or -1. Scans only those bytes. */
72
+ function findBreakBefore(chunk, layout, from, to) {
73
+ const lowest = Math.max(from, chunk.offset);
74
+ let limit = Math.min(to + unitBytes(layout) - 1, chunkEnd(chunk));
75
+ while (limit > lowest) {
76
+ const index = chunk.bytes
77
+ .subarray(lowest - chunk.offset, limit - chunk.offset)
78
+ .lastIndexOf(0x0a);
79
+ if (index === -1)
80
+ return -1;
81
+ const found = lowest + index;
82
+ const unit = breakUnitAt(chunk, layout, found);
83
+ if (unit >= from && unit < to)
84
+ return unit;
85
+ limit = found;
86
+ }
87
+ return -1;
88
+ }
89
+ /**
90
+ * The first character boundary at or after `position`: past UTF-8
91
+ * continuation bytes, onto an even offset for UTF-16, and past the low half
92
+ * of a surrogate pair. Looks at no more than four bytes.
93
+ */
94
+ export function alignToCharacter(chunk, layout, position) {
95
+ switch (layout.encoding) {
96
+ case 'utf-8': {
97
+ let aligned = position;
98
+ while (aligned - position < 3 && ((byteAt(chunk, aligned) ?? 0) & 0xc0) === 0x80) {
99
+ aligned += 1;
100
+ }
101
+ return aligned;
102
+ }
103
+ case 'utf-16le':
104
+ case 'utf-16be': {
105
+ const even = position + (position % 2);
106
+ const high = byteAt(chunk, layout.encoding === 'utf-16le' ? even + 1 : even) ?? 0;
107
+ return high >= 0xdc && high <= 0xdf ? even + 2 : even;
108
+ }
109
+ case 'latin-1':
110
+ return position;
111
+ }
112
+ }
113
+ /** The segment boundary a row's split is measured from: the next one at least a segment past it. */
114
+ function nextSegmentBoundary({ start, kind }) {
115
+ const base = kind === 'line' ? start : start - (start % SEGMENT_BYTES);
116
+ return Math.ceil((base + SEGMENT_BYTES) / SEGMENT_BYTES) * SEGMENT_BYTES;
117
+ }
118
+ /**
119
+ * The row starting at `row`, or null when the chunk ends before its end can
120
+ * be known. `row` must be a row start: a line start or a segment break.
121
+ */
122
+ export function rowAt(chunk, layout, row) {
123
+ const unit = unitBytes(layout);
124
+ const end = chunkEnd(chunk);
125
+ const split = alignToCharacter(chunk, layout, nextSegmentBoundary(row));
126
+ // A break up to one unit past the split still ends this row, so a line
127
+ // break (with its carriage return) is never split from its line.
128
+ const lineBreak = findBreak(chunk, layout, row.start, split + 2 * unit);
129
+ if (lineBreak === -1 && !chunk.eof && split + 2 * unit + 4 > end) {
130
+ // The split's character and any break right after it lie past the chunk.
131
+ return null;
132
+ }
133
+ if (lineBreak !== -1) {
134
+ const carriageReturn = lineBreak - unit >= row.start && unitIs(chunk, layout, lineBreak - unit, 0x0d);
135
+ const textEnd = carriageReturn ? lineBreak - unit : lineBreak;
136
+ if (textEnd <= split) {
137
+ const nextStart = lineBreak + unit;
138
+ return {
139
+ ...row,
140
+ end: nextStart,
141
+ textEnd,
142
+ next: chunk.eof && nextStart >= end ? null : { start: nextStart, kind: 'line' }
143
+ };
144
+ }
145
+ }
146
+ if (chunk.eof && split >= end) {
147
+ return { ...row, end, textEnd: end, next: null };
148
+ }
149
+ return { ...row, end: split, textEnd: split, next: { start: split, kind: 'continuation' } };
150
+ }
151
+ /**
152
+ * The row holding byte `position`: back up to the last line start within
153
+ * `LOOKBEHIND_BYTES`, or, inside a longer line, to a segment break that
154
+ * line is sure to have, then walk forward to the row. Null for an empty file.
155
+ * A position past the end of the file finds the last row.
156
+ */
157
+ export function rowContaining(chunk, layout, position) {
158
+ const end = chunkEnd(chunk);
159
+ let target = Math.max(position, layout.contentStart);
160
+ if (chunk.eof && target >= end) {
161
+ if (end <= layout.contentStart)
162
+ return null;
163
+ target = end - 1;
164
+ }
165
+ const from = Math.max(layout.contentStart, target - LOOKBEHIND_BYTES, chunk.offset);
166
+ const lastBreak = findBreakBefore(chunk, layout, from, target - unitBytes(layout) + 1);
167
+ let row;
168
+ if (lastBreak !== -1) {
169
+ row = { start: lastBreak + unitBytes(layout), kind: 'line' };
170
+ }
171
+ else if (from === layout.contentStart) {
172
+ row = { start: layout.contentStart, kind: 'line' };
173
+ }
174
+ else {
175
+ // The line began before `from`, so it breaks at every segment boundary
176
+ // a segment or more past `from` until it ends, which is after `target`.
177
+ const boundary = Math.ceil((from + SEGMENT_BYTES) / SEGMENT_BYTES) * SEGMENT_BYTES;
178
+ row = { start: alignToCharacter(chunk, layout, boundary), kind: 'continuation' };
179
+ }
180
+ for (;;) {
181
+ const bounds = rowAt(chunk, layout, row);
182
+ if (bounds === null || bounds.next === null || bounds.next.start > target)
183
+ return row;
184
+ row = bounds.next;
185
+ }
186
+ }
187
+ /**
188
+ * The start of the line holding byte `position`, looking back no further
189
+ * than `maxBytes`; null when no line starts that close in the chunk (the
190
+ * line is longer, or the chunk does not reach back far enough).
191
+ */
192
+ export function lineStartBefore(chunk, layout, position, maxBytes) {
193
+ const from = Math.max(layout.contentStart, position - maxBytes);
194
+ const lastBreak = findBreakBefore(chunk, layout, Math.max(from, chunk.offset), position - unitBytes(layout) + 1);
195
+ if (lastBreak !== -1)
196
+ return lastBreak + unitBytes(layout);
197
+ return from === layout.contentStart && from >= chunk.offset ? from : null;
198
+ }
199
+ /**
200
+ * Up to `count` rows from `row` onward, and whether they reach the end of
201
+ * the file. Stops early, with `end` false, where the chunk runs out.
202
+ */
203
+ export function rowsFrom(chunk, layout, row, count) {
204
+ const rows = [];
205
+ let next = row;
206
+ while (next !== null && rows.length < count) {
207
+ if (chunk.eof && next.start >= chunkEnd(chunk))
208
+ return { rows, end: true };
209
+ const bounds = rowAt(chunk, layout, next);
210
+ if (bounds === null)
211
+ return { rows, end: false };
212
+ rows.push(bounds);
213
+ next = bounds.next;
214
+ }
215
+ return { rows, end: next === null };
216
+ }
217
+ /** Up to `count` row starts before `row`, nearest last; fewer at the start of the file. */
218
+ export function rowsBefore(chunk, layout, row, count) {
219
+ if (count <= 0 || row.start <= layout.contentStart)
220
+ return [];
221
+ const from = rowContaining(chunk, layout, row.start - count * MAX_ROW_BYTES - 1);
222
+ const starts = [];
223
+ let next = from;
224
+ while (next !== null && next.start < row.start) {
225
+ starts.push(next);
226
+ next = rowAt(chunk, layout, next)?.next ?? null;
227
+ }
228
+ return starts.slice(-count);
229
+ }
230
+ /** Line breaks starting in [from, to), as far as the chunk holds them. */
231
+ export function countLineBreaks(chunk, layout, from, to) {
232
+ let count = 0;
233
+ let search = from;
234
+ for (;;) {
235
+ const found = findBreak(chunk, layout, search, to);
236
+ if (found === -1)
237
+ return count;
238
+ count += 1;
239
+ search = found + unitBytes(layout);
240
+ }
241
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * The text view's selection. The screen holds only the rows in view, so the
3
+ * selection is kept in file terms (a row's starting byte and a character in
4
+ * its text) and survives scrolling the rows it spans out of the page. Copy
5
+ * reads the selected rows in one bounded read and refuses a selection
6
+ * larger than that, with a message, instead of reading the file whole.
7
+ */
8
+ import type { ViewerLabels } from '../labels/viewer.js';
9
+ import { type RowKind } from './TextRows.logic.js';
10
+ /** A place in the text: character `char` of the row starting at byte `row`. */
11
+ export interface TextPoint {
12
+ readonly row: number;
13
+ readonly kind: RowKind;
14
+ readonly char: number;
15
+ }
16
+ /** Where a selection started and where it was taken to; either may come first in the file. */
17
+ export interface TextSelection {
18
+ readonly anchor: TextPoint;
19
+ readonly focus: TextPoint;
20
+ }
21
+ /** The largest selection copy reads, in bytes of the file. */
22
+ export declare const MAX_COPY_BYTES: number;
23
+ /** Copy for a selection larger than copy will read. */
24
+ export declare function selectionTooLarge(labels: ViewerLabels): string;
25
+ /** Orders two points by their place in the file. */
26
+ export declare function comparePoints(a: TextPoint, b: TextPoint): number;
27
+ /** The selection's start and end in file order. */
28
+ export declare function orderedSelection({ anchor, focus }: TextSelection): readonly [TextPoint, TextPoint];
29
+ /** Whether a selection selects nothing. */
30
+ export declare const isCollapsed: (selection: TextSelection) => boolean;
31
+ /**
32
+ * The part of a row a selection covers, as character offsets into the row's
33
+ * text of `length` characters, or null when it covers none of it.
34
+ */
35
+ export declare function selectedSpan(selection: TextSelection, row: number, length: number): {
36
+ readonly from: number;
37
+ readonly to: number;
38
+ } | null;
39
+ /**
40
+ * The bytes copy reads for a selection: from its first row's start to the
41
+ * end of its last row, which is at most a row past where that row starts.
42
+ * Null when that is more than copy will read.
43
+ */
44
+ export declare function copyRange(selection: TextSelection, fileEnd: number): {
45
+ readonly offset: number;
46
+ readonly length: number;
47
+ } | null;
48
+ /**
49
+ * A row as copy sees it: its bytes [start, end), whether it begins a line,
50
+ * its text, and the line break that ends it as the file spells it (empty for
51
+ * a row a segment break ends, or the file's last row without one).
52
+ */
53
+ export interface CopyRow {
54
+ readonly start: number;
55
+ readonly end: number;
56
+ readonly kind: RowKind;
57
+ readonly text: string;
58
+ readonly lineBreak: string;
59
+ }
60
+ /**
61
+ * The selected text from the rows it spans, in order from the selection's
62
+ * first row: rows of one line join directly, and a row's line break, CRLF
63
+ * or LF as the file has it, is copied when the selection runs past the row,
64
+ * including to the end of the file.
65
+ */
66
+ export declare function selectedText(selection: TextSelection, rows: readonly CopyRow[]): string;
67
+ /**
68
+ * The word a double click at character `char` of `text` selects, as
69
+ * character offsets: a run of word characters, a run of spaces, or the one
70
+ * other character there, both halves of it for a character past U+FFFF (an
71
+ * emoji). A click past the last character takes the last.
72
+ */
73
+ export declare function wordAt(text: string, char: number): {
74
+ readonly from: number;
75
+ readonly to: number;
76
+ };
77
+ /** A stretch of a row drawn as one span: characters [from, to), whether selected, and its token style. */
78
+ export interface RowPart {
79
+ readonly from: number;
80
+ readonly to: number;
81
+ readonly selected: boolean;
82
+ /** An index into the highlighting's styles, or null for the text's own style. */
83
+ readonly style: number | null;
84
+ }
85
+ /**
86
+ * A row of `length` characters cut where the selected `span` and the
87
+ * highlighting `tokens` (sorted, not overlapping) begin and end, so each
88
+ * part draws in one style, selected or not.
89
+ */
90
+ export declare function rowParts(length: number, tokens: readonly {
91
+ readonly start: number;
92
+ readonly end: number;
93
+ readonly style: number;
94
+ }[] | null, span: {
95
+ readonly from: number;
96
+ readonly to: number;
97
+ } | null): RowPart[];
@@ -0,0 +1,139 @@
1
+ /**
2
+ * The text view's selection. The screen holds only the rows in view, so the
3
+ * selection is kept in file terms (a row's starting byte and a character in
4
+ * its text) and survives scrolling the rows it spans out of the page. Copy
5
+ * reads the selected rows in one bounded read and refuses a selection
6
+ * larger than that, with a message, instead of reading the file whole.
7
+ */
8
+ import { VIEWER_MAX_READ_BYTES } from './types.js';
9
+ import { MAX_ROW_BYTES } from './TextRows.logic.js';
10
+ /** The largest selection copy reads, in bytes of the file. */
11
+ export const MAX_COPY_BYTES = VIEWER_MAX_READ_BYTES;
12
+ /** Copy for a selection larger than copy will read. */
13
+ export function selectionTooLarge(labels) {
14
+ return labels.text.copyTooLarge(labels.size(MAX_COPY_BYTES));
15
+ }
16
+ /** Orders two points by their place in the file. */
17
+ export function comparePoints(a, b) {
18
+ return a.row !== b.row ? a.row - b.row : a.char - b.char;
19
+ }
20
+ /** The selection's start and end in file order. */
21
+ export function orderedSelection({ anchor, focus }) {
22
+ return comparePoints(anchor, focus) <= 0 ? [anchor, focus] : [focus, anchor];
23
+ }
24
+ /** Whether a selection selects nothing. */
25
+ export const isCollapsed = (selection) => comparePoints(selection.anchor, selection.focus) === 0;
26
+ /**
27
+ * The part of a row a selection covers, as character offsets into the row's
28
+ * text of `length` characters, or null when it covers none of it.
29
+ */
30
+ export function selectedSpan(selection, row, length) {
31
+ const [start, end] = orderedSelection(selection);
32
+ if (row < start.row || row > end.row)
33
+ return null;
34
+ const from = row === start.row ? Math.min(start.char, length) : 0;
35
+ const to = row === end.row ? Math.min(end.char, length) : length;
36
+ return to > from ? { from, to } : null;
37
+ }
38
+ /**
39
+ * The bytes copy reads for a selection: from its first row's start to the
40
+ * end of its last row, which is at most a row past where that row starts.
41
+ * Null when that is more than copy will read.
42
+ */
43
+ export function copyRange(selection, fileEnd) {
44
+ const [start, end] = orderedSelection(selection);
45
+ const stop = Math.min(fileEnd, end.row + MAX_ROW_BYTES);
46
+ const length = Math.max(0, stop - start.row);
47
+ return length <= MAX_COPY_BYTES ? { offset: start.row, length } : null;
48
+ }
49
+ /**
50
+ * The selected text from the rows it spans, in order from the selection's
51
+ * first row: rows of one line join directly, and a row's line break, CRLF
52
+ * or LF as the file has it, is copied when the selection runs past the row,
53
+ * including to the end of the file.
54
+ */
55
+ export function selectedText(selection, rows) {
56
+ const [start, end] = orderedSelection(selection);
57
+ let text = '';
58
+ for (const row of rows) {
59
+ if (row.start < start.row)
60
+ continue;
61
+ if (row.start > end.row)
62
+ break;
63
+ const from = row.start === start.row ? start.char : 0;
64
+ const to = row.start === end.row ? end.char : row.text.length;
65
+ text += row.text.slice(from, Math.max(from, to));
66
+ if (end.row >= row.end)
67
+ text += row.lineBreak;
68
+ }
69
+ return text;
70
+ }
71
+ /** Characters a word is made of: letters, marks, digits, and the underscore, as a code editor counts them. */
72
+ const WORD_CHARACTER = /[\p{L}\p{M}\p{N}_]/u;
73
+ const SPACE_CHARACTER = /[^\S\n]/u;
74
+ /**
75
+ * The word a double click at character `char` of `text` selects, as
76
+ * character offsets: a run of word characters, a run of spaces, or the one
77
+ * other character there, both halves of it for a character past U+FFFF (an
78
+ * emoji). A click past the last character takes the last.
79
+ */
80
+ export function wordAt(text, char) {
81
+ if (text.length === 0)
82
+ return { from: 0, to: 0 };
83
+ // A caret right after a word (a click on its last letter's right half)
84
+ // means that word, not the character after it.
85
+ let at = Math.max(0, Math.min(char, text.length - 1));
86
+ if (!WORD_CHARACTER.test(text[at] ?? '') && WORD_CHARACTER.test(text[at - 1] ?? ''))
87
+ at -= 1;
88
+ const character = text[at] ?? '';
89
+ const kind = WORD_CHARACTER.test(character)
90
+ ? WORD_CHARACTER
91
+ : SPACE_CHARACTER.test(character)
92
+ ? SPACE_CHARACTER
93
+ : null;
94
+ if (kind === null) {
95
+ const low = character >= '\udc00' && character <= '\udfff';
96
+ const from = low && (text.codePointAt(at - 1) ?? 0) > 0xffff ? at - 1 : at;
97
+ return { from, to: from + ((text.codePointAt(from) ?? 0) > 0xffff ? 2 : 1) };
98
+ }
99
+ let from = at;
100
+ let to = at + 1;
101
+ while (from > 0 && kind.test(text[from - 1] ?? ''))
102
+ from -= 1;
103
+ while (to < text.length && kind.test(text[to] ?? ''))
104
+ to += 1;
105
+ return { from, to };
106
+ }
107
+ /**
108
+ * A row of `length` characters cut where the selected `span` and the
109
+ * highlighting `tokens` (sorted, not overlapping) begin and end, so each
110
+ * part draws in one style, selected or not.
111
+ */
112
+ export function rowParts(length, tokens, span) {
113
+ const cuts = new Set([0, length]);
114
+ if (span !== null) {
115
+ cuts.add(span.from);
116
+ cuts.add(span.to);
117
+ }
118
+ for (const token of tokens ?? []) {
119
+ cuts.add(Math.min(token.start, length));
120
+ cuts.add(Math.min(token.end, length));
121
+ }
122
+ const points = [...cuts].sort((a, b) => a - b);
123
+ const parts = [];
124
+ let next = 0;
125
+ for (let index = 0; index + 1 < points.length; index += 1) {
126
+ const from = points[index] ?? 0;
127
+ const to = points[index + 1] ?? 0;
128
+ while (tokens && next < tokens.length && (tokens[next]?.end ?? 0) <= from)
129
+ next += 1;
130
+ const token = tokens?.[next];
131
+ parts.push({
132
+ from,
133
+ to,
134
+ selected: span !== null && from >= span.from && to <= span.to,
135
+ style: token !== undefined && token.start <= from ? token.style : null
136
+ });
137
+ }
138
+ return parts;
139
+ }
@@ -0,0 +1,13 @@
1
+ import { type ReactElement } from 'react';
2
+ import type { ViewerViewProps } from './ViewerViews.js';
3
+ /**
4
+ * The text view: the file's text a screen at a time, scrolled by byte
5
+ * position like `less`, with a scaled scrollbar whose thumb maps to a byte,
6
+ * line numbers once the background index reaches the screen (byte offsets
7
+ * until then), a wrap toggle, a selection kept in file terms so it survives
8
+ * scrolling and copies in one bounded read, and syntax highlighting for a
9
+ * file in a known language, tokenized a screen at a time in a worker. A
10
+ * highlighted row selects, copies, wraps, and splits exactly as a plain one.
11
+ */
12
+ declare function TextView({ reader, info }: Pick<ViewerViewProps, 'reader' | 'info'>): ReactElement;
13
+ export default TextView;