|
import type { GetOffsetFn } from '@atlaskit/pragmatic-drag-and-drop/dist/types/public-utils/element/custom-native-drag-preview/types'; |
|
import type { Input } from '@atlaskit/pragmatic-drag-and-drop/types'; |
|
import type { SystemStyleObject } from '@invoke-ai/ui-library'; |
|
import { noop } from 'lodash-es'; |
|
import type { CSSProperties } from 'react'; |
|
|
|
|
|
|
|
|
|
export const DND_IMAGE_DRAG_PREVIEW_SIZE = 32 satisfies SystemStyleObject['w']; |
|
|
|
|
|
|
|
|
|
|
|
export function preserveOffsetOnSourceFallbackCentered({ |
|
element, |
|
input, |
|
}: { |
|
element: HTMLElement; |
|
input: Input; |
|
}): GetOffsetFn { |
|
return ({ container }) => { |
|
const sourceRect = element.getBoundingClientRect(); |
|
const containerRect = container.getBoundingClientRect(); |
|
|
|
let offsetX = input.clientX - sourceRect.x; |
|
let offsetY = input.clientY - sourceRect.y; |
|
|
|
if (offsetY > containerRect.height || offsetX > containerRect.width) { |
|
offsetX = containerRect.width / 2; |
|
offsetY = containerRect.height / 2; |
|
} |
|
|
|
return { x: offsetX, y: offsetY }; |
|
}; |
|
} |
|
|
|
|
|
|
|
export function triggerPostMoveFlash(element: HTMLElement, backgroundColor: CSSProperties['backgroundColor']) { |
|
element.animate([{ backgroundColor }, {}], { |
|
duration: 700, |
|
easing: 'cubic-bezier(0.25, 0.1, 0.25, 1.0)', |
|
iterations: 1, |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const firefoxDndFix = (element: HTMLElement): (() => void) => { |
|
if (!navigator.userAgent.includes('Firefox')) { |
|
return noop; |
|
} |
|
|
|
const abortController = new AbortController(); |
|
|
|
element.addEventListener( |
|
'mouseover', |
|
(event) => { |
|
if (event.target instanceof HTMLTextAreaElement || event.target instanceof HTMLInputElement) { |
|
element.setAttribute('draggable', 'false'); |
|
} |
|
}, |
|
{ signal: abortController.signal } |
|
); |
|
|
|
element.addEventListener( |
|
'mouseout', |
|
(event) => { |
|
if (event.target instanceof HTMLTextAreaElement || event.target instanceof HTMLInputElement) { |
|
element.setAttribute('draggable', 'true'); |
|
} |
|
}, |
|
{ signal: abortController.signal } |
|
); |
|
|
|
return () => { |
|
element.setAttribute('draggable', 'true'); |
|
abortController.abort(); |
|
}; |
|
}; |
|
|