Skip to content

Conversation

SinghaAnirban005
Copy link
Contributor

@SinghaAnirban005 SinghaAnirban005 commented Jul 27, 2025

What does this PR do?

This PR resolves the issue in the Editor component where users upon updating text inside the editor, the text get duplicated and appended to the very same text.
For eg -> user typing "i am a dev" after updation woukld become --> "i am a devi am a dev"

Visual Demo (For contributors especially)

Screencast.from.2025-07-27.14-30-41.webm

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. N/A
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Are there environment variables that should be set?
  • What are the minimal test data to have?
  • What is expected (happy path) to have (input and output)?
  • Any other important info that could help to test that PR

@SinghaAnirban005 SinghaAnirban005 requested review from a team as code owners July 27, 2025 10:10
Copy link

vercel bot commented Jul 27, 2025

@SinghaAnirban005 is attempting to deploy a commit to the cal Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Jul 27, 2025

Walkthrough

The changes focus on the ToolbarPlugin component and its test suite. In the main component file, the logic for initializing the editor and handling updates when the updateTemplate prop changes was refactored and consolidated. The initialization process now clears the editor's root and inserts new content based on the latest template, ensuring previous content is not duplicated. The update listener logic was also improved for more reliable cleanup and state handling. In the test file, two new tests were added to verify that the editor properly reinitializes and does not duplicate content when updateTemplate changes. Minor test cleanups and clarifying comments were also introduced.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Complexity: Moderate. The changes involve non-trivial refactoring of editor initialization and update logic, as well as the addition of targeted test cases to verify specific behaviors related to template updates.
  • Scope: The changes are focused on a single component and its test file, but the logic is central to correct editor behavior and requires careful review to ensure correctness and regression prevention.
  • Estimated review time: Approximately 20 minutes, accounting for understanding the refactored effect, the new initialization function, the update listener registration/cleanup, and the thoroughness of the new tests.

Assessment against linked issues

Objective Addressed Explanation
Prevent duplication of content when updateTemplate prop changes in ToolbarPlugin (#22758, CAL-6158)
Ensure editor reinitializes and calls setText properly when updateTemplate changes (#22758, CAL-6158)
Verify that previous editor content is cleared and not concatenated on template update (#22758, CAL-6158)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes detected. All code modifications relate directly to the objectives of fixing content duplication on template updates and improving related test coverage.

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 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 0c3a683 and 28241fb.

📒 Files selected for processing (2)
  • packages/ui/components/editor/plugins/ToolbarPlugin.test.tsx (3 hunks)
  • packages/ui/components/editor/plugins/ToolbarPlugin.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/ui/components/editor/plugins/ToolbarPlugin.tsx
  • packages/ui/components/editor/plugins/ToolbarPlugin.test.tsx
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Install dependencies / Yarn install & cache
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests 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.

@graphite-app graphite-app bot added the community Created by Linear-GitHub Sync label Jul 27, 2025
@github-actions github-actions bot added the 🐛 bug Something isn't working label Jul 27, 2025
Copy link

graphite-app bot commented Jul 27, 2025

Graphite Automations

"Add community label" took an action on this PR • (07/27/25)

1 label was added to this PR based on Keith Williams's automation.

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

🧹 Nitpick comments (1)
packages/ui/components/editor/plugins/ToolbarPlugin.tsx (1)

376-392: Simplify the update listener registration logic.

The condition if (!unregisterUpdateListener) at line 376 is always true since the variable is initialized to null at line 345. This conditional check appears redundant.

Consider simplifying by removing the conditional:

-    if (!unregisterUpdateListener) {
-      unregisterUpdateListener = editor.registerUpdateListener(({ editorState, prevEditorState }) => {
+    unregisterUpdateListener = editor.registerUpdateListener(({ editorState, prevEditorState }) => {
        editorState.read(() => {
          const textInHtml = $generateHtmlFromNodes(editor).replace(/&lt;/g, "<").replace(/&gt;/g, ">");
          const cleanedHtml = textInHtml.replace(
            /<p\s+class="editor-paragraph"[^>]*>\s*<br>\s*<\/p>/g,
            "<p class='editor-paragraph'></p>"
          );

          props.setText(cleanedHtml);
        });

        if (!prevEditorState._selection) {
          editor.blur();
        }
      });
-    }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ee073cd and f17d3ab.

📒 Files selected for processing (2)
  • packages/ui/components/editor/plugins/ToolbarPlugin.test.tsx (3 hunks)
  • packages/ui/components/editor/plugins/ToolbarPlugin.tsx (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

Flag excessive Day.js use in performance-critical code. Functions like .add, .diff, .isBefore, and .isAfter are slow, especially in timezone mode. Prefer .utc() for better performance. Where possible, replace with native Date and direct .valueOf() comparisons for faster execution. Recommend using native methods or Day.js .utc() consistently in hot paths like loops.

Files:

  • packages/ui/components/editor/plugins/ToolbarPlugin.test.tsx
  • packages/ui/components/editor/plugins/ToolbarPlugin.tsx
🔇 Additional comments (5)
packages/ui/components/editor/plugins/ToolbarPlugin.test.tsx (3)

194-224: Good test improvements for clarity and isolation.

The added comments and mock clearing improve test readability and ensure proper isolation of the update behavior being tested.


253-291: Well-structured test for editor reinitialization.

This test effectively validates that the editor reinitializes when updateTemplate changes to true, which is crucial for the duplication fix.


293-339: Excellent test coverage for the duplication bug fix.

This test directly validates the core issue by ensuring content is replaced rather than duplicated. The regex pattern check effectively catches any duplication scenarios.

packages/ui/components/editor/plugins/ToolbarPlugin.tsx (2)

347-372: Correct implementation of the duplication fix.

The root.clear() call at line 354 is the key fix that prevents content duplication by ensuring old content is removed before inserting new content. The fallback handling for empty content and insertion failures is also well-implemented.


344-403: Well-structured effect with proper cleanup and dependencies.

The refactored useEffect correctly consolidates initialization and update logic, properly cleans up listeners, and has the right dependency to trigger reinitialization when updateTemplate changes.

@SinghaAnirban005 SinghaAnirban005 force-pushed the bug/editor branch 4 times, most recently from ef9eabe to 0c3a683 Compare July 29, 2025 13:00
@SinghaAnirban005
Copy link
Contributor Author

@anikdhabal
Kindly review and let me know
Thank You

Copy link
Contributor

@anikdhabal anikdhabal left a comment

Choose a reason for hiding this comment

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

It only happens in the development environment due to React’s double rendering. So the current approach isn't correct

@kart1ka kart1ka marked this pull request as draft August 20, 2025 18:18
@anikdhabal
Copy link
Contributor

Closing due to inactivity

@anikdhabal anikdhabal closed this Aug 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working community Created by Linear-GitHub Sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rich text editor duplicates content on template updates
3 participants