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): fill in the hidden tablecell #343

Merged
merged 2 commits into from
Nov 13, 2024

Conversation

cycleccc
Copy link
Owner

@cycleccc cycleccc commented Nov 13, 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

  • Bug Fixes

    • Resolved an issue with hidden table cells in the editor, ensuring proper management during operations.
  • Improvements

    • Enhanced table cell handling for better parsing and structure, particularly with cell spans in the editor.

@cycleccc cycleccc linked an issue Nov 13, 2024 that may be closed by this pull request
Copy link

changeset-bot bot commented Nov 13, 2024

⚠️ No Changeset found

Latest commit: c6c755a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Nov 13, 2024

Walkthrough

The changes in this pull request focus on enhancing the handling of table functionality within the @wangeditor/editor and @wangeditor-next/table-module packages. Specifically, modifications were made to the parseRowHtml function to improve the management of hidden table cells, ensuring that they are filled appropriately during operations. Additionally, some variable naming was standardized for consistency throughout the codebase.

Changes

File Path Change Summary
.changeset/famous-dolls-shave.md Introduced patches for @wangeditor/editor and @wangeditor-next/table-module, fixing hidden table cell handling.
packages/table-module/src/module/parse-elem-html.ts Modified parseRowHtml to initialize tableCellChildren, ensuring correct handling of table-cell elements and hidden cells. Renamed colspan to colSpan.

Possibly related PRs

🐇 In the editor's domain,
Hidden cells now find their home,
With spans and rows aligned just right,
Our tables shine, a joyful sight!
So hop along, let’s celebrate,
The changes made, 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: 2

🧹 Outside diff range and nitpick comments (3)
.changeset/famous-dolls-shave.md (1)

6-6: Consider enhancing the commit message description

While the message indicates the fix, it would be more helpful to include:

  • What was the specific issue with hidden table cells
  • How it was addressed (e.g., "Added support for creating hidden cells when colSpan > 1")
  • Impact on users/behavior

Consider expanding it to:

-fix(table): fill in the hidden table cell
+fix(table): properly handle hidden cells for tables with colspan
+
+When a table cell has colspan > 1, ensure hidden cells are properly
+created and filled, maintaining correct table structure. This fixes
+issues with table rendering and manipulation when using colspan.
packages/table-module/src/module/parse-elem-html.ts (2)

66-67: Add validation for colSpan value

The colSpan value should be validated to prevent potential issues with invalid values.

Add validation:

      // @ts-ignore
-      const colSpan = child.colSpan || 1
+      const colSpan = Math.max(1, Math.floor(child.colSpan || 1))

139-144: Consider extracting the default width as a named constant

The default width of 180 is hardcoded. Consider extracting it as a named constant for better maintainability and documentation.

Add at the top of the file:

+ const DEFAULT_CELL_WIDTH = 180; // Width in pixels for table cells

Then update the width calculation:

-      const width = parseInt(getStyleValue($(td), 'width') || '180', 10)
+      const width = parseInt(getStyleValue($(td), 'width') || String(DEFAULT_CELL_WIDTH), 10)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a6ca7a9 and 9d2e435.

📒 Files selected for processing (2)
  • .changeset/famous-dolls-shave.md (1 hunks)
  • packages/table-module/src/module/parse-elem-html.ts (2 hunks)
🧰 Additional context used
🪛 Biome
packages/table-module/src/module/parse-elem-html.ts

[error] 85-86: Unnecessary continue statement

Unsafe fix: Delete the unnecessary continue statement

(lint/correctness/noUnnecessaryContinue)

🔇 Additional comments (2)
.changeset/famous-dolls-shave.md (1)

1-4: LGTM on version bumps

The patch version bumps are appropriate since this is a bug fix that maintains backward compatibility.

packages/table-module/src/module/parse-elem-html.ts (1)

96-96: LGTM: Proper assignment of processed table cells

The change ensures that only properly processed table cells, including hidden ones, are assigned to the row.

Comment on lines +74 to +82
if (
nextChild
&& DomEditor.getNodeType(nextChild) === 'table-cell'
// @ts-ignore
&& nextChild.style?.display === 'none'
) {
// 已有隐藏的 table-cell,无需补充
continue
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve type safety and simplify condition block

The type assertion and condition block can be improved:

  1. Replace unsafe type assertion with proper type guard
  2. Remove unnecessary continue statement

Apply this diff:

        if (
          nextChild
          && DomEditor.getNodeType(nextChild) === 'table-cell'
-          // @ts-ignore
-          && nextChild.style?.display === 'none'
+          && 'hidden' in nextChild
+          && nextChild.hidden === true
        ) {
-          // 已有隐藏的 table-cell,无需补充
-          continue
-        } else {
+          // Skip if hidden cell exists
+        } else {

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

Comment on lines +58 to +92
const tableCellChildren: TableCellElement[] = []

for (let i = 0; i < children.length; i += 1) {
const child = children[i]

// 确保是 table-cell 类型
if (DomEditor.getNodeType(child) === 'table-cell') {
// @ts-ignore
const colSpan = child.colSpan || 1

tableCellChildren.push(child as TableCellElement) // 先添加当前单元格

// 如果 colSpan > 1,检查是否存在足够的隐藏 table-cell
for (let j = 1; j < colSpan; j += 1) {
const nextChild = children[i + j]

if (
nextChild
&& DomEditor.getNodeType(nextChild) === 'table-cell'
// @ts-ignore
&& nextChild.style?.display === 'none'
) {
// 已有隐藏的 table-cell,无需补充
continue
} else {
// 补齐缺少的隐藏 table-cell
tableCellChildren.push({
type: 'table-cell',
children: [{ text: '' }],
hidden: true,
})
}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Add comprehensive test cases for colSpan scenarios

The current test suite does not cover the following critical scenarios:

  • Table with multiple cells having colSpan > 1
  • Existing hidden cells
  • Missing hidden cells
  • Edge cases: colSpan = 0, negative values, non-numeric values
🔗 Analysis chain

Verify table structure with various colSpan configurations

The changes look good but should be thoroughly tested with different scenarios.

Please verify these test cases:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for existing table-related test files
echo "Searching for table-related test files..."
fd -e test.ts -e spec.ts . | grep -i table

# Search for table-related test cases
echo "Searching for colSpan-related test cases..."
rg -l "colSpan.*table" -t ts

# Ensure test coverage for various scenarios:
echo "Test scenarios to verify:"
cat << EOF
1. Table with single colSpan > 1
2. Table with multiple cells having colSpan > 1
3. Table with existing hidden cells
4. Table with missing hidden cells
5. Edge cases: colSpan = 0, negative values, non-numeric values
EOF

Length of output: 1257


Script:

#!/bin/bash
# List of table-related test files identified previously
TEST_FILES=(
  packages/table-module/__tests__/elem-to-html.test.ts
  packages/table-module/__tests__/menu/delete-col.test.ts
  packages/table-module/__tests__/menu/delete-row.test.ts
  packages/table-module/__tests__/menu/delete-table.test.ts
  packages/table-module/__tests__/menu/full-width.test.ts
  packages/table-module/__tests__/menu/insert-col.test.ts
  packages/table-module/__tests__/menu/insert-row.test.ts
  packages/table-module/__tests__/menu/insert-table.test.ts
  packages/table-module/__tests__/menu/table-header.test.ts
  packages/table-module/__tests__/parse-html.test.ts
  packages/table-module/__tests__/plugin.test.ts
  packages/table-module/__tests__/render-elem.test.ts
)

# Define test scenarios
SCENARIOS=(
  "single colSpan > 1"
  "multiple cells having colSpan > 1"
  "existing hidden cells"
  "missing hidden cells"
  "colSpan = 0"
  "colSpan < 0"
  "non-numeric colSpan"
)

# Search each test file for each scenario
for file in "${TEST_FILES[@]}"; do
  echo "Checking $file for test scenarios..."
  for scenario in "${SCENARIOS[@]}"; do
    rg -i "$scenario" "$file" && echo "$scenario found in $file" || echo "$scenario NOT found in $file"
  done
done

Length of output: 29378

🧰 Tools
🪛 Biome

[error] 85-86: Unnecessary continue statement

Unsafe fix: Delete the unnecessary continue statement

(lint/correctness/noUnnecessaryContinue)

@cycleccc cycleccc merged commit f5d2961 into master Nov 13, 2024
2 checks passed
cycleccc added a commit that referenced this pull request Nov 13, 2024
* fix(table): fill in the hidden tablecell

* Create famous-dolls-shave.md
@coderabbitai coderabbitai bot mentioned this pull request Nov 14, 2024
5 tasks
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: v5.6.10 wps表格插入行报错
1 participant