Skip to content

fix: move an image doesn't work with drag and drop (#2603)#2628

Open
miadnguyen wants to merge 1 commit intoTypeCellOS:mainfrom
miadnguyen:fix/image-drag-drop-toolbar
Open

fix: move an image doesn't work with drag and drop (#2603)#2628
miadnguyen wants to merge 1 commit intoTypeCellOS:mainfrom
miadnguyen:fix/image-drag-drop-toolbar

Conversation

@miadnguyen
Copy link
Copy Markdown

@miadnguyen miadnguyen commented Apr 4, 2026

Summary

Fixes #2603. When dragging an image block using the side menu drag handle, the formatting toolbar incorrectly opens mid-drag.

Rationale

The formatting toolbar should only appear when the user clicks on the image or during text selection, not during a drag-and-drop operation. The unexpected toolbar appearance is disruptive.

Changes

  • Added a preventShowWhileDragging flag in FormattingToolbar.ts that mirrors the existing preventShowWhileMouseDown pattern
  • Registered dragstart and dragend listeners on editor.prosemirrorView.root to set and clear this flag for the full duration of any drag operation
  • Added a Playwright end-to-end test verifying the toolbar does not appear while dragging an image block

Impact

The toolbar is now suppressed during any block drag operation. Normal toolbar behavior is unaffected.

Testing

  • Manually verified fix on Chrome and Firefox
  • Added Playwright test in tests/src/end-to-end/images/images.test.ts: "Formatting toolbar should not appear when dragging image block"
  • Existing delete image test continues to pass

Screenshots/Video

image-drag-drop-toolbar.mov

Checklist

  • Code follows the project's coding standards.
  • Unit tests covering the new feature have been added.
  • All existing tests pass.
  • The documentation has been updated to reflect the new feature

Additional Notes

When the drag handle is used, ProseMirror sets a NodeSelection on the image node before the drag begins. The existing preventShowWhileMouseDown flag did not cover HTML drag events (dragstart/dragend), only pointer events, so the toolbar was not suppressed during drags initiated from the side menu.

Summary by CodeRabbit

  • Bug Fixes

    • Formatting toolbar now remains hidden while dragging elements, eliminating visual interference during drag operations.
  • Tests

    • Added coverage for formatting toolbar behavior during drag interactions.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 4, 2026

@miadnguyen is attempting to deploy a commit to the TypeCell Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 4, 2026

📝 Walkthrough

Walkthrough

Added preventShowWhileDragging flag to FormattingToolbar to suppress toolbar visibility during block drag operations. ProseMirror dragstart/dragend event listeners manage this state. Updated test to verify toolbar remains hidden while dragging image blocks.

Changes

Cohort / File(s) Summary
FormattingToolbar Enhancement
packages/core/src/extensions/FormattingToolbar/FormattingToolbar.ts
Introduced preventShowWhileDragging flag to prevent toolbar display during drag operations. Enhanced editor.onChange and editor.onSelectionChange handlers with early-return logic when dragging. Added dragstart/dragend event listeners to manage drag state and force toolbar closure on drag initiation.
Drag-and-Drop Test
tests/src/end-to-end/images/images.test.ts
Added test case for image block dragging via drag handle. Implements manual mouse-driven drag sequence with assertion verifying formatting toolbar remains hidden throughout the drag operation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Hooray! The toolbar learns to hide,
When images glide far and wide,
No popups shall block the drag's sweet flow,
Now blocks can move where users go!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly references the issue #2603 and clearly describes the specific bug fix about image drag-and-drop functionality.
Linked Issues check ✅ Passed The PR successfully addresses issue #2603 by preventing the formatting toolbar from appearing during drag operations and enabling proper image block movement via drag-and-drop.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the image drag-and-drop issue: toolbar flag logic, drag event listeners, and related test coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The pull request description comprehensively covers all required sections with specific details about the fix, implementation, testing, and impact.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/core/src/extensions/FormattingToolbar/FormattingToolbar.ts (1)

103-110: ⚠️ Potential issue | 🟠 Major

pointercancel should clear preventShowWhileMouseDown, not set it.

Line 107 contradicts the comment above it. In this file, the only reset path for preventShowWhileMouseDown is the pointerup handler, so setting it to true here can leave the toolbar suppressed after a canceled drag/pointer sequence.

Suggested change
      dom.addEventListener(
        "pointercancel",
        () => {
-          preventShowWhileMouseDown = true;
+          preventShowWhileMouseDown = false;
        },
        { signal, capture: true },
      );
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/core/src/extensions/FormattingToolbar/FormattingToolbar.ts` around
lines 103 - 110, The pointercancel handler currently sets
preventShowWhileMouseDown to true which contradicts its comment and leaves the
toolbar suppressed; update the "pointercancel" event listener so it clears the
flag (set preventShowWhileMouseDown to false) — mirror the reset behavior of the
existing pointerup handler and ensure the handler on "pointercancel" (inside
FormattingToolbar.ts near the preventShowWhileMouseDown logic) resets the state
instead of setting it.
🧹 Nitpick comments (1)
tests/src/end-to-end/images/images.test.ts (1)

166-171: Please assert the drop result after mouse.up().

This currently proves only that the toolbar stays hidden mid-drag. It still passes if the handle path never reorders the document, which is the user-visible part of #2603. Add a post-drop snapshot or DOM-order assertion.

Possible follow-up
     const toolbar = page.locator(".bn-formatting-toolbar");
     await expect(toolbar).not.toBeVisible();
     
     await page.mouse.up();
+    await compareDocToSnapshot(page, "dragImageViaHandle");
   });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/src/end-to-end/images/images.test.ts` around lines 166 - 171, After the
drag is finished (after the existing await page.mouse.up()), add a post-drop
assertion to verify the actual reorder happened rather than only that the
toolbar is hidden: use the same test's DOM locators to either take a snapshot of
the document and compare it (post-drop snapshot) or assert the DOM order of the
moved elements (e.g., grab the image/handle nodes and assert their
src/text/index positions have changed). Place this assertion immediately after
await page.mouse.up() (near the existing toolbar locator) so the test fails if
the handle never reorders the document.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@packages/core/src/extensions/FormattingToolbar/FormattingToolbar.ts`:
- Around line 103-110: The pointercancel handler currently sets
preventShowWhileMouseDown to true which contradicts its comment and leaves the
toolbar suppressed; update the "pointercancel" event listener so it clears the
flag (set preventShowWhileMouseDown to false) — mirror the reset behavior of the
existing pointerup handler and ensure the handler on "pointercancel" (inside
FormattingToolbar.ts near the preventShowWhileMouseDown logic) resets the state
instead of setting it.

---

Nitpick comments:
In `@tests/src/end-to-end/images/images.test.ts`:
- Around line 166-171: After the drag is finished (after the existing await
page.mouse.up()), add a post-drop assertion to verify the actual reorder
happened rather than only that the toolbar is hidden: use the same test's DOM
locators to either take a snapshot of the document and compare it (post-drop
snapshot) or assert the DOM order of the moved elements (e.g., grab the
image/handle nodes and assert their src/text/index positions have changed).
Place this assertion immediately after await page.mouse.up() (near the existing
toolbar locator) so the test fails if the handle never reorders the document.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5b850337-2b91-4ef2-bdbc-615cbe5cb0d4

📥 Commits

Reviewing files that changed from the base of the PR and between 38be5fd and 589c7e7.

📒 Files selected for processing (2)
  • packages/core/src/extensions/FormattingToolbar/FormattingToolbar.ts
  • tests/src/end-to-end/images/images.test.ts

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.

Move an image doesn't work with drag and drop

1 participant