-
Notifications
You must be signed in to change notification settings - Fork 21
Fix/#847 MacOS drag and drop shapes #848
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0bfd6f0
feat: implement macOS drag-and-drop bridge for VS Code webview
Ivanruii c955c05
chore: added changeset
Ivanruii 113cd57
feat: enhance macOS detection
Ivanruii d6fa583
feat: implement macOS drag-and-drop enhancements for VS Code webview
Ivanruii 884871f
refactor: streamline macOS drag-and-drop comments and logic for clarity
Ivanruii bca1805
feat: add utility to check if screen position is inside a div element…
Ivanruii a33a04d
fix: improve drag start handling and native drag preview for macOS
Ivanruii e193472
Merge branch 'dev' into fix/#847-macos-drag-and-drop
Ivanruii 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 hidden or 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,8 @@ | ||
| --- | ||
| 'quickmock': patch | ||
| --- | ||
|
|
||
| Fix component-gallery drag-and-drop in the VS Code extension on macOS, | ||
| where HTML5 drag events targeting the inner iframe were dispatched to | ||
| the webview shell instead of into the iframe (microsoft/vscode#193558). | ||
| Linux and Windows are unaffected. |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| export function isMacOS() { | ||
| return navigator.userAgent.toLowerCase().includes('mac'); | ||
| interface NavigatorWithUserAgentData extends Navigator { | ||
| userAgentData?: { platform: string }; | ||
| } | ||
|
|
||
| export function isWindowsOrLinux() { | ||
| return !isMacOS(); | ||
| export function isMacOS(): boolean { | ||
| const userAgentData = (navigator as NavigatorWithUserAgentData).userAgentData; | ||
| if (userAgentData?.platform) { | ||
| return userAgentData.platform === 'macOS'; | ||
| } | ||
| // Fallback for runtimes without UA-CH (Firefox, Safari, older Chromium). | ||
| return /Mac/i.test(navigator.userAgent); | ||
| } |
This file contains hidden or 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 hidden or 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,72 @@ | ||
| import { isMacOS } from '#common/helpers/platform.helpers.ts'; | ||
| import { isVSCodeEnv } from '#common/utils/env.utils'; | ||
| import { parentOrigin } from '#common/utils/vscode-bridge.utils'; | ||
| import { ShapeType } from '#core/model'; | ||
| import { | ||
| type DragBridgeAppMessage, | ||
| DRAG_BRIDGE_MESSAGE_TYPE, | ||
| } from '@lemoncode/quickmock-bridge-protocol'; | ||
|
|
||
| // macOS workaround for microsoft/vscode#193558: the native HTML5 drag preview | ||
| // from the nested iframe is unreliable, so the shell paints its own preview | ||
| // from a thumbnail data URL the iframe sends on drag-start. | ||
| export const shouldUseMacWebviewDragBridge = (): boolean => { | ||
| return isVSCodeEnv() && isMacOS(); | ||
| }; | ||
|
|
||
| const postMessageToWebviewShell = (message: DragBridgeAppMessage): void => { | ||
| window.parent.postMessage(message, parentOrigin); | ||
| }; | ||
|
|
||
| export const notifyDragStartToWebviewShell = ( | ||
| shapeType: ShapeType, | ||
| thumbnailDataUrl: string | ||
| ): void => { | ||
| if (!shouldUseMacWebviewDragBridge()) { | ||
| return; | ||
| } | ||
| postMessageToWebviewShell({ | ||
| type: DRAG_BRIDGE_MESSAGE_TYPE.DRAG_START, | ||
| payload: { shapeType, thumbnailDataUrl }, | ||
| }); | ||
| }; | ||
|
|
||
| export const notifyDragMoveToWebviewShell = ( | ||
| clientX: number, | ||
| clientY: number | ||
| ): void => { | ||
| if (!shouldUseMacWebviewDragBridge()) { | ||
| return; | ||
| } | ||
| postMessageToWebviewShell({ | ||
| type: DRAG_BRIDGE_MESSAGE_TYPE.DRAG_MOVE, | ||
| payload: { clientX, clientY }, | ||
| }); | ||
| }; | ||
|
|
||
| export const notifyDragEndToWebviewShell = (): void => { | ||
| if (!shouldUseMacWebviewDragBridge()) { | ||
| return; | ||
| } | ||
| postMessageToWebviewShell({ type: DRAG_BRIDGE_MESSAGE_TYPE.DRAG_END }); | ||
| }; | ||
|
|
||
| const thumbnailDataUrlCache = new Map<string, Promise<string>>(); | ||
|
|
||
| export const loadThumbnailAsDataUrl = (src: string): Promise<string> => { | ||
| const cached = thumbnailDataUrlCache.get(src); | ||
| if (cached) return cached; | ||
| const promise = fetch(src) | ||
| .then(response => response.blob()) | ||
| .then( | ||
| blob => | ||
| new Promise<string>((resolve, reject) => { | ||
| const reader = new FileReader(); | ||
| reader.onload = () => resolve(reader.result as string); | ||
| reader.onerror = () => reject(reader.error); | ||
| reader.readAsDataURL(blob); | ||
| }) | ||
| ); | ||
| thumbnailDataUrlCache.set(src, promise); | ||
| return promise; | ||
| }; |
122 changes: 122 additions & 0 deletions
122
apps/web/src/core/vscode/use-mac-webview-drag-bridge.hook.ts
This file contains hidden or 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,122 @@ | ||
| import { ShapeType } from '#core/model'; | ||
| import { useCanvasContext } from '#core/providers'; | ||
| import { | ||
| convertFromDivElementCoordsToKonvaCoords, | ||
| getScrollFromDiv, | ||
| isScreenPositionInsideDivElement, | ||
| portScreenPositionToDivCoordinates, | ||
| } from '#pods/canvas/canvas.util'; | ||
| import { calculateShapeOffsetToXDropCoordinate } from '#pods/canvas/use-monitor.business'; | ||
| import { | ||
| type DragBridgeHostMessage, | ||
| DRAG_BRIDGE_MESSAGE_TYPE, | ||
| } from '@lemoncode/quickmock-bridge-protocol'; | ||
| import { useEffect } from 'react'; | ||
| import { | ||
| notifyDragMoveToWebviewShell, | ||
| shouldUseMacWebviewDragBridge, | ||
| } from './mac-webview-drag-bridge.utils'; | ||
|
|
||
| // macOS workaround for microsoft/vscode#193558: drag events on the inner | ||
| // iframe route to the shell, so the shell-side bridge captures the drop and | ||
| // forwards coordinates here; this reproduces the insertion useMonitorShape | ||
| // performs natively on other platforms. | ||
|
|
||
| type GalleryDropMessage = Extract< | ||
| DragBridgeHostMessage, | ||
| { type: typeof DRAG_BRIDGE_MESSAGE_TYPE.GALLERY_DROP } | ||
| >; | ||
|
|
||
| const isGalleryDropMessage = (data: unknown): data is GalleryDropMessage => { | ||
| if (!data || typeof data !== 'object') { | ||
| return false; | ||
| } | ||
| const message = data as { | ||
| type?: unknown; | ||
| payload?: { | ||
| shapeType?: unknown; | ||
| clientX?: unknown; | ||
| clientY?: unknown; | ||
| }; | ||
| }; | ||
| return ( | ||
| message.type === DRAG_BRIDGE_MESSAGE_TYPE.GALLERY_DROP && | ||
| typeof message.payload?.shapeType === 'string' && | ||
| typeof message.payload?.clientX === 'number' && | ||
| typeof message.payload?.clientY === 'number' | ||
| ); | ||
| }; | ||
|
|
||
| export const useMacWebviewDragBridge = ( | ||
| dropRef: React.MutableRefObject<null>, | ||
| addNewShape: (type: ShapeType, x: number, y: number) => void | ||
| ) => { | ||
| const { stageRef } = useCanvasContext(); | ||
|
|
||
| useEffect(() => { | ||
| if (!shouldUseMacWebviewDragBridge()) { | ||
| return; | ||
| } | ||
|
|
||
| const handleGalleryDrop = (event: MessageEvent): void => { | ||
| if (!isGalleryDropMessage(event.data)) { | ||
| return; | ||
| } | ||
| const { shapeType, clientX, clientY } = event.data.payload; | ||
|
|
||
| const dropDivElement = dropRef.current as HTMLDivElement | null; | ||
| const stageInstance = stageRef.current; | ||
| if (!dropDivElement || !stageInstance) { | ||
| return; | ||
| } | ||
|
|
||
| const screenPosition = { x: clientX, y: clientY }; | ||
| if (!isScreenPositionInsideDivElement(dropDivElement, screenPosition)) { | ||
| return; | ||
| } | ||
|
|
||
| const relativeDivPosition = portScreenPositionToDivCoordinates( | ||
| dropDivElement, | ||
| screenPosition | ||
| ); | ||
| const { scrollLeft, scrollTop } = getScrollFromDiv( | ||
| dropRef as unknown as React.MutableRefObject<HTMLDivElement> | ||
| ); | ||
| const konvaCoordinate = convertFromDivElementCoordsToKonvaCoords( | ||
| stageInstance, | ||
| { | ||
| screenPosition, | ||
| relativeDivPosition, | ||
| scroll: { x: scrollLeft, y: scrollTop }, | ||
| } | ||
| ); | ||
|
|
||
| const shapeOffsetX = calculateShapeOffsetToXDropCoordinate( | ||
| konvaCoordinate.x, | ||
| shapeType as ShapeType | ||
| ); | ||
| const positionX = konvaCoordinate.x - shapeOffsetX; | ||
| const positionY = konvaCoordinate.y; | ||
|
|
||
| addNewShape(shapeType as ShapeType, positionX, positionY); | ||
| }; | ||
|
|
||
| window.addEventListener('message', handleGalleryDrop); | ||
| return () => { | ||
| window.removeEventListener('message', handleGalleryDrop); | ||
| }; | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!shouldUseMacWebviewDragBridge()) { | ||
| return; | ||
| } | ||
| const handleDragOver = (event: DragEvent): void => { | ||
| notifyDragMoveToWebviewShell(event.clientX, event.clientY); | ||
| }; | ||
| document.addEventListener('dragover', handleDragOver, true); | ||
| return () => { | ||
| document.removeEventListener('dragover', handleDragOver, true); | ||
| }; | ||
| }, []); | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Fixed