Skip to content

Commit 382812a

Browse files
dannyrbJamesAPetts
andcommitted
feat: 🎸 dispatch vtk scroll event
* chore: add uuid func * feat: emit vtkscrollevent * feat: set uuid on api and for interactorStyle's model * chore: shift the responsibility of setting the UID on the iStyle to the components consumer Co-authored-by: James Petts <[email protected]>
1 parent 5d71b4b commit 382812a

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

‎src/VTKViewport/View2D.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ViewTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants';
1313
import { createSub } from '../lib/createSub.js';
1414
import realsApproximatelyEqual from '../lib/math/realsApproximatelyEqual';
1515
import createLabelPipeline from './createLabelPipeline';
16+
import { uuidv4 } from './../helpers';
1617

1718
const minSlabThickness = 0.1; // TODO -> Should this be configurable or not?
1819

@@ -74,6 +75,9 @@ export default class View2D extends Component {
7475
}
7576

7677
componentDidMount() {
78+
// Tracking ID to tie emitted events to this component
79+
const uid = uuidv4();
80+
7781
this.genericRenderWindow = vtkGenericRenderWindow.newInstance({
7882
background: [0, 0, 0],
7983
});
@@ -213,6 +217,7 @@ export default class View2D extends Component {
213217
* we make with consumers of this component.
214218
*/
215219
const api = {
220+
uid, // Tracking id available on `api`
216221
genericRenderWindow: this.genericRenderWindow,
217222
widgetManager: this.widgetManager,
218223
svgWidgetManager: this.svgWidgetManager,

‎src/VTKViewport/vtkInteractorStyleMPRSlice.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ function vtkInteractorStyleMPRSlice(publicAPI, model) {
200200
let cameraSub = null;
201201
let interactorSub = null;
202202
const superSetInteractor = publicAPI.setInteractor;
203+
203204
publicAPI.setInteractor = interactor => {
204205
superSetInteractor(interactor);
205206

@@ -339,6 +340,12 @@ function vtkInteractorStyleMPRSlice(publicAPI, model) {
339340
// Only run the onScroll callback if called from scrolling,
340341
// preventing manual setSlice calls from triggering the CB.
341342
publicAPI.scrollToSlice = slice => {
343+
// Dispatch custom event
344+
const vtkScrollEvent = new CustomEvent('vtkscrollevent', {
345+
detail: { uid: publicAPI.getUid() },
346+
});
347+
window.dispatchEvent(vtkScrollEvent);
348+
342349
const slicePoint = publicAPI.setSlice(slice);
343350

344351
// run Callback
@@ -560,6 +567,14 @@ function vtkInteractorStyleMPRSlice(publicAPI, model) {
560567
camera.setThicknessFromFocalPoint(slabThickness);
561568
};
562569

570+
publicAPI.setUid = uid => {
571+
model.uid = uid;
572+
};
573+
574+
publicAPI.getUid = () => {
575+
return model.uid;
576+
};
577+
563578
setManipulators();
564579
}
565580

@@ -569,6 +584,7 @@ function vtkInteractorStyleMPRSlice(publicAPI, model) {
569584

570585
const DEFAULT_VALUES = {
571586
slabThickness: 0.1,
587+
uid: '',
572588
};
573589

574590
// ----------------------------------------------------------------------------
@@ -593,6 +609,7 @@ export function extend(publicAPI, model, initialValues = {}) {
593609

594610
// ----------------------------------------------------------------------------
595611

612+
// Returns new instance factory, takes initial values object
596613
export const newInstance = macro.newInstance(
597614
extend,
598615
'vtkInteractorStyleMPRSlice'

‎src/helpers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import formatDA from './formatDA';
33
import formatTM from './formatTM';
44
import formatNumberPrecision from './formatNumberPrecision';
55
import isValidNumber from './isValidNumber';
6+
import uuidv4 from './uuidv4.js';
67

78
const helpers = {
89
formatPN,
@@ -12,4 +13,4 @@ const helpers = {
1213
isValidNumber,
1314
};
1415

15-
export { helpers };
16+
export { helpers, uuidv4 };

‎src/helpers/uuidv4.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// From: https://stackoverflow.com/a/2117523/1867984
2+
3+
// prettier-ignore
4+
export default function uuidv4() {
5+
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
6+
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
7+
);
8+
}

0 commit comments

Comments
 (0)