Repository navigation
fix(core): keep the editor focused dropping & moving blocks #3231
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
Merged
+756
−66
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
917629c
fix(core): keep the editor focused after a block drop and after side …
YousefED f2fb438
fix(core)!: remove blockDragEnd, hide the side menu after a drop
YousefED fa15295
fix(core): keep focus handling correct for cross-document drops and s…
YousefED cc4f00a
fix(core): preserve visible block selections and drag-end compatibility
nperez0111 fa8a46d
fix(core): address nested drag selection and column drop history
nperez0111 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
50 changes: 50 additions & 0 deletions
50
packages/core/src/extensions/DropCursor/DropCursor.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,50 @@ | ||
| import { afterEach, describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; | ||
| import "../../editor/editor.css"; | ||
|
|
||
| const cleanup: (() => void)[] = []; | ||
|
|
||
| afterEach(() => { | ||
| cleanup | ||
| .splice(0) | ||
| .reverse() | ||
| .forEach((dispose) => dispose()); | ||
| }); | ||
|
|
||
| describe("focus after a drop from another root", () => { | ||
| it("finishes a drop in a shadow root without a local dragend", async () => { | ||
| const host = document.createElement("div"); | ||
| document.body.appendChild(host); | ||
| cleanup.push(() => host.remove()); | ||
| const root = host.attachShadow({ mode: "open" }); | ||
| const container = document.createElement("div"); | ||
| root.appendChild(container); | ||
| const editor = BlockNoteEditor.create({ | ||
| initialContent: [{ type: "paragraph", content: "Target" }], | ||
| }); | ||
| editor.mount(container); | ||
| cleanup.push(() => editor._tiptapEditor.destroy()); | ||
|
|
||
| const view = editor.prosemirrorView; | ||
| const rect = view.dom.getBoundingClientRect(); | ||
| const dataTransfer = new DataTransfer(); | ||
| dataTransfer.setData("text/html", "<p>Incoming</p>"); | ||
| dataTransfer.setData("blocknote/html", "<p>Incoming</p>"); | ||
| // A drag from a different document/root delivers drop here, but its | ||
| // dragend fires at the source and never reaches this root. | ||
| view.dom.dispatchEvent( | ||
| new DragEvent("drop", { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| clientX: rect.left + 5, | ||
| clientY: rect.top + rect.height / 2, | ||
| dataTransfer, | ||
| }), | ||
| ); | ||
|
|
||
| expect(view.state.doc.textContent).toContain("Incoming"); | ||
| await expect.poll(() => view.state.selection.empty).toBe(true); | ||
| expect(root.activeElement).toBe(view.dom); | ||
| }); | ||
| }); |
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
127 changes: 127 additions & 0 deletions
127
packages/core/src/extensions/SideMenu/MultipleNodeSelection.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,127 @@ | ||
| // @vitest-environment node | ||
| import { history, redo, undo } from "@tiptap/pm/history"; | ||
| import { Schema } from "prosemirror-model"; | ||
| import { EditorState, Selection, TextSelection } from "prosemirror-state"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { MultipleNodeSelection } from "./MultipleNodeSelection.js"; | ||
|
|
||
| const schema = new Schema({ | ||
| nodes: { | ||
| doc: { content: "paragraph+" }, | ||
| paragraph: { content: "text*" }, | ||
| text: {}, | ||
| }, | ||
| }); | ||
|
|
||
| function paragraph(text: string) { | ||
| return schema.nodes.paragraph.create(null, schema.text(text)); | ||
| } | ||
|
|
||
| const doc = schema.nodes.doc.create(null, [ | ||
| paragraph("A"), | ||
| paragraph("B"), | ||
| paragraph("C"), | ||
| ]); | ||
|
|
||
| describe("MultipleNodeSelection bookmarks", () => { | ||
| it("round-trips a block selection through editor-state JSON", () => { | ||
| const selection = MultipleNodeSelection.create(doc, 0, 3); | ||
| const state = EditorState.create({ doc, selection }); | ||
| const restored = EditorState.fromJSON({ schema }, state.toJSON()); | ||
|
|
||
| expect(restored.selection.eq(selection)).toBe(true); | ||
| }); | ||
|
|
||
| it.each([1, 2])("restores a selection of %i blocks", (count) => { | ||
| const selection = MultipleNodeSelection.create(doc, 0, count * 3); | ||
| const restored = selection.getBookmark().resolve(doc); | ||
|
|
||
| expect(restored).toBeInstanceOf(MultipleNodeSelection); | ||
| expect(restored.eq(selection)).toBe(true); | ||
| }); | ||
|
|
||
| it("maps a selection past inserted blocks", () => { | ||
| const selection = MultipleNodeSelection.create(doc, 3, 6); | ||
| const tr = EditorState.create({ doc }).tr.insert(0, paragraph("X")); | ||
| const restored = selection.getBookmark().map(tr.mapping).resolve(tr.doc); | ||
|
|
||
| expect(restored).toBeInstanceOf(MultipleNodeSelection); | ||
| expect(restored.eq(MultipleNodeSelection.create(tr.doc, 6, 9))).toBe(true); | ||
| }); | ||
|
|
||
| it("maps edits inside the selected blocks", () => { | ||
| const selection = MultipleNodeSelection.create(doc, 0, 6); | ||
| const tr = EditorState.create({ doc }).tr.insertText("more", 2); | ||
| const restored = selection.getBookmark().map(tr.mapping).resolve(tr.doc); | ||
|
|
||
| expect(restored).toBeInstanceOf(MultipleNodeSelection); | ||
| expect(restored.eq(MultipleNodeSelection.create(tr.doc, 0, 10))).toBe(true); | ||
| }); | ||
|
|
||
| it.each([3, 6])("excludes blocks inserted at boundary %i", (pos) => { | ||
| const selection = MultipleNodeSelection.create(doc, 3, 6); | ||
| const tr = EditorState.create({ doc }).tr.insert(pos, paragraph("X")); | ||
| const restored = selection.getBookmark().map(tr.mapping).resolve(tr.doc); | ||
| const from = pos === 3 ? 6 : 3; | ||
|
|
||
| expect(restored).toBeInstanceOf(MultipleNodeSelection); | ||
| expect( | ||
| restored.eq(MultipleNodeSelection.create(tr.doc, from, from + 3)), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it.each([3, 6])("keeps the live selection stable at boundary %i", (pos) => { | ||
| const selection = MultipleNodeSelection.create(doc, 3, 6); | ||
| const tr = EditorState.create({ doc }).tr.insert(pos, paragraph("X")); | ||
| const mapped = selection.map(tr.doc, tr.mapping); | ||
| const from = pos === 3 ? 6 : 3; | ||
|
|
||
| expect( | ||
| mapped.eq(MultipleNodeSelection.create(tr.doc, from, from + 3)), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it("falls back to a cursor when the selected blocks are deleted", () => { | ||
| const selection = MultipleNodeSelection.create(doc, 0, 6); | ||
| const tr = EditorState.create({ doc }).tr.delete(0, 6); | ||
| const restored = selection.getBookmark().map(tr.mapping).resolve(tr.doc); | ||
|
|
||
| expect(restored).toBeInstanceOf(TextSelection); | ||
| expect(restored.empty).toBe(true); | ||
| expect(restored.eq(Selection.near(tr.doc.resolve(0)))).toBe(true); | ||
| }); | ||
|
|
||
| it("falls back when joining blocks removes a selection boundary", () => { | ||
| const selection = MultipleNodeSelection.create(doc, 0, 3); | ||
| const tr = EditorState.create({ doc }).tr.join(3); | ||
| const restored = selection.getBookmark().map(tr.mapping).resolve(tr.doc); | ||
|
|
||
| expect(restored).toBeInstanceOf(TextSelection); | ||
| expect(restored.empty).toBe(true); | ||
| }); | ||
|
|
||
| it.each([1, 2])("preserves %i selected blocks through undo/redo", (count) => { | ||
| const selection = MultipleNodeSelection.create(doc, 0, count * 3); | ||
| let state = EditorState.create({ doc, selection, plugins: [history()] }); | ||
| const content = selection.content().content; | ||
| const tr = state.tr.delete(selection.from, selection.to); | ||
| tr.insert(tr.doc.content.size, content); | ||
| tr.setSelection(Selection.near(tr.doc.resolve(tr.doc.content.size))); | ||
| state = state.apply(tr); | ||
| const movedDoc = state.doc; | ||
|
|
||
| function dispatch(transaction: typeof tr) { | ||
| state = state.apply(transaction); | ||
| } | ||
|
|
||
| expect(undo(state, dispatch)).toBe(true); | ||
| expect(state.doc.eq(doc)).toBe(true); | ||
| expect(state.selection).toBeInstanceOf(MultipleNodeSelection); | ||
| expect(state.selection.eq(selection)).toBe(true); | ||
| expect(redo(state, dispatch)).toBe(true); | ||
| expect(state.doc.eq(movedDoc)).toBe(true); | ||
| expect(undo(state, dispatch)).toBe(true); | ||
| expect(state.selection.eq(selection)).toBe(true); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 5870
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 42199
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 24628
Handle drops from another document at the target drop.
onDropsetsblocksDropped, butonRootDragEndlistens only on the target editor’sroot. A block drag from an editor in another window dispatchesdragendin the source document, so the target does not run the focus correction. The flag remains set, and a later unrelated target-rootdragendcan blur and refocus this editor.Schedule the same correction from the target-side drop. Keep the root listener for same-document drags.
Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents