|
| 1 | +import { ShapeType } from '#core/model'; |
| 2 | +import { useCanvasContext } from '#core/providers'; |
| 3 | +import { |
| 4 | + convertFromDivElementCoordsToKonvaCoords, |
| 5 | + getScrollFromDiv, |
| 6 | + isScreenPositionInsideDivElement, |
| 7 | + portScreenPositionToDivCoordinates, |
| 8 | +} from '#pods/canvas/canvas.util'; |
| 9 | +import { calculateShapeOffsetToXDropCoordinate } from '#pods/canvas/use-monitor.business'; |
| 10 | +import { |
| 11 | + type DragBridgeHostMessage, |
| 12 | + DRAG_BRIDGE_MESSAGE_TYPE, |
| 13 | +} from '@lemoncode/quickmock-bridge-protocol'; |
| 14 | +import { useEffect } from 'react'; |
| 15 | +import { |
| 16 | + notifyDragMoveToWebviewShell, |
| 17 | + shouldUseMacWebviewDragBridge, |
| 18 | +} from './mac-webview-drag-bridge.utils'; |
| 19 | + |
| 20 | +// macOS workaround for microsoft/vscode#193558: drag events on the inner |
| 21 | +// iframe route to the shell, so the shell-side bridge captures the drop and |
| 22 | +// forwards coordinates here; this reproduces the insertion useMonitorShape |
| 23 | +// performs natively on other platforms. |
| 24 | + |
| 25 | +type GalleryDropMessage = Extract< |
| 26 | + DragBridgeHostMessage, |
| 27 | + { type: typeof DRAG_BRIDGE_MESSAGE_TYPE.GALLERY_DROP } |
| 28 | +>; |
| 29 | + |
| 30 | +const isGalleryDropMessage = (data: unknown): data is GalleryDropMessage => { |
| 31 | + if (!data || typeof data !== 'object') { |
| 32 | + return false; |
| 33 | + } |
| 34 | + const message = data as { |
| 35 | + type?: unknown; |
| 36 | + payload?: { |
| 37 | + shapeType?: unknown; |
| 38 | + clientX?: unknown; |
| 39 | + clientY?: unknown; |
| 40 | + }; |
| 41 | + }; |
| 42 | + return ( |
| 43 | + message.type === DRAG_BRIDGE_MESSAGE_TYPE.GALLERY_DROP && |
| 44 | + typeof message.payload?.shapeType === 'string' && |
| 45 | + typeof message.payload?.clientX === 'number' && |
| 46 | + typeof message.payload?.clientY === 'number' |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
| 50 | +export const useMacWebviewDragBridge = ( |
| 51 | + dropRef: React.MutableRefObject<null>, |
| 52 | + addNewShape: (type: ShapeType, x: number, y: number) => void |
| 53 | +) => { |
| 54 | + const { stageRef } = useCanvasContext(); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + if (!shouldUseMacWebviewDragBridge()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const handleGalleryDrop = (event: MessageEvent): void => { |
| 62 | + if (!isGalleryDropMessage(event.data)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + const { shapeType, clientX, clientY } = event.data.payload; |
| 66 | + |
| 67 | + const dropDivElement = dropRef.current as HTMLDivElement | null; |
| 68 | + const stageInstance = stageRef.current; |
| 69 | + if (!dropDivElement || !stageInstance) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const screenPosition = { x: clientX, y: clientY }; |
| 74 | + if (!isScreenPositionInsideDivElement(dropDivElement, screenPosition)) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const relativeDivPosition = portScreenPositionToDivCoordinates( |
| 79 | + dropDivElement, |
| 80 | + screenPosition |
| 81 | + ); |
| 82 | + const { scrollLeft, scrollTop } = getScrollFromDiv( |
| 83 | + dropRef as unknown as React.MutableRefObject<HTMLDivElement> |
| 84 | + ); |
| 85 | + const konvaCoordinate = convertFromDivElementCoordsToKonvaCoords( |
| 86 | + stageInstance, |
| 87 | + { |
| 88 | + screenPosition, |
| 89 | + relativeDivPosition, |
| 90 | + scroll: { x: scrollLeft, y: scrollTop }, |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + const shapeOffsetX = calculateShapeOffsetToXDropCoordinate( |
| 95 | + konvaCoordinate.x, |
| 96 | + shapeType as ShapeType |
| 97 | + ); |
| 98 | + const positionX = konvaCoordinate.x - shapeOffsetX; |
| 99 | + const positionY = konvaCoordinate.y; |
| 100 | + |
| 101 | + addNewShape(shapeType as ShapeType, positionX, positionY); |
| 102 | + }; |
| 103 | + |
| 104 | + window.addEventListener('message', handleGalleryDrop); |
| 105 | + return () => { |
| 106 | + window.removeEventListener('message', handleGalleryDrop); |
| 107 | + }; |
| 108 | + }, []); |
| 109 | + |
| 110 | + useEffect(() => { |
| 111 | + if (!shouldUseMacWebviewDragBridge()) { |
| 112 | + return; |
| 113 | + } |
| 114 | + const handleDragOver = (event: DragEvent): void => { |
| 115 | + notifyDragMoveToWebviewShell(event.clientX, event.clientY); |
| 116 | + }; |
| 117 | + document.addEventListener('dragover', handleDragOver, true); |
| 118 | + return () => { |
| 119 | + document.removeEventListener('dragover', handleDragOver, true); |
| 120 | + }; |
| 121 | + }, []); |
| 122 | +}; |
0 commit comments