Skip to content

Conversation

@ShivamBarot2602
Copy link

@ShivamBarot2602 ShivamBarot2602 commented Apr 22, 2025

Description:

  1. Added logic to support custom crop buttons in the toolbar.
  2. Updated components to handle custom crop presets and render custom crop buttons accordingly.
  3. Ensured compatibility with existing crop tools.

Summary by CodeRabbit

  • New Features

    • Introduced a dynamic crop presets toolbar for quick selection of common aspect ratios (1:1, 4:5, 16:9, 4:3, 9:16) in the image editor.
    • Added new translation labels for crop aspect ratios.
    • Enhanced crop tool configuration with an option to enable dynamic crop buttons.
  • Improvements

    • Toolbar adapts contextually, showing crop presets when editing in the "ADJUST" tab.
    • Toolbar filtering and tool selection logic improved for a streamlined user experience.

@coderabbitai
Copy link

coderabbitai bot commented Apr 22, 2025

Walkthrough

The changes introduce dynamic crop presets to the image editor's crop tool. A new CustomCropButtons React component is added to handle crop ratio selection and related actions. The ToolsBar component is enhanced to support dynamic crop buttons and an upper toolbar variant, with logic to display these buttons based on the crop tool's configuration. Translations for common crop ratios are included, and the configuration typings are updated to support a new dynamicButtons property for crops. The main application component is updated to conditionally render the dynamic crop toolbar when appropriate.

Changes

File(s) Change Summary
src/actions/selectTool.js Refactored tool selection logic to always return a new state object, conditionally updating dynamicCropToolId and handling dynamicButtons in the payload.
src/components/App/index.jsx Updated to import TABS_IDS, destructure tabId and Crop, and conditionally render a dynamic crop toolbar above the main canvas when in the "ADJUST" tab and crop dynamic buttons are enabled; also pass dynamicButtons to the main toolbar.
src/components/ToolsBar/index.jsx Enhanced with dynamicButtons and upperToolbar props, logic to display custom crop buttons (via CustomCropButtons), conditional tool filtering excluding crop tool when dynamicButtons is true, updated tool selection dispatch, and updated PropTypes and defaultProps.
src/components/tools/Crop/CustomCropButtons.jsx New component to render crop preset buttons, handle crop ratio changes, tool selection, resizing, and zooming; includes prop types.
src/context/defaultTranslations.js Added translation keys for common crop aspect ratios (e.g., "Square 1:1", "16:9", "4:3", "9:16", "Portrait 4:5").
src/index.d.ts Typings updated: clarified type aliases with parentheses, reformatted Watermark properties for clarity, added optional dynamicButtons to crop config, and reformatted lockCropAreaAt union type for readability.
public/demo-config.js Added (commented-out) sample config lines for dynamicButtons and ratio under crop tool settings.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App
    participant ToolsBar
    participant CustomCropButtons
    participant Store

    User->>App: Open "ADJUST" tab
    App->>ToolsBar: Render with dynamicButtons/upperToolbar if Crop.dynamicButtons enabled
    ToolsBar->>CustomCropButtons: Render crop preset buttons
    User->>CustomCropButtons: Click crop preset button
    CustomCropButtons->>Store: Dispatch selectTool (crop) and setCropRatio
    CustomCropButtons->>Store: Optionally dispatch autoResize and zoomCanvas
Loading

Poem

🥕
A toolbar sprouts above the field,
With crop shapes new, their power revealed!
Buttons for squares, for widescreens too,
Each click reshapes the image view.
The bunny hops, adjusts with glee—
Dynamic crops for all to see!
🌱


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 08ec0ba and be52513.

📒 Files selected for processing (3)
  • packages/react-filerobot-image-editor/src/actions/selectTool.js (1 hunks)
  • packages/react-filerobot-image-editor/src/components/ToolsBar/index.jsx (3 hunks)
  • packages/react-filerobot-image-editor/src/components/tools/Crop/CustomCropButtons.jsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/react-filerobot-image-editor/src/actions/selectTool.js
  • packages/react-filerobot-image-editor/src/components/ToolsBar/index.jsx
  • packages/react-filerobot-image-editor/src/components/tools/Crop/CustomCropButtons.jsx

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this 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

@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: 5

🧹 Nitpick comments (2)
packages/react-filerobot-image-editor/src/components/ToolsBar/index.jsx (1)

63-76: Incomplete dependency list for items memo

items depends on upperToolbar, allPresets, and the translation function t, yet none of them are in the dependency list.
React will therefore re‑render with stale values if any of these change.

-  }, [tabTools, currentToolId, dynamicCropToolId]);
+  }, [
+    tabTools,
+    currentToolId,
+    dynamicCropToolId,
+    upperToolbar,
+    allPresets,
+    t,
+  ]);
packages/react-filerobot-image-editor/src/components/tools/Crop/CustomCropButtons.jsx (1)

89-93: Selection condition is fragile and hard‑to‑read

  • Comparing currentRatio to the literal string "Crop" seems incorrect (the ratio is numeric / 'original' / 'custom').
  • Re‑computing ratio via toPrecisedFloat(width/height) on every render is unnecessary.

Consider simplifying:

-        isSelected ||
-        (currentRatio === 'Crop' && item.ratio === 'custom') ||
-        currentRatio ===
-          (item.ratio ?? toPrecisedFloat(item.width / item.height))
+        isSelected ||
+        currentRatio === (item.ratio ?? toPrecisedFloat(item.width / item.height))

This keeps the semantics and removes the confusing 'Crop' comparison.
Optionally memoise the derived ratio for performance.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3d198dd and 08ec0ba.

📒 Files selected for processing (7)
  • packages/react-filerobot-image-editor/src/actions/selectTool.js (1 hunks)
  • packages/react-filerobot-image-editor/src/components/App/index.jsx (4 hunks)
  • packages/react-filerobot-image-editor/src/components/ToolsBar/index.jsx (3 hunks)
  • packages/react-filerobot-image-editor/src/components/tools/Crop/CustomCropButtons.jsx (1 hunks)
  • packages/react-filerobot-image-editor/src/context/defaultTranslations.js (1 hunks)
  • packages/react-filerobot-image-editor/src/index.d.ts (3 hunks)
  • public/demo-config.js (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/react-filerobot-image-editor/src/actions/selectTool.js (2)
packages/react-filerobot-image-editor/src/components/ToolsBar/index.jsx (1)
  • selectTool (41-50)
packages/react-filerobot-image-editor/src/actions/updateState.js (1)
  • payload (7-10)
packages/react-filerobot-image-editor/src/index.d.ts (1)
packages/react-filerobot-image-editor/src/components/tools/Watermark/Watermark.jsx (1)
  • loadAndSetWatermarkImg (138-159)
🪛 Biome (1.9.4)
packages/react-filerobot-image-editor/src/index.d.ts

[error] 276-276: void is confusing inside a union type.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

🔇 Additional comments (9)
packages/react-filerobot-image-editor/src/context/defaultTranslations.js (1)

116-120: Added translations for custom crop presets

These new translation keys provide text labels for the custom crop aspect ratio options, which is a nice touch for internationalization support. The naming is clear and follows the existing pattern in the file.

public/demo-config.js (1)

176-177: Placeholder configuration for new dynamic buttons feature

These commented out options show how to enable the new dynamic crop buttons feature. The configuration is well-placed within the existing crop tool settings and provides a good reference for future users.

packages/react-filerobot-image-editor/src/actions/selectTool.js (1)

3-19: Improved state handling for dynamic crop buttons

The refactored selectTool function now properly handles dynamic crop button selection, even when the same tool is selected multiple times. This is necessary to support changing crop ratios without changing the currently selected tool.

A few observations:

  1. The code uses optional chaining (payload?.dynamicButtons) which is good for safety
  2. The function now consistently returns a new state object which helps with reactivity
  3. The spread operator is used correctly to only add properties when needed
packages/react-filerobot-image-editor/src/components/App/index.jsx (5)

6-6: Import TABS_IDS for conditional toolbar rendering

Added import for TABS_IDS which will be used for conditional rendering of the dynamic crop toolbar.


53-54: Added state and config access for dynamic toolbar

Extracting tabId from store and Crop from config to enable context-aware dynamic crop buttons. This provides the necessary information to conditionally render the toolbar.

Also applies to: 71-72


340-348: Added conditional dynamic crop toolbar

This is the core UI implementation of the custom crop buttons feature. The conditional rendering ensures the toolbar only appears when:

  1. Crop's dynamicButtons is enabled
  2. We're not in canvas-only mode
  3. The current tab is the ADJUST tab

The additional toolbar is positioned above the canvas, providing a good UX for crop operations.


350-355: Added dynamic buttons support to main toolbar

Updated the main toolbar to also receive the dynamic buttons flag, ensuring consistency between both toolbars. This follows the pattern of passing the same prop to both toolbar instances.


340-355:

❓ Verification inconclusive

Verify mobile responsiveness of dual toolbars

With the addition of a second toolbar in some cases, it's important to ensure the UI remains responsive on mobile devices.


🏁 Script executed:

#!/bin/bash
# Check if there are any specific mobile or responsive styles for the toolbars

# Look for responsive styles related to toolbars
echo "Checking for responsive styles for toolbars..."
rg -A 5 "isPhoneScreen|@media|responsive|mobile" --type jsx --type css packages/react-filerobot-image-editor/src/components/ToolsBar/

Length of output: 266


🏁 Script executed:

#!/bin/bash
# List all JSX and CSS files in the ToolsBar component directory
echo "Files under ToolsBar/:"
find packages/react-filerobot-image-editor/src/components/ToolsBar -type f \( -iname "*.jsx" -o -iname "*.css" \) -print

# Search for responsive or mobile-specific styling in JS/JSX
echo -e "\nSearching JS/JSX for isPhoneScreen, mobile, tablet, or responsive flags..."
rg -n "isPhoneScreen|mobile|tablet|responsive" packages/react-filerobot-image-editor/src/components/ToolsBar

# Search CSS for media queries
echo -e "\nSearching CSS for @media queries..."
rg -n "@media" packages/react-filerobot-image-editor/src/components/ToolsBar

Length of output: 3838


Verify mobile rendering of dual toolbars

We inspected packages/react-filerobot-image-editor/src/components/ToolsBar/* and confirmed that responsiveness is handled via the isPhoneScreen prop and Styled Components rather than CSS media queries:

  • ToolsBar/index.jsx accepts and passes down isPhoneScreen.
  • ToolsBar.styled.js applies conditional styles based on isPhoneScreen.
  • ToolsBarItemButton.jsx uses usePhoneScreen(320) to adjust icon sizes and spacing.

No CSS @media rules were found, as all responsive behavior is JS‑driven. Please manually test the dual‑toolbar layout on various mobile screen sizes to ensure both upper and lower toolbars render correctly and remain usable.

packages/react-filerobot-image-editor/src/index.d.ts (1)

291-303: Type declaration looks good

The addition of the dynamicButtons?: boolean; field and the reformatted lockCropAreaAt union accurately reflect the new feature and improve readability. No issues spotted here.

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.

1 participant