react-cheminfo 0.16.0 → 0.18.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 (170) hide show
  1. package/lib/parallel/core/index.d.ts +7 -5
  2. package/lib/parallel/core/index.d.ts.map +1 -1
  3. package/lib/parallel/core/index.js +5 -3
  4. package/lib/parallel/core/index.js.map +1 -1
  5. package/lib/parallel/core/parallelBrush.d.ts +22 -1
  6. package/lib/parallel/core/parallelBrush.d.ts.map +1 -1
  7. package/lib/parallel/core/parallelBrush.js +53 -3
  8. package/lib/parallel/core/parallelBrush.js.map +1 -1
  9. package/lib/parallel/core/parallelFilter.d.ts +14 -1
  10. package/lib/parallel/core/parallelFilter.d.ts.map +1 -1
  11. package/lib/parallel/core/parallelFilter.js +44 -11
  12. package/lib/parallel/core/parallelFilter.js.map +1 -1
  13. package/lib/parallel/core/parallelOrder.d.ts +36 -0
  14. package/lib/parallel/core/parallelOrder.d.ts.map +1 -0
  15. package/lib/parallel/core/parallelOrder.js +65 -0
  16. package/lib/parallel/core/parallelOrder.js.map +1 -0
  17. package/lib/parallel/core/parallelPaint.d.ts +4 -0
  18. package/lib/parallel/core/parallelPaint.d.ts.map +1 -1
  19. package/lib/parallel/core/parallelPaint.js +15 -6
  20. package/lib/parallel/core/parallelPaint.js.map +1 -1
  21. package/lib/parallel/core/parallelPalette.d.ts +10 -1
  22. package/lib/parallel/core/parallelPalette.d.ts.map +1 -1
  23. package/lib/parallel/core/parallelPalette.js +14 -1
  24. package/lib/parallel/core/parallelPalette.js.map +1 -1
  25. package/lib/parallel/core/parallelSelection.d.ts +43 -0
  26. package/lib/parallel/core/parallelSelection.d.ts.map +1 -0
  27. package/lib/parallel/core/parallelSelection.js +87 -0
  28. package/lib/parallel/core/parallelSelection.js.map +1 -0
  29. package/lib/parallel/core/parallelTicks.d.ts.map +1 -1
  30. package/lib/parallel/core/parallelTicks.js +7 -4
  31. package/lib/parallel/core/parallelTicks.js.map +1 -1
  32. package/lib/parallel/core/parallelTypes.d.ts +12 -1
  33. package/lib/parallel/core/parallelTypes.d.ts.map +1 -1
  34. package/lib/parallel/core/parallelTypes.js.map +1 -1
  35. package/lib/parallel/ui/ParallelAxesLayer.d.ts +2 -2
  36. package/lib/parallel/ui/ParallelAxesLayer.d.ts.map +1 -1
  37. package/lib/parallel/ui/ParallelAxesLayer.js +2 -2
  38. package/lib/parallel/ui/ParallelAxesLayer.js.map +1 -1
  39. package/lib/parallel/ui/ParallelCoordinates.d.ts.map +1 -1
  40. package/lib/parallel/ui/ParallelCoordinates.js +10 -5
  41. package/lib/parallel/ui/ParallelCoordinates.js.map +1 -1
  42. package/lib/parallel/ui/ParallelLabels.d.ts +6 -0
  43. package/lib/parallel/ui/ParallelLabels.d.ts.map +1 -1
  44. package/lib/parallel/ui/ParallelLabels.js +14 -2
  45. package/lib/parallel/ui/ParallelLabels.js.map +1 -1
  46. package/lib/parallel/ui/index.d.ts +4 -0
  47. package/lib/parallel/ui/index.d.ts.map +1 -1
  48. package/lib/parallel/ui/index.js +2 -0
  49. package/lib/parallel/ui/index.js.map +1 -1
  50. package/lib/parallel/ui/parallelCoordinatesProps.d.ts +26 -8
  51. package/lib/parallel/ui/parallelCoordinatesProps.d.ts.map +1 -1
  52. package/lib/parallel/ui/parallelGestureModel.d.ts +37 -6
  53. package/lib/parallel/ui/parallelGestureModel.d.ts.map +1 -1
  54. package/lib/parallel/ui/parallelGestureModel.js +42 -12
  55. package/lib/parallel/ui/parallelGestureModel.js.map +1 -1
  56. package/lib/parallel/ui/parallelPlotModel.d.ts +1 -1
  57. package/lib/parallel/ui/parallelPlotModel.d.ts.map +1 -1
  58. package/lib/parallel/ui/parallelPlotModel.js +3 -3
  59. package/lib/parallel/ui/parallelPlotModel.js.map +1 -1
  60. package/lib/parallel/ui/parallelStyles.d.ts +26 -3
  61. package/lib/parallel/ui/parallelStyles.d.ts.map +1 -1
  62. package/lib/parallel/ui/parallelStyles.js +26 -4
  63. package/lib/parallel/ui/parallelStyles.js.map +1 -1
  64. package/lib/parallel/ui/useParallelAxisDrag.d.ts +60 -0
  65. package/lib/parallel/ui/useParallelAxisDrag.d.ts.map +1 -0
  66. package/lib/parallel/ui/useParallelAxisDrag.js +96 -0
  67. package/lib/parallel/ui/useParallelAxisDrag.js.map +1 -0
  68. package/lib/parallel/ui/useParallelGesture.d.ts +10 -5
  69. package/lib/parallel/ui/useParallelGesture.d.ts.map +1 -1
  70. package/lib/parallel/ui/useParallelGesture.js +46 -21
  71. package/lib/parallel/ui/useParallelGesture.js.map +1 -1
  72. package/lib/structure/core/editorGuide.d.ts +69 -0
  73. package/lib/structure/core/editorGuide.d.ts.map +1 -0
  74. package/lib/structure/core/editorGuide.js +162 -0
  75. package/lib/structure/core/editorGuide.js.map +1 -0
  76. package/lib/structure/core/editorToolbar.d.ts +45 -0
  77. package/lib/structure/core/editorToolbar.d.ts.map +1 -0
  78. package/lib/structure/core/editorToolbar.js +132 -0
  79. package/lib/structure/core/editorToolbar.js.map +1 -0
  80. package/lib/structure/core/editorToolbarGeometry.d.ts +36 -0
  81. package/lib/structure/core/editorToolbarGeometry.d.ts.map +1 -0
  82. package/lib/structure/core/editorToolbarGeometry.js +61 -0
  83. package/lib/structure/core/editorToolbarGeometry.js.map +1 -0
  84. package/lib/structure/core/index.d.ts +6 -0
  85. package/lib/structure/core/index.d.ts.map +1 -1
  86. package/lib/structure/core/index.js +3 -0
  87. package/lib/structure/core/index.js.map +1 -1
  88. package/lib/structure/core/pendingCall.d.ts +21 -0
  89. package/lib/structure/core/pendingCall.d.ts.map +1 -0
  90. package/lib/structure/core/pendingCall.js +37 -0
  91. package/lib/structure/core/pendingCall.js.map +1 -0
  92. package/lib/structure/ui/EditorCanvas.d.ts +1 -16
  93. package/lib/structure/ui/EditorCanvas.d.ts.map +1 -1
  94. package/lib/structure/ui/EditorCanvas.js +5 -18
  95. package/lib/structure/ui/EditorCanvas.js.map +1 -1
  96. package/lib/structure/ui/EditorHelpButton.d.ts +22 -0
  97. package/lib/structure/ui/EditorHelpButton.d.ts.map +1 -0
  98. package/lib/structure/ui/EditorHelpButton.js +38 -0
  99. package/lib/structure/ui/EditorHelpButton.js.map +1 -0
  100. package/lib/structure/ui/KeyCaps.d.ts +12 -0
  101. package/lib/structure/ui/KeyCaps.d.ts.map +1 -0
  102. package/lib/structure/ui/KeyCaps.js +29 -0
  103. package/lib/structure/ui/KeyCaps.js.map +1 -0
  104. package/lib/structure/ui/StructureEditor.d.ts +9 -2
  105. package/lib/structure/ui/StructureEditor.d.ts.map +1 -1
  106. package/lib/structure/ui/StructureEditor.js +17 -24
  107. package/lib/structure/ui/StructureEditor.js.map +1 -1
  108. package/lib/structure/ui/StructureEditorHelp.d.ts +18 -0
  109. package/lib/structure/ui/StructureEditorHelp.d.ts.map +1 -0
  110. package/lib/structure/ui/StructureEditorHelp.js +68 -0
  111. package/lib/structure/ui/StructureEditorHelp.js.map +1 -0
  112. package/lib/structure/ui/ToolbarTooltip.d.ts +20 -0
  113. package/lib/structure/ui/ToolbarTooltip.d.ts.map +1 -0
  114. package/lib/structure/ui/ToolbarTooltip.js +49 -0
  115. package/lib/structure/ui/ToolbarTooltip.js.map +1 -0
  116. package/lib/structure/ui/editorChange.d.ts +52 -0
  117. package/lib/structure/ui/editorChange.d.ts.map +1 -0
  118. package/lib/structure/ui/editorChange.js +34 -0
  119. package/lib/structure/ui/editorChange.js.map +1 -0
  120. package/lib/structure/ui/index.d.ts +3 -1
  121. package/lib/structure/ui/index.d.ts.map +1 -1
  122. package/lib/structure/ui/index.js +1 -0
  123. package/lib/structure/ui/index.js.map +1 -1
  124. package/lib/structure/ui/useToolbarFloor.d.ts.map +1 -1
  125. package/lib/structure/ui/useToolbarFloor.js +6 -45
  126. package/lib/structure/ui/useToolbarFloor.js.map +1 -1
  127. package/lib/structure/ui/useToolbarHover.d.ts +28 -0
  128. package/lib/structure/ui/useToolbarHover.d.ts.map +1 -0
  129. package/lib/structure/ui/useToolbarHover.js +96 -0
  130. package/lib/structure/ui/useToolbarHover.js.map +1 -0
  131. package/lib/structure/ui/watchToolbar.d.ts +17 -0
  132. package/lib/structure/ui/watchToolbar.d.ts.map +1 -0
  133. package/lib/structure/ui/watchToolbar.js +56 -0
  134. package/lib/structure/ui/watchToolbar.js.map +1 -0
  135. package/package.json +1 -1
  136. package/src/parallel/core/index.ts +16 -0
  137. package/src/parallel/core/parallelBrush.ts +69 -3
  138. package/src/parallel/core/parallelFilter.ts +52 -15
  139. package/src/parallel/core/parallelOrder.ts +73 -0
  140. package/src/parallel/core/parallelPaint.ts +16 -5
  141. package/src/parallel/core/parallelPalette.ts +14 -1
  142. package/src/parallel/core/parallelSelection.ts +99 -0
  143. package/src/parallel/core/parallelTicks.ts +7 -3
  144. package/src/parallel/core/parallelTypes.ts +13 -1
  145. package/src/parallel/ui/ParallelAxesLayer.tsx +10 -4
  146. package/src/parallel/ui/ParallelCoordinates.tsx +22 -3
  147. package/src/parallel/ui/ParallelLabels.tsx +23 -2
  148. package/src/parallel/ui/index.ts +15 -0
  149. package/src/parallel/ui/parallelCoordinatesProps.ts +26 -8
  150. package/src/parallel/ui/parallelGestureModel.ts +63 -13
  151. package/src/parallel/ui/parallelPlotModel.ts +3 -3
  152. package/src/parallel/ui/parallelStyles.ts +46 -4
  153. package/src/parallel/ui/useParallelAxisDrag.ts +147 -0
  154. package/src/parallel/ui/useParallelGesture.ts +61 -28
  155. package/src/structure/core/editorGuide.ts +221 -0
  156. package/src/structure/core/editorToolbar.ts +175 -0
  157. package/src/structure/core/editorToolbarGeometry.ts +80 -0
  158. package/src/structure/core/index.ts +29 -0
  159. package/src/structure/core/pendingCall.ts +59 -0
  160. package/src/structure/ui/EditorCanvas.tsx +9 -35
  161. package/src/structure/ui/EditorHelpButton.tsx +70 -0
  162. package/src/structure/ui/KeyCaps.tsx +40 -0
  163. package/src/structure/ui/StructureEditor.tsx +39 -21
  164. package/src/structure/ui/StructureEditorHelp.tsx +136 -0
  165. package/src/structure/ui/ToolbarTooltip.tsx +95 -0
  166. package/src/structure/ui/editorChange.ts +81 -0
  167. package/src/structure/ui/index.ts +5 -1
  168. package/src/structure/ui/useToolbarFloor.ts +7 -46
  169. package/src/structure/ui/useToolbarHover.ts +134 -0
  170. package/src/structure/ui/watchToolbar.ts +59 -0
@@ -5,6 +5,7 @@ import { PARALLEL_MARGIN } from '../core/parallelAxes.ts';
5
5
  import type { ParallelAxis } from '../core/parallelTypes.ts';
6
6
 
7
7
  import { PARALLEL_LABELS_STYLE, parallelLabelStyle } from './parallelStyles.ts';
8
+ import type { ParallelAxisDrag } from './useParallelAxisDrag.ts';
8
9
 
9
10
  /** What {@link ParallelLabels} writes. */
10
11
  export interface ParallelLabelsProps {
@@ -17,6 +18,11 @@ export interface ParallelLabelsProps {
17
18
  * @default undefined
18
19
  */
19
20
  render?: ((axis: ParallelAxis, index: number) => ReactNode) | undefined;
21
+ /**
22
+ * The gesture that moves an axis by its name.
23
+ * @default undefined — the axes are fixed
24
+ */
25
+ drag?: ParallelAxisDrag | undefined;
20
26
  }
21
27
 
22
28
  /**
@@ -29,17 +35,32 @@ export interface ParallelLabelsProps {
29
35
  * @returns The names.
30
36
  */
31
37
  export function ParallelLabels(props: ParallelLabelsProps): ReactElement {
32
- const { axes, layouts, render } = props;
38
+ const { axes, layouts, render, drag } = props;
39
+ const draggable = drag?.enabled ?? false;
40
+ const active = drag?.active ?? null;
33
41
 
34
42
  return (
35
43
  <div className="parallel-coordinates-labels" style={PARALLEL_LABELS_STYLE}>
36
44
  {layouts.map((layout, index) => {
37
45
  const axis = axes[index];
46
+ const moving = active !== null && active.from === index;
38
47
  return (
39
48
  <div
40
49
  key={layout.id}
41
50
  className="parallel-axis-label"
42
- style={parallelLabelStyle(PARALLEL_MARGIN.left + layout.x)}
51
+ data-parallel-label={layout.id}
52
+ style={parallelLabelStyle({
53
+ left: PARALLEL_MARGIN.left + layout.x,
54
+ draggable,
55
+ dx: moving ? active.dx : null,
56
+ })}
57
+ {...drag?.propsFor(index)}
58
+ {...(draggable
59
+ ? {
60
+ role: 'button',
61
+ 'aria-label': `${layout.label}: move this axis with the left and right arrow keys`,
62
+ }
63
+ : null)}
43
64
  >
44
65
  {axis !== undefined && render !== undefined
45
66
  ? render(axis, index)
@@ -29,6 +29,14 @@ export {
29
29
  } from './parallelInk.ts';
30
30
  export { ParallelLabels } from './ParallelLabels.tsx';
31
31
  export type { ParallelLabelsProps } from './ParallelLabels.tsx';
32
+ export type { ParallelCommit, ParallelDraft } from './parallelGestureModel.ts';
33
+ export {
34
+ parallelAxisById,
35
+ parallelAxisNear,
36
+ parallelBandsOf,
37
+ parallelCommitOf,
38
+ parallelPointOf,
39
+ } from './parallelGestureModel.ts';
32
40
  export {
33
41
  parallelColorOf,
34
42
  parallelFigureLabel,
@@ -43,6 +51,13 @@ export type {
43
51
  ParallelCanvases,
44
52
  } from './useParallelCanvas.ts';
45
53
  export { useParallelCanvas } from './useParallelCanvas.ts';
54
+ export type {
55
+ ParallelAxisDrag,
56
+ ParallelAxisDragOptions,
57
+ ParallelAxisDragState,
58
+ ParallelAxisHandleProps,
59
+ } from './useParallelAxisDrag.ts';
60
+ export { useParallelAxisDrag } from './useParallelAxisDrag.ts';
46
61
  export type {
47
62
  ParallelGesture,
48
63
  ParallelGestureOptions,
@@ -84,10 +84,11 @@ export interface ParallelCoordinatesProps {
84
84
  height?: number;
85
85
 
86
86
  /**
87
- * The interval each axis keeps, keyed by axis id. Present, the caller owns
88
- * the brushes — which is what lets a table beside the figure filter on the
89
- * same ranges. The band being dragged stays inside the component either way,
90
- * so a controlled parent is never asked to re-render sixty times a second.
87
+ * What each axis keeps, keyed by axis id: one interval, several, or `null`.
88
+ * Present, the caller owns the brushes — which is what lets a table beside
89
+ * the figure filter on the same ranges. The band being dragged stays inside
90
+ * the component either way, so a controlled parent is never asked to
91
+ * re-render sixty times a second.
91
92
  * @default undefined — the figure keeps its own
92
93
  */
93
94
  ranges?: ParallelRanges;
@@ -97,17 +98,25 @@ export interface ParallelCoordinatesProps {
97
98
  */
98
99
  defaultRanges?: ParallelRanges;
99
100
  /**
100
- * Called when a brush is released, with the interval it keeps or `null` when
101
- * it was cleared — never while the band is being dragged.
101
+ * Whether one axis may keep several intervals at once — the light molecules
102
+ * and the heavy ones, with nothing in between. A reader brushes a second
103
+ * interval by dragging on a bare part of an axis that already carries one,
104
+ * and takes one away by clicking it.
105
+ * @default false — a new brush replaces the one the axis carried
106
+ */
107
+ several?: boolean;
108
+ /**
109
+ * Called when a brush is released, with every interval that axis now keeps —
110
+ * empty when the reader cleared it, and never while a band is being dragged.
102
111
  * @default undefined
103
112
  */
104
- onRangeChange?: (axisId: string, range: ParallelRange | null) => void;
113
+ onRangeChange?: (axisId: string, ranges: readonly ParallelRange[]) => void;
105
114
  /**
106
115
  * Called on every frame a band grows, for a caption saying what letting go
107
116
  * would keep. Nothing else should be wired to it.
108
117
  * @default undefined
109
118
  */
110
- onRangePreview?: (axisId: string, range: ParallelRange | null) => void;
119
+ onRangePreview?: (axisId: string, ranges: readonly ParallelRange[]) => void;
111
120
  /**
112
121
  * Which rows the brushes keep, one entry per row, a zero meaning excluded.
113
122
  * Left out, it is worked out from the ranges with `parallelIncludedMask`,
@@ -170,6 +179,15 @@ export interface ParallelCoordinatesProps {
170
179
  * @default the axis label, and its unit after it
171
180
  */
172
181
  renderAxisLabel?: (axis: ParallelAxis, index: number) => ReactNode;
182
+ /**
183
+ * Called with the axis ids in the order the reader dragged them into.
184
+ * Present, every axis name becomes a handle: drag it, or focus it and press
185
+ * the left and right arrow keys. Two columns only show their relationship
186
+ * when they stand next to each other, so this is how a reader asks a
187
+ * different question of the same figure.
188
+ * @default undefined — the axes stand in the order the caller gave them
189
+ */
190
+ onAxisOrder?: (ids: readonly string[]) => void;
173
191
  /**
174
192
  * What is shown in place of the figure when there is nothing to draw.
175
193
  * @default undefined — an empty figure of the given height
@@ -15,13 +15,21 @@ import type { ParallelBand } from '../core/parallelBrush.ts';
15
15
  import {
16
16
  PARALLEL_BRUSH_HALF_WIDTH,
17
17
  parallelBandOf,
18
+ parallelIsBand,
19
+ parallelRangeOf,
18
20
  } from '../core/parallelBrush.ts';
19
- import type { ParallelRanges } from '../core/parallelTypes.ts';
21
+ import {
22
+ parallelRangeList,
23
+ parallelWriteRange,
24
+ } from '../core/parallelSelection.ts';
25
+ import type { ParallelRange, ParallelRanges } from '../core/parallelTypes.ts';
20
26
 
21
27
  /** A band being dragged, before the interval it keeps is reported. */
22
28
  export interface ParallelDraft {
23
29
  /** The axis it is on. */
24
30
  axis: string;
31
+ /** Which of that axis's bands it is, or `-1` when it is a new one. */
32
+ index: number;
25
33
  /** Where it stands this frame. */
26
34
  band: ParallelBand;
27
35
  }
@@ -83,27 +91,69 @@ export function parallelAxisById(
83
91
  }
84
92
 
85
93
  /**
86
- * The band to draw on each axis: the one being dragged where there is one, and
87
- * the interval in force everywhere else.
94
+ * The bands to draw on each axis: the one being dragged where there is one,
95
+ * and the intervals in force everywhere else.
88
96
  * @param axes - The axes, from left to right.
89
- * @param ranges - The interval each axis keeps, or `null` for none.
97
+ * @param ranges - What each axis keeps: one interval, several, or none.
90
98
  * @param draft - The band being dragged, or `null` when nothing is.
91
- * @returns The bands, keyed by axis id; an axis with none is left out.
99
+ * @returns The bands, keyed by axis id; an axis with none is left out. The
100
+ * band being dragged replaces the interval it came from rather than being
101
+ * drawn beside it, so an interval never appears twice mid-gesture.
92
102
  */
93
103
  export function parallelBandsOf(
94
104
  axes: readonly ParallelAxisLayout[],
95
105
  ranges: ParallelRanges,
96
106
  draft: ParallelDraft | null,
97
- ): ReadonlyMap<string, ParallelBand> {
98
- const drawn = new Map<string, ParallelBand>();
107
+ ): ReadonlyMap<string, ParallelBand[]> {
108
+ const drawn = new Map<string, ParallelBand[]>();
99
109
  for (const axis of axes) {
100
- if (draft !== null && draft.axis === axis.id) {
101
- drawn.set(axis.id, draft.band);
102
- continue;
110
+ const list = parallelRangeList(ranges[axis.id]);
111
+ const dragging = draft !== null && draft.axis === axis.id;
112
+ const bands: ParallelBand[] = [];
113
+ for (let index = 0; index < list.length; index++) {
114
+ if (dragging && index === draft.index) continue;
115
+ bands.push(parallelBandOf(list[index] as ParallelRange, axis));
103
116
  }
104
- const range = ranges[axis.id];
105
- if (range === null || range === undefined) continue;
106
- drawn.set(axis.id, parallelBandOf(range, axis));
117
+ if (dragging) bands.push(draft.band);
118
+ if (bands.length > 0) drawn.set(axis.id, bands);
107
119
  }
108
120
  return drawn;
109
121
  }
122
+
123
+ /** What a released band does to the intervals its axis already kept. */
124
+ export interface ParallelCommit {
125
+ /** The intervals in force on that axis. */
126
+ list: readonly ParallelRange[];
127
+ /** Which of them the drag took hold of, or `-1` when it began a new one. */
128
+ index: number;
129
+ /** The band as the pointer left it. */
130
+ band: ParallelBand;
131
+ /** The axis it is on, to read the band back into its own units. */
132
+ layout: ParallelAxisLayout;
133
+ /** Whether the press took an interval and let it go without moving it. */
134
+ still: boolean;
135
+ /** Whether the axis may keep more than one interval. */
136
+ several: boolean;
137
+ }
138
+
139
+ /**
140
+ * The intervals an axis keeps once a band has been let go.
141
+ *
142
+ * A press that keeps nothing is a click, and a click says "take this away":
143
+ * the interval under the pointer when it landed on one, and every interval on
144
+ * the axis when it landed on bare axis. That is the only way to be rid of one
145
+ * interval on an axis carrying four, and it is the gesture that already
146
+ * cleared an axis before it could carry more than one.
147
+ * @param commit - See {@link ParallelCommit}.
148
+ * @returns The intervals, sorted and disjoint.
149
+ */
150
+ export function parallelCommitOf(commit: ParallelCommit): ParallelRange[] {
151
+ const { list, index, band, layout, still, several } = commit;
152
+ if (still) return parallelWriteRange(list, index, null);
153
+ if (!parallelIsBand(band)) {
154
+ return index === -1 ? [] : parallelWriteRange(list, index, null);
155
+ }
156
+ const range = parallelRangeOf(band, layout);
157
+ if (!several && index === -1) return [range];
158
+ return parallelWriteRange(list, index, range);
159
+ }
@@ -10,6 +10,7 @@
10
10
 
11
11
  import { formatInteger } from '../../format/core/numbers.ts';
12
12
  import type { ParallelHighlight } from '../core/parallelPaint.ts';
13
+ import { parallelHasRange } from '../core/parallelSelection.ts';
13
14
  import type {
14
15
  ParallelAxis,
15
16
  ParallelColorBy,
@@ -71,7 +72,7 @@ export function parallelColorOf(
71
72
  /**
72
73
  * Whether any axis on the figure carries an interval at all.
73
74
  * @param axes - The axes, whose ids the intervals are keyed by.
74
- * @param ranges - The interval each axis keeps.
75
+ * @param ranges - What each axis keeps.
75
76
  * @returns Whether anything is brushed, which is what decides whether a line
76
77
  * is ever drawn in the excluded ink.
77
78
  */
@@ -80,8 +81,7 @@ export function parallelIsBrushed(
80
81
  ranges: ParallelRanges,
81
82
  ): boolean {
82
83
  for (const axis of axes) {
83
- const range = ranges[axis.id];
84
- if (range !== null && range !== undefined) return true;
84
+ if (parallelHasRange(ranges[axis.id])) return true;
85
85
  }
86
86
  return false;
87
87
  }
@@ -62,22 +62,64 @@ export const PARALLEL_LABELS_STYLE = {
62
62
  pointerEvents: 'none',
63
63
  } as const satisfies CSSProperties;
64
64
 
65
+ /** How one axis name is placed, and whether it is being moved. */
66
+ export interface ParallelLabelPlacement {
67
+ /** Where the axis stands, in pixels from the figure's left edge. */
68
+ left: number;
69
+ /**
70
+ * Whether the name can be dragged to move its axis.
71
+ * @default false
72
+ */
73
+ draggable?: boolean;
74
+ /**
75
+ * How far it has been dragged, in pixels, or `null` when it is not moving.
76
+ * @default null
77
+ */
78
+ dx?: number | null;
79
+ }
80
+
65
81
  /**
66
82
  * One axis name, centred over its axis and taking the pointer back.
67
- * @param left - Where the axis stands, in pixels from the figure's left edge.
68
- * @returns The name's rules.
83
+ * @param placement - See {@link ParallelLabelPlacement}.
84
+ * @returns The name's rules. A name being dragged follows the pointer and is
85
+ * drawn over its neighbours, so it is never hidden behind the name it is
86
+ * about to change places with.
69
87
  */
70
- export function parallelLabelStyle(left: number): CSSProperties {
88
+ export function parallelLabelStyle(
89
+ placement: ParallelLabelPlacement,
90
+ ): CSSProperties {
91
+ const { left, draggable = false, dx = null } = placement;
92
+ const moving = dx !== null;
71
93
  return {
72
94
  position: 'absolute',
95
+ zIndex: moving ? 2 : undefined,
73
96
  top: 2,
74
97
  left,
75
- transform: 'translateX(-50%)',
98
+ transform:
99
+ dx === null ? 'translateX(-50%)' : `translateX(calc(-50% + ${dx}px))`,
76
100
  color: TOKEN.text,
77
101
  fontSize: 11,
78
102
  fontWeight: 600,
79
103
  whiteSpace: 'nowrap',
80
104
  pointerEvents: 'auto',
105
+ cursor: draggable ? (moving ? 'grabbing' : 'grab') : undefined,
106
+ opacity: moving ? 0.85 : undefined,
107
+ touchAction: draggable ? 'none' : undefined,
108
+ userSelect: draggable ? 'none' : undefined,
109
+ };
110
+ }
111
+
112
+ /**
113
+ * The axis a dragged name would take the place of, marked while it is moving.
114
+ * @param ink - The colour the figure singles things out in.
115
+ * @returns The mark's rules.
116
+ */
117
+ export function parallelDropMarkStyle(ink: string): CSSProperties {
118
+ return {
119
+ stroke: ink,
120
+ strokeWidth: 2,
121
+ strokeOpacity: 0.7,
122
+ strokeDasharray: '4 3',
81
123
  };
82
124
  }
83
125
 
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Dragging an axis by its name to stand somewhere else.
3
+ *
4
+ * Two columns only show their relationship when they are neighbours, so the
5
+ * order the axes stand in is a question the reader asks, not a decision the
6
+ * caller takes once. The name is the handle: it is the only part of an axis
7
+ * that is not already spoken for by the brush, and it is what a reader reaches
8
+ * for anyway.
9
+ *
10
+ * The gesture moves the name and nothing else. Reordering the axes for real
11
+ * would repaint every line on every frame, which a library of ten thousand
12
+ * molecules cannot afford — so the figure shows where the axis would land and
13
+ * moves it once, on release.
14
+ */
15
+
16
+ import type { KeyboardEvent, PointerEvent } from 'react';
17
+ import { useRef, useState } from 'react';
18
+
19
+ import type { ParallelAxisLayout } from '../core/parallelAxes.ts';
20
+ import {
21
+ parallelAxisOrder,
22
+ parallelDropIndex,
23
+ parallelMoveAxis,
24
+ } from '../core/parallelOrder.ts';
25
+
26
+ /** What {@link useParallelAxisDrag} needs. */
27
+ export interface ParallelAxisDragOptions {
28
+ /** The axes, from left to right, already placed. */
29
+ layouts: readonly ParallelAxisLayout[];
30
+ /**
31
+ * Called with the axis ids in the order the reader put them in.
32
+ * @default undefined — the axes cannot be moved
33
+ */
34
+ onAxisOrder?: ((ids: readonly string[]) => void) | undefined;
35
+ }
36
+
37
+ /** The name being dragged, and where it would land. */
38
+ export interface ParallelAxisDragState {
39
+ /** Which axis is moving, as an index into the axes. */
40
+ from: number;
41
+ /** How far its name has been dragged, in pixels. */
42
+ dx: number;
43
+ /** Which place it would take if let go now. */
44
+ to: number;
45
+ }
46
+
47
+ /** The reorder gesture, as the figure renders it. */
48
+ export interface ParallelAxisDrag {
49
+ /** Whether the axes can be moved at all. */
50
+ enabled: boolean;
51
+ /** The name being dragged, or `null` when none is. */
52
+ active: ParallelAxisDragState | null;
53
+ /** Spread onto one axis's name. */
54
+ propsFor: (index: number) => ParallelAxisHandleProps;
55
+ }
56
+
57
+ /** What one axis name carries so it can be dragged and stepped with the keys. */
58
+ export interface ParallelAxisHandleProps {
59
+ onPointerDown: (event: PointerEvent<HTMLElement>) => void;
60
+ onPointerMove: (event: PointerEvent<HTMLElement>) => void;
61
+ onPointerUp: (event: PointerEvent<HTMLElement>) => void;
62
+ onPointerCancel: (event: PointerEvent<HTMLElement>) => void;
63
+ onKeyDown: (event: KeyboardEvent<HTMLElement>) => void;
64
+ tabIndex: number;
65
+ }
66
+
67
+ /** A name that has not been dragged, and is not moving anything. */
68
+ const IDLE: ParallelAxisHandleProps = {
69
+ onPointerDown: noop,
70
+ onPointerMove: noop,
71
+ onPointerUp: noop,
72
+ onPointerCancel: noop,
73
+ onKeyDown: noop,
74
+ tabIndex: -1,
75
+ };
76
+
77
+ /**
78
+ * Move an axis by dragging its name, or by stepping it with the arrow keys.
79
+ * @param options - See {@link ParallelAxisDragOptions}.
80
+ * @returns The gesture. See {@link ParallelAxisDrag}.
81
+ */
82
+ export function useParallelAxisDrag(
83
+ options: ParallelAxisDragOptions,
84
+ ): ParallelAxisDrag {
85
+ const { layouts, onAxisOrder } = options;
86
+ const [active, setActive] = useState<ParallelAxisDragState | null>(null);
87
+ const originRef = useRef(0);
88
+
89
+ function move(from: number, to: number): void {
90
+ if (onAxisOrder === undefined) return;
91
+ const order = parallelAxisOrder(layouts);
92
+ if (to < 0 || to >= order.length || to === from) return;
93
+ onAxisOrder(parallelMoveAxis(order, from, to));
94
+ }
95
+
96
+ function dropAt(from: number, dx: number): number {
97
+ const layout = layouts[from];
98
+ if (layout === undefined) return from;
99
+ return parallelDropIndex(layout.x + dx, layouts);
100
+ }
101
+
102
+ function propsFor(index: number): ParallelAxisHandleProps {
103
+ if (onAxisOrder === undefined) return IDLE;
104
+ return {
105
+ tabIndex: 0,
106
+ onPointerDown: (event) => {
107
+ if (event.button !== 0) return;
108
+ event.preventDefault();
109
+ event.currentTarget.setPointerCapture(event.pointerId);
110
+ originRef.current = event.clientX;
111
+ setActive({ from: index, dx: 0, to: index });
112
+ },
113
+ onPointerMove: (event) => {
114
+ setActive((current) => {
115
+ if (current?.from !== index) return current;
116
+ const dx = event.clientX - originRef.current;
117
+ return { from: index, dx, to: dropAt(index, dx) };
118
+ });
119
+ },
120
+ onPointerUp: (event) => {
121
+ event.currentTarget.releasePointerCapture?.(event.pointerId);
122
+ setActive((current) => {
123
+ if (current !== null && current.from === index) {
124
+ move(current.from, current.to);
125
+ }
126
+ return null;
127
+ });
128
+ },
129
+ onPointerCancel: () => {
130
+ setActive(null);
131
+ },
132
+ onKeyDown: (event) => {
133
+ const step =
134
+ event.key === 'ArrowLeft' ? -1 : event.key === 'ArrowRight' ? 1 : 0;
135
+ if (step === 0) return;
136
+ event.preventDefault();
137
+ move(index, index + step);
138
+ },
139
+ };
140
+ }
141
+
142
+ return { enabled: onAxisOrder !== undefined, active, propsFor };
143
+ }
144
+
145
+ function noop(): void {
146
+ // A figure whose axes are fixed hands its names no handlers at all.
147
+ }
@@ -25,11 +25,10 @@ import type { ParallelBand, ParallelBrushDrag } from '../core/parallelBrush.ts';
25
25
  import {
26
26
  parallelBandAt,
27
27
  parallelBandOf,
28
- parallelBrushGrip,
29
- parallelIsBand,
30
- parallelRangeOf,
28
+ parallelBrushTarget,
31
29
  } from '../core/parallelBrush.ts';
32
30
  import { parallelNearestRow } from '../core/parallelHit.ts';
31
+ import { parallelRangeList } from '../core/parallelSelection.ts';
33
32
  import type { ParallelRange, ParallelRanges } from '../core/parallelTypes.ts';
34
33
 
35
34
  import type { ParallelDraft } from './parallelGestureModel.ts';
@@ -37,6 +36,7 @@ import {
37
36
  parallelAxisById,
38
37
  parallelAxisNear,
39
38
  parallelBandsOf,
39
+ parallelCommitOf,
40
40
  parallelPointOf,
41
41
  } from './parallelGestureModel.ts';
42
42
 
@@ -54,11 +54,16 @@ export interface ParallelGestureOptions {
54
54
  included: Uint8Array | null;
55
55
  /** The intervals in force, whoever owns them. */
56
56
  ranges: ParallelRanges;
57
- /** Called when a brush is released, with the interval it keeps or `null`. */
58
- onRangeChange: (axisId: string, range: ParallelRange | null) => void;
57
+ /**
58
+ * Whether one axis may keep several intervals at once.
59
+ * @default false
60
+ */
61
+ several?: boolean | undefined;
62
+ /** Called when a brush is released, with every interval that axis now keeps. */
63
+ onRangeChange: (axisId: string, ranges: readonly ParallelRange[]) => void;
59
64
  /** Called on every frame a band grows. */
60
65
  onRangePreview?:
61
- ((axisId: string, range: ParallelRange | null) => void) | undefined;
66
+ ((axisId: string, ranges: readonly ParallelRange[]) => void) | undefined;
62
67
  /** The row the caller says is hovered, if it owns that. */
63
68
  hovered?: number | undefined;
64
69
  /** Called with the row under the pointer, or `-1`. */
@@ -73,8 +78,8 @@ export interface ParallelGesture {
73
78
  surface: ScatterSurfaceProps;
74
79
  /** Put on the same rectangle, for the click that picks a line. */
75
80
  onClick: (event: ReactMouseEvent<Element>) => void;
76
- /** The band to draw on each axis, the one being dragged included. */
77
- bands: ReadonlyMap<string, ParallelBand>;
81
+ /** The bands to draw on each axis, the one being dragged included. */
82
+ bands: ReadonlyMap<string, ParallelBand[]>;
78
83
  /** The row under the pointer, or `-1`. */
79
84
  hovered: number;
80
85
  /** Where the pointer is in the figure, or `null` when it is away. */
@@ -92,6 +97,7 @@ export function useParallelGesture(
92
97
  options: ParallelGestureOptions,
93
98
  ): ParallelGesture {
94
99
  const { layouts, values, count, innerHeight, included, ranges } = options;
100
+ const { several = false } = options;
95
101
  const { onRangeChange, onRangePreview, onHoverChange, onRowClick } = options;
96
102
 
97
103
  const [draft, setDraft] = useState<ParallelDraft | null>(null);
@@ -104,10 +110,20 @@ export function useParallelGesture(
104
110
  const reportedRef = useRef(-1);
105
111
  const claimedRef = useRef(false);
106
112
 
107
- function rangeOf(axisId: string, band: ParallelBand): ParallelRange | null {
108
- const layout = parallelAxisById(layouts, axisId);
109
- if (layout === undefined || !parallelIsBand(band)) return null;
110
- return parallelRangeOf(band, layout);
113
+ function commit(
114
+ drag: ParallelBrushDrag,
115
+ band: ParallelBand,
116
+ ): readonly ParallelRange[] {
117
+ const layout = parallelAxisById(layouts, drag.axis);
118
+ if (layout === undefined) return parallelRangeList(ranges[drag.axis]);
119
+ return parallelCommitOf({
120
+ list: parallelRangeList(ranges[drag.axis]),
121
+ index: drag.index,
122
+ band,
123
+ layout,
124
+ still: unmoved(drag, band),
125
+ several,
126
+ });
111
127
  }
112
128
 
113
129
  function report(index: number): void {
@@ -135,22 +151,24 @@ export function useParallelGesture(
135
151
  claimedRef.current = false;
136
152
  return false;
137
153
  }
138
- const current = ranges[layout.id];
139
- const band =
140
- current === null || current === undefined
141
- ? null
142
- : parallelBandOf(current, layout);
154
+ const list = parallelRangeList(ranges[layout.id]);
155
+ const bands: ParallelBand[] = [];
156
+ for (const range of list) bands.push(parallelBandOf(range, layout));
157
+ const target = parallelBrushTarget(point.y, bands);
158
+ const held = target.index === -1 ? undefined : bands[target.index];
143
159
  const drag: ParallelBrushDrag = {
144
160
  axis: layout.id,
145
- grip: parallelBrushGrip(point.y, band),
161
+ index: target.index,
162
+ grip: target.grip,
146
163
  originY: point.y,
147
- origin: band ?? { top: point.y, bottom: point.y },
164
+ origin: held ?? { top: point.y, bottom: point.y },
148
165
  };
149
166
  dragRef.current = drag;
150
167
  pointRef.current = point;
151
168
  claimedRef.current = true;
152
169
  setDraft({
153
170
  axis: layout.id,
171
+ index: target.index,
154
172
  band: parallelBandAt(drag, point.y, innerHeight),
155
173
  });
156
174
  return true;
@@ -161,8 +179,8 @@ export function useParallelGesture(
161
179
  const drag = dragRef.current;
162
180
  if (claimed && drag !== null) {
163
181
  const band = parallelBandAt(drag, point.y, innerHeight);
164
- setDraft({ axis: drag.axis, band });
165
- onRangePreview?.(drag.axis, rangeOf(drag.axis, band));
182
+ setDraft({ axis: drag.axis, index: drag.index, band });
183
+ onRangePreview?.(drag.axis, commit(drag, band));
166
184
  return;
167
185
  }
168
186
  setPointer({
@@ -172,22 +190,22 @@ export function useParallelGesture(
172
190
  report(findRow(point));
173
191
  }
174
192
 
175
- function release(event: SurfaceEvent | null, commit: boolean): void {
193
+ function release(event: SurfaceEvent | null, keep: boolean): void {
176
194
  const drag = dragRef.current;
177
195
  dragRef.current = null;
178
196
  setDraft(null);
179
197
  if (drag === null) return;
180
198
  if (event !== null) pointRef.current = parallelPointOf(event);
181
- if (!commit) {
182
- onRangePreview?.(drag.axis, ranges[drag.axis] ?? null);
199
+ if (!keep) {
200
+ onRangePreview?.(drag.axis, parallelRangeList(ranges[drag.axis]));
183
201
  return;
184
202
  }
185
- const range = rangeOf(
186
- drag.axis,
203
+ const kept = commit(
204
+ drag,
187
205
  parallelBandAt(drag, pointRef.current.y, innerHeight),
188
206
  );
189
- onRangePreview?.(drag.axis, range);
190
- onRangeChange(drag.axis, range);
207
+ onRangePreview?.(drag.axis, kept);
208
+ onRangeChange(drag.axis, kept);
191
209
  }
192
210
 
193
211
  const { surface } = useCapturedPointer({
@@ -225,3 +243,18 @@ export function useParallelGesture(
225
243
  brushing: draft !== null,
226
244
  };
227
245
  }
246
+
247
+ /**
248
+ * Whether a press took hold of an interval and let it go where it found it,
249
+ * which is a click on that interval rather than a move of it.
250
+ * @param drag - The drag, as it began.
251
+ * @param band - The band as the pointer left it.
252
+ * @returns Whether nothing moved.
253
+ */
254
+ function unmoved(drag: ParallelBrushDrag, band: ParallelBand): boolean {
255
+ return (
256
+ drag.index !== -1 &&
257
+ band.top === drag.origin.top &&
258
+ band.bottom === drag.origin.bottom
259
+ );
260
+ }