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

[lexical-playground] Fix bug: Insert an image inside another image's caption (#6109) #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

local-dev-korbit-ai-mentor[bot]
Copy link

@local-dev-korbit-ai-mentor local-dev-korbit-ai-mentor bot commented Aug 15, 2024

Context:
Now users are able to insert an image/GIF/table etc, into another image's caption.

Solution:
This PR limits what the users can insert into the image caption and its nested editor. Excluding some controls from the toolbar if the selection is inside the nested editor that belongs to the image caption.

Closes #6109

Test plan

  1. Open https://playground.lexical.dev/
  2. Insert a sample image with a caption.
  3. Focus on the caption.
  4. Insert a sample image/table/GIF etc.

Before

before-image-caption.mov

After

image-caption.mov

Description by Korbit AI

What change is being made?

Fix the bug that allows inserting an image inside another image's caption by updating the ImageComponent, ImageNode, and ToolbarPlugin components to handle nested editors correctly.

Why are these changes being made?

This change addresses a bug where images could be inserted into the caption of another image, which is not the intended behavior. By adding checks for nested editors and updating the toolbar functionality, we ensure that the editor behaves as expected and maintains a clean user experience.

Summary by CodeRabbit

  • New Features

    • Enhanced the ImageComponent to support a wider variety of content types in captions, including hashtags and links.
    • Improved the ToolbarPlugin for better contextual awareness, adapting options based on the editing context.
    • Introduced a new function to check if an editor is a nested editor, enhancing editor management capabilities.
  • Bug Fixes

    • Streamlined toolbar options to reduce clutter when editing within an image caption context.

Copy link
Author

Clone of the PR facebook/lexical#6230

Copy link

korbit-ai bot commented Aug 15, 2024

My review is in progress 📖 - I will have feedback for you in a few minutes!

Copy link

coderabbitai bot commented Aug 15, 2024

Walkthrough

The recent updates introduce enhanced functionality to the ImageComponent, ImageNode, and ToolbarPlugin, improving user experience in caption editing and toolbar interactions. Key changes include expanded node support for captions, refined editor initialization, and contextual toolbar options based on editing state. Additionally, a new utility function allows for determining nested editor status, enriching the overall capability and flexibility of the Lexical framework.

Changes

Files Change Summary
.../lexical-playground/src/nodes/ImageComponent.tsx
.../lexical-playground/src/nodes/ImageNode.tsx
Enhancements to the ImageComponent and ImageNode for improved caption editing. Expanded initialNodes support in captions and refined initialization logic for the editor.
.../lexical-playground/src/plugins/ToolbarPlugin/index.tsx Updates to ToolbarPlugin for improved contextual awareness, introducing new state variables and conditional rendering based on editing context to streamline the UI.
.../lexical-utils/src/index.ts Added $isEditorIsNestedEditor function to check if an editor is nested, enhancing module functionality for managing editor hierarchies.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ToolbarPlugin
    participant ImageComponent
    participant ImageNode

    User->>ToolbarPlugin: Interacts with toolbar
    ToolbarPlugin->>ImageComponent: Checks editing context
    ImageComponent->>ImageNode: Loads caption with nodes
    ToolbarPlugin->>User: Updates available options
Loading

🐰 In the fields so wide and green,
Our captions now can gleam and beam.
With links and hashtags we delight,
The toolbar dances, oh what a sight!
Nested editors, oh what a treat,
With joy, we hop and skip our beat! 🌸


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

I have reviewed your code and found 3 potential issues.

Comment on lines +156 to +160
this.__caption =
caption ||
createEditor({
nodes: [],
});
Copy link

Choose a reason for hiding this comment

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

category Functionality

The change to initialize the caption editor with an empty 'nodes' array might unintentionally restrict the types of content that can be added to image captions. Consider if there are specific node types that should be allowed in captions and include them in the 'nodes' array. For example, if you want to allow text formatting in captions, you might need to include text nodes and formatting nodes.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines +610 to +612
export function $isEditorIsNestedEditor(editor: LexicalEditor): boolean {
return editor._parentEditor !== null;
}
Copy link

Choose a reason for hiding this comment

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

category Functionality

The function name '$isEditorIsNestedEditor' is redundant and grammatically incorrect. Consider renaming it to '$isNestedEditor' for better clarity and consistency with Lexical naming conventions. The function implementation itself is correct.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines 674 to 684
useEffect(() => {
return editor.registerCommand(
SELECTION_CHANGE_COMMAND,
(_payload, newEditor) => {
$updateToolbar();
setActiveEditor(newEditor);
$updateToolbar();
return false;
},
COMMAND_PRIORITY_CRITICAL,
);
}, [editor, $updateToolbar]);
Copy link

Choose a reason for hiding this comment

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

category Functionality

The toolbar state is not being updated correctly when switching between the main editor and nested editors (like image captions). Consider updating the toolbar state whenever the active editor changes, not just on selection changes. This can be done by adding a useEffect hook that listens for changes to the activeEditor state.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

@furwellness
Copy link
Owner

/review

Copy link

codiumai-pr-agent-free bot commented Aug 16, 2024

PR Reviewer Guide 🔍

(Review updated until commit 1769e5e)

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Key issues to review

Code Complexity
The addition of multiple node types and the configuration of the LexicalNestedComposer in the ImageComponent significantly increases the complexity. Consider simplifying or breaking down the configuration.

Default Value Handling
The handling of default values for caption in ImageNode constructor is modified to include an empty nodes array. Ensure this change aligns with expected behavior across all usages of ImageNode.

Conditional Rendering
The logic for rendering toolbar options based on isImageCaption state could be prone to errors if not handled correctly. Ensure that all dependencies are properly managed and that the state updates do not cause unnecessary re-renders or UI inconsistencies.

Copy link

Persistent review updated to latest commit 1769e5e

@furwellness furwellness changed the base branch from cloned_main_ba9e8 to main August 21, 2024 17:05
@furwellness
Copy link
Owner

@coderabbitai full review

Copy link

coderabbitai bot commented Aug 21, 2024

Actions performed

Full review triggered.

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4cbb85f and 1769e5e.

Files selected for processing (4)
  • packages/lexical-playground/src/nodes/ImageComponent.tsx (4 hunks)
  • packages/lexical-playground/src/nodes/ImageNode.tsx (1 hunks)
  • packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx (9 hunks)
  • packages/lexical-utils/src/index.ts (1 hunks)
Additional comments not posted (10)
packages/lexical-playground/src/nodes/ImageNode.tsx (1)

156-160: Skip similar comment.

The change to initialize the caption editor with an empty 'nodes' array might unintentionally restrict the types of content that can be added to image captions. This has been addressed in previous comments.

packages/lexical-playground/src/nodes/ImageComponent.tsx (1)

18-19: LGTM! Verify the usage of new node types.

The inclusion of HashtagNode, LinkNode, EmojiNode, and KeywordNode enhances the flexibility of the caption editor.

Ensure that these new node types are correctly integrated and utilized within the application.

Run the following script to verify the usage of new node types:

Also applies to: 45-49, 66-68

Verification successful

New node types are integrated and utilized in the codebase.

The new node types HashtagNode, LinkNode, EmojiNode, and KeywordNode are present and used across multiple files, including plugins and tests. This indicates they are integrated into the application.

  • HashtagNode: Found in packages/lexical-text/src/registerLexicalTextEntity.ts and packages/lexical-react/src/LexicalHashtagPlugin.ts.
  • LinkNode: Extensively used in various files such as packages/lexical-playground/src/plugins/ContextMenuPlugin/index.tsx and packages/lexical-react/src/LexicalLinkPlugin.ts.
  • EmojiNode: Used in examples/vanilla-js-plugin/src/emoji-plugin/EmojiPlugin.ts and packages/lexical-playground/src/plugins/EmojisPlugin/index.ts.
  • KeywordNode: Found in packages/lexical-playground/src/plugins/KeywordsPlugin/index.ts.

These nodes are being registered with editors and used in transformations, confirming their integration.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of new node types in the codebase.

# Test: Search for the usage of new node types. Expect: Occurrences of the new node types.
rg --type ts -A 5 $'HashtagNode|LinkNode|EmojiNode|KeywordNode'

Length of output: 131491

packages/lexical-utils/src/index.ts (1)

610-612: Skip similar comment.

The function name $isEditorIsNestedEditor is redundant and grammatically incorrect. This has been addressed in previous comments.

packages/lexical-playground/src/plugins/ToolbarPlugin/index.tsx (7)

549-549: LGTM: Addition of isImageCaption state.

The addition of the isImageCaption state is appropriate for tracking the context of image captions.


554-563: LGTM: Update to $updateToolbar function.

The changes to the $updateToolbar function correctly handle the context of nested editors and update the isImageCaption state.


686-690: LGTM: Addition of useEffect hook for $updateToolbar.

The useEffect hook ensures the toolbar updates according to the active editor's state, enhancing context awareness.


861-862: LGTM: Addition of computed variables for toolbar visibility.

The computed variables canViewerSeeInsertDropdown and canViewerSeeInsertCodeButton are correctly used to conditionally render toolbar options.


973-985: LGTM: Conditional rendering of insert code button.

The conditional rendering of the insert code button based on canViewerSeeInsertCodeButton is appropriate for the intended functionality.


1064-1232: LGTM: Conditional rendering of insert dropdown.

The conditional rendering of the insert dropdown based on canViewerSeeInsertDropdown enhances usability by reducing clutter.


Line range hint 895-930: LGTM: Replacement of editor prop with activeEditor.

The use of activeEditor ensures consistent handling of the editor's state, which is crucial for toolbar actions.

@furwellness
Copy link
Owner

Zero comments from coderabbit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant