-
-
Notifications
You must be signed in to change notification settings - Fork 772
fix: allow selecting tables alongside other blocks #3085
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
Open
Hbrehman
wants to merge
1
commit into
TypeCellOS:main
Choose a base branch
from
Hbrehman:cursor/fix-table-cross-block-selection-b800
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/core/src/blocks/Table/tableCrossBlockSelection.browser.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { TextSelection } from "prosemirror-state"; | ||
| import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; | ||
|
|
||
| // Mouse-drag selection across a table and neighbouring blocks needs a real | ||
| // layout (getBoundingClientRect / posAtCoords). The matching node tests in | ||
| // `tableCrossBlockSelection.test.ts` cover programmatic selection and Mod-a. | ||
|
|
||
| describe("table + neighbouring block mouse selection", () => { | ||
| let editor: BlockNoteEditor; | ||
| let mountPoint: HTMLElement; | ||
|
|
||
| beforeEach(() => { | ||
| mountPoint = document.createElement("div"); | ||
| document.body.appendChild(mountPoint); | ||
|
|
||
| editor = BlockNoteEditor.create({ | ||
| initialContent: [ | ||
| { id: "paragraph-before", type: "paragraph", content: "Before table" }, | ||
| { | ||
| id: "table-0", | ||
| type: "table", | ||
| content: { | ||
| type: "tableContent", | ||
| rows: [ | ||
| { cells: ["Cell 1", "Cell 2"] }, | ||
| { cells: ["Cell 3", "Cell 4"] }, | ||
| ], | ||
| }, | ||
| }, | ||
| { id: "paragraph-after", type: "paragraph", content: "After table" }, | ||
| ], | ||
| }); | ||
| editor.mount(mountPoint); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| editor.unmount(); | ||
| editor._tiptapEditor.destroy(); | ||
| mountPoint.remove(); | ||
| }); | ||
|
|
||
| function queryText(text: string) { | ||
| const walker = document.createTreeWalker(mountPoint, NodeFilter.SHOW_TEXT); | ||
| let node: Node | null; | ||
| while ((node = walker.nextNode())) { | ||
| if (node.textContent === text) { | ||
| return node; | ||
| } | ||
| } | ||
| throw new Error(`Text node "${text}" not found`); | ||
| } | ||
|
|
||
| function clientPoint(text: string, atEnd = false) { | ||
| const node = queryText(text); | ||
| const range = document.createRange(); | ||
| range.setStart(node, atEnd ? (node.textContent?.length ?? 0) : 0); | ||
| range.setEnd(node, atEnd ? (node.textContent?.length ?? 0) : 0); | ||
| const rect = range.getBoundingClientRect(); | ||
| return { | ||
| clientX: rect.left + Math.min(2, rect.width / 2), | ||
| clientY: rect.top + rect.height / 2, | ||
| }; | ||
| } | ||
|
|
||
| function dragSelect(fromText: string, toText: string) { | ||
| const from = clientPoint(fromText); | ||
| const to = clientPoint(toText, true); | ||
| const view = editor.prosemirrorView; | ||
|
|
||
| view.dom.dispatchEvent( | ||
| new MouseEvent("mousedown", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| button: 0, | ||
| buttons: 1, | ||
| clientX: from.clientX, | ||
| clientY: from.clientY, | ||
| }), | ||
| ); | ||
| view.dom.dispatchEvent( | ||
| new MouseEvent("mousemove", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| button: 0, | ||
| buttons: 1, | ||
| clientX: to.clientX, | ||
| clientY: to.clientY, | ||
| }), | ||
| ); | ||
| view.dom.dispatchEvent( | ||
| new MouseEvent("mouseup", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| button: 0, | ||
| buttons: 0, | ||
| clientX: to.clientX, | ||
| clientY: to.clientY, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| it("selects a paragraph, the table, and the next paragraph by dragging", () => { | ||
| dragSelect("Before table", "After table"); | ||
|
|
||
| expect(editor.getSelection()?.blocks.map((block) => block.type)).toEqual([ | ||
| "paragraph", | ||
| "table", | ||
| "paragraph", | ||
| ]); | ||
| expect(editor.prosemirrorView.state.selection).toBeInstanceOf( | ||
| TextSelection, | ||
| ); | ||
| }); | ||
|
|
||
| it("selects the table together with the following paragraph when dragging out of a cell", () => { | ||
| dragSelect("Cell 1", "After table"); | ||
|
|
||
| expect(editor.getSelection()?.blocks.map((block) => block.type)).toEqual([ | ||
| "table", | ||
| "paragraph", | ||
| ]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 31489
🤖 get_repo_knowledge executed:
get_repo_knowledge TypeCellOS/BlockNote /tmp/coderabbit-repo-knowledge/typecellos-blocknote-375222ac/learnings /tmp/coderabbit-repo-knowledge/typecellos-blocknote-375222ac/conventionsLength of output: 7101
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 50376
🌐 Web query:
prosemirror-state 1.4.4 TextSelection.create endpoint not pointing into a node with inline content source💡 Result:
<search_synthesis>
The warning "TextSelection endpoint not pointing into a node with inline content" is a diagnostic message introduced in prosemirror-state version 1.4.0 [1]. It occurs when a TextSelection is created with a position that points to a node that does not support inline content—most commonly the top-level &
#39;doc' node (e.g., position 0) or a block-level container [2][1][3]. In ProseMirror, a TextSelection must be anchored within a node that allows inline content (text or marks) [2][4]. When code attempts to set a selection at an invalid position (like the root 'doc' node), the library issues this warning to alert developers that the selection may behave unexpectedly or be invalid for standard text editing operations [2][1][3]. Common causes and solutions include: 1. Setting selections at position 0: Developers often use TextSelection.create(doc, 0) to reset or clear a selection. Because position 0 often points to the 'doc' node, this triggers the warning [3][5]. Use Selection.atStart(doc) instead, which intelligently resolves to the first valid cursor position within the document [3][5]. 2. Using inappropriate selection methods: If you are programmatically setting selections, avoid forcing a TextSelection at arbitrary positions. Use methods like Selection.near(doc.resolve(pos)) or Selection.atStart(doc) to find a valid, nearby position automatically [6][4]. 3. Plugin or Command logic: Some older patterns in editor extensions (such as clearing selections on blur or bulk-setting content) use hardcoded positions that might not be valid in all schemas [1][7][3]. Reviewing and updating these patterns to use safe selection resolution methods generally resolves the issue [8][3][5]. This warning does not necessarily break editor functionality immediately, but it signals that the selection state is technically invalid according to ProseMirror's model constraints [2][3].</search_synthesis>
<source_evidence>
Citations:
🌐 Web query:
prosemirror-tables cellAround start(-1) table boundary position source💡 Result:
<search_synthesis>
In the prosemirror-tables library, the expression $pos.start(-1) is a standard ProseMirror ResolvedPos method call used to determine the start position of the ancestor node located one level up from the current resolved position [1][2]. When applied within the context of table operations, it returns the document position where the table node begins [3][4]. The function cellAround(pos: ResolvedPos) is a utility provided by prosemirror-tables that identifies the table cell nearest to a given resolved position [4]. It does not take an argument like start(-1); rather, it expects a ResolvedPos object and traverses the document structure to locate the containing cell [4][5]. The usage of $pos.start(-1) frequently appears in library code (such as inside utility functions or when calculating table boundaries) to reference the beginning of the table relative to the document [3][1]. For example, in internal logic, code often checks if a position is within table boundaries by ensuring a position is between $pos.start(-1) and $pos.end(-1) [2]. If you are encountering code that looks like cellAround(something.start(-1)), this is likely incorrect usage or a misunderstanding of the API, as start(-1) returns a number (a document position), whereas cellAround requires a ResolvedPos object [4][2][5]. To resolve a document position into a ResolvedPos, you would typically use view.state.doc.resolve(pos) [6][2].
</search_synthesis>
<source_evidence>
Citations:
Use a boundary-capable selection for boundary tables.
When
cellAroundfinds a cell at the document start or end,getWholeDocTextSelectioncreates endpoints before or after the table. These positions resolve in a non-inline parent, soTextSelection.createinprosemirror-state@1.4.4emitsTextSelection endpoint not pointing into a node with inline content. Mapping can replace such endpoints withSelection.near, which can change the selected range. UseAllSelectionor another boundary-capable selection for these cases, and add coverage for table-only, leading-table, and trailing-table documents.🤖 Prompt for AI Agents