-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed two exceptions on cvat-canvas often occuring on app.cvat.ai #8067
Conversation
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe recent update resolves a Firefox-specific issue involving exceptions during window resizing while dragging or resizing shapes on the canvas. To achieve this, new private properties and methods have been introduced to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (3)
- changelog.d/20240621_133156_sekachev.bs_fixed_two_issues_canvas.md (1 hunks)
- changelog.d/20240621_133257_sekachev.bs_fixed_two_issues_canvas.md (1 hunks)
- cvat-canvas/src/typescript/canvasView.ts (11 hunks)
Files skipped from review due to trivial changes (2)
- changelog.d/20240621_133156_sekachev.bs_fixed_two_issues_canvas.md
- changelog.d/20240621_133257_sekachev.bs_fixed_two_issues_canvas.md
Additional comments not posted (1)
cvat-canvas/src/typescript/canvasView.ts (1)
86-87
: Addition of private properties for draggable and resizable shapes.The new properties
draggableShape
andresizableShape
have been correctly declared as private with appropriate types. This encapsulation ensures that these properties are only accessible within theCanvasViewImpl
class, which is a good practice for maintaining the integrity of the object's state.
} | ||
}) | ||
.on('resizedone', (): void => { | ||
if (aborted) { | ||
return; | ||
} | ||
|
||
onResizeEnd(); | ||
this.resizableShape = null; | ||
if (state.shapeType === 'skeleton') { | ||
let { rotation } = (shape as SVG.G).transform(); | ||
// be sure, that rotation in range [0; 360] | ||
while (rotation < 0) rotation += 360; | ||
rotation %= 360; | ||
this.mode = Mode.IDLE; | ||
|
||
if (resized) { | ||
if (rotation) { | ||
this.onEditDone(state, state.points, rotation); | ||
} else { | ||
const points: number[] = []; | ||
|
||
state.elements.forEach((element: any) => { | ||
const elementShape = (shape as SVG.G).children() | ||
.find((child: SVG.Shape) => ( | ||
child.id() === `cvat_canvas_shape_${element.clientID}` | ||
)); | ||
|
||
if (elementShape) { | ||
points.push(...this.translateFromCanvas( | ||
readPointsFromShape(elementShape), | ||
)); | ||
} | ||
}); | ||
|
||
this.onEditDone(state, points, rotation); | ||
} | ||
|
||
this.canvas.dispatchEvent( | ||
new CustomEvent('canvas.resizeshape', { | ||
bubbles: false, | ||
cancelable: true, | ||
detail: { | ||
id: state.clientID, | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (resized) { | ||
let rotation = shape.transform().rotation || 0; | ||
|
||
// be sure, that rotation in range [0; 360] | ||
while (rotation < 0) rotation += 360; | ||
rotation %= 360; | ||
|
||
// these points does not take into account possible transformations, applied on the element | ||
// so, if any (like rotation) we need to map them to canvas coordinate space | ||
let points = readPointsFromShape(shape); | ||
|
||
// let's keep current points, but they could be rewritten in updateObjects | ||
this.drawnStates[state.clientID].points = this.translateFromCanvas(points); | ||
this.drawnStates[state.clientID].rotation = rotation; | ||
if (rotation) { | ||
points = this.translatePointsFromRotatedShape(shape, points); | ||
} | ||
|
||
this.onEditDone(state, this.translateFromCanvas(points), rotation); | ||
this.canvas.dispatchEvent( | ||
new CustomEvent('canvas.resizeshape', { | ||
bubbles: false, | ||
cancelable: true, | ||
detail: { | ||
id: state.clientID, | ||
}, | ||
}), | ||
); | ||
} | ||
}).on('resizeabort', () => { | ||
onResizeEnd(); | ||
aborted = true; | ||
this.resizableShape = null; | ||
// disable internal drag events of SVG.js | ||
window.dispatchEvent(new MouseEvent('mouseup')); | ||
}); | ||
} else { | ||
if (this.resizableShape === shape) { | ||
resizableInstance.fire('resizeabort'); | ||
} | ||
|
||
(shape as any).off('resizestart'); | ||
(shape as any).off('resizing'); | ||
(shape as any).off('resizedone'); | ||
(shape as any).off('resizeabort'); | ||
(shape as any).resize('stop'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review of the resizable
method implementation.
The resizable
method is designed to manage the resize functionality of shapes. Similar to the draggable
method, it is well-implemented but could benefit from the following improvements:
- Refactoring for clarity: The method is lengthy and handles multiple responsibilities. Refactoring into smaller methods could enhance clarity and maintainability.
- Consistency in error handling: As with the draggable method, adding error handling could prevent runtime errors from unhandled exceptions.
- Use of hard-coded values: The method uses hard-coded values for
snapToGrid
andsnapToAngle
. These could be externalized as configurable parameters.
+ // Suggest breaking down into smaller methods
+ private initializeResizableInstance() {...}
+ private handleResizeStart() {...}
+ private handleResizing() {...}
+ private handleResizeEnd() {...}
+ // Add error handling
+ try {
+ // existing code
+ } catch (error) {
+ console.error("Failed to handle resize event:", error);
+ }
Committable suggestion was skipped due to low confidence.
}).on('dragmove', (e: CustomEvent): void => { | ||
onDragMove(); | ||
if (state.shapeType === 'skeleton') { | ||
const { instance } = e.target as any; | ||
const [x, y] = [instance.x(), instance.y()]; | ||
const prevXtl = +draggableInstance.attr('data-xtl'); | ||
const prevYtl = +draggableInstance.attr('data-ytl'); | ||
|
||
for (const child of (shape as SVG.G).children()) { | ||
if (child.type === 'circle') { | ||
const childClientID = child.attr('data-client-id'); | ||
if (state.elements.find((el: any) => el.clientID === childClientID).lock || false) { | ||
continue; | ||
} | ||
child.center(child.cx() - prevXtl + x, child.cy() - prevYtl + y); | ||
} | ||
} | ||
|
||
draggableInstance.attr('data-xtl', x); | ||
draggableInstance.attr('data-ytl', y); | ||
draggableInstance.attr('data-xbr', x + instance.width()); | ||
draggableInstance.attr('data-ybr', y + instance.height()); | ||
|
||
setupSkeletonEdges(shape as SVG.G, makeSVGFromTemplate(state.label.structure.svg)); | ||
} | ||
}).on('dragend', (e: CustomEvent): void => { | ||
if (aborted) { | ||
return; | ||
} | ||
|
||
onDragEnd(); | ||
this.draggableShape = null; | ||
const p1 = e.detail.handler.startPoints.point; | ||
const p2 = e.detail.p; | ||
const dx2 = (p1.x - p2.x) ** 2; | ||
const dy2 = (p1.y - p2.y) ** 2; | ||
if (Math.sqrt(dx2 + dy2) > 0) { | ||
if (state.shapeType === 'mask') { | ||
const { points } = state; | ||
const x = Math.trunc(shape.x()) - this.geometry.offset; | ||
const y = Math.trunc(shape.y()) - this.geometry.offset; | ||
points.splice(-4); | ||
points.push(x, y, x + shape.width() - 1, y + shape.height() - 1); | ||
this.onEditDone(state, points); | ||
} else if (state.shapeType === 'skeleton') { | ||
const points = []; | ||
state.elements.forEach((element: any) => { | ||
const elementShape = (shape as SVG.G).children() | ||
.find((child: SVG.Shape) => ( | ||
child.id() === `cvat_canvas_shape_${element.clientID}` | ||
)); | ||
|
||
if (elementShape) { | ||
points.push(...this.translateFromCanvas(readPointsFromShape(elementShape))); | ||
} | ||
}); | ||
this.onEditDone(state, points); | ||
} else { | ||
// these points does not take into account possible transformations, applied on the element | ||
// so, if any (like rotation) we need to map them to canvas coordinate space | ||
let points = readPointsFromShape(shape); | ||
const { rotation } = shape.transform(); | ||
if (rotation) { | ||
points = this.translatePointsFromRotatedShape(shape, points); | ||
} | ||
|
||
this.onEditDone(state, this.translateFromCanvas(points)); | ||
} | ||
|
||
this.canvas.dispatchEvent( | ||
new CustomEvent('canvas.dragshape', { | ||
bubbles: false, | ||
cancelable: true, | ||
detail: { | ||
id: state.clientID, | ||
}, | ||
}), | ||
); | ||
} | ||
}).on('dragabort', (): void => { | ||
onDragEnd(); | ||
this.draggableShape = null; | ||
aborted = true; | ||
// disable internal drag events of SVG.js | ||
window.dispatchEvent(new MouseEvent('mouseup')); | ||
}); | ||
} else { | ||
shape.removeClass('cvat_canvas_shape_draggable'); | ||
|
||
if (this.draggableShape === shape) { | ||
draggableInstance.fire('dragabort'); | ||
} | ||
|
||
draggableInstance.off('dragstart'); | ||
draggableInstance.off('dragmove'); | ||
draggableInstance.off('dragend'); | ||
draggableInstance.off('dragabort'); | ||
(draggableInstance as any).draggable(false); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review of the draggable
method implementation.
The draggable
method has been added to handle the drag functionality. This method is well-structured and covers various scenarios including different shape types and drag events. However, there are a few points to consider:
- Error handling: There is no error handling for the operations within the event listeners. Adding error handling could improve robustness.
- Complexity: The method is quite long and handles multiple aspects of the drag functionality. Consider breaking it down into smaller, more focused methods to improve readability and maintainability.
- Hard-coded values: The method contains hard-coded values (e.g.,
snapToGrid: 1
). It might be beneficial to define these as constants or configurable properties to enhance flexibility.
+ // Consider adding error handling inside event listeners
+ try {
+ // existing code
+ } catch (error) {
+ console.error("Failed to handle drag event:", error);
+ }
+ // Suggest breaking down into smaller methods
+ private initializeDraggableInstance() {...}
+ private handleDragStart() {...}
+ private handleDragMove() {...}
+ private handleDragEnd() {...}
Committable suggestion was skipped due to low confidence.
Quality Gate passedIssues Measures |
Motivation and context
Additionally, strongly reduced code duplication
How has this been tested?
Checklist
develop
branch(cvat-canvas,
cvat-core,
cvat-data and
cvat-ui)
License
Feel free to contact the maintainers if that's a concern.
Summary by CodeRabbit
Bug Fixes
New Features