Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

fix: LEAP-240: Prevent creating region on clicking outside selected one #1667

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/components/ImageView/ImageView.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ export default observer(
crosshairRef = createRef();
handleDeferredMouseDown = null;
deferredClickTimeout = [];
skipMouseUp = false;
skipNextMouseDown = false;
skipNextClick = false;
skipNextMouseUp = false;
mouseDownPoint = null;

constructor(props) {
Expand All @@ -522,8 +524,8 @@ export default observer(
if (isFF(FF_DEV_1442)) {
this.handleDeferredMouseDown?.();
}
if (this.skipMouseUp) {
this.skipMouseUp = false;
if (this.skipNextClick) {
this.skipNextClick = false;
return;
}

Expand Down Expand Up @@ -575,6 +577,7 @@ export default observer(
const isPanTool = item.getToolsManager().findSelectedTool()?.fullName === 'ZoomPanTool';
const isMoveTool = item.getToolsManager().findSelectedTool()?.fullName === 'MoveTool';

this.skipNextMouseDown = this.skipNextMouseUp = this.skipNextClick = false;
if (isFF(FF_LSDV_4930)) {
this.mouseDownPoint = { x: e.evt.offsetX, y: e.evt.offsetY };
}
Expand Down Expand Up @@ -626,6 +629,10 @@ export default observer(

this.canvasX = left;
this.canvasY = top;
if (this.skipNextMouseDown) {
this.skipNextMouseDown = false;
return true;
}
item.event('mousedown', e, x, y);

return true;
Expand All @@ -652,7 +659,9 @@ export default observer(

const handleDeselection = () => {
item.annotation.unselectAll();
this.skipMouseUp = true;
this.skipNextMouseDown = true;
this.skipNextMouseUp = true;
this.skipNextClick = true;
};

this.handleDeferredClick(handleMouseDown, handleDeselection, eligibleToDeselect);
Expand Down Expand Up @@ -680,7 +689,7 @@ export default observer(

item.freezeHistory();

return item.event('mouseup', e, x - this.canvasX, y - this.canvasY);
return this.triggerMouseUp(e, x - this.canvasX, y - this.canvasY);
};

handleGlobalMouseMove = e => {
Expand All @@ -705,7 +714,17 @@ export default observer(
item.freezeHistory();
item.setSkipInteractions(false);

return item.event('mouseup', e, e.evt.offsetX, e.evt.offsetY);
return this.triggerMouseUp(e, e.evt.offsetX, e.evt.offsetY);
};

triggerMouseUp = (e, x, y) => {
if (this.skipNextMouseUp) {
this.skipNextMouseUp = false;
return;
}
const { item } = this.props;

return item.event('mouseup', e, x, y);
};

handleMouseMove = e => {
Expand Down
32 changes: 32 additions & 0 deletions tests/functional/data/image_segmentation/tools/rect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const rectangleToolAndLabelsConfig = `<View>
<Image name="img" value="$image" />
<Rectangle name="rect" toName="img" />
<Labels name="labels" toName="img">
<Label value="Label 1" background="blue" />
<Label value="Label 2" background="red" />
</Labels>
</View>`;

export const simpleImageData = {
image: 'https://htx-misc.s3.amazonaws.com/opensource/label-studio/examples/images/nick-owuor-astro-nic-visuals-wDifg5xc9Z4-unsplash.jpg',
};

export const simpleRectangleResult = [
{
'id': 'rect_1',
'original_width': 2242,
'original_height': 2802,
'image_rotation': 0,
'value': {
'x': 20,
'y': 20,
'width': 20,
'height': 20,
'rotation': 0,
},
'from_name': 'rect',
'to_name': 'img',
'type': 'rectangle',
'origin': 'manual',
},
];
51 changes: 51 additions & 0 deletions tests/functional/specs/image_segmentation/tools/rect.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ImageView, LabelStudio, Sidebar } from '@heartexlabs/ls-test/helpers/LSF';
import {
rectangleToolAndLabelsConfig,
simpleImageData,
simpleRectangleResult
} from 'data/image_segmentation/tools/rect';
import { FF_DEV_1442 } from '../../../../../src/utils/feature-flags';

describe('Rectangle tool', () => {
it('should not draw rectangle when clicking outside to unselect (FF_DEV_1442 = true)', () => {
LabelStudio.addFeatureFlagsOnPageLoad({
[FF_DEV_1442]: true,
});

LabelStudio.params()
.config(rectangleToolAndLabelsConfig)
.data(simpleImageData)
.withResult(simpleRectangleResult)
.init();

LabelStudio.waitForObjectsReady();

Sidebar.hasRegions(1);
Sidebar.toggleRegionSelection(0);

ImageView.clickAtRelative(0.5, 0.5);
ImageView.clickAtRelative(0.7, 0.7);
Sidebar.hasRegions(1);
});

it('should draw rectangle when clicking outside (FF_DEV_1442 = false)', () => {
LabelStudio.addFeatureFlagsOnPageLoad({
[FF_DEV_1442]: false,
});

LabelStudio.params()
.config(rectangleToolAndLabelsConfig)
.data(simpleImageData)
.withResult(simpleRectangleResult)
.init();

LabelStudio.waitForObjectsReady();

Sidebar.hasRegions(1);
Sidebar.toggleRegionSelection(0);

ImageView.clickAtRelative(0.5, 0.5);
ImageView.clickAtRelative(0.7, 0.7);
Sidebar.hasRegions(2);
});
});
Loading