copper3d 4.1.3 → 4.1.4

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.
package/README.md CHANGED
@@ -756,7 +756,8 @@ yourself. It throws on an unknown layer, or a channel outside `[1, 255]`.
756
756
  | **Data** | `reset()` | Reset all volumes, undo histories, canvases, sphere data |
757
757
  | | `clearActiveLayer()` | Clear active layer volume + undo history |
758
758
  | | `clearActiveSlice()` | Clear current slice (undoable) |
759
- | | `clearChannel(id, ch)` | Erase one channel across a whole layer (undoable); throws on unknown layer or channel outside [1, 255] |
759
+ | | `clearChannel(id, ch)` | Erase one channel across a whole layer (undoable); returns a `ChannelClear` handle, or `null` if the channel was empty; throws on unknown layer or channel outside [1, 255] |
760
+ | | `undoChannelClear(clear)` | Reverse that specific clear on its own layer, whatever layer is active; `false` if a later operation sits on it |
760
761
  | | `setAllSlices(slices)` | Load NRRD slices, init MaskVolumes |
761
762
  | | `setMasksFromNIfTI(map, bar?)` | Load saved NIfTI voxel data (grids must be registered first) |
762
763
  | | `registerNiftiMaskGrid(data, dims)` | *(module export)* Record a mask buffer's NIfTI grid for validation |
@@ -852,6 +853,13 @@ interface RGBAColor { r: number; g: number; b: number; a: number; } // 0-255
852
853
 
853
854
  type ChannelColorMap = Record<number, RGBAColor>; // key = channel number, 1-255
854
855
 
856
+ // One channel clear, as clearChannel returns it, for undoChannelClear
857
+ interface ChannelClear {
858
+ readonly layerId: string;
859
+ readonly channel: number;
860
+ readonly entry: UndoEntry; // compared by identity
861
+ }
862
+
855
863
  // What an undo or redo changed; getMaskData's last argument, set only for undo/redo
856
864
  interface IMaskHistoryChange {
857
865
  /** Channels whose voxels the undone or redone delta changed, ascending. */
@@ -1100,6 +1100,47 @@ Only slices that actually carry the label are touched, so a small annotation in
1100
1100
  costs one scan and a handful of `getMaskData` callbacks rather than one callback per slice.
1101
1101
  If the channel is empty, nothing is pushed and nothing fires.
1102
1102
 
1103
+ ##### Reversing *that* clear — `undoChannelClear()` <Badge type="tip" text="4.1.4" />
1104
+
1105
+ `undo()` reverses the latest operation on the **active** layer. That is the wrong tool for an
1106
+ "Undo" affordance next to a deleted finding: by the time the user clicks it, the active layer
1107
+ may have moved on, and plain `undo()` would reverse something else entirely.
1108
+
1109
+ `clearChannel` now hands back a handle for exactly that:
1110
+
1111
+ ```typescript
1112
+ const clear = nrrdTools.clearChannel('layer1', 3); // ChannelClear | null
1113
+
1114
+ // …the user switches to layer2, then clicks "Undo delete" on the toast
1115
+ if (clear && nrrdTools.undoChannelClear(clear)) {
1116
+ toast.dismiss(); // finding 3 is back on layer1
1117
+ }
1118
+ ```
1119
+
1120
+ | | |
1121
+ |---|---|
1122
+ | Returns `null` | The channel held nothing, so there is nothing to undo |
1123
+ | `undoChannelClear` → `true` | The clear was reversed, on **its own** layer, whatever layer is active |
1124
+ | `undoChannelClear` → `false` | A later operation is stacked on it on that layer, or it was already undone |
1125
+
1126
+ ::: tip It refuses rather than reaching past newer work
1127
+ A later edit on that layer is newer work, and reversing the clear underneath it would discard
1128
+ that edit silently. So `undoChannelClear` reverses the clear only while it is still that
1129
+ layer's latest operation; otherwise it changes nothing and returns `false`, and `undo()` is
1130
+ the way back — it steps through the stack in order.
1131
+
1132
+ Edits on a **different** layer do not block it; each layer has its own stack.
1133
+ :::
1134
+
1135
+ It is a once-only reversal, and it leaves the clear **redoable** exactly as `Ctrl+Z` does. The
1136
+ restored voxels are reported through `onMaskChanged` with their channels, like any other undo.
1137
+
1138
+ ::: warning It repaints every slice, not just the visible one
1139
+ `undo()` repaints only the slice on screen when it lies on the undone delta's axis. A channel
1140
+ clear spans every slice carrying the label, so this repaints them all — the same work
1141
+ `clearChannel` itself does.
1142
+ :::
1143
+
1103
1144
  ::: warning Errors are thrown, not swallowed
1104
1145
  - Unknown `layerId` → `Error` listing the known layers. It deliberately does **not** go
1105
1146
  through `getVolumeForLayer`, which warns and falls back to the first layer; for a
@@ -1946,6 +1987,13 @@ interface IKeyBoardSettings {
1946
1987
  }
1947
1988
 
1948
1989
  // 3D coordinate (used in sphere position callbacks)
1990
+ // One channel clear, as clearChannel returns it, for undoChannelClear
1991
+ interface ChannelClear {
1992
+ readonly layerId: string;
1993
+ readonly channel: number;
1994
+ readonly entry: UndoEntry; // compared by identity
1995
+ }
1996
+
1949
1997
  // What an undo or redo changed; passed as getMaskData's last argument
1950
1998
  interface IMaskHistoryChange {
1951
1999
  /** Channels whose voxels the undone or redone delta changed, ascending. */
@@ -1990,7 +2038,8 @@ cap it below what the byte allows. Capping it lower is your product's call, not
1990
2038
  | **Data** | `reset()` | Reset all volumes, undo histories, canvases, and sphere data |
1991
2039
  | | `clearActiveLayer()` | Clear annotations and undo history for the *currently active layer* |
1992
2040
  | | `clearActiveSlice()` | Clear annotations exclusively on the *currently viewed slice* of the active layer |
1993
- | | `clearChannel(id, ch)` | Erase one channel across a whole layer, undoably. Throws on an unknown layer or a channel outside [1, 255] |
2041
+ | | `clearChannel(id, ch)` | Erase one channel across a whole layer, undoably. Returns a `ChannelClear` handle, or `null` if the channel was empty. Throws on an unknown layer or a channel outside [1, 255] |
2042
+ | | `undoChannelClear(clear)` | Reverse that specific clear on its own layer, whatever layer is active. `false` if a later operation sits on it, or it was already undone |
1994
2043
  | | `setAllSlices(slices)` | Load NRRD slices, init MaskVolumes |
1995
2044
  | | `setMasksFromNIfTI(map, bar?)` | Load saved NIfTI voxel data (each buffer must be registered via `registerNiftiMaskGrid` first, or it is refused) |
1996
2045
  | | `registerNiftiMaskGrid(data, dims)` | *(module export, not a method)* Record a mask buffer's NIfTI voxel grid so `setMasksFromNIfTI` can validate it |
@@ -118,7 +118,8 @@ protectedData.maskData.volumes = {
118
118
  |--------|-----------|-------------|
119
119
  | `setActiveLayer` | `(layerId: string): void` | Set the active Layer; also updates fillColor/brushColor |
120
120
  | `setActiveChannel` | `(channel: ChannelValue): void` | Set the active Channel (1–`MAX_ENGINE_CHANNEL`); updates brush color |
121
- | `clearChannel` | `(layerId: string, channel: number): void` | Erase one label across a whole layer, undoably. Throws on an unknown layer (deliberately not via `getVolumeForLayer`, whose fallback would erase a different layer) or a channel outside [1, 255] |
121
+ | `clearChannel` | `(layerId, channel): ChannelClear \| null` | Erase one label across a whole layer, undoably. Returns the clear as a handle for `undoChannelClear`, or `null` when the channel held nothing. Throws on an unknown layer (deliberately not via `getVolumeForLayer`, whose fallback would erase a different layer) or a channel outside [1, 255] |
122
+ | `undoChannelClear` | `(clear: ChannelClear): boolean` | Reverse that clear on **its own** layer, whatever layer is active, and only while it is still that layer's latest operation. `false` when a later operation is stacked on it or it was already undone. Repaints every slice, since a clear spans them all |
122
123
  | `setCurrentLayer` | `(): { ctx, canvas } \| null` *(ToolHost)* | The layer canvas drawing should target. **Returns `null`** when the instance has no layers at all — `new NrrdTools(el, { layers: [] })`, a read-only reference viewer. Every caller must skip its work on `null` rather than dereference |
123
124
  | `getLayerVolume` | `(layerId: string): Uint8Array \| null` | A **copy** of the layer's voxel buffer — not the live array, which a caller could mutate with nothing invalidating the slice cache. Delegates to `DrawToolCore.getLayerVolume` |
124
125
  | `replaceLayerVolume` | `(layerId, data, opts?): void` | Replace the whole buffer. `{ undoable: true }` pushes one `VolumeSnapshot`. Does **not** fire `onLayerVolumeReplaced` itself — that fires only on a later undo/redo, when the backend holds the newer volume and must be told to fall back |
@@ -1524,6 +1525,48 @@ broke in two different ways for any other id — and `NrrdTools`'s constructor h
1524
1525
  needs to know which layers exist, and `clearAll()` now iterates the maps rather than a fixed
1525
1526
  id list.
1526
1527
 
1528
+ ### Undoing a named entry <Badge type="tip" text="4.1.4" />
1529
+
1530
+ `undo()` always acts on the *active* layer's top entry. Reversing one specific earlier
1531
+ operation — a channel clear the host offered an "Undo" button for — needs to address the entry
1532
+ itself, which is what these add:
1533
+
1534
+ | Member | Does |
1535
+ |--------|------|
1536
+ | `undoOn(layerId)` | Like `undo()`, but on the layer named rather than the active one. The entry moves to that layer's redo stack just the same |
1537
+ | `peek(layerId)` | The entry an undo on that layer *would* take next, left in place |
1538
+ | `entryLayer(entry)` | The layer an entry belongs to — `entry[0].layerId` for a delta group, `entry.layerId` for a volume snapshot |
1539
+
1540
+ ```ts
1541
+ interface ChannelClear {
1542
+ readonly layerId: string;
1543
+ readonly channel: number;
1544
+ /** The undo entry the clear pushed; compared by identity. */
1545
+ readonly entry: UndoEntry;
1546
+ }
1547
+ ```
1548
+
1549
+ `DrawToolCore.undoEntry(entry)` is the guard that makes this safe:
1550
+
1551
+ ```
1552
+ undoEntry(entry)
1553
+ ├─ layerId = entryLayer(entry) → undefined? refuse
1554
+ ├─ undoManager.peek(layerId) !== entry → refuse ◀ a later op is stacked on it
1555
+ ├─ undoManager.undoOn(layerId)
1556
+ └─ applyUndo(entry) → write "before" back, repaint, report
1557
+ ```
1558
+
1559
+ The identity comparison in `peek(layerId) !== entry` is the whole mechanism: it reverses the
1560
+ entry only while it is still the top of its layer's stack. A later edit there is newer work,
1561
+ and silently reversing underneath it would discard that edit — so it refuses and returns
1562
+ `false`, leaving `undo()` to step back through the stack in order. It is also what makes a
1563
+ second call a no-op: after the first, the entry has moved to the redo stack and is no longer
1564
+ the top of the undo stack.
1565
+
1566
+ `applyUndo` was split out of `undoLastPainting` so both paths share one implementation: the
1567
+ write-back, the repaint and the `onMaskChanged` report are identical whichever way the entry
1568
+ was chosen.
1569
+
1527
1570
  **Undo flow:**
1528
1571
 
1529
1572
  ```
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
- "copper3dVersion": "4.1.3",
4
- "generatedAt": "2026-11-05T09:16:06.602Z",
5
- "symbolCount": 1206,
3
+ "copper3dVersion": "4.1.4",
4
+ "generatedAt": "2026-11-10T07:12:41.579Z",
5
+ "symbolCount": 1207,
6
6
  "guides": [
7
7
  {
8
8
  "id": "4D-MRI-LV-rendering",
@@ -90,7 +90,7 @@
90
90
  "title": "NrrdTools Usage Guide",
91
91
  "summary": "Copper3D `NrrdTools` — Medical Image Segmentation Annotation Engine",
92
92
  "lang": "en",
93
- "bytes": 84625
93
+ "bytes": 87023
94
94
  },
95
95
  {
96
96
  "id": "on-demand-controls",
@@ -130,7 +130,7 @@
130
130
  "title": "Segmentation Module Documentation",
131
131
  "summary": "⚠️ **Note**: All line number references in this document are from historical versions. After multiple rounds of refactoring (State Management Refactor, NrrdTools God Class Split, inheritance → composition refactor), these references are ou…",
132
132
  "lang": "en",
133
- "bytes": 85404
133
+ "bytes": 87754
134
134
  },
135
135
  {
136
136
  "id": "surface-annotation-api",