diff --git a/packages/sanity/src/core/form/inputs/common/fileTarget/fileTarget.tsx b/packages/sanity/src/core/form/inputs/common/fileTarget/fileTarget.tsx index 041f0407020..bd8e69d3377 100644 --- a/packages/sanity/src/core/form/inputs/common/fileTarget/fileTarget.tsx +++ b/packages/sanity/src/core/form/inputs/common/fileTarget/fileTarget.tsx @@ -20,6 +20,25 @@ export type FileInfo = { type: DataTransferItem['type'] // mime type of file or string } +type CamelToKebab = S extends `${infer P1}${infer P2}` + ? P2 extends Uncapitalize + ? `${Lowercase}${CamelToKebab}` + : `${Lowercase}-${CamelToKebab>}` + : S + +type DataAttribute = `data-${CamelToKebab}` + +const fileTargetAttributeName = 'isFileTarget' +const fileTargetDataAttribute: Record, 'true'> = { + 'data-is-file-target': 'true', +} + +/** + * @internal + */ +export const isFileTargetElement = (el: HTMLElement): boolean => + el?.dataset?.[fileTargetAttributeName] === 'true' + type Props = { // Triggered when the target component receives one or more files, either originating from a drop event or a paste event onFiles?: (files: File[]) => void @@ -189,6 +208,7 @@ export function fileTarget(Component: ComponentType {!disabled && showPasteInput && (
diff --git a/packages/sanity/src/core/hooks/useGlobalCopyPasteElementHandler.ts b/packages/sanity/src/core/hooks/useGlobalCopyPasteElementHandler.ts index c6db8dfe6f2..a9c5d0e4ca1 100644 --- a/packages/sanity/src/core/hooks/useGlobalCopyPasteElementHandler.ts +++ b/packages/sanity/src/core/hooks/useGlobalCopyPasteElementHandler.ts @@ -3,6 +3,7 @@ import {isHotkey} from 'is-hotkey-esm' import {useCallback, useEffect, useRef} from 'react' import {type FormDocumentValue} from 'sanity' +import {isFileTargetElement} from '../form/inputs/common/fileTarget/fileTarget' import {useCopyPaste} from '../studio/copyPaste' import {hasSelection, isEmptyFocusPath, isNativeEditableElement} from '../studio/copyPaste/utils' @@ -56,7 +57,8 @@ export function useGlobalCopyPasteElementHandler({ if (isPasteHotKey(event)) { if ( isNativeEditableElement(targetElement as HTMLElement) || - isEmptyFocusPath(focusPathRef.current) + isEmptyFocusPath(focusPathRef.current) || + isFileTargetElement(targetElement as HTMLElement) ) { return }