Skip to content
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

add html handling in native types #2949

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .yarn/versions/88ed6e63.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
releases:
react-dnd-documentation: patch
react-dnd-examples-decorators: patch
react-dnd-examples-hooks: patch
react-dnd-html5-backend: patch
1 change: 1 addition & 0 deletions packages/backend-html5/src/HTML5BackendImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ export class HTML5BackendImpl implements Backend {
this.dropTargetIds = []

if (this.isDraggingNativeItem()) {
e.preventDefault();
this.currentNativeSource?.loadDataTransfer(e.dataTransfer)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export const nativeTypesConfig: {
},
matchesTypes: ['Files'],
},
[NativeTypes.HTML]: {
exposeProperties: {
html: (dataTransfer: DataTransfer, matchesTypes: string[]): string => getDataFromDataTransfer(dataTransfer, matchesTypes, ''),
},
matchesTypes: ['Html', 'text/html'],
},
[NativeTypes.URL]: {
exposeProperties: {
urls: (dataTransfer: DataTransfer, matchesTypes: string[]): string[] =>
Expand Down
1 change: 1 addition & 0 deletions packages/backend-html5/src/NativeTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const FILE = '__NATIVE_FILE__'
export const URL = '__NATIVE_URL__'
export const TEXT = '__NATIVE_TEXT__'
export const HTML = '__NATIVE_HTML__'
10 changes: 10 additions & 0 deletions packages/docsite/markdown/examples/other/native-html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
path: '/examples/other/native-html'
title: 'Native Html'
---

Example demonstrating drag and drop of native html.
Works great for dragging images from another tab or iframe.

<view-source name="06-other/native-html" component="other-native-html">
</view-source>
4 changes: 4 additions & 0 deletions packages/docsite/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ export const ExamplePages: PageGroup[] = [
location: '/examples/other/native-files',
title: 'Native Files',
},
OTHER_HTML_FILES: {
location: '/examples/other/native-html',
title: 'Native Html',
},
},
},
{
Expand Down
27 changes: 27 additions & 0 deletions packages/examples-hooks/src/06-other/native-html/Container.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import { useState, useCallback } from 'react'
import { DropTargetMonitor } from 'react-dnd'
import { TargetBox } from './TargetBox'
import { HTMLContent } from './HTMLContent'

export const Container: React.FC = () => {
const [droppedHTML, setDroppedHTML] = useState<string>('')

const handleHTMLDrop = useCallback(
(item: any, monitor: DropTargetMonitor) => {
if (monitor) {
const html = monitor.getItem().html
setDroppedHTML(html)
}
},
[],
)

return (
<>
<iframe srcDoc={`<img src='https://react-dnd.github.io/react-dnd/favicon-32x32.png' />`} />
<TargetBox onDrop={handleHTMLDrop} />
<HTMLContent html={droppedHTML} />
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'

export interface HTMLContentProps {
html: string
}

export const HTMLContent: React.FC<HTMLContentProps> = ({ html }) => {
if (html.length === 0) {
return <div>Nothing to display</div>
}

return <div>
{html}
</div>
}
38 changes: 38 additions & 0 deletions packages/examples-hooks/src/06-other/native-html/TargetBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react'
import { NativeTypes } from 'react-dnd-html5-backend'
import { useDrop, DropTargetMonitor } from 'react-dnd'

const style: React.CSSProperties = {
border: '1px solid gray',
height: '15rem',
width: '15rem',
padding: '2rem',
textAlign: 'center',
}

export interface TargetBoxProps {
onDrop: (props: TargetBoxProps, monitor: DropTargetMonitor) => void
}

export const TargetBox: React.FC<TargetBoxProps> = (props) => {
const { onDrop } = props
const [{ canDrop, isOver }, drop] = useDrop({
accept: [NativeTypes.HTML],
drop(item, monitor) {
if (onDrop) {
onDrop(props, monitor)
}
},
collect: (monitor) => ({
isOver: monitor.isOver(),
canDrop: monitor.canDrop(),
}),
})

const isActive = canDrop && isOver
return (
<div ref={drop} style={style}>
{isActive ? 'Release to drop' : 'Drag HTML here'}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Container as default } from './Container'
2 changes: 2 additions & 0 deletions packages/examples-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import sortableStressTest from './04-sortable/stress-test'
import customizeDropEffects from './05-customize/drop-effects'
import customizeHandlesAndPreviews from './05-customize/handles-and-previews'
import otherNativeFiles from './06-other/native-files'
import otherNativeHtml from './06-other/native-html'
import dragSourceRerender from './07-regression/drag-source-rerender'
import remountWithCorrectProps from './07-regression/remount-with-correct-props'
import otherChainedConnectors from './07-regression/chained-connectors'
Expand All @@ -41,6 +42,7 @@ export const componentIndex: {
'other-drag-source-rerender': dragSourceRerender,
'other-remount-with-correct-props': remountWithCorrectProps,
'other-native-files': otherNativeFiles,
'other-native-html': otherNativeHtml,
'other-chained-connectors': otherChainedConnectors,
'other-previews-memory-leak': otherPreviewsMemoryLeak,
}