@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,230 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useCallback, useEffect, useRef, useState } from 'react';
3
+ import { Button } from '../button/Button.js';
4
+ import { tooltip } from '../tooltip/Tooltip.logic.js';
5
+ import { useLabels } from '../labels/UiokProvider.js';
6
+ import { canPan, fitLayout, formatScale, panBy, placeImage, stepScale, wheelZoomFactor, zoomAbout } from './ImageZoom.logic.js';
7
+ import { useElementSize } from './useElementSize.js';
8
+ import { isViewerEvent } from './ViewerScope.js';
9
+ /** How far an arrow key moves a zoomed image. */
10
+ const ARROW_PAN_PX = 48;
11
+ /** A wheel delta in lines (rare on macOS) as pixels. */
12
+ const LINE_PX = 16;
13
+ const FIT = { mode: 'fit' };
14
+ const ARROWS = {
15
+ ArrowUp: [0, ARROW_PAN_PX],
16
+ ArrowDown: [0, -ARROW_PAN_PX],
17
+ ArrowLeft: [ARROW_PAN_PX, 0],
18
+ ArrowRight: [-ARROW_PAN_PX, 0]
19
+ };
20
+ /** Whether `options` turn the decoded `image` into a bitmap; the bitmap is released at once. */
21
+ const rasterizes = (image, options) => createImageBitmap(image, options).then((bitmap) => {
22
+ bitmap.close();
23
+ return true;
24
+ }, () => false);
25
+ /**
26
+ * Whether Chromium gives a decoded SVG natural dimensions, asked of Chromium
27
+ * rather than read from the markup. A size-less SVG (a viewBox alone, or
28
+ * percentages) still reports a natural size, the default object size of 300
29
+ * px wide at its aspect ratio, so the numbers cannot tell. Chromium refuses to
30
+ * rasterize one without both resize dimensions, though: a 1 px wide bitmap is
31
+ * refused and a 1 × 1 one is not. An SVG too large to rasterize at all
32
+ * refuses both, and keeps the size it reported. Each bitmap is one pixel
33
+ * wide, so asking costs a paint of the SVG, never the memory of a full
34
+ * raster.
35
+ */
36
+ async function hasNaturalSize(image) {
37
+ if (await rasterizes(image, { resizeWidth: 1 }))
38
+ return true;
39
+ return !(await rasterizes(image, { resizeWidth: 1, resizeHeight: 1 }));
40
+ }
41
+ /**
42
+ * The image view: Chromium decodes the file straight from its media URL
43
+ * (`info.mediaUrl`) into an `<img>`, so animated GIF, APNG, and
44
+ * WebP play, and nothing here parses the format. An SVG shows the same way,
45
+ * and as an image Chromium runs none of its scripts and loads nothing it
46
+ * references. It opens fitted to the window, over a checkerboard that shows
47
+ * through transparency. ⌘+ and ⌘- step the zoom about the centre, ⌘0 shows
48
+ * actual size, ⌘9 fits again; Command with the wheel, or a pinch, zooms
49
+ * about the pointer; a drag, the wheel, or the arrows pan an image larger
50
+ * than the window. The status bar
51
+ * gives the pixel dimensions, read from the decoded image, and the zoom.
52
+ * An SVG with no size of its own says so instead, and fits by filling the
53
+ * window, since it has no actual size to keep. A file Chromium cannot decode
54
+ * hands the window to its fallback view with a note: hex, or an SVG's source.
55
+ */
56
+ function ImageView({ info, onFallback }) {
57
+ const labels = useLabels().viewer;
58
+ const svg = info.content.kind === 'svg';
59
+ // Why the view gives way: Chromium could not decode the bytes as the image
60
+ // they claim to be (hex), or could not render an SVG (malformed XML, or not
61
+ // SVG at all), which shows its source.
62
+ const fallbackNote = svg ? labels.image.fallbackSvg : labels.image.fallbackDecode;
63
+ const [canvasRef, viewport] = useElementSize();
64
+ const canvas = useRef(null);
65
+ const setCanvas = useCallback((element) => {
66
+ canvas.current = element;
67
+ canvasRef(element);
68
+ }, [canvasRef]);
69
+ const [natural, setNatural] = useState(null);
70
+ // An SVG with no natural dimensions: `natural` is Chromium's default object size.
71
+ const [sizeless, setSizeless] = useState(false);
72
+ const maxFitScale = sizeless ? Infinity : 1;
73
+ const [zoom, setZoom] = useState(FIT);
74
+ const [dragging, setDragging] = useState(false);
75
+ const dragFrom = useRef(null);
76
+ // Counts loads and the unmount, so an SVG size check that settles after
77
+ // the view is gone (a switch to hex or source, a reload's new generation)
78
+ // or after a newer load changes nothing, and least of all falls back.
79
+ const loadCount = useRef(0);
80
+ useEffect(() => () => {
81
+ loadCount.current += 1;
82
+ }, []);
83
+ const layout = natural && viewport
84
+ ? zoom.mode === 'fit'
85
+ ? fitLayout(natural, viewport, maxFitScale)
86
+ : placeImage(zoom.layout, natural, viewport)
87
+ : null;
88
+ // The latest geometry, for listeners that outlive a render.
89
+ const geometry = useRef({ natural, viewport, sizeless, maxFitScale });
90
+ geometry.current = { natural, viewport, sizeless, maxFitScale };
91
+ /** Applies `change` to the layout as shown now; a no-op until the image and the window are measured. */
92
+ const relayout = useCallback((change) => {
93
+ setZoom((previous) => {
94
+ const { natural: image, viewport: room, maxFitScale: maxFit } = geometry.current;
95
+ if (!image || !room)
96
+ return previous;
97
+ const current = previous.mode === 'fit'
98
+ ? fitLayout(image, room, maxFit)
99
+ : placeImage(previous.layout, image, room);
100
+ const next = change(current, image, room);
101
+ return next === null ? previous : { mode: 'free', layout: next };
102
+ });
103
+ }, []);
104
+ const zoomTo = useCallback((scale, anchor) => {
105
+ relayout((current, image, room) => zoomAbout(current, scale(current.scale), anchor ?? { x: room.width / 2, y: room.height / 2 }, image, room));
106
+ }, [relayout]);
107
+ const pan = useCallback((dx, dy) => {
108
+ relayout((current, image, room) => canPan(current, image, room) ? panBy(current, dx, dy, image, room) : null);
109
+ }, [relayout]);
110
+ const fit = useCallback(() => setZoom(FIT), []);
111
+ // A size-less SVG has no actual size: its natural size is only Chromium's default.
112
+ const actualSize = useCallback(() => {
113
+ if (!geometry.current.sizeless)
114
+ zoomTo(() => 1);
115
+ }, [zoomTo]);
116
+ // The zoom chords and arrows answer anywhere in the viewer, or with nothing
117
+ // focused; a control of the page beside it keeps its own keys.
118
+ useEffect(() => {
119
+ const onKeyDown = (event) => {
120
+ if (!isViewerEvent(event))
121
+ return;
122
+ const command = event.metaKey && !event.ctrlKey && !event.altKey;
123
+ const plain = !event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey;
124
+ const arrow = ARROWS[event.key];
125
+ if (command && (event.code === 'Equal' || event.key === '+'))
126
+ zoomTo((s) => stepScale(s, 1));
127
+ else if (command && event.code === 'Minus')
128
+ zoomTo((s) => stepScale(s, -1));
129
+ else if (command && event.code === 'Digit0')
130
+ actualSize();
131
+ else if (command && event.code === 'Digit9')
132
+ fit();
133
+ else if (plain && arrow)
134
+ pan(arrow[0], arrow[1]);
135
+ else
136
+ return;
137
+ event.preventDefault();
138
+ };
139
+ window.addEventListener('keydown', onKeyDown);
140
+ return () => window.removeEventListener('keydown', onKeyDown);
141
+ }, [actualSize, fit, pan, zoomTo]);
142
+ // A native listener, since React's wheel listener is passive and could
143
+ // not stop the page from scrolling. A pinch arrives as a wheel with
144
+ // Control held.
145
+ useEffect(() => {
146
+ const element = canvas.current;
147
+ if (!element)
148
+ return;
149
+ const onWheel = (event) => {
150
+ event.preventDefault();
151
+ const unit = event.deltaMode === WheelEvent.DOM_DELTA_LINE ? LINE_PX : 1;
152
+ if (event.ctrlKey || event.metaKey) {
153
+ const box = element.getBoundingClientRect();
154
+ zoomTo((s) => s * wheelZoomFactor(event.deltaY * unit), {
155
+ x: event.clientX - box.left,
156
+ y: event.clientY - box.top
157
+ });
158
+ }
159
+ else {
160
+ pan(-event.deltaX * unit, -event.deltaY * unit);
161
+ }
162
+ };
163
+ element.addEventListener('wheel', onWheel, { passive: false });
164
+ return () => element.removeEventListener('wheel', onWheel);
165
+ }, [pan, zoomTo]);
166
+ const onPointerDown = (event) => {
167
+ if (event.button !== 0 || !layout || !natural || !viewport)
168
+ return;
169
+ if (!canPan(layout, natural, viewport))
170
+ return;
171
+ event.preventDefault();
172
+ event.currentTarget.setPointerCapture(event.pointerId);
173
+ dragFrom.current = { x: event.clientX, y: event.clientY };
174
+ setDragging(true);
175
+ };
176
+ const onPointerMove = (event) => {
177
+ const from = dragFrom.current;
178
+ if (!from)
179
+ return;
180
+ dragFrom.current = { x: event.clientX, y: event.clientY };
181
+ pan(event.clientX - from.x, event.clientY - from.y);
182
+ };
183
+ const endDrag = () => {
184
+ dragFrom.current = null;
185
+ setDragging(false);
186
+ };
187
+ const pannable = layout !== null && natural !== null && viewport !== null && canPan(layout, natural, viewport);
188
+ return (_jsxs("div", { className: "image-view", children: [_jsx("div", { className: "image-canvas", "data-dragging": dragging, "data-pannable": pannable, onPointerCancel: endDrag, onPointerDown: onPointerDown, onPointerMove: onPointerMove, onPointerUp: endDrag, ref: setCanvas, children: _jsx("img", { alt: info.name, decoding: "async", draggable: false, onError: () => onFallback(fallbackNote), onLoad: (event) => {
189
+ const image = event.currentTarget;
190
+ const { naturalWidth: width, naturalHeight: height } = image;
191
+ if (svg) {
192
+ loadCount.current += 1;
193
+ const load = loadCount.current;
194
+ void hasNaturalSize(image).then((intrinsic) => {
195
+ if (load !== loadCount.current)
196
+ return;
197
+ if (!intrinsic) {
198
+ // Chromium rounds the default size of a very wide or tall
199
+ // viewBox to 0 on its short side; one pixel keeps a box to
200
+ // fill, and the SVG still draws at its own aspect ratio in it.
201
+ setSizeless(true);
202
+ setNatural({ width: Math.max(width, 1), height: Math.max(height, 1) });
203
+ }
204
+ else if (width > 0 && height > 0) {
205
+ setNatural({ width, height });
206
+ }
207
+ else {
208
+ onFallback(fallbackNote);
209
+ }
210
+ });
211
+ }
212
+ else if (width > 0 && height > 0) {
213
+ setNatural({ width, height });
214
+ }
215
+ else {
216
+ onFallback(fallbackNote);
217
+ }
218
+ }, src: info.mediaUrl, style: layout && natural
219
+ ? {
220
+ width: natural.width * layout.scale,
221
+ height: natural.height * layout.scale,
222
+ transform: `translate(${layout.x}px, ${layout.y}px)`
223
+ }
224
+ : { visibility: 'hidden' } }) }), _jsxs("footer", { className: "viewer-status image-status", children: [_jsxs("span", { className: "image-facts", children: [natural
225
+ ? sizeless
226
+ ? labels.image.noIntrinsicSize
227
+ : labels.image.pixelSize(labels.number(natural.width), labels.number(natural.height))
228
+ : labels.image.loading, layout && !sizeless ? _jsx("span", { children: formatScale(layout.scale, labels) }) : null] }), _jsx(Button, { disabled: zoom.mode === 'fit', onClick: fit, size: "small", ...tooltip(labels.image.fitTooltip, '⌘9'), children: labels.image.fit }), _jsx(Button, { disabled: layout === null || sizeless || (zoom.mode === 'free' && layout.scale === 1), onClick: actualSize, size: "small", ...tooltip(labels.image.actualSize, '⌘0'), children: labels.image.actualSize })] })] }));
229
+ }
230
+ export default ImageView;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Pure geometry behind the image view: the scale that fits an image in the
3
+ * window, the zoom steps, zooming about a point, and panning, all clamped so
4
+ * an image smaller than the window sits centred and a larger one never
5
+ * leaves a gap at an edge. Sizes and positions are CSS pixels; one image
6
+ * pixel per CSS pixel is actual size.
7
+ */
8
+ import type { ViewerLabels } from '../labels/viewer.js';
9
+ /** A width and a height in pixels. */
10
+ export interface Size {
11
+ readonly width: number;
12
+ readonly height: number;
13
+ }
14
+ /** Where the image sits: its scale, and its top-left corner in the viewport. */
15
+ export interface ImageLayout {
16
+ readonly scale: number;
17
+ readonly x: number;
18
+ readonly y: number;
19
+ }
20
+ /** The deepest zoom: an image pixel 64 screen points wide. */
21
+ export declare const MAX_SCALE = 64;
22
+ /** The farthest zoom out, unless fitting a huge image takes it farther. */
23
+ export declare const MIN_SCALE: number;
24
+ /**
25
+ * The scales ⌘+ and ⌘- step through, as the percentages people expect.
26
+ * Fit and a wheel or pinch can land between them; a step goes to the next
27
+ * one past the current scale.
28
+ */
29
+ export declare const ZOOM_STEPS: readonly number[];
30
+ /**
31
+ * The scale that shows the whole image in the viewport: shrunk to fit, but
32
+ * never enlarged past `maxScale`. That is actual size by default, so an icon
33
+ * opens crisp rather than blown up; an SVG with no size of its own has no
34
+ * actual size, and passes Infinity to fill the viewport. 1 while either size
35
+ * is unknown.
36
+ */
37
+ export declare function fitScale(image: Size, viewport: Size, maxScale?: number): number;
38
+ /** The zoom range for this image in this viewport: fitting a huge image may go below `MIN_SCALE`. */
39
+ export declare function clampScale(scale: number, image: Size, viewport: Size): number;
40
+ /** The layout with its scale in range and its position clamped to the viewport. */
41
+ export declare function placeImage(layout: ImageLayout, image: Size, viewport: Size): ImageLayout;
42
+ /** The whole image, fitted and centred, enlarged at most to `maxScale` (see `fitScale`). */
43
+ export declare function fitLayout(image: Size, viewport: Size, maxScale?: number): ImageLayout;
44
+ /**
45
+ * Rescales to `scale` about `anchor` (a point in the viewport), so the image
46
+ * pixel under the anchor stays under it, as far as the edges allow.
47
+ */
48
+ export declare function zoomAbout(layout: ImageLayout, scale: number, anchor: {
49
+ readonly x: number;
50
+ readonly y: number;
51
+ }, image: Size, viewport: Size): ImageLayout;
52
+ /**
53
+ * The next zoom step past `scale` in `direction` (1 in, -1 out), or the end
54
+ * of the ladder. Past the end a step stays put: a huge image fitted below
55
+ * `MIN_SCALE` has no step out, and must not jump in.
56
+ */
57
+ export declare function stepScale(scale: number, direction: 1 | -1): number;
58
+ /** The scale factor for one wheel or pinch event: a pinch moves a little, a notch about 1.4×. */
59
+ export declare function wheelZoomFactor(deltaY: number): number;
60
+ /** Moves the image by (dx, dy), clamped so it never leaves a gap at an edge. */
61
+ export declare function panBy(layout: ImageLayout, dx: number, dy: number, image: Size, viewport: Size): ImageLayout;
62
+ /** Whether the image is larger than the viewport along either axis, so a drag or an arrow moves it. */
63
+ export declare function canPan(layout: ImageLayout, image: Size, viewport: Size): boolean;
64
+ /** The scale as a whole percentage: "25%", "100%", "1600%"; under 1% keeps one decimal. */
65
+ export declare function formatScale(scale: number, labels: ViewerLabels): string;
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Pure geometry behind the image view: the scale that fits an image in the
3
+ * window, the zoom steps, zooming about a point, and panning, all clamped so
4
+ * an image smaller than the window sits centred and a larger one never
5
+ * leaves a gap at an edge. Sizes and positions are CSS pixels; one image
6
+ * pixel per CSS pixel is actual size.
7
+ */
8
+ /** The deepest zoom: an image pixel 64 screen points wide. */
9
+ export const MAX_SCALE = 64;
10
+ /** The farthest zoom out, unless fitting a huge image takes it farther. */
11
+ export const MIN_SCALE = 1 / 64;
12
+ /**
13
+ * The scales ⌘+ and ⌘- step through, as the percentages people expect.
14
+ * Fit and a wheel or pinch can land between them; a step goes to the next
15
+ * one past the current scale.
16
+ */
17
+ export const ZOOM_STEPS = [
18
+ 1 / 64,
19
+ 1 / 32,
20
+ 1 / 16,
21
+ 1 / 12,
22
+ 1 / 8,
23
+ 1 / 6,
24
+ 1 / 4,
25
+ 1 / 3,
26
+ 1 / 2,
27
+ 2 / 3,
28
+ 1,
29
+ 1.5,
30
+ 2,
31
+ 3,
32
+ 4,
33
+ 6,
34
+ 8,
35
+ 12,
36
+ 16,
37
+ 24,
38
+ 32,
39
+ 48,
40
+ 64
41
+ ];
42
+ /**
43
+ * The scale that shows the whole image in the viewport: shrunk to fit, but
44
+ * never enlarged past `maxScale`. That is actual size by default, so an icon
45
+ * opens crisp rather than blown up; an SVG with no size of its own has no
46
+ * actual size, and passes Infinity to fill the viewport. 1 while either size
47
+ * is unknown.
48
+ */
49
+ export function fitScale(image, viewport, maxScale = 1) {
50
+ if (image.width <= 0 || image.height <= 0 || viewport.width <= 0 || viewport.height <= 0) {
51
+ return 1;
52
+ }
53
+ return Math.min(maxScale, viewport.width / image.width, viewport.height / image.height);
54
+ }
55
+ /** The zoom range for this image in this viewport: fitting a huge image may go below `MIN_SCALE`. */
56
+ export function clampScale(scale, image, viewport) {
57
+ return Math.min(MAX_SCALE, Math.max(Math.min(MIN_SCALE, fitScale(image, viewport)), scale));
58
+ }
59
+ /** One axis: centred when the image is narrower than the viewport, else no gap at either edge. */
60
+ const clampAxis = (position, extent, room) => extent <= room
61
+ ? Math.round((room - extent) / 2)
62
+ : Math.round(Math.min(0, Math.max(room - extent, position)));
63
+ /** The layout with its scale in range and its position clamped to the viewport. */
64
+ export function placeImage(layout, image, viewport) {
65
+ const scale = clampScale(layout.scale, image, viewport);
66
+ return {
67
+ scale,
68
+ x: clampAxis(layout.x, image.width * scale, viewport.width),
69
+ y: clampAxis(layout.y, image.height * scale, viewport.height)
70
+ };
71
+ }
72
+ /** The whole image, fitted and centred, enlarged at most to `maxScale` (see `fitScale`). */
73
+ export function fitLayout(image, viewport, maxScale = 1) {
74
+ return placeImage({ scale: fitScale(image, viewport, maxScale), x: 0, y: 0 }, image, viewport);
75
+ }
76
+ /**
77
+ * Rescales to `scale` about `anchor` (a point in the viewport), so the image
78
+ * pixel under the anchor stays under it, as far as the edges allow.
79
+ */
80
+ export function zoomAbout(layout, scale, anchor, image, viewport) {
81
+ const next = clampScale(scale, image, viewport);
82
+ const ratio = next / layout.scale;
83
+ return placeImage({
84
+ scale: next,
85
+ x: anchor.x - (anchor.x - layout.x) * ratio,
86
+ y: anchor.y - (anchor.y - layout.y) * ratio
87
+ }, image, viewport);
88
+ }
89
+ /**
90
+ * The next zoom step past `scale` in `direction` (1 in, -1 out), or the end
91
+ * of the ladder. Past the end a step stays put: a huge image fitted below
92
+ * `MIN_SCALE` has no step out, and must not jump in.
93
+ */
94
+ export function stepScale(scale, direction) {
95
+ // A scale within a hair of a step counts as on it, so ⌘+ from 100% goes to 150%.
96
+ const tolerance = 1e-6;
97
+ if (direction === 1) {
98
+ return ZOOM_STEPS.find((step) => step > scale * (1 + tolerance)) ?? MAX_SCALE;
99
+ }
100
+ return ([...ZOOM_STEPS].reverse().find((step) => step < scale * (1 - tolerance)) ??
101
+ Math.min(scale, MIN_SCALE));
102
+ }
103
+ /** The scale factor for one wheel or pinch event: a pinch moves a little, a notch about 1.4×. */
104
+ export function wheelZoomFactor(deltaY) {
105
+ return 2 ** (-Math.max(-50, Math.min(50, deltaY)) / 100);
106
+ }
107
+ /** Moves the image by (dx, dy), clamped so it never leaves a gap at an edge. */
108
+ export function panBy(layout, dx, dy, image, viewport) {
109
+ return placeImage({ ...layout, x: layout.x + dx, y: layout.y + dy }, image, viewport);
110
+ }
111
+ /** Whether the image is larger than the viewport along either axis, so a drag or an arrow moves it. */
112
+ export function canPan(layout, image, viewport) {
113
+ return (image.width * layout.scale > viewport.width || image.height * layout.scale > viewport.height);
114
+ }
115
+ /** The scale as a whole percentage: "25%", "100%", "1600%"; under 1% keeps one decimal. */
116
+ export function formatScale(scale, labels) {
117
+ const percent = scale * 100;
118
+ return labels.image.scale(percent < 1 ? percent.toFixed(1) : String(Math.round(percent)));
119
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Latest viewport wins: the read policy of the text and hex views. A view
3
+ * asks for targets as fast as the user scrolls; a target its cached bytes
4
+ * already cover shows at once, and the rest share one read at a time. While
5
+ * a read is in flight, newer targets only replace the one waiting, so
6
+ * flinging through a huge file never queues up reads, and a read the user
7
+ * has scrolled past is dropped instead of drawn.
8
+ */
9
+ /** What the reader needs from its view. */
10
+ export interface LatestReadOptions<Target, Result> {
11
+ /** Whether the bytes the view holds already answer `target`. */
12
+ readonly covered: (target: Target) => boolean;
13
+ /** One bounded read for `target`. Must not reject. */
14
+ readonly read: (target: Target) => Promise<Result>;
15
+ /** Keeps a read's bytes for later targets; false drops them (a failure, or a stale file). */
16
+ readonly store: (result: Result) => boolean;
17
+ /** Draws `target` from the bytes the view holds. */
18
+ readonly show: (target: Target) => void;
19
+ }
20
+ /** A view's reader: request targets, dispose with the view. */
21
+ export interface LatestRead<Target> {
22
+ readonly request: (target: Target) => void;
23
+ /** Drops whatever is in flight; nothing shows after it. */
24
+ readonly dispose: () => void;
25
+ }
26
+ /** A reader with at most one read in flight, always for the newest target it could not show. */
27
+ export declare function latestRead<Target, Result>(options: LatestReadOptions<Target, Result>): LatestRead<Target>;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Latest viewport wins: the read policy of the text and hex views. A view
3
+ * asks for targets as fast as the user scrolls; a target its cached bytes
4
+ * already cover shows at once, and the rest share one read at a time. While
5
+ * a read is in flight, newer targets only replace the one waiting, so
6
+ * flinging through a huge file never queues up reads, and a read the user
7
+ * has scrolled past is dropped instead of drawn.
8
+ */
9
+ /** A reader with at most one read in flight, always for the newest target it could not show. */
10
+ export function latestRead(options) {
11
+ let latest = null;
12
+ let reading = false;
13
+ let disposed = false;
14
+ const start = (target) => {
15
+ reading = true;
16
+ void options.read(target).then((result) => {
17
+ reading = false;
18
+ if (disposed || latest === null)
19
+ return;
20
+ const wanted = latest;
21
+ // The user came back to bytes the view holds; this read is past.
22
+ if (wanted !== target && options.covered(wanted))
23
+ return;
24
+ const kept = options.store(result);
25
+ if (options.covered(wanted)) {
26
+ options.show(wanted);
27
+ }
28
+ else if (wanted === target) {
29
+ // Fewer bytes than the target asked for: show what there is, and
30
+ // never read the same target twice in a row.
31
+ if (kept)
32
+ options.show(wanted);
33
+ }
34
+ else {
35
+ start(wanted);
36
+ }
37
+ });
38
+ };
39
+ return {
40
+ request: (target) => {
41
+ if (disposed)
42
+ return;
43
+ latest = target;
44
+ if (options.covered(target)) {
45
+ options.show(target);
46
+ }
47
+ else if (!reading) {
48
+ start(target);
49
+ }
50
+ },
51
+ dispose: () => {
52
+ disposed = true;
53
+ }
54
+ };
55
+ }
@@ -0,0 +1,25 @@
1
+ import { type ReactElement, type ReactNode } from 'react';
2
+ interface MediaControlsProps {
3
+ /** The element the view renders, null until it mounts. */
4
+ readonly media: HTMLMediaElement | null;
5
+ /** The view's whole player (its stage and this bar), null until it mounts. */
6
+ readonly player: HTMLElement | null;
7
+ /** Offers F and a Full Screen button, which show `player` full screen. */
8
+ readonly fullscreen?: boolean;
9
+ /** Facts about the file (a video's pixel size), shown after the duration. */
10
+ readonly children?: ReactNode;
11
+ }
12
+ /**
13
+ * The media player's control bar, under the audio and video views: play and
14
+ * pause, the elapsed time, a seek bar, the duration, any facts the view
15
+ * adds, mute and volume, and for a video full screen. Every control drives
16
+ * `media`, the element the view renders, and the bar shows what the element
17
+ * reports, so Chromium does all the playing and seeking (through range
18
+ * requests on the media URL). Nothing starts playback but the user. The
19
+ * player's keys (Space, ← and →, Home and End, and F for a video:
20
+ * `mediaKeyAction`) work while focus is inside the player or on nothing a
21
+ * key operates, except that the volume slider keeps its own arrows, Home,
22
+ * and End. Escape leaves full screen, as Chromium does on its own.
23
+ */
24
+ declare function MediaControls({ media, player, fullscreen, children }: MediaControlsProps): ReactElement;
25
+ export default MediaControls;