-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancelAction.ts
More file actions
44 lines (40 loc) · 1.23 KB
/
cancelAction.ts
File metadata and controls
44 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { Draft } from 'immer';
import { assert } from '../../utilities/assert.js';
import { denormalizeBox } from '../../utilities/box.js';
import type { CancelActionPayload, ReactRoiState } from '../roiReducer.js';
export function cancelAction(
draft: Draft<ReactRoiState>,
payload: CancelActionPayload,
/**
* If you want to cancel action for a specific ROI, provide its ID here.
* Otherwise, all ROIs with non-idle actions will be cancelled.
*/
roiId?: string,
) {
const rois = draft.rois.filter((roi) =>
roiId ? roi.id === roiId : roi.action.type !== 'idle',
);
if (rois.length === 0) {
return;
}
for (const roi of rois) {
if (roi.action.type === 'drawing') {
const roiIndex = draft.rois.findIndex((r) => r.id === roi.id);
draft.rois.splice(roiIndex, 1);
if (payload.noUnselection) {
draft.selectedRoi = roi.action.previousSelectedRoi;
} else {
draft.selectedRoi = undefined;
}
} else {
// Revert to the previous state
const committedRoi = draft.committedRois.find((r) => r.id === roi.id);
assert(committedRoi);
roi.box = denormalizeBox(committedRoi);
}
roi.action = {
type: 'idle',
};
}
draft.action = 'idle';
}