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

[WEB-2528] fix: side menu rendering even if created already #5687

Merged
merged 2 commits into from
Sep 24, 2024

Conversation

Palanikannan1437
Copy link
Collaborator

@Palanikannan1437 Palanikannan1437 commented Sep 24, 2024

Description

  1. Side menu is rendering multiple times even though it's already created.
  2. Fixed drag handle positioning for all editors

Summary by CodeRabbit

  • New Features
    • Enhanced the CustomImageBlock component with updated styling for improved visual consistency.
  • Bug Fixes
    • Improved the SideMenu component to prevent duplication of AI handle and drag handle views, ensuring a cleaner user interface.
    • Streamlined element matching in the drag-handle functionality for better performance.
  • Style
    • Adjusted CSS for drag-and-drop elements, impacting their visual rendering and alignment.

Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

Walkthrough

The changes involve modifications across several components within the editor. Key updates include altering the className of an image component, adding conditional checks in the SideMenu to prevent duplicate views, removing a function related to DOM node selection in the ai-handle, and streamlining the image detection logic in the drag-handle. Additionally, a CSS property affecting horizontal positioning was removed, which may impact visual rendering.

Changes

File Change Summary
packages/editor/src/core/extensions/... Modified className in CustomImageBlock to include "image-component".
packages/editor/src/core/extensions/... Added checks in SideMenu to prevent multiple initializations of aiHandleView and dragHandleView.
packages/editor/src/core/plugins/ai-handle.ts Removed nodeDOMAtCoords function, affecting DOM interactions for node selection.
packages/editor/src/plugins/drag-handle.ts Updated nodeDOMAtCoords to streamline element matching, removing nested image checks.
packages/editor/src/styles/drag-drop.css Removed transform: translateX(-50%); property from CSS, affecting element positioning.

Possibly related PRs

  • [WEB-2390] fix: Clickable Area for Issue List Layout Item #5536: The changes in the main PR involve modifying a class name in a React component, while the retrieved PR also deals with structural changes in a React component, specifically enhancing the clickable area, which may relate to how components are styled and interacted with.

Suggested labels

🐛bug, 🌟enhancement, 🌐frontend

Suggested reviewers

  • anmolsinghbhatia
  • SatishGandham

Poem

In the side menu, oh what a sight,
Handles added with care, just right.
No duplicates here, all neat and fine,
A tidy space where features align.
Hopping with joy, we celebrate,
A cleaner code, oh, isn’t it great? 🐇✨


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

Outside diff range and nitpick comments (3)
packages/editor/src/core/extensions/side-menu.tsx (3)

81-83: Approve: Conditional initialization of AI handle

This change effectively prevents multiple initializations of the AI handle, addressing the issue described in the PR. Good job!

Consider caching the result of editorSideMenu.querySelector("#ai-handle") to avoid repeated DOM queries:

const aiHandle = editorSideMenu.querySelector("#ai-handle");
if (handlesConfig.ai && !aiHandle) {
  aiHandleView(view, editorSideMenu);
}

This minor optimization could improve performance, especially if this code is executed frequently.


85-87: Approve: Conditional initialization of drag handle

This change successfully prevents multiple initializations of the drag handle, addressing the issue described in the PR. Well done!

Similar to the AI handle, consider caching the result of the querySelector to optimize performance:

const dragHandle = editorSideMenu.querySelector("#drag-handle");
if (handlesConfig.dragDrop && !dragHandle) {
  dragHandleView(view, editorSideMenu);
}

This minor optimization could improve performance, especially if this code is executed frequently.


Line range hint 41-157: Consider optimizing handle initialization

The current implementation checks for the existence of handles on every mousemove event. While this works, it may be more efficient to perform these checks once during the plugin's initialization.

Consider refactoring the SideMenu plugin to initialize the handles only once:

const SideMenu = (options: SideMenuPluginProps) => {
  const { handlesConfig } = options;
  const editorSideMenu: HTMLDivElement | null = document.createElement("div");
  editorSideMenu.id = "editor-side-menu";
  
  // ... (other code)

  let aiHandleInitialized = false;
  let dragHandleInitialized = false;

  return new Plugin({
    key: new PluginKey("sideMenu"),
    view: (view) => {
      hideSideMenu();
      view?.dom.parentElement?.appendChild(editorSideMenu);
      
      // Initialize handles once
      if (handlesConfig.ai && !aiHandleInitialized) {
        aiHandleView(view, editorSideMenu);
        aiHandleInitialized = true;
      }
      
      if (handlesConfig.dragDrop && !dragHandleInitialized) {
        dragHandleView(view, editorSideMenu);
        dragHandleInitialized = true;
      }

      // ... (rest of the code)
    },
    props: {
      handleDOMEvents: {
        mousemove: (view, event) => {
          // ... (existing mousemove logic)
          
          // Remove handle checks from here
          showSideMenu();
          if (handlesConfig.dragDrop) {
            dragHandleDOMEvents?.mousemove();
          }
          if (handlesConfig.ai) {
            aiHandleDOMEvents?.mousemove?.();
          }
        },
        // ... (other event handlers)
      },
    },
  });
};

This approach would initialize the handles only once when the plugin is created, potentially improving performance by avoiding repeated DOM queries on every mousemove event.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8dabe83 and bd379f3.

Files selected for processing (1)
  • packages/editor/src/core/extensions/side-menu.tsx (1 hunks)

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 (2)
packages/editor/src/core/plugins/ai-handle.ts (1)

Multiple Usages of nodeDOMAtCoords Still Present in Codebase

The nodeDOMAtCoords function is still being used in several files, which will cause runtime errors if not addressed.

Affected Files:

  • packages/editor/src/core/plugins/ai-handle.ts
  • packages/editor/src/core/plugins/drag-handle.ts
  • packages/editor/src/core/extensions/side-menu.tsx
Analysis chain

Line range hint 71-75: Update handleClick function to handle removal of nodeDOMAtCoords

The nodeDOMAtCoords function has been removed, but it's still being used in the handleClick function. This will cause a runtime error. The functionality of nodeDOMAtCoords needs to be replaced or reimplemented.

Please update the handleClick function to handle the removal of nodeDOMAtCoords. This may involve reimplementing the functionality directly in this function or using an alternative method to determine the node at the given coordinates.

To verify the usage of nodeDOMAtCoords in other parts of the codebase, run the following script:

This will help identify if the function is still being used elsewhere and needs to be updated or if it has been completely removed from the project.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other usages of nodeDOMAtCoords in the codebase

# Test: Search for usages of nodeDOMAtCoords
rg --type typescript 'nodeDOMAtCoords'

Length of output: 73


Script:

#!/bin/bash
# Description: Check for other usages of nodeDOMAtCoords in the codebase

# Test: Search for usages of nodeDOMAtCoords
rg --type ts 'nodeDOMAtCoords'

Length of output: 715

packages/editor/src/core/extensions/custom-image/components/image-block.tsx (1)

Line range hint 148-152: LGTM! Consider documenting the purpose of the new class.

The addition of the "image-component" class to the image element is a good change. It provides a more specific selector for styling or JavaScript operations targeting these image components.

To improve maintainability, consider adding a brief comment explaining the purpose of the "image-component" class, especially if it's used for specific styling or functionality elsewhere in the application.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bd379f3 and a3a3953.

Files selected for processing (5)
  • packages/editor/src/core/extensions/custom-image/components/image-block.tsx (1 hunks)
  • packages/editor/src/core/extensions/side-menu.tsx (2 hunks)
  • packages/editor/src/core/plugins/ai-handle.ts (1 hunks)
  • packages/editor/src/core/plugins/drag-handle.ts (1 hunks)
  • packages/editor/src/styles/drag-drop.css (0 hunks)
Files not reviewed due to no reviewable changes (1)
  • packages/editor/src/styles/drag-drop.css
Files skipped from review as they are similar to previous changes (1)
  • packages/editor/src/core/extensions/side-menu.tsx
Additional comments not posted (2)
packages/editor/src/core/plugins/drag-handle.ts (2)

Line range hint 1-380: Overall assessment of changes

The modifications to the nodeDOMAtCoords function in this file represent an improvement in the handling of image components and element selection for the drag-and-drop functionality. The code is now more streamlined and potentially more performant due to the removal of nested image detection.

However, it's crucial to ensure that these changes don't introduce any unintended side effects, particularly in:

  1. Handling of nested images
  2. Behavior of the drag handle for the first paragraph in the editor
  3. Overall drag-and-drop functionality for various element types

To maintain the integrity of the editor's functionality, I recommend:

  1. Thoroughly testing the drag-and-drop behavior with various content types, especially images and nested structures.
  2. Updating any relevant documentation to reflect the new behavior of element selection.
  3. Reviewing and updating any affected unit or integration tests to cover the new scenarios introduced by these changes.

44-45: Improved selector handling for image components

The addition of .image-component and .image-upload-component to the generalSelectors array is a positive change. It suggests that the editor now has dedicated components for handling images, which is good for maintainability and extensibility.

However, there are a few points to consider:

  1. The removal of nested image detection logic (previously handled by hasNestedImg) might affect how nested images are handled in terms of selection and drag-and-drop operations. Please ensure that this change doesn't introduce any regressions in image handling.

  2. The function now returns the first child <p> tag of the .ProseMirror element. This might change the behavior of the drag handle for the first paragraph in the editor. Verify that this change aligns with the expected editor behavior.

  3. The new check for non-empty <p> tags in table cells (td and th) is a good improvement for usability, as it prevents selection of empty cells.

To ensure these changes don't introduce any regressions, please run the following verification script:

This script will help identify any potential issues related to the removal of hasNestedImg and verify the usage of the new image component selectors across the codebase.

@@ -2,45 +2,12 @@ import { NodeSelection } from "@tiptap/pm/state";
import { EditorView } from "@tiptap/pm/view";
// extensions
import { SideMenuHandleOptions, SideMenuPluginProps } from "@/extensions";
// plugins
import { nodeDOMAtCoords } from "@/plugins/drag-handle";
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 unused import nodeDOMAtCoords

The nodeDOMAtCoords function is imported but not used in this file after the recent changes. To maintain clean code and avoid potential confusion, it's recommended to remove unused imports.

Apply this diff to remove the unused import:

-import { nodeDOMAtCoords } from "@/plugins/drag-handle";
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
import { nodeDOMAtCoords } from "@/plugins/drag-handle";

@Palanikannan1437 Palanikannan1437 changed the title fix: side menu rendering even if created already [WEB-2528] fix: side menu rendering even if created already Sep 24, 2024
@pushya22 pushya22 merged commit 20e5692 into preview Sep 24, 2024
14 of 15 checks passed
@pushya22 pushya22 deleted the fix/side-menu-rendering branch September 24, 2024 14:42
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.

4 participants