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 duplicated content and other bugs related to drag and drop handling #4238

Merged
merged 2 commits into from
May 5, 2021

Conversation

clauderic
Copy link
Collaborator

@clauderic clauderic commented May 4, 2021

Description
This PR fixes a number of different issues that happen today with drag and drop.
It's loosely based off of this abandonned PR: #3562

Issue
Fixes:
#4222
#3522
#3137
#2076

Examples

Simple.drag.and.drop.-.Before.mp4
Simple.drag.and.drop.-.After.mp4
Complex.example.-.Before.mp4
Complex.example.-.After.mp4
Void.nodes.-.Before.mp4
Void.nodes.-.After.mp4
Dragging.between.editors.mp4

Context

The main things this PR does:

  • Keeps track of whether the drag start event happened internally
  • If the drag start event happened internally, the original dragged range needs to be deleted before inserting a new fragment
  • It's better to use insertFragment and call preventDefault() in the drop event rather than letting the beforeinput event go through. This strategy works consistently across all browsers and would also work with other input reconciliation strategies once Android support lands.

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

@changeset-bot
Copy link

changeset-bot bot commented May 4, 2021

🦋 Changeset detected

Latest commit: 3827dfc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate-react Patch

Not sure what this means? Click here to learn what changesets are.

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

!readOnly &&
!state.isComposing &&
!state.isUpdatingSelection &&
!state.isDraggingInternally
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Noticed issues in Firefox when trying to reconcile the selection while dragging

Comment on lines +758 to +759
Editor.isVoid(editor, node) ||
Editor.void(editor, { at: path, voids: true })
Copy link
Collaborator Author

@clauderic clauderic May 4, 2021

Choose a reason for hiding this comment

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

Not sure why Editor.void doesn't check the current path, seems it only checks above the current path which is a bit counter-intuitive, so we also need to also if the node at the current path is void.

Comment on lines -776 to -784
if (
!HAS_BEFORE_INPUT_SUPPORT ||
(!IS_SAFARI && event.dataTransfer.files.length > 0)
) {
event.preventDefault()
const range = ReactEditor.findEventRange(editor, event)
const data = event.dataTransfer
Transforms.select(editor, range)
ReactEditor.insertData(editor, data)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This logic should run on all browsers, not just the ones that don't have beforeinput support. Deferring to beforeinput causes most of the text-insertion related bugs that this PR fixes. This change is aligned with how copy paste is handled in Slate.

@clauderic clauderic force-pushed the drag-and-drop-fixes branch from c3fca0e to 3827dfc Compare May 4, 2021 20:58
@ianstormtaylor
Copy link
Owner

Thanks @clauderic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants