react-cheminfo 0.127.0 → 0.129.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 (151) hide show
  1. package/README.md +79 -43
  2. package/lib/drawing/core/camera.d.ts +22 -0
  3. package/lib/drawing/core/camera.d.ts.map +1 -1
  4. package/lib/drawing/core/camera.js +37 -0
  5. package/lib/drawing/core/camera.js.map +1 -1
  6. package/lib/drawing/core/graphics.d.ts.map +1 -1
  7. package/lib/drawing/core/graphics.js +2 -10
  8. package/lib/drawing/core/graphics.js.map +1 -1
  9. package/lib/drawing/core/groupFiles.d.ts +22 -0
  10. package/lib/drawing/core/groupFiles.d.ts.map +1 -0
  11. package/lib/drawing/core/groupFiles.js +51 -0
  12. package/lib/drawing/core/groupFiles.js.map +1 -0
  13. package/lib/drawing/core/groups.d.ts +76 -0
  14. package/lib/drawing/core/groups.d.ts.map +1 -0
  15. package/lib/drawing/core/groups.js +190 -0
  16. package/lib/drawing/core/groups.js.map +1 -0
  17. package/lib/drawing/core/index.d.ts +7 -2
  18. package/lib/drawing/core/index.d.ts.map +1 -1
  19. package/lib/drawing/core/index.js +5 -2
  20. package/lib/drawing/core/index.js.map +1 -1
  21. package/lib/drawing/core/selectionFrame.d.ts +70 -0
  22. package/lib/drawing/core/selectionFrame.d.ts.map +1 -0
  23. package/lib/drawing/core/selectionFrame.js +156 -0
  24. package/lib/drawing/core/selectionFrame.js.map +1 -0
  25. package/lib/drawing/core/transform.d.ts +30 -0
  26. package/lib/drawing/core/transform.d.ts.map +1 -1
  27. package/lib/drawing/core/transform.js +59 -0
  28. package/lib/drawing/core/transform.js.map +1 -1
  29. package/lib/drawing/ui/FrameMarks.d.ts +30 -0
  30. package/lib/drawing/ui/FrameMarks.d.ts.map +1 -0
  31. package/lib/drawing/ui/FrameMarks.js +19 -0
  32. package/lib/drawing/ui/FrameMarks.js.map +1 -0
  33. package/lib/drawing/ui/curveHandleMarks.d.ts +10 -0
  34. package/lib/drawing/ui/curveHandleMarks.d.ts.map +1 -1
  35. package/lib/drawing/ui/curveHandleMarks.js +20 -2
  36. package/lib/drawing/ui/curveHandleMarks.js.map +1 -1
  37. package/lib/drawing/ui/index.d.ts +5 -1
  38. package/lib/drawing/ui/index.d.ts.map +1 -1
  39. package/lib/drawing/ui/index.js +3 -1
  40. package/lib/drawing/ui/index.js.map +1 -1
  41. package/lib/drawing/ui/useCanvasNavigation.d.ts +35 -0
  42. package/lib/drawing/ui/useCanvasNavigation.d.ts.map +1 -0
  43. package/lib/drawing/ui/useCanvasNavigation.js +95 -0
  44. package/lib/drawing/ui/useCanvasNavigation.js.map +1 -0
  45. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  46. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  47. package/lib/ir/ui/BandsPanel.d.ts +3 -3
  48. package/lib/ir/ui/BandsPanel.d.ts.map +1 -1
  49. package/lib/ir/ui/BandsPanel.js +7 -7
  50. package/lib/ir/ui/BandsPanel.js.map +1 -1
  51. package/lib/ir/ui/BandsToolbar.d.ts.map +1 -1
  52. package/lib/ir/ui/BandsToolbar.js +2 -4
  53. package/lib/ir/ui/BandsToolbar.js.map +1 -1
  54. package/lib/ir/ui/IrEditor.d.ts +7 -0
  55. package/lib/ir/ui/IrEditor.d.ts.map +1 -1
  56. package/lib/ir/ui/IrEditor.js.map +1 -1
  57. package/lib/ir/ui/IrShell.d.ts.map +1 -1
  58. package/lib/ir/ui/IrShell.js +3 -2
  59. package/lib/ir/ui/IrShell.js.map +1 -1
  60. package/lib/ir/ui/IrStatusBar.js +2 -2
  61. package/lib/ir/ui/IrStatusBar.js.map +1 -1
  62. package/lib/ir/ui/IrToolbar.js +2 -2
  63. package/lib/ir/ui/IrToolbar.js.map +1 -1
  64. package/lib/ir/ui/VibrationalEditor.d.ts +7 -0
  65. package/lib/ir/ui/VibrationalEditor.d.ts.map +1 -1
  66. package/lib/ir/ui/VibrationalEditor.js +2 -1
  67. package/lib/ir/ui/VibrationalEditor.js.map +1 -1
  68. package/lib/ir/ui/useIrEditor.d.ts +12 -0
  69. package/lib/ir/ui/useIrEditor.d.ts.map +1 -1
  70. package/lib/ir/ui/useIrEditor.js +10 -3
  71. package/lib/ir/ui/useIrEditor.js.map +1 -1
  72. package/lib/locales/de.json +9 -0
  73. package/lib/locales/en.json +9 -0
  74. package/lib/locales/es.json +9 -0
  75. package/lib/locales/fr.json +9 -0
  76. package/lib/locales/it.json +9 -0
  77. package/lib/pedagogy/ui/ExerciseActions.d.ts +10 -2
  78. package/lib/pedagogy/ui/ExerciseActions.d.ts.map +1 -1
  79. package/lib/pedagogy/ui/ExerciseActions.js +7 -5
  80. package/lib/pedagogy/ui/ExerciseActions.js.map +1 -1
  81. package/lib/share/core/exampleMark.d.ts +31 -0
  82. package/lib/share/core/exampleMark.d.ts.map +1 -0
  83. package/lib/share/core/exampleMark.js +31 -0
  84. package/lib/share/core/exampleMark.js.map +1 -0
  85. package/lib/share/core/index.d.ts +5 -0
  86. package/lib/share/core/index.d.ts.map +1 -1
  87. package/lib/share/core/index.js +3 -0
  88. package/lib/share/core/index.js.map +1 -1
  89. package/lib/share/core/linkBytes.d.ts +29 -0
  90. package/lib/share/core/linkBytes.d.ts.map +1 -0
  91. package/lib/share/core/linkBytes.js +70 -0
  92. package/lib/share/core/linkBytes.js.map +1 -0
  93. package/lib/share/core/linkValue.d.ts +34 -0
  94. package/lib/share/core/linkValue.d.ts.map +1 -0
  95. package/lib/share/core/linkValue.js +58 -0
  96. package/lib/share/core/linkValue.js.map +1 -0
  97. package/lib/share/core/opensOn.d.ts +133 -0
  98. package/lib/share/core/opensOn.d.ts.map +1 -0
  99. package/lib/share/core/opensOn.js +82 -0
  100. package/lib/share/core/opensOn.js.map +1 -0
  101. package/lib/share/core/seal.d.ts.map +1 -1
  102. package/lib/share/core/seal.js +3 -32
  103. package/lib/share/core/seal.js.map +1 -1
  104. package/lib/share/ui/ShareOpensOn.d.ts +32 -0
  105. package/lib/share/ui/ShareOpensOn.d.ts.map +1 -0
  106. package/lib/share/ui/ShareOpensOn.js +49 -0
  107. package/lib/share/ui/ShareOpensOn.js.map +1 -0
  108. package/lib/share/ui/index.d.ts +4 -0
  109. package/lib/share/ui/index.d.ts.map +1 -1
  110. package/lib/share/ui/index.js +2 -0
  111. package/lib/share/ui/index.js.map +1 -1
  112. package/lib/share/ui/useOpensOnShare.d.ts +65 -0
  113. package/lib/share/ui/useOpensOnShare.d.ts.map +1 -0
  114. package/lib/share/ui/useOpensOnShare.js +60 -0
  115. package/lib/share/ui/useOpensOnShare.js.map +1 -0
  116. package/package.json +1 -1
  117. package/src/drawing/core/camera.ts +45 -0
  118. package/src/drawing/core/graphics.ts +2 -12
  119. package/src/drawing/core/groupFiles.ts +55 -0
  120. package/src/drawing/core/groups.ts +221 -0
  121. package/src/drawing/core/index.ts +29 -1
  122. package/src/drawing/core/selectionFrame.ts +192 -0
  123. package/src/drawing/core/transform.ts +80 -0
  124. package/src/drawing/ui/FrameMarks.tsx +57 -0
  125. package/src/drawing/ui/curveHandleMarks.tsx +43 -10
  126. package/src/drawing/ui/index.ts +8 -1
  127. package/src/drawing/ui/useCanvasNavigation.ts +120 -0
  128. package/src/ir/ui/BandsPanel.tsx +15 -10
  129. package/src/ir/ui/BandsToolbar.tsx +2 -4
  130. package/src/ir/ui/IrEditor.tsx +7 -0
  131. package/src/ir/ui/IrShell.tsx +4 -2
  132. package/src/ir/ui/IrStatusBar.tsx +2 -2
  133. package/src/ir/ui/IrToolbar.tsx +2 -2
  134. package/src/ir/ui/VibrationalEditor.tsx +9 -0
  135. package/src/ir/ui/useIrEditor.ts +23 -4
  136. package/src/locales/de.json +9 -0
  137. package/src/locales/en.json +9 -0
  138. package/src/locales/es.json +9 -0
  139. package/src/locales/fr.json +9 -0
  140. package/src/locales/it.json +9 -0
  141. package/src/pedagogy/ui/ExerciseActions.tsx +22 -7
  142. package/src/share/core/exampleMark.ts +52 -0
  143. package/src/share/core/index.ts +25 -0
  144. package/src/share/core/linkBytes.ts +75 -0
  145. package/src/share/core/linkValue.ts +66 -0
  146. package/src/share/core/opensOn.ts +203 -0
  147. package/src/share/core/seal.ts +3 -36
  148. package/src/share/ui/ShareOpensOn.tsx +169 -0
  149. package/src/share/ui/index.ts +7 -0
  150. package/src/share/ui/useOpensOnShare.tsx +152 -0
  151. package/styles/chrome.css +18 -0
@@ -8,7 +8,9 @@ export {
8
8
  ZOOM_RANGE,
9
9
  fillCamera,
10
10
  frameCamera,
11
+ pannedCamera,
11
12
  visibleBox,
13
+ wheelZoomFactor,
12
14
  zoomAround,
13
15
  zoomOfShare,
14
16
  zoomShare,
@@ -204,8 +206,34 @@ export {
204
206
  } from './shapeHandles.ts';
205
207
  export type { Patching } from './shared.ts';
206
208
  export { idsOf, patchOf, sharedValue } from './shared.ts';
209
+ export type { DrawGroup } from './groups.ts';
210
+ export {
211
+ expandToGroups,
212
+ leafMembers,
213
+ outermostUnits,
214
+ parentsOf,
215
+ reconcileGroups,
216
+ withGroup,
217
+ withoutGroups,
218
+ } from './groups.ts';
219
+ export { parseGroups, renumberGroups } from './groupFiles.ts';
220
+ export type { FrameHandle } from './selectionFrame.ts';
221
+ export {
222
+ ROTATE_HANDLE_GAP,
223
+ frameCorners,
224
+ frameCursor,
225
+ frameHandleAt,
226
+ frameHandles,
227
+ frameTransform,
228
+ framedBox,
229
+ } from './selectionFrame.ts';
207
230
  export type { Similarity } from './transform.ts';
208
- export { pointMap } from './transform.ts';
231
+ export {
232
+ mapCurve,
233
+ pointMap,
234
+ transformCaption,
235
+ transformPicture,
236
+ } from './transform.ts';
209
237
  export type { Box, DrawPage, FigureSize, Point } from './types.ts';
210
238
  export { arrayOf, isFiniteNumber, isRecord } from './values.ts';
211
239
  export { along, leftOf, turnAbout, unit } from './vectors.ts';
@@ -0,0 +1,192 @@
1
+ /**
2
+ * The frame around a selection and its handles, the same on every canvas of
3
+ * the family: a corner scales everything selected about the corner across
4
+ * from it, keeping its proportions, and the round handle above it turns it
5
+ * about the frame's middle, by 15° steps; Shift frees the angle. A group is a
6
+ * selection like any other, so it is scaled and turned the same way. Handles
7
+ * keep their size on screen, so where they sit depends on the zoom.
8
+ *
9
+ * What the frame surrounds is the site's to measure — the box of what its
10
+ * selection draws — and how each of its items takes a {@link Similarity} is
11
+ * the site's too.
12
+ */
13
+
14
+ import { roundCoordinate } from './geometry.ts';
15
+ import { normalizeDegrees } from './pictures.ts';
16
+ import type { Similarity } from './transform.ts';
17
+ import { pointMap } from './transform.ts';
18
+ import type { Box, Point } from './types.ts';
19
+
20
+ /** A corner of the frame, or the handle that turns it. */
21
+ export type FrameHandle = 'nw' | 'ne' | 'se' | 'sw' | 'rotate';
22
+
23
+ /** How far above a frame its turning handle sits, in screen pixels. */
24
+ export const ROTATE_HANDLE_GAP = 24;
25
+
26
+ /** How far the frame stands off what it frames, in screen pixels. */
27
+ const FRAME_GAP = 6;
28
+
29
+ /** How far from a handle the pointer still takes it, in screen pixels. */
30
+ const HANDLE_REACH = 7;
31
+
32
+ /** The step a selection turns by unless Shift is held, in degrees. */
33
+ const ROTATION_STEP = 15;
34
+
35
+ /** The smallest the longer side of a frame is scaled down to, in its units. */
36
+ const MIN_SIDE = 4;
37
+
38
+ /** Where each corner sits, as signs of the half-sides. */
39
+ const CORNERS = {
40
+ nw: { x: -1, y: -1 },
41
+ ne: { x: 1, y: -1 },
42
+ se: { x: 1, y: 1 },
43
+ sw: { x: -1, y: 1 },
44
+ } as const satisfies Record<Exclude<FrameHandle, 'rotate'>, Point>;
45
+
46
+ /**
47
+ * The frame around the box of what a selection draws, standing off it.
48
+ * @param box - The box of what the selection draws.
49
+ * @param pixel - Units per screen pixel.
50
+ * @returns The frame, or undefined when there is nothing a scale or a turn
51
+ * would change.
52
+ */
53
+ export function framedBox(
54
+ box: Box | undefined,
55
+ pixel: number,
56
+ ): Box | undefined {
57
+ if (!box || Math.max(box.width, box.height) < 1) return undefined;
58
+ const gap = FRAME_GAP * pixel;
59
+ return {
60
+ x: box.x - gap,
61
+ y: box.y - gap,
62
+ width: box.width + 2 * gap,
63
+ height: box.height + 2 * gap,
64
+ };
65
+ }
66
+
67
+ /**
68
+ * Where the handles of a frame are drawn.
69
+ * @param frame - The frame.
70
+ * @param pixel - Units per screen pixel.
71
+ * @returns Each handle and its centre; the turning handle first.
72
+ */
73
+ export function frameHandles(
74
+ frame: Box,
75
+ pixel: number,
76
+ ): Array<{ handle: FrameHandle; point: Point }> {
77
+ const handles: Array<{ handle: FrameHandle; point: Point }> = [
78
+ {
79
+ handle: 'rotate',
80
+ point: {
81
+ x: frame.x + frame.width / 2,
82
+ y: frame.y - ROTATE_HANDLE_GAP * pixel,
83
+ },
84
+ },
85
+ ];
86
+ for (const [handle, sign] of Object.entries(CORNERS)) {
87
+ handles.push({
88
+ handle: handle as FrameHandle,
89
+ point: cornerOf(frame, sign),
90
+ });
91
+ }
92
+ return handles;
93
+ }
94
+
95
+ /**
96
+ * The handle of a frame under the pointer, if any.
97
+ * @param frame - The frame.
98
+ * @param point - The pointer.
99
+ * @param pixel - Units per screen pixel.
100
+ * @returns The closest handle within reach, or undefined.
101
+ */
102
+ export function frameHandleAt(
103
+ frame: Box,
104
+ point: Point,
105
+ pixel: number,
106
+ ): FrameHandle | undefined {
107
+ let best: FrameHandle | undefined;
108
+ let bestDistance = HANDLE_REACH * pixel;
109
+ for (const { handle, point: centre } of frameHandles(frame, pixel)) {
110
+ const value = Math.hypot(point.x - centre.x, point.y - centre.y);
111
+ if (value <= bestDistance) {
112
+ best = handle;
113
+ bestDistance = value;
114
+ }
115
+ }
116
+ return best;
117
+ }
118
+
119
+ /**
120
+ * The scale or the turn a handle dragged from one point to another asks for.
121
+ * @param frame - The frame as it was when the handle was taken.
122
+ * @param handle - The handle.
123
+ * @param start - Where the pointer took it.
124
+ * @param pointer - Where the pointer is.
125
+ * @param free - Whether Shift is held, which frees the angle of a turn.
126
+ * @returns The transform.
127
+ */
128
+ export function frameTransform(
129
+ frame: Box,
130
+ handle: FrameHandle,
131
+ start: Point,
132
+ pointer: Point,
133
+ free: boolean,
134
+ ): Similarity {
135
+ if (handle === 'rotate') {
136
+ const pivot = {
137
+ x: frame.x + frame.width / 2,
138
+ y: frame.y + frame.height / 2,
139
+ };
140
+ const turned =
141
+ ((Math.atan2(pointer.y - pivot.y, pointer.x - pivot.x) -
142
+ Math.atan2(start.y - pivot.y, start.x - pivot.x)) *
143
+ 180) /
144
+ Math.PI;
145
+ const rotation = free
146
+ ? Math.round(turned * 10) / 10
147
+ : Math.round(turned / ROTATION_STEP) * ROTATION_STEP;
148
+ return { pivot, scale: 1, rotation: normalizeDegrees(rotation) };
149
+ }
150
+ const sign = CORNERS[handle];
151
+ const pivot = cornerOf(frame, { x: -sign.x, y: -sign.y });
152
+ const corner = cornerOf(frame, sign);
153
+ const diagonal = { x: corner.x - pivot.x, y: corner.y - pivot.y };
154
+ const reach =
155
+ ((pointer.x - pivot.x) * diagonal.x + (pointer.y - pivot.y) * diagonal.y) /
156
+ (diagonal.x * diagonal.x + diagonal.y * diagonal.y);
157
+ const floor = MIN_SIDE / Math.max(frame.width, frame.height);
158
+ const scale = Math.max(Math.round(reach * 1000) / 1000, floor);
159
+ return { pivot, scale, rotation: 0 };
160
+ }
161
+
162
+ /**
163
+ * The corners of a frame once a transform is applied, to draw it while a
164
+ * handle is dragged.
165
+ * @param frame - The frame.
166
+ * @param transform - The transform.
167
+ * @returns The four corners, in order around the frame.
168
+ */
169
+ export function frameCorners(frame: Box, transform: Similarity): Point[] {
170
+ const map = pointMap(transform);
171
+ return [CORNERS.nw, CORNERS.ne, CORNERS.se, CORNERS.sw].map((sign) =>
172
+ map(cornerOf(frame, sign)),
173
+ );
174
+ }
175
+
176
+ /**
177
+ * The pointer's shape over a handle.
178
+ * @param handle - The handle.
179
+ * @param held - Whether it is held.
180
+ * @returns A CSS cursor.
181
+ */
182
+ export function frameCursor(handle: FrameHandle, held: boolean): string {
183
+ if (handle === 'rotate') return held ? 'grabbing' : 'grab';
184
+ return handle === 'nw' || handle === 'se' ? 'nwse-resize' : 'nesw-resize';
185
+ }
186
+
187
+ function cornerOf(frame: Box, sign: Point): Point {
188
+ return {
189
+ x: roundCoordinate(frame.x + ((sign.x + 1) / 2) * frame.width),
190
+ y: roundCoordinate(frame.y + ((sign.y + 1) / 2) * frame.height),
191
+ };
192
+ }
@@ -3,7 +3,12 @@
3
3
  * them.
4
4
  */
5
5
 
6
+ import type { DrawText } from './captions.ts';
7
+ import { clampCaptionSize } from './captions.ts';
8
+ import type { CurveControl } from './curves.ts';
6
9
  import { roundCoordinate } from './geometry.ts';
10
+ import type { DrawPicture } from './pictures.ts';
11
+ import { normalizeDegrees, normalizePicture } from './pictures.ts';
7
12
  import type { Point } from './types.ts';
8
13
  import { turnAbout } from './vectors.ts';
9
14
 
@@ -29,3 +34,78 @@ export function pointMap(transform: Similarity): (point: Point) => Point {
29
34
  return { x: roundCoordinate(x), y: roundCoordinate(y) };
30
35
  };
31
36
  }
37
+
38
+ /**
39
+ * A curve — a shape's path, a membrane, a strand — whose points and hand-set
40
+ * controls a map takes elsewhere.
41
+ * @param curve - The curve.
42
+ * @param map - Where each point goes.
43
+ * @returns The curve, moved.
44
+ */
45
+ export function mapCurve<
46
+ T extends {
47
+ points: readonly Point[];
48
+ controls?: ReadonlyArray<CurveControl | null>;
49
+ },
50
+ >(curve: T, map: (point: Point) => Point): T {
51
+ const moved = { ...curve, points: curve.points.map(map) };
52
+ if (curve.controls) {
53
+ moved.controls = curve.controls.map((control) =>
54
+ control
55
+ ? { before: map(control.before), after: map(control.after) }
56
+ : null,
57
+ );
58
+ }
59
+ return moved;
60
+ }
61
+
62
+ /**
63
+ * A caption scaled and turned with a selection: its anchor follows, its size
64
+ * scales within the bounds a caption keeps, and its turn adds up.
65
+ * @param text - The caption.
66
+ * @param transform - The scale and the turn.
67
+ * @param defaultSize - The size a caption without its own is drawn at.
68
+ * @returns The caption, transformed.
69
+ */
70
+ export function transformCaption<T extends DrawText>(
71
+ text: T,
72
+ transform: Similarity,
73
+ defaultSize: number,
74
+ ): T {
75
+ const { rotation: turn, ...rest } = text;
76
+ const moved = { ...rest, ...pointMap(transform)(text) } as T;
77
+ if (transform.scale !== 1) {
78
+ moved.size = clampCaptionSize(
79
+ roundCoordinate((text.size ?? defaultSize) * transform.scale),
80
+ );
81
+ }
82
+ const nextTurn = normalizeDegrees((turn ?? 0) + transform.rotation);
83
+ if (nextTurn !== 0) moved.rotation = nextTurn;
84
+ return moved;
85
+ }
86
+
87
+ /**
88
+ * A picture scaled and turned with a selection, about its centre.
89
+ * @param picture - The picture.
90
+ * @param transform - The scale and the turn.
91
+ * @returns The picture, transformed and normalized.
92
+ */
93
+ export function transformPicture<T extends DrawPicture>(
94
+ picture: T,
95
+ transform: Similarity,
96
+ ): T {
97
+ const width = picture.width * transform.scale;
98
+ const height = picture.height * transform.scale;
99
+ const centre = pointMap(transform)({
100
+ x: picture.x + picture.width / 2,
101
+ y: picture.y + picture.height / 2,
102
+ });
103
+ return normalizePicture({
104
+ ...picture,
105
+ x: centre.x - width / 2,
106
+ y: centre.y - height / 2,
107
+ width,
108
+ height,
109
+ rotation: (picture.rotation ?? 0) + transform.rotation,
110
+ }) as T;
111
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * The frame around the selection, as every canvas of the family draws it:
3
+ * four corners that scale it, and one handle above it, on a stem, that turns
4
+ * it. While a handle is dragged, the frame follows the scale or the turn.
5
+ */
6
+
7
+ import type { ReactElement } from 'react';
8
+
9
+ import { boxCorners } from '../core/geometry.ts';
10
+ import { frameHandles } from '../core/selectionFrame.ts';
11
+ import type { Box, Point } from '../core/types.ts';
12
+
13
+ import { DashedOutline, HandleMark } from './DashedOutline.tsx';
14
+
15
+ /** What {@link FrameMarks} draws. */
16
+ export interface FrameMarksProps {
17
+ /**
18
+ * The frame around the selection, drawn with its handles.
19
+ * @default undefined — no frame
20
+ */
21
+ frame?: Box;
22
+ /**
23
+ * Where the corners of the frame went while one of its handles is dragged,
24
+ * drawn without handles.
25
+ * @default undefined — the frame as it stands
26
+ */
27
+ corners?: readonly Point[];
28
+ /** Units per screen pixel, so the marks keep their size when zoomed. */
29
+ pixel: number;
30
+ }
31
+
32
+ /**
33
+ * The frame around the selection and its handles.
34
+ * @param props - See {@link FrameMarksProps}.
35
+ * @returns The marks, or nothing when there is no frame.
36
+ */
37
+ export function FrameMarks(props: FrameMarksProps): ReactElement | null {
38
+ const { frame, corners, pixel } = props;
39
+ const outline = corners ?? (frame ? boxCorners(frame) : undefined);
40
+ if (!outline) return null;
41
+ return (
42
+ <g data-testid="selection-frame">
43
+ <DashedOutline points={outline} pixel={pixel} />
44
+ {frame && !corners
45
+ ? frameHandles(frame, pixel).map(({ handle, point }) => (
46
+ <HandleMark
47
+ key={handle}
48
+ handle={handle}
49
+ point={point}
50
+ pixel={pixel}
51
+ stem={{ x: point.x, y: frame.y }}
52
+ />
53
+ ))
54
+ : null}
55
+ </g>
56
+ );
57
+ }
@@ -26,7 +26,6 @@ export function curveHandleMarks(
26
26
  named: (handle: CurveHandle) => string | undefined = () => undefined,
27
27
  ): ReactElement[] {
28
28
  const resolved = curveControls(shape.points, shape.closed, shape.controls);
29
- const size = POINT_SIZE * pixel;
30
29
  const arm = (key: string, from: Point, to: Point) => (
31
30
  <line
32
31
  key={key}
@@ -63,17 +62,51 @@ export function curveHandleMarks(
63
62
  {after ? arm('after-arm', point, after) : null}
64
63
  {before ? ring({ vertex, part: 'before' }, before) : null}
65
64
  {after ? ring({ vertex, part: 'after' }, after) : null}
66
- <rect
67
- data-handle={named({ vertex, part: 'point' })}
68
- className="draw-handle draw-handle--point"
69
- x={point.x - size / 2}
70
- y={point.y - size / 2}
71
- width={size}
72
- height={size}
73
- strokeWidth={pixel * 1.5}
74
- />
65
+ {pointSquare(point, pixel, named({ vertex, part: 'point' }))}
75
66
  </g>,
76
67
  );
77
68
  }
78
69
  return marks;
79
70
  }
71
+
72
+ /**
73
+ * The squares on the points of a path, without its tangents: the corners of
74
+ * a polyline or a polygon being drawn, as {@link curveHandleMarks} draws its
75
+ * points.
76
+ * @param points - The points.
77
+ * @param pixel - Points per screen pixel, so marks keep their size.
78
+ * @returns One square per point.
79
+ */
80
+ export function pointHandleMarks(
81
+ points: readonly Point[],
82
+ pixel: number,
83
+ ): ReactElement[] {
84
+ const marks: ReactElement[] = [];
85
+ for (let vertex = 0; vertex < points.length; vertex++) {
86
+ marks.push(
87
+ <g key={vertex} data-vertex={vertex}>
88
+ {pointSquare(points[vertex] as Point, pixel, undefined)}
89
+ </g>,
90
+ );
91
+ }
92
+ return marks;
93
+ }
94
+
95
+ function pointSquare(
96
+ point: Point,
97
+ pixel: number,
98
+ name: string | undefined,
99
+ ): ReactElement {
100
+ const size = POINT_SIZE * pixel;
101
+ return (
102
+ <rect
103
+ data-handle={name}
104
+ className="draw-handle draw-handle--point"
105
+ x={point.x - size / 2}
106
+ y={point.y - size / 2}
107
+ width={size}
108
+ height={size}
109
+ strokeWidth={pixel * 1.5}
110
+ />
111
+ );
112
+ }
@@ -1,12 +1,19 @@
1
1
  export { ArrangeIcon } from './ArrangeIcon.tsx';
2
2
  export type { BoxSizeFieldsProps } from './BoxSizeFields.tsx';
3
3
  export { BoxSizeFields } from './BoxSizeFields.tsx';
4
- export { curveHandleMarks } from './curveHandleMarks.tsx';
4
+ export { curveHandleMarks, pointHandleMarks } from './curveHandleMarks.tsx';
5
+ export type {
6
+ CanvasNavigation,
7
+ CanvasNavigationOptions,
8
+ } from './useCanvasNavigation.ts';
9
+ export { useCanvasNavigation } from './useCanvasNavigation.ts';
5
10
  export type { DashedOutlineProps, HandleMarkProps } from './DashedOutline.tsx';
6
11
  export { DashedOutline, HandleMark } from './DashedOutline.tsx';
7
12
  export type { DashSelectProps } from './DashSelect.tsx';
8
13
  export { DashSelect } from './DashSelect.tsx';
9
14
  export { FigureOutline } from './FigureOutline.tsx';
15
+ export type { FrameMarksProps } from './FrameMarks.tsx';
16
+ export { FrameMarks } from './FrameMarks.tsx';
10
17
  export type { FullscreenStageProps } from './FullscreenStage.tsx';
11
18
  export { FullscreenStage } from './FullscreenStage.tsx';
12
19
  export { GradientDefs } from './GradientDefs.tsx';
@@ -0,0 +1,120 @@
1
+ /**
2
+ * How a visitor moves around a canvas, the same on every one of ours: the
3
+ * wheel zooms about the point under the pointer — a pinch reaches the page as
4
+ * a wheel with Ctrl held, and zooms the same way — and a drag with the right
5
+ * or the middle button pans, over an item as over empty space. The left
6
+ * button is left to the tools: a press on an item takes it, a drag across
7
+ * empty space selects what it encloses. The browser's own menu is refused over
8
+ * the canvas so the right button is free to pan; a text field keeps its menu.
9
+ *
10
+ * The listeners are the element's own and attached once: React hears the
11
+ * wheel passively, so the page would scroll under the zoom. They read the
12
+ * camera through a ref refreshed on every commit.
13
+ */
14
+
15
+ import type { RefObject } from 'react';
16
+ import { useEffect, useLayoutEffect, useRef, useState } from 'react';
17
+
18
+ import type { Camera } from '../core/camera.ts';
19
+ import { pannedCamera, wheelZoomFactor, zoomAround } from '../core/camera.ts';
20
+
21
+ /** What {@link useCanvasNavigation} moves, and where it says so. */
22
+ export interface CanvasNavigationOptions {
23
+ /** Where the view looks now; `null` while it is not laid out. */
24
+ camera: Camera | null;
25
+ /** Called with where the view looks next, on every wheel turn and drag step. */
26
+ onCamera: (camera: Camera) => void;
27
+ }
28
+
29
+ /** What {@link useCanvasNavigation} says about the gesture under way. */
30
+ export interface CanvasNavigation {
31
+ /** Whether a button is dragging the view, for a grabbing cursor. */
32
+ panning: boolean;
33
+ }
34
+
35
+ /**
36
+ * Zoom a canvas with the wheel and pan it with the right or the middle button.
37
+ * @param ref - The element the canvas fills, which hears the gestures.
38
+ * @param options - See {@link CanvasNavigationOptions}.
39
+ * @returns See {@link CanvasNavigation}.
40
+ */
41
+ export function useCanvasNavigation(
42
+ ref: RefObject<HTMLElement | null>,
43
+ options: CanvasNavigationOptions,
44
+ ): CanvasNavigation {
45
+ const latest = useRef(options);
46
+ useLayoutEffect(() => {
47
+ latest.current = options;
48
+ });
49
+ const [panning, setPanning] = useState(false);
50
+
51
+ useEffect(() => {
52
+ const element = ref.current;
53
+ if (element === null) return;
54
+ let pan: { pointerId: number; x: number; y: number } | null = null;
55
+
56
+ function onWheel(event: WheelEvent) {
57
+ const { camera, onCamera } = latest.current;
58
+ if (camera === null || element === null) return;
59
+ event.preventDefault();
60
+ const rect = element.getBoundingClientRect();
61
+ onCamera(
62
+ zoomAround(camera, wheelZoomFactor(event, rect.height), {
63
+ x: event.clientX - rect.left - rect.width / 2,
64
+ y: event.clientY - rect.top - rect.height / 2,
65
+ }),
66
+ );
67
+ }
68
+ function onPointerDown(event: PointerEvent) {
69
+ if (event.button !== 1 && event.button !== 2) return;
70
+ if (isTextField(event.target) || latest.current.camera === null) return;
71
+ // The middle button would otherwise start the browser's autoscroll.
72
+ event.preventDefault();
73
+ element?.setPointerCapture(event.pointerId);
74
+ pan = { pointerId: event.pointerId, x: event.clientX, y: event.clientY };
75
+ setPanning(true);
76
+ }
77
+ function onPointerMove(event: PointerEvent) {
78
+ const { camera, onCamera } = latest.current;
79
+ if (pan?.pointerId !== event.pointerId || camera === null) return;
80
+ onCamera(
81
+ pannedCamera(camera, event.clientX - pan.x, event.clientY - pan.y),
82
+ );
83
+ pan.x = event.clientX;
84
+ pan.y = event.clientY;
85
+ }
86
+ function onPointerEnd(event: PointerEvent) {
87
+ if (pan?.pointerId !== event.pointerId) return;
88
+ pan = null;
89
+ setPanning(false);
90
+ }
91
+ function onContextMenu(event: MouseEvent) {
92
+ if (!isTextField(event.target)) event.preventDefault();
93
+ }
94
+
95
+ element.addEventListener('wheel', onWheel, { passive: false });
96
+ element.addEventListener('pointerdown', onPointerDown);
97
+ element.addEventListener('pointermove', onPointerMove);
98
+ element.addEventListener('pointerup', onPointerEnd);
99
+ element.addEventListener('pointercancel', onPointerEnd);
100
+ element.addEventListener('contextmenu', onContextMenu);
101
+ return () => {
102
+ element.removeEventListener('wheel', onWheel);
103
+ element.removeEventListener('pointerdown', onPointerDown);
104
+ element.removeEventListener('pointermove', onPointerMove);
105
+ element.removeEventListener('pointerup', onPointerEnd);
106
+ element.removeEventListener('pointercancel', onPointerEnd);
107
+ element.removeEventListener('contextmenu', onContextMenu);
108
+ };
109
+ }, [ref]);
110
+
111
+ return { panning };
112
+ }
113
+
114
+ function isTextField(target: EventTarget | null): boolean {
115
+ return (
116
+ target instanceof HTMLInputElement ||
117
+ target instanceof HTMLTextAreaElement ||
118
+ (target instanceof HTMLElement && target.isContentEditable)
119
+ );
120
+ }
@@ -2,6 +2,7 @@ import {
2
2
  panelBodyStyle,
3
3
  panelEmptyStyle,
4
4
  panelHeaderCellStyle,
5
+ panelScrollAreaStyle,
5
6
  panelStyle,
6
7
  panelTableStyle,
7
8
  } from '../../panel/core/panelStyles.ts';
@@ -48,18 +49,18 @@ export interface BandsPanelProps {
48
49
  * list found automatically again. Once edited it is the spectrum's as picked
49
50
  * by hand, and the threshold no longer moves it.
50
51
  *
51
- * For a technique the correlation table does not cover — Raman — the column of
52
- * candidates and the button naming them are left out, and the table is the
53
- * bands themselves.
52
+ * For a technique the correlation table does not cover — Raman — or a host that
53
+ * switched the table off — an exercise — the column of candidates and the
54
+ * button naming them are left out, and the table is the bands themselves.
54
55
  * @param props - Component props.
55
56
  * @returns The panel.
56
57
  */
57
58
  export function BandsPanel(props: BandsPanelProps) {
58
59
  const { highlight = null, onHighlight } = props;
59
- const { state, selectedSpectrum, assigned } = useIrEditorState();
60
+ const { state, selectedSpectrum, assigned, assignments } = useIrEditorState();
60
61
  const actions = useIrActions();
61
62
  const { mode, pickBands, technique } = state.settings;
62
- const { assignments, unit } = IR_TECHNIQUE_RULES[technique];
63
+ const { unit } = IR_TECHNIQUE_RULES[technique];
63
64
  const empty = emptyMessage(selectedSpectrum, pickBands, assigned.length);
64
65
 
65
66
  function remove(band: IrBand): void {
@@ -75,8 +76,10 @@ export function BandsPanel(props: BandsPanelProps) {
75
76
  <div style={panelStyle}>
76
77
  <BandsToolbar />
77
78
 
78
- <div style={panelBodyStyle}>
79
- {empty === null ? (
79
+ {/* The table scrolls in a box with no padding: a padded one keeps a
80
+ strip above the sticky heading where the rows show through. */}
81
+ {empty === null ? (
82
+ <div style={panelScrollAreaStyle}>
80
83
  <table style={panelTableStyle}>
81
84
  <thead>
82
85
  <tr>
@@ -106,10 +109,12 @@ export function BandsPanel(props: BandsPanelProps) {
106
109
  ))}
107
110
  </tbody>
108
111
  </table>
109
- ) : (
112
+ </div>
113
+ ) : (
114
+ <div style={panelBodyStyle}>
110
115
  <p style={panelEmptyStyle}>{empty}</p>
111
- )}
112
- </div>
116
+ </div>
117
+ )}
113
118
  </div>
114
119
  );
115
120
  }
@@ -2,7 +2,6 @@ import { useMemo } from 'react';
2
2
  import { Button, Toolbar } from 'react-science/ui';
3
3
 
4
4
  import { panelToolbarStyle } from '../../panel/core/panelStyles.ts';
5
- import { IR_TECHNIQUE_RULES } from '../core/irTechnique.ts';
6
5
 
7
6
  import { bandsToTsv } from './bandsTsv.ts';
8
7
  import { useIrCommandProps } from './irCommandContext.tsx';
@@ -16,12 +15,11 @@ import { useIrActions, useIrEditorState } from './irStateContext.ts';
16
15
  * @returns The row.
17
16
  */
18
17
  export function BandsToolbar() {
19
- const { state, selectedSpectrum, assigned } = useIrEditorState();
18
+ const { state, selectedSpectrum, assigned, assignments } = useIrEditorState();
20
19
  const actions = useIrActions();
21
20
  const command = useIrCommandProps();
22
21
  const { tool } = state.view;
23
- const { mode, pickBands, showAssignments, technique } = state.settings;
24
- const { assignments } = IR_TECHNIQUE_RULES[technique];
22
+ const { mode, pickBands, showAssignments } = state.settings;
25
23
  const byHand = selectedSpectrum?.bandsByHand === true;
26
24
 
27
25
  const tsv = useMemo(