-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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 shadow DOM - drag-and-drop text throws error #5754
Open
Hentuloo
wants to merge
2
commits into
ianstormtaylor:main
Choose a base branch
from
Hentuloo:fix-shadow-dom-drag-and-drop-text
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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
99 changes: 99 additions & 0 deletions
99
packages/slate-react/src/utils/find-closest-character-in-element-by-cursor-point.ts
This file contains 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,99 @@ | ||
import { findTextNodesInNode } from './dom' | ||
|
||
export type CursorPoint = { | ||
x: number | ||
y: number | ||
} | ||
|
||
export type ClosestCharacter = { | ||
offset: number | ||
distance: number | ||
textNode: Text | ||
} | ||
|
||
type CharDistanceFromCursorPoint = { | ||
distanceX: number | ||
distanceY: number | ||
charCenterX: number | ||
charCenterY: number | ||
isCursorOnSameLine: boolean | ||
} | ||
|
||
const calculateCharDistance = ( | ||
cursorPoint: CursorPoint, | ||
range: Range | ||
): CharDistanceFromCursorPoint => { | ||
const rect = range.getBoundingClientRect() | ||
const charCenterX = (rect.left + rect.right) / 2 | ||
const charCenterY = (rect.top + rect.bottom) / 2 | ||
|
||
return { | ||
distanceX: Math.abs(cursorPoint.x - charCenterX), | ||
distanceY: Math.abs(cursorPoint.y - charCenterY), | ||
charCenterX, | ||
charCenterY, | ||
isCursorOnSameLine: | ||
rect.top <= cursorPoint.y && rect.bottom >= cursorPoint.y, | ||
} | ||
} | ||
|
||
const findClosestCharacterInSingleTextNode = ( | ||
textNode: Text, | ||
cursorPoint: CursorPoint | ||
): ClosestCharacter | null => { | ||
let closestChar: ClosestCharacter | null = null | ||
const textLength = textNode.textContent?.length ?? 0 | ||
|
||
for (let i = 0; i < textLength; i++) { | ||
const range = document.createRange() | ||
range.setStart(textNode, i) | ||
range.setEnd(textNode, i + 1) | ||
|
||
const { distanceX, charCenterX, isCursorOnSameLine } = | ||
calculateCharDistance(cursorPoint, range) | ||
|
||
if (isCursorOnSameLine) { | ||
const prevDistance: number = closestChar?.distance ?? Infinity | ||
const newDistance: number = distanceX | ||
const isCloser = newDistance < prevDistance | ||
|
||
const isOnLeftSideOfCursor = cursorPoint.x < charCenterX | ||
const offset = isOnLeftSideOfCursor ? i : i + 1 | ||
|
||
closestChar = isCloser | ||
? { offset, distance: newDistance, textNode } | ||
: closestChar | ||
} | ||
} | ||
|
||
return closestChar | ||
} | ||
|
||
const findClosestCharacterForTextNodes = ( | ||
textNodes: Text[], | ||
cursorPoint: CursorPoint | ||
): ClosestCharacter | null => { | ||
let closestInNode = null | ||
|
||
for (const textNode of textNodes) { | ||
const newResult = findClosestCharacterInSingleTextNode( | ||
textNode, | ||
cursorPoint | ||
) | ||
const prevDistance: number = closestInNode?.distance ?? Infinity | ||
const newDistance: number = newResult?.distance ?? Infinity | ||
const isCloser = newDistance < prevDistance | ||
|
||
closestInNode = isCloser ? newResult : closestInNode | ||
} | ||
|
||
return closestInNode | ||
} | ||
|
||
export const findClosestCharacterToCursorPoint = ( | ||
elementUnderCursor: Element, | ||
cursorPoint: CursorPoint | ||
): ClosestCharacter | null => { | ||
const textNodes = findTextNodesInNode(elementUnderCursor) | ||
return findClosestCharacterForTextNodes(textNodes, cursorPoint) | ||
} |
This file contains 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 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,43 @@ | ||
import { Page } from '@playwright/test' | ||
|
||
export type DisptachDropEvent = { | ||
page: Page | ||
element: Element | ||
droppedText: string | ||
clientX: number | ||
clientY: number | ||
} | ||
|
||
export const dispatchDropEvent = async ({ | ||
page, | ||
element, | ||
droppedText, | ||
clientX, | ||
clientY, | ||
}: DisptachDropEvent) => | ||
page.evaluate( | ||
({ | ||
element, | ||
droppedText, | ||
clientX, | ||
clientY, | ||
}: Omit<DisptachDropEvent, 'page'>) => { | ||
const fragmentData = JSON.stringify([{ type: 'text', text: droppedText }]) | ||
const encodedFragment = btoa(encodeURIComponent(fragmentData)) | ||
|
||
const dataTransfer = new DataTransfer() | ||
dataTransfer.setData(`application/x-slate-fragment`, encodedFragment) | ||
const eventInit: DragEventInit = { | ||
bubbles: true, | ||
cancelable: true, | ||
clientX, | ||
clientY, | ||
dataTransfer: dataTransfer, | ||
} | ||
|
||
const event = new DragEvent('drop', eventInit) | ||
|
||
element.dispatchEvent(event) | ||
}, | ||
{ element, droppedText, clientX, clientY } | ||
) |
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.
I also tried it in a slightly different way, but unfortunately, it didn't work with Firefox.