Skip to content

Commit

Permalink
fix: filemetadata available on dragstart (#1610)
Browse files Browse the repository at this point in the history
* fix: filemetadata available on dragstart

* refactor: rename mutateItemByReadingDataTransfer to loadDataTransfer

This commit also streamlines the condition we use to pass the dataTransfer object to the NativeDragSource.

* refactor: streamline datatransfer passing

* docs: improve native-files typings

* fix: update loadDataTransfer to accept null

* fix: linting issue
  • Loading branch information
LeopoldLerch authored and darthtrevino committed Nov 22, 2019
1 parent 043e55a commit 1b5c2a3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/core/html5-backend/src/HTML5Backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ export default class HTML5Backend implements Backend {
)
}

private beginDragNativeItem(type: string) {
private beginDragNativeItem(type: string, dataTransfer?: DataTransfer) {
this.clearCurrentDragSourceNode()

this.currentNativeSource = createNativeDragSource(type)
this.currentNativeSource = createNativeDragSource(type, dataTransfer)
this.currentNativeHandle = this.registry.addSource(
type,
this.currentNativeSource,
Expand Down Expand Up @@ -499,7 +499,7 @@ export default class HTML5Backend implements Backend {

if (nativeType) {
// A native item (such as file or URL) dragged from outside the document
this.beginDragNativeItem(nativeType)
this.beginDragNativeItem(nativeType, dataTransfer as DataTransfer)
}
}

Expand Down Expand Up @@ -614,7 +614,7 @@ export default class HTML5Backend implements Backend {
e.preventDefault()

if (this.isDraggingNativeItem()) {
this.currentNativeSource!.mutateItemByReadingDataTransfer(e.dataTransfer)
this.currentNativeSource!.loadDataTransfer(e.dataTransfer)
}

this.enterLeaveCounter.reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class NativeDragSource {
public constructor(config: NativeItemConfig) {
this.config = config
this.item = {}
this.initializeExposedProperties()
}

private initializeExposedProperties() {
Object.keys(this.config.exposeProperties).forEach(property => {
Object.defineProperty(this.item, property, {
configurable: true, // This is needed to allow redefining it later
Expand All @@ -23,19 +27,21 @@ export class NativeDragSource {
})
}

public mutateItemByReadingDataTransfer(dataTransfer: DataTransfer | null) {
const newProperties: PropertyDescriptorMap = {}
public loadDataTransfer(dataTransfer: DataTransfer | null | undefined) {
if (dataTransfer) {
const newProperties: PropertyDescriptorMap = {}
Object.keys(this.config.exposeProperties).forEach(property => {
newProperties[property] = {
value: this.config.exposeProperties[property](
dataTransfer,
this.config.matchesTypes,
),
configurable: true,
enumerable: true,
}
})
Object.defineProperties(this.item, newProperties)
}
Object.defineProperties(this.item, newProperties)
}

public canDrag() {
Expand Down
9 changes: 7 additions & 2 deletions packages/core/html5-backend/src/NativeDragSources/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { nativeTypesConfig } from './nativeTypesConfig'
import { NativeDragSource } from './NativeDragSource'

export function createNativeDragSource(type: string): NativeDragSource {
return new NativeDragSource(nativeTypesConfig[type])
export function createNativeDragSource(
type: string,
dataTransfer?: DataTransfer,
): NativeDragSource {
const result = new NativeDragSource(nativeTypesConfig[type])
result.loadDataTransfer(dataTransfer)
return result
}

export function matchNativeItemType(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react'
import React, { useMemo } from 'react'

export interface FileListProps {
files: any[]
files: File[]
}

function list(files: any[]) {
const label = (file: { size: string; name: string; type: string }) =>
function list(files: File[]) {
const label = (file: File) =>
`'${file.name}' of size '${file.size}' and type '${file.type}'`
return files.map(file => <li key={file.name}>{label(file)}</li>)
}
Expand All @@ -14,7 +14,8 @@ const FileList: React.FC<FileListProps> = ({ files }) => {
if (files.length === 0) {
return <div>Nothing to display</div>
}
return <div>{list(files)}</div>
const fileList = useMemo(() => list(files), [files])
return <div>{fileList}</div>
}

export default FileList
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import TargetBox from './TargetBox'
import FileList from './FileList'

const Container: React.FC = () => {
const [droppedFiles, setDroppedFiles] = useState([])
const [droppedFiles, setDroppedFiles] = useState<File[]>([])

const handleFileDrop = useCallback(
(item: any, monitor: DropTargetMonitor) => {
Expand Down

0 comments on commit 1b5c2a3

Please sign in to comment.