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 shadow DOM - drag-and-drop text throws error #5754

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Hentuloo
Copy link

@Hentuloo Hentuloo commented Oct 28, 2024

Fix an error when dragging and dropping text in a Slate editor inside a Shadow DOM, resulting in:
Error: Cannot resolve a Slate point from DOM point: [object HTMLDivElement],0.

Issue
Fixes: [Link to issue]

Screencastfrom2024-10-2020-38-28-ezgif com-video-to-gif-converter

Context

The existing support for the third parameter options in caretPositionFromPoint is limited. To address this problem, additional logic was implemented to find the nearest character next to the cursor at the time of the drop event.

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

Copy link

changeset-bot bot commented Oct 28, 2024

⚠️ No Changeset found

Latest commit: 1b53ae2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

const droppedText = 'droppedText'
const textboxEl = (await textbox.elementHandle()) as HTMLElement
const { x, y, width, height } = await textbox.boundingBox()
await dispatchDropEvent({
Copy link
Author

@Hentuloo Hentuloo Oct 28, 2024

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.

import { test, expect } from '@playwright/test'

test.describe('shadow-dom example', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('http://localhost:3000/examples/shadow-dom')
  })

  test('drag and drop text above the textbox', async ({ page }) => {
    const outerShadow = page.locator('[data-cy="outer-shadow-root"]')
    const innerShadow = outerShadow.locator('> div')

    // Locate the textbox within the shadow DOM
    const textbox = innerShadow.getByRole('textbox')
    await textbox.fill('test1 test2')

    await page.waitForTimeout(1500)
    // Locate the span element that contains "test1 test2"
    const textSpan = innerShadow.locator('span[data-slate-string="true"]')

    // Wait for the content to be available
    await textSpan.waitFor()
    await textSpan.hover()

    await page.waitForTimeout(100)

    // Locate the text nodes for "test1" and "test2"
    const test1 = textSpan.locator('text=test1')

    const test1BoundingBox = await test1.boundingBox()
    console.log('test1BoundingBox:', test1BoundingBox)
    // Check if the bounding box was retrieved successfully
    if (test1BoundingBox) {
      // Drag "test1" and drop it after "test2"
      await page.mouse.click(
        test1BoundingBox.x,
        test1BoundingBox.y + test1BoundingBox.height / 2,
        { force: true }
      )

      await page.mouse.down()

      await page.waitForTimeout(100)

      await page.waitForTimeout(100)
      await page.mouse.move(
        test1BoundingBox.x + test1BoundingBox.width / 2,
        test1BoundingBox.y + test1BoundingBox.height / 2
      )
      await page.mouse.up()

      await page.waitForTimeout(100)

      await page.mouse.move(
        test1BoundingBox.x,
        test1BoundingBox.y + test1BoundingBox.height / 2
      )
      await page.mouse.down()

      await page.waitForTimeout(100)
      await page.mouse.move(
        test1BoundingBox.x + test1BoundingBox.width,
        test1BoundingBox.y + test1BoundingBox.height / 2
      )
      await page.mouse.up()

      await page.waitForTimeout(100)

      await expect(textbox).toHaveText('test2test1')
    }
  })
})```

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.

Shadow DOM - Drag-and-drop text throws error
1 participant