|
| 1 | +/* |
| 2 | + * Copyright 2026 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {server, userEvent} from 'vitest/browser'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Creates a DataTransfer wrapper that works around Chromium restrictions on synthetic events. |
| 17 | + * |
| 18 | + * Chromium silently ignores writes to effectAllowed and dropEffect on DataTransfer objects |
| 19 | + * attached to untrusted (dispatched) DragEvents. This proxy intercepts those writes and |
| 20 | + * stores them in a local overlay, while forwarding everything else to the real DataTransfer. |
| 21 | + */ |
| 22 | +function createWritableDataTransfer(): {dataTransfer: DataTransfer, proxy: DataTransfer} { |
| 23 | + let dataTransfer = new DataTransfer(); |
| 24 | + let overrides: Record<string, string> = {}; |
| 25 | + |
| 26 | + let proxy = new Proxy(dataTransfer, { |
| 27 | + get(target, prop) { |
| 28 | + if (prop in overrides) { |
| 29 | + return overrides[prop as string]; |
| 30 | + } |
| 31 | + let value = Reflect.get(target, prop, target); |
| 32 | + if (typeof value === 'function') { |
| 33 | + return value.bind(target); |
| 34 | + } |
| 35 | + return value; |
| 36 | + }, |
| 37 | + set(target, prop, value) { |
| 38 | + if (prop === 'effectAllowed' || prop === 'dropEffect') { |
| 39 | + overrides[prop] = value; |
| 40 | + try { (target as any)[prop] = value; } catch { /* noop - Chromium rejects this */ } |
| 41 | + return true; |
| 42 | + } |
| 43 | + (target as any)[prop] = value; |
| 44 | + return true; |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + return {dataTransfer, proxy}; |
| 49 | +} |
| 50 | + |
| 51 | +interface DragAndDropOptions { |
| 52 | + /** Clicks on the source element at this point relative to the top-left corner of the element's padding box. */ |
| 53 | + sourcePosition?: {x: number, y: number}, |
| 54 | + /** Drops on the target element at this point relative to the top-left corner of the element's padding box. */ |
| 55 | + targetPosition?: {x: number, y: number} |
| 56 | +} |
| 57 | + |
| 58 | +/** Returns the clientX/clientY for a position relative to an element's padding box, or the element's center if no position is given. */ |
| 59 | +function resolveClientPosition(element: Element, position?: {x: number, y: number}): {clientX: number, clientY: number} { |
| 60 | + let rect = element.getBoundingClientRect(); |
| 61 | + if (position) { |
| 62 | + return {clientX: rect.left + position.x, clientY: rect.top + position.y}; |
| 63 | + } |
| 64 | + return {clientX: rect.left + rect.width / 2, clientY: rect.top + rect.height / 2}; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Perform a drag-and-drop from source to target. |
| 69 | + * |
| 70 | + * On non-Chromium browsers, delegates to `userEvent.dragAndDrop` which uses the |
| 71 | + * browser provider's native drag support. |
| 72 | + * |
| 73 | + * On Chromium, Playwright's CDP-based drag bypasses the native dragstart event, |
| 74 | + * which prevents useDrag from populating the DataTransfer. We fall back to |
| 75 | + * dispatching the full DragEvent lifecycle with synthetic events instead. |
| 76 | + */ |
| 77 | +export async function dragAndDrop(source: Element, target: Element, options?: DragAndDropOptions): Promise<void> { |
| 78 | + if (server.browser !== 'chromium') { |
| 79 | + await userEvent.dragAndDrop(source, target, options); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + let {dataTransfer, proxy} = createWritableDataTransfer(); |
| 84 | + |
| 85 | + let sourcePos = resolveClientPosition(source, options?.sourcePosition); |
| 86 | + let targetPos = resolveClientPosition(target, options?.targetPosition); |
| 87 | + |
| 88 | + // Patch DragEvent.prototype.dataTransfer to return our proxy whenever the |
| 89 | + // event carries our real dataTransfer. This is the only reliable way to make |
| 90 | + // effectAllowed/dropEffect writable in Chromium for synthetic events. |
| 91 | + let origDesc = Object.getOwnPropertyDescriptor(DragEvent.prototype, 'dataTransfer')!; |
| 92 | + Object.defineProperty(DragEvent.prototype, 'dataTransfer', { |
| 93 | + get() { |
| 94 | + let real = origDesc.get!.call(this); |
| 95 | + return real === dataTransfer ? proxy : real; |
| 96 | + }, |
| 97 | + configurable: true |
| 98 | + }); |
| 99 | + |
| 100 | + try { |
| 101 | + // Dispatch dragstart so useDrag populates the DataTransfer with items and sets effectAllowed. |
| 102 | + source.dispatchEvent(new DragEvent('dragstart', {dataTransfer, bubbles: true, cancelable: true, ...sourcePos})); |
| 103 | + |
| 104 | + // Allow React state updates from the dragstart handler to flush. |
| 105 | + await new Promise(resolve => requestAnimationFrame(resolve)); |
| 106 | + |
| 107 | + target.dispatchEvent(new DragEvent('dragenter', {dataTransfer, bubbles: true, cancelable: true, ...targetPos})); |
| 108 | + target.dispatchEvent(new DragEvent('dragover', {dataTransfer, bubbles: true, cancelable: true, ...targetPos})); |
| 109 | + |
| 110 | + // In a real browser drag, the drop event only fires when dropEffect is not 'none'. |
| 111 | + // useDrop sets dropEffect during dragenter/dragover based on getDropOperation. |
| 112 | + if (proxy.dropEffect !== 'none') { |
| 113 | + target.dispatchEvent(new DragEvent('drop', {dataTransfer, bubbles: true, cancelable: true, ...targetPos})); |
| 114 | + } |
| 115 | + |
| 116 | + source.dispatchEvent(new DragEvent('dragend', {dataTransfer, bubbles: true, cancelable: true, ...targetPos})); |
| 117 | + } finally { |
| 118 | + // Restore the original dataTransfer getter. |
| 119 | + Object.defineProperty(DragEvent.prototype, 'dataTransfer', origDesc); |
| 120 | + } |
| 121 | +} |
0 commit comments