fix: improve cell selection with fallback strategy for merged cells#323
Conversation
Enhance the cellUnderMouse function to use a two-step fallback approach when resolving cell positions from mouse coordinates: 1. First attempt: Try using mousePos.inside for better accuracy with merged cells (cells with rowspan/colspan) 2. Fallback: If inside position doesn't resolve to a valid cell, fall back to mousePos.pos This fixes edge cases where: - Column-merged cells may not be selectable in certain regions when using only the inside position - The previous single-choice approach (inside OR pos) could fail when cellAround returns null for the chosen position The new strategy ensures more robust cell selection across all scenarios including complex merged cell layouts.
commit: |
Coverage Report
File Coverage |
|
Thanks! Could you please add some tests? I'm particularly interested in the following cases mentioned in your description. Please note that the test should fail without your patch and should pass after your patch.
|
Thanks for the review! You're right - I should provide more context. Let me explain the situation honestly: Background The Problem with Previous Fix const pos = mousePos.inside >= 0 ? mousePos.inside : mousePos.pos;
return cellAround(view.state.doc.resolve(pos));This worked for rowspan cells, but I later discovered it broke selection in certain regions of colspan cells. The issue is: when The Current Fix if (mousePos.inside >= 0) {
const $cell = cellAround(view.state.doc.resolve(mousePos.inside));
if ($cell) return $cell;
}
return cellAround(view.state.doc.resolve(mousePos.pos));Visual Demonstration About Testing
|
ocavue
left a comment
There was a problem hiding this comment.
LGTM. The GIFs are enough, I think.


📋 Summary
Improves cell selection logic for merged cells (rowspan/colspan) by implementing a robust fallback strategy in the
cellUnderMousefunction.🐛 Problem
The previous fix (#311) improved cell selection for row-merged cells (rowspan) by preferring
mousePos.insideovermousePos.pos. However, this introduced a regression where column-merged cells became harder to select in certain regions, particularly when clicking in the lower half of the merged area.Root cause: The previous code used a simple either-or approach:
If
inside >= 0, it would always useinsideposition. But ifcellAround()returnsnullfor that position (which can happen in edge cases), the function would returnnullwithout trying theposposition as a fallback.✅ Solution
Implement a two-step fallback strategy:
First attempt: Try
mousePos.insidewhen available (inside >= 0)cellAround()successfully finds a cell, return it immediatelyFallback: If the first attempt fails or
inside < 0, trymousePos.posNew code:
🎯 Benefits
🧪 Testing
Manually tested with:
All selection scenarios now work correctly.
📚 Related