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

fix(table): fix merge row show error #435

Conversation

cycleccc
Copy link
Collaborator

@cycleccc cycleccc commented Dec 15, 2024

Changes Overview

Implementation Approach

Testing Done

Verification Steps

Additional Notes

Checklist

  • I have created a changeset for this PR if necessary.
  • My changes do not break the library.
  • I have added tests where applicable.
  • I have followed the project guidelines.
  • I have fixed any lint issues.

Related Issues

Summary by CodeRabbit

  • New Features
    • Improved cell merging functionality in the table module, enhancing the user experience during table edits.
  • Bug Fixes
    • Resolved an error related to the display of merged rows in tables, ensuring accurate visual representation.

Copy link
Contributor

coderabbitai bot commented Dec 15, 2024

Walkthrough

The pull request introduces a new private method needKeepCell in the MergeCell class within the table module. This method determines whether a cell should be retained during a merge operation by checking sibling cells and the structure of the parent table. The merge method has been updated to utilize this new method, replacing the previous inline logic for sibling node checks, thus streamlining the merging process.

Changes

File Change Summary
packages/table-module/src/module/menu/MergeCell.ts Added private method needKeepCell to determine cell retention during merge operations. Modified merge method to use needKeepCell.
.changeset/strong-laws-walk.md Introduced a patch for @wangeditor-next/table-module and @wangeditor-next/editor to fix an error related to the display of merged rows in tables.

Poem

🐰 In the table's embrace, where cells intertwine,
A method named needKeepCell makes logic align.
With each merge and hop, the rows find their place,
A dance of data, a harmonious space!
Code flows like a river, so tidy and neat,
Celebrating changes, a rabbit's sweet treat! 🥕✨

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. This feature will be included in our Pro Plan when released.


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

🧹 Nitpick comments (1)
packages/table-module/src/module/menu/MergeCell.ts (1)

21-30: Consider extracting table structure validation logic

The needKeepCell method currently handles both table structure validation and cell visibility decisions. Consider extracting the table structure validation into a separate service or utility class for better separation of concerns and easier testing.

This would allow:

  1. Reuse of table structure validation logic across different operations
  2. Easier unit testing of validation rules
  3. Better maintainability as table structure rules evolve
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d298d36 and 3d5ccfa.

📒 Files selected for processing (1)
  • packages/table-module/src/module/menu/MergeCell.ts (2 hunks)
🔇 Additional comments (1)
packages/table-module/src/module/menu/MergeCell.ts (1)

125-128: Verify merge behavior with different table configurations

The implementation looks correct, but we should verify the merge behavior in various scenarios:

  • Tables with different sizes
  • Merging cells across multiple rows/columns
  • Edge cases with already merged cells

Let's analyze the usage patterns:

✅ Verification successful

The previous search had issues with the file type. Let's try a more comprehensive search with specific file patterns and context to understand the merge cell implementation and its test coverage.


Let's analyze the implementation details of the merge functionality and related code to better understand the behavior and type safety.


We need to examine the complete implementation and type definitions to understand how the merge functionality handles different table configurations and cell states.


Implementation verified for table cell merging

The implementation is correct and handles various table configurations safely:

  1. The needKeepCell method properly checks both row and column context:
    • Verifies sibling cells in the same row
    • Checks for other rows in the parent table
  2. The merge operation is type-safe with proper TableCellElement interface
  3. The hidden property is consistently used across the codebase:
    • In merge operations
    • In HTML parsing
    • In test cases
  4. The implementation includes proper validation through canMerge method that prevents invalid merges:
    • Empty selections
    • Cross-section merges (thead/tbody/tfoot)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for test cases or example usage of the merge functionality
rg -l "MergeCell.*merge" --type ts

# Look for existing tests related to cell merging
rg -l "describe.*merge.*cell" --type test

Length of output: 109


Script:

#!/bin/bash
# Search for test files with different extensions
rg -l "describe.*merge.*cell" -g "*.test.*" -g "*.spec.*"

# Look for merge cell implementations and usages
rg "MergeCell" -A 5 --type ts

# Search for test scenarios or documentation mentioning merge cells
rg "merge.*cell" -i -A 3 --type md

Length of output: 3129


Script:

#!/bin/bash
# Let's examine the implementation details of the merge functionality
ast-grep --pattern 'class MergeCell {
  $$$
  needKeepCell($$$) {
    $$$
  }
  $$$
}'

# Look for related split cell implementation for comparison
rg "class SplitCell" -A 10 --type ts

# Search for table cell element type definitions
ast-grep --pattern 'interface TableCellElement {
  $$$
}'

Length of output: 986


Script:

#!/bin/bash
# Let's examine the full implementation of MergeCell class
rg "class MergeCell" -A 50 --type ts

# Look for table cell type definitions in different locations
rg "type.*TableCell.*=|interface.*TableCell" -A 5 --type ts

# Search for usage of hidden property in table cells
rg "hidden.*:.*true" -A 3 --type ts

Length of output: 5926

Comment on lines +21 to +30
private needKeepCell(editor: Editor, trPath: Path): boolean {
// 检查同行是否有其他单元格
const [, rowSibling] = Node.children(editor, trPath)

// 检查同列是否有其他单元格
const parentTable = Editor.parent(editor, trPath)
const hasOtherRows = parentTable[0].children.length > 1

return !!rowSibling || hasOtherRows
}
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

Fix misleading comment and potential edge cases

Several issues need attention in this method:

  1. The comment on line 25 is incorrect - it's checking for other rows, not columns
  2. The row sibling check doesn't handle potential errors from Node.children
  3. The row count check includes the current row, which might lead to incorrect behavior

Consider this improved implementation:

 private needKeepCell(editor: Editor, trPath: Path): boolean {
   // 检查同行是否有其他单元格
-  const [, rowSibling] = Node.children(editor, trPath)
+  let hasRowSibling = false
+  try {
+    const [, rowSibling] = Node.children(editor, trPath)
+    hasRowSibling = !!rowSibling
+  } catch (e) {
+    // Handle case where row has no children
+  }

-  // 检查同列是否有其他单元格
+  // 检查是否有其他行
   const parentTable = Editor.parent(editor, trPath)
-  const hasOtherRows = parentTable[0].children.length > 1
+  const currentRowIndex = trPath[trPath.length - 1]
+  const hasOtherRows = parentTable[0].children.length > 1 && 
+    (currentRowIndex < parentTable[0].children.length - 1)

-  return !!rowSibling || hasOtherRows
+  return hasRowSibling || hasOtherRows
 }

Committable suggestion skipped: line range outside the PR's diff.

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)
.changeset/strong-laws-walk.md (1)

1-6: Please provide a more detailed changeset description.

The current description "fix table merge row show error" is too brief. Since this is a bug fix affecting multiple packages, please include:

  1. A clear description of the bug that was fixed
  2. How it was fixed (mention the new needKeepCell method and changes to merge logic)
  3. The relationship between the table-module and editor package changes
  4. Any potential migration steps or breaking changes

This will help users understand the impact of these changes when reading the changelog.

Example format:

---
'@wangeditor-next/table-module': patch
'@wangeditor-next/editor': patch
---

fix: table merge row display error

- Fixed an issue where merged table rows were not displaying correctly
- Added new cell retention logic via `needKeepCell` method
- Refactored merge operation to improve reliability
- No breaking changes or migration steps required
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3d5ccfa and 776012a.

📒 Files selected for processing (1)
  • .changeset/strong-laws-walk.md (1 hunks)

@cycleccc cycleccc merged commit 230e46d into master Dec 15, 2024
2 checks passed
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.

Bug:表格在只有一列时合并单元格获取的 HTML 会多出来一列
1 participant