Skip to content
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

[PE-56] regression: image aspect ratio fix #5792

Merged
merged 3 commits into from
Oct 18, 2024
Merged

Conversation

Palanikannan1437
Copy link
Collaborator

@Palanikannan1437 Palanikannan1437 commented Oct 10, 2024

Description

Fixing a regression issues that's causing old image's aspect ratio to be 1.

Summary by CodeRabbit

  • New Features

    • Improved handling of image dimensions and aspect ratios in the Custom Image Block component.
    • Aspect ratio now defaults to null, enhancing flexibility during image loading and resizing.
  • Bug Fixes

    • Updated logic ensures attributes are only modified when there is a discrepancy in aspect ratios, reducing unnecessary updates.
  • Refactor

    • Enhanced rendering logic for conditional application of aspect ratio styles based on image dimensions.
    • Clarified variable names for better readability and maintenance.

Copy link
Contributor

coderabbitai bot commented Oct 10, 2024

Walkthrough

The changes made in this pull request focus on the CustomImageBlock component within the image-block.tsx file. The primary modification involves the destructuring of image attributes for clarity, renaming properties to nodeWidth, nodeHeight, and nodeAspectRatio. The default value of aspectRatio is now null, and the calculation logic has been adjusted accordingly. Additionally, the rendering logic and effect dependencies have been updated to reflect these changes, enhancing the management of image attributes and resizing behavior.

Changes

File Change Summary
packages/editor/src/core/extensions/custom-image/components/image-block.tsx - Renamed destructured attributes to nodeWidth, nodeHeight, nodeAspectRatio.
- Default of aspectRatio changed from 1 to null in state initialization.
- Updated aspect ratio calculation to aspectRatioCalculated.
- Adjusted conditional logic to use new variable names for width and aspect ratio.
- Updated dependencies for useEffect and useLayoutEffect hooks.
- Modified rendering logic to apply styles conditionally based on size.aspectRatio.
- Updated types for aspectRatio in ImageAttributes and Size to `number

Possibly related PRs

Suggested labels

✍️editor

Suggested reviewers

  • SatishGandham
  • aaryan610
  • rahulramesha

Poem

🐇 In the garden where images bloom,
Aspect ratios now find room.
With a tweak here and a change there,
Our images dance with style and flair!
So let’s hop and cheer with glee,
For a block that’s better, just wait and see! 🌼


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
packages/editor/src/core/extensions/custom-image/components/image-block.tsx (1)

219-219: Use Explicit Null Checks When Applying Aspect Ratio Style

In the style objects on lines 219 and 245, using (size.aspectRatio && { aspectRatio: size.aspectRatio }) may inadvertently skip an aspectRatio of 0 due to its falsy value in JavaScript. While an aspectRatio of 0 is invalid, for clarity and to prevent potential bugs, consider using explicit null checks.

Apply this diff:

-            ...(size.aspectRatio && { aspectRatio: size.aspectRatio }),
+            ...(size.aspectRatio != null && { aspectRatio: size.aspectRatio }),

Also applies to: 245-245

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1e1733f and 1cfefda.

📒 Files selected for processing (1)
  • packages/editor/src/core/extensions/custom-image/components/image-block.tsx (5 hunks)
🧰 Additional context used
🔇 Additional comments (1)
packages/editor/src/core/extensions/custom-image/components/image-block.tsx (1)

65-65: LGTM on Aspect Ratio Initialization

Setting aspectRatio to aspectRatio || null ensures that it defaults to null when not provided. This change aligns with the goal of resolving the regression issue where older images had their aspect ratio incorrectly set to 1.

@Palanikannan1437 Palanikannan1437 changed the title regression: image aspect ratio fix [PE-56] regression: image aspect ratio fix Oct 10, 2024
@Palanikannan1437 Palanikannan1437 self-assigned this Oct 10, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
packages/editor/src/core/extensions/custom-image/components/image-block.tsx (1)

107-117: Improved aspect ratio calculation and naming

The changes in this section enhance the accuracy and clarity of aspect ratio calculations:

  • Renaming aspectRatio to aspectRatioCalculated clearly distinguishes between the calculated value and the stored attribute.
  • Using nodeWidth in the condition is consistent with the earlier renaming.
  • The calculation of initialHeight and setting of aspectRatio in initialComputedSize now correctly use the calculated aspect ratio.

These modifications directly address the PR objective of fixing the aspect ratio regression.

Consider adding a check to prevent division by zero when calculating aspectRatioCalculated:

-    const aspectRatioCalculated = img.naturalWidth / img.naturalHeight;
+    const aspectRatioCalculated = img.naturalHeight !== 0 ? img.naturalWidth / img.naturalHeight : null;
+    if (aspectRatioCalculated === null) {
+      console.error("Invalid image dimensions: height is zero");
+      return;
+    }

This addition would improve the robustness of the code by handling potential edge cases.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5117541 and 378d407.

📒 Files selected for processing (1)
  • packages/editor/src/core/extensions/custom-image/components/image-block.tsx (6 hunks)
🧰 Additional context used
🪛 Biome
packages/editor/src/core/extensions/custom-image/components/image-block.tsx

[error] 62-62: Shouldn't redeclare 'remoteImageSrc'. Consider to delete it or rename it.

'remoteImageSrc' is defined here:

(lint/suspicious/noRedeclare)

🔇 Additional comments (6)
packages/editor/src/core/extensions/custom-image/components/image-block.tsx (6)

62-67: Improved clarity and handling of image attributes

The changes in destructuring node.attrs and initializing the size state are well-implemented:

  • Renaming properties to nodeWidth, nodeHeight, and nodeAspectRatio improves code clarity by distinguishing between node attributes and local state.
  • Setting the default aspectRatio to null is more appropriate, as it correctly represents the absence of a value rather than assuming a square aspect ratio.

These modifications align well with the PR objective of fixing the aspect ratio regression for older images.

🧰 Tools
🪛 Biome

[error] 62-62: Shouldn't redeclare 'remoteImageSrc'. Consider to delete it or rename it.

'remoteImageSrc' is defined here:

(lint/suspicious/noRedeclare)


Line range hint 127-140: Crucial fix for aspect ratio handling in older images

These changes are essential for addressing the aspect ratio regression issue:

  • The updated condition ensures that the aspect ratio is correctly set for older images or when there's a mismatch between stored and calculated values.
  • Using aspectRatioCalculated in the newSize object ensures consistency with the newly calculated aspect ratio.
  • Adding nodeAspectRatio to the effect dependencies ensures that the effect runs when the aspect ratio changes, keeping the component reactive to aspect ratio updates.

These modifications directly fulfill the PR objective of fixing the aspect ratio regression for older images.


146-149: Improved synchronization between node attributes and component state

These changes enhance the component's reactivity to size changes:

  • Using nodeWidth and nodeHeight in the setSize function ensures that the component's size state accurately reflects the node's attributes.
  • Updating the effect dependencies to include nodeWidth and nodeHeight ensures that the effect runs when these attributes change, maintaining proper synchronization.

These modifications contribute to the overall goal of correctly handling image dimensions and aspect ratios.


221-221: Improved handling of aspect ratio in container styling

This change enhances the robustness of the component's styling:

  • The conditional spread operator ensures that the aspectRatio style is only applied when size.aspectRatio is not null.
  • This prevents potential issues with invalid aspect ratio values being applied to the container, which could lead to unexpected rendering behavior.

This modification aligns well with the earlier changes to handle aspect ratios more carefully and contributes to the overall goal of correctly managing image dimensions.


247-247: Consistent aspect ratio handling for image element

This change maintains consistency in aspect ratio handling:

  • The conditional spread operator ensures that the aspectRatio style is only applied to the image element when size.aspectRatio is not null.
  • This approach is consistent with the similar change applied to the container element (line 221).
  • It prevents potential styling issues that could arise from applying invalid aspect ratio values to the image.

This modification contributes to the overall robustness of the component's styling and aspect ratio management.


Line range hint 1-292: Overall assessment: Well-implemented fix for image aspect ratio regression

This pull request effectively addresses the regression issue affecting the aspect ratio of older images. The changes implemented in the CustomImageBlock component demonstrate a thorough approach to solving the problem:

  1. Improved clarity in attribute and state management.
  2. More accurate and robust aspect ratio calculations.
  3. Proper handling of aspect ratios for both new and existing images.
  4. Consistent application of aspect ratio styles to both container and image elements.

The code changes are well-structured, maintain consistency, and align perfectly with the PR objectives. The suggested minor improvements (error handling for aspect ratio calculation and removal of a redundant declaration) will further enhance the robustness of the implementation.

Great job on this fix! Once the minor suggestions are addressed, this PR will be ready for merging.

@@ -59,12 +59,12 @@ export const CustomImageBlock: React.FC<CustomImageBlockProps> = (props) => {
src: remoteImageSrc,
setEditorContainer,
} = props;
const { width, height, aspectRatio } = node.attrs;
const { src: remoteImageSrc, width: nodeWidth, height: nodeHeight, aspectRatio: nodeAspectRatio } = node.attrs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove redundant destructuring of 'remoteImageSrc'

There's a redundant destructuring of 'remoteImageSrc' on this line, which has already been destructured from the props earlier in the component.

To resolve this, please remove 'remoteImageSrc' from the destructuring statement:

- const { src: remoteImageSrc, width: nodeWidth, height: nodeHeight, aspectRatio: nodeAspectRatio } = node.attrs;
+ const { width: nodeWidth, height: nodeHeight, aspectRatio: nodeAspectRatio } = node.attrs;

This change will eliminate the redeclaration and improve code clarity.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { src: remoteImageSrc, width: nodeWidth, height: nodeHeight, aspectRatio: nodeAspectRatio } = node.attrs;
const { width: nodeWidth, height: nodeHeight, aspectRatio: nodeAspectRatio } = node.attrs;
🧰 Tools
🪛 Biome

[error] 62-62: Shouldn't redeclare 'remoteImageSrc'. Consider to delete it or rename it.

'remoteImageSrc' is defined here:

(lint/suspicious/noRedeclare)

@pushya22 pushya22 merged commit 81550ab into preview Oct 18, 2024
12 of 15 checks passed
@pushya22 pushya22 deleted the fix/img-aspect-ratio branch October 18, 2024 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants