fix(measurement-tracking): restore tracked state on undo after Delete all (#5994)
* fix: always show delete confirmation when measurements exist
This commit is contained in:
1 parent
8cd8ccc163
commit
397aa4d0e3
10 files changed
+238
-30
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { eventTarget, Types } from '@cornerstonejs/core';
|
||||
import { eventTarget, Types, utilities as csUtils } from '@cornerstonejs/core';
|
||||
import { Enums, annotation, cancelActiveManipulations } from '@cornerstonejs/tools';
|
||||
import { DicomMetadataStore } from '@ohif/core';
|
||||
|
||||
@@ -17,6 +17,8 @@ const { CORNERSTONE_3D_TOOLS_SOURCE_NAME, CORNERSTONE_3D_TOOLS_SOURCE_VERSION }
|
||||
const { removeAnnotation } = annotation.state;
|
||||
const csToolsEvents = Enums.Events;
|
||||
|
||||
const { DefaultHistoryMemo } = csUtils.HistoryMemo;
|
||||
|
||||
const initMeasurementService = (
|
||||
measurementService,
|
||||
displaySetService,
|
||||
@@ -354,7 +356,7 @@ const connectMeasurementServiceToTools = ({
|
||||
const { MEASUREMENT_REMOVED, MEASUREMENTS_CLEARED, MEASUREMENT_UPDATED, RAW_MEASUREMENT_ADDED } =
|
||||
measurementService.EVENTS;
|
||||
|
||||
measurementService.subscribe(MEASUREMENTS_CLEARED, ({ measurements }) => {
|
||||
measurementService.subscribe(MEASUREMENTS_CLEARED, ({ measurements, trackingContext }) => {
|
||||
if (!Object.keys(measurements).length) {
|
||||
return;
|
||||
}
|
||||
@@ -373,6 +375,29 @@ const connectMeasurementServiceToTools = ({
|
||||
options: { deleting: true },
|
||||
});
|
||||
}
|
||||
|
||||
// If tracking context was provided, push a memo that keeps XState in sync with
|
||||
// Cornerstone's annotation history across unlimited undo/redo cycles:
|
||||
// undo → re-populate trackedStudy/trackedSeries so the panel reflects the
|
||||
// restored annotations.
|
||||
// redo → wipe trackedStudy/trackedSeries because the annotations have been
|
||||
// re-deleted by their own Cornerstone memos; no measurements are
|
||||
// deleted here (they are already gone), so CLEAR_TRACKING_CONTEXT is
|
||||
// used instead of UNTRACK_ALL to avoid a double-delete.
|
||||
if (trackingContext) {
|
||||
DefaultHistoryMemo.push({
|
||||
id: csUtils.uuidv4(),
|
||||
operationType: 'trackingState',
|
||||
restoreMemo(undo?: boolean) {
|
||||
if (undo === true) {
|
||||
commandsManager.run('restoreTrackedSeries', trackingContext);
|
||||
} else if (undo === false) {
|
||||
commandsManager.run('clearTrackedSeries');
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
commandsManager.run('endRecordingForAnnotationGroup');
|
||||
|
||||
// trigger a render
|
||||
|
||||
@@ -101,6 +101,7 @@ function ViewerHeader({ appConfig }: withAppTypes<{ appConfig: AppTypes.Config }
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="hover:bg-muted"
|
||||
data-cy="undo-btn"
|
||||
onClick={() => {
|
||||
commandsManager.run('undo');
|
||||
}}
|
||||
@@ -110,6 +111,7 @@ function ViewerHeader({ appConfig }: withAppTypes<{ appConfig: AppTypes.Config }
|
||||
<Button
|
||||
variant="ghost"
|
||||
className="hover:bg-muted"
|
||||
data-cy="redo-btn"
|
||||
onClick={() => {
|
||||
commandsManager.run('redo');
|
||||
}}
|
||||
|
||||
+21
-4
@@ -159,7 +159,13 @@ function TrackedMeasurementsContextProvider(
|
||||
}
|
||||
},
|
||||
clearAllMeasurements: (ctx, evt) => {
|
||||
measurementService.clearMeasurements();
|
||||
const trackingContext = evt.trackedStudy
|
||||
? { StudyInstanceUID: evt.trackedStudy, SeriesInstanceUIDs: [...(evt.trackedSeries || [])] }
|
||||
: undefined;
|
||||
measurementService.clearMeasurements(
|
||||
undefined,
|
||||
trackingContext ? { trackingContext } : undefined
|
||||
);
|
||||
measurementService.setIsMeasurementDeletedIndividually(false);
|
||||
},
|
||||
clearDisplaySetHydratedState: (ctx, evt) => {
|
||||
@@ -250,7 +256,7 @@ function TrackedMeasurementsContextProvider(
|
||||
simplifiedAndLoadSR: (ctx, evt, condMeta) => {
|
||||
return (
|
||||
appConfig?.measurementTrackingMode === measurementTrackingMode.SIMPLIFIED &&
|
||||
evt.data.isBackupSave === false
|
||||
evt.data?.isBackupSave === false
|
||||
);
|
||||
},
|
||||
hasDirtyAndSimplified: (ctx, evt, condMeta) => {
|
||||
@@ -369,11 +375,22 @@ function TrackedMeasurementsContextProvider(
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
// The command needs to be bound to the context's sendTrackedMeasurementsEvent
|
||||
// so the command has to be registered in a React component.
|
||||
// These commands are bound to the context's sendTrackedMeasurementsEvent, so they have
|
||||
// to be registered from inside this (React) component. Re-registration on each
|
||||
// render is safe — registerCommand overwrites by key.
|
||||
commandsManager.registerCommand('DEFAULT', 'loadTrackedSRMeasurements', {
|
||||
commandFn: props => sendTrackedMeasurementsEvent('HYDRATE_SR', props),
|
||||
});
|
||||
commandsManager.registerCommand('DEFAULT', 'restoreTrackedSeries', {
|
||||
commandFn: ({ StudyInstanceUID, SeriesInstanceUIDs }) =>
|
||||
sendTrackedMeasurementsEvent('SET_TRACKED_SERIES', {
|
||||
StudyInstanceUID,
|
||||
SeriesInstanceUIDs,
|
||||
}),
|
||||
});
|
||||
commandsManager.registerCommand('DEFAULT', 'clearTrackedSeries', {
|
||||
commandFn: () => sendTrackedMeasurementsEvent('CLEAR_TRACKING_CONTEXT'),
|
||||
});
|
||||
}, [commandsManager, sendTrackedMeasurementsEvent]);
|
||||
|
||||
return (
|
||||
|
||||
+11
@@ -43,6 +43,9 @@ const machineConfiguration = {
|
||||
idle: {
|
||||
entry: 'clearContext',
|
||||
on: {
|
||||
// No-op: idle entry already cleared context. Redo can still send
|
||||
// CLEAR_TRACKING_CONTEXT (tracking memo); required when strict mode is on.
|
||||
CLEAR_TRACKING_CONTEXT: {},
|
||||
TRACK_SERIES: [
|
||||
{
|
||||
target: 'promptLabelAnnotation',
|
||||
@@ -137,6 +140,14 @@ const machineConfiguration = {
|
||||
],
|
||||
},
|
||||
],
|
||||
// Redo of measurement clear: annotations are already deleted via Cornerstone memo;
|
||||
// this only clears XState tracking context. UNTRACK_ALL would clear measurements again.
|
||||
CLEAR_TRACKING_CONTEXT: [
|
||||
{
|
||||
target: 'tracking',
|
||||
actions: ['clearContext', 'setIsDirtyToClean', 'clearDisplaySetHydratedState'],
|
||||
},
|
||||
],
|
||||
SET_TRACKED_SERIES: [
|
||||
{
|
||||
target: 'tracking',
|
||||
|
||||
@@ -13,8 +13,7 @@ import {
|
||||
import { useTrackedMeasurements } from '../getContextModule';
|
||||
import { UntrackSeriesModal } from './PanelStudyBrowserTracking/untrackSeriesModal';
|
||||
|
||||
const { filterMeasurementsBySeriesUID, filterAny } =
|
||||
utils.MeasurementFilters;
|
||||
const { filterMeasurementsBySeriesUID, filterAny } = utils.MeasurementFilters;
|
||||
|
||||
function PanelMeasurementTableTracking(props) {
|
||||
const [viewportGrid] = useViewportGrid();
|
||||
@@ -23,28 +22,26 @@ function PanelMeasurementTableTracking(props) {
|
||||
|
||||
const [trackedMeasurements, sendTrackedMeasurementsEvent] = useTrackedMeasurements();
|
||||
const { trackedStudy, trackedSeries } = trackedMeasurements.context;
|
||||
const measurementFilter = trackedStudy
|
||||
? filterMeasurementsBySeriesUID(trackedSeries)
|
||||
: filterAny;
|
||||
const measurementFilter = trackedStudy ? filterMeasurementsBySeriesUID(trackedSeries) : filterAny;
|
||||
|
||||
const onUntrackConfirm = () => {
|
||||
sendTrackedMeasurementsEvent('UNTRACK_ALL', {});
|
||||
sendTrackedMeasurementsEvent('UNTRACK_ALL', { trackedStudy, trackedSeries });
|
||||
};
|
||||
|
||||
const onDelete = () => {
|
||||
const hasDirtyMeasurements = measurementService
|
||||
.getMeasurements()
|
||||
.some(measurement => measurement.isDirty);
|
||||
hasDirtyMeasurements
|
||||
? uiModalService.show({
|
||||
title: 'Untrack Study',
|
||||
content: UntrackSeriesModal,
|
||||
contentProps: {
|
||||
onConfirm: onUntrackConfirm,
|
||||
message: 'Are you sure you want to untrack study and delete all measurements?',
|
||||
},
|
||||
})
|
||||
: onUntrackConfirm();
|
||||
const hasMeasurements = measurementService.getMeasurements().length > 0;
|
||||
if (hasMeasurements) {
|
||||
uiModalService.show({
|
||||
title: 'Untrack Study',
|
||||
content: UntrackSeriesModal,
|
||||
contentProps: {
|
||||
onConfirm: onUntrackConfirm,
|
||||
message: 'Are you sure you want to untrack study and delete all measurements?',
|
||||
},
|
||||
});
|
||||
} else {
|
||||
onUntrackConfirm();
|
||||
}
|
||||
};
|
||||
|
||||
const EmptyComponent = () => (
|
||||
|
||||
Reference in new issue
Block a user