fix: Resolve the issue of sub canvas drift#4051
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
|
||
| initDefaultShortcut(lf.value, lf.value.graphModel) | ||
| lf.value.graphModel.get_provide = (node: any, graph: any) => { | ||
| return { |
There was a problem hiding this comment.
The code contains several irregularities and improvements that should be considered:
-
Scope Issues: The
HtmlPointToCanvasPointfunction is trying to access thetransformModelproperties of thelf.value.nodeModel.graphModel, which might not exist if these values are accessed outside its intended scope. -
Function Redundancy: The line
props.nodeModel.graphModel.transformModel.props = graphModelTransformProps;seems redundant given it's already assigning the same value. It needs further review or simplification based on actual requirements. -
Potential Security Risks: The code does not include checks for security risks such as input sanitization when handling potentially untrusted data like HTML points.
-
Optimization Suggestions:
- If
scaleX,scaleY,translateX, andtranslateYare constant throughout rendering, consider declaring them once as variables before callingHtmlPointToCanvasPoint. - Use functional programming practices wherever possible, especially with arrow functions and template literals, to improve readability and maintainability. For example, using
(scaleFactor * currentScale) / anotherScaleinstead of multiplying twice could make the logic clearer.
- If
Here is an improved version of the code incorporating some of these optimizations:
const renderGraphData = (data?: any) => {
// Existing code...
}
function HtmlPointToCanvasPoint(point: { x: number, y: number }): { x: number, y: number } | null {
if (!point || !point.x || !point.y) {
return null;
}
const transform = lf.value.graphModel.transformModel;
let scale = transform.SCALE ?? 1;
let translate = transform.TRANSLATE ?? { x: 0, y: 0 };
const canvasPoint = [
((point.x - translate.x) / scale),
((point.y - translate.y) / scale),
];
return canvasPoint;
}
// Bind the function properly without overriding existing prototype property
if (!('HtmlPointToCanvasPoint' in lf.value.graphModel.transformModel)) {
Object.assign(lf.value.graphModel.transformModel, { HtmlPointToCanvasPoint });
}
lf.value.graphModel.transformModel.HtmlPointToCanvasPoint = HtmlPointToCanvasPoint.bind(
lf.value.graphModel.transformModel,
);
initDefaultShortcut(lf.value, lf.value.graphModel);In this revised version:
- Added type checking for
pointparameters inHtmlPointToCanvasPoint. - Simplified scaling operations within the function.
- Ensured proper binding of the function to
transformModelwithout overwriting any existing method.
fix: Resolve the issue of sub canvas drift