Skip to content

Commit 6fc602c

Browse files
authored
fix(core): early return if paste target is a file/image (#7269)
1 parent 6f84e1f commit 6fc602c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/sanity/src/core/form/inputs/common/fileTarget/fileTarget.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ export type FileInfo = {
2020
type: DataTransferItem['type'] // mime type of file or string
2121
}
2222

23+
type CamelToKebab<S extends string> = S extends `${infer P1}${infer P2}`
24+
? P2 extends Uncapitalize<P2>
25+
? `${Lowercase<P1>}${CamelToKebab<P2>}`
26+
: `${Lowercase<P1>}-${CamelToKebab<Uncapitalize<P2>>}`
27+
: S
28+
29+
type DataAttribute<S extends string> = `data-${CamelToKebab<S>}`
30+
31+
const fileTargetAttributeName = 'isFileTarget'
32+
const fileTargetDataAttribute: Record<DataAttribute<typeof fileTargetAttributeName>, 'true'> = {
33+
'data-is-file-target': 'true',
34+
}
35+
36+
/**
37+
* @internal
38+
*/
39+
export const isFileTargetElement = (el: HTMLElement): boolean =>
40+
el?.dataset?.[fileTargetAttributeName] === 'true'
41+
2342
type Props = {
2443
// Triggered when the target component receives one or more files, either originating from a drop event or a paste event
2544
onFiles?: (files: File[]) => void
@@ -189,6 +208,7 @@ export function fileTarget<ComponentProps>(Component: ComponentType<ComponentPro
189208
onDragLeave={disabled ? undefined : handleDragLeave}
190209
onDrop={disabled ? undefined : handleDrop}
191210
data-test-id="file-target"
211+
{...fileTargetDataAttribute}
192212
/>
193213
{!disabled && showPasteInput && (
194214
<div contentEditable onPaste={handlePaste} ref={pasteInput} style={PASTE_INPUT_STYLE} />

packages/sanity/src/core/hooks/useGlobalCopyPasteElementHandler.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {isHotkey} from 'is-hotkey-esm'
33
import {useCallback, useEffect, useRef} from 'react'
44
import {type FormDocumentValue} from 'sanity'
55

6+
import {isFileTargetElement} from '../form/inputs/common/fileTarget/fileTarget'
67
import {useCopyPaste} from '../studio/copyPaste'
78
import {hasSelection, isEmptyFocusPath, isNativeEditableElement} from '../studio/copyPaste/utils'
89

@@ -56,7 +57,8 @@ export function useGlobalCopyPasteElementHandler({
5657
if (isPasteHotKey(event)) {
5758
if (
5859
isNativeEditableElement(targetElement as HTMLElement) ||
59-
isEmptyFocusPath(focusPathRef.current)
60+
isEmptyFocusPath(focusPathRef.current) ||
61+
isFileTargetElement(targetElement as HTMLElement)
6062
) {
6163
return
6264
}

0 commit comments

Comments
 (0)