|
| 1 | +import * as UI from '../../lib' |
| 2 | + |
| 3 | +import { Canvas, Meta, Story, Props } from '@storybook/addon-docs/blocks'; |
| 4 | +import { action } from '@storybook/addon-actions' |
| 5 | + |
| 6 | +<Meta title="Drag & Drop"/> |
| 7 | + |
| 8 | +## Drag & Drop |
| 9 | + |
| 10 | +DragAndDrop has three functions that can be used to handle dragging and dropping of uris, files, and images. |
| 11 | + |
| 12 | +`makeDraggable` is used to make an element Draggable. Along with the HTML Element, it is also necessary to pass an |
| 13 | + object with a uri. This is used to set the data on the EventListener for 'dragstart'. |
| 14 | + |
| 15 | +`makeDropTarget` is used to make an element enabled as a drop target. Along with the HTML Element, it is also |
| 16 | + necessary to pass in two callback functions; one to handle draggable uris and the other to handle draggable files and images. |
| 17 | + |
| 18 | +`uploadFiles` is used to process the files that a user is uploading. |
| 19 | + |
| 20 | +Drag the message from the "Draggable" story or drag any file or image and drop it on the element in the "Drop Target" story to see it |
| 21 | + work. |
| 22 | + |
| 23 | +<Canvas> |
| 24 | + <Story name="Draggable"> |
| 25 | + {() => { |
| 26 | + const dragElement = document.createElement("div") |
| 27 | + dragElement.appendChild(document.createTextNode("Drag me to the target!")) |
| 28 | + const uri = new UI.rdf.NamedNode('https://exampleuser.inrupt.net') |
| 29 | + UI.widgets.makeDraggable(dragElement, uri) |
| 30 | + return dragElement |
| 31 | + }} |
| 32 | + </Story> |
| 33 | +</Canvas> |
| 34 | + |
| 35 | +<Canvas> |
| 36 | + <Story name="Drop Target"> |
| 37 | + {() => { |
| 38 | + const target = document.createElement("div") |
| 39 | + target.style = "padding: 1em; text-align:center; border: 1px solid black; width: 100px; height: 100px" |
| 40 | + target.appendChild(document.createTextNode("Drop things (URIs, files, ...) here")) |
| 41 | + UI.widgets.makeDropTarget(target, action('dropped uri'), action('dropped file')) |
| 42 | + return target |
| 43 | + }} |
| 44 | + </Story> |
| 45 | +</Canvas> |
| 46 | + |
| 47 | +## uploadFiles |
| 48 | + |
| 49 | +The function `uploadFiles` could for example be used in the droppedFileHandler function which is passed to the |
| 50 | + makeDropTarget function. |
| 51 | + |
| 52 | +<Canvas> |
| 53 | + <Story name="Upload Files" args={{ fileBase: "https://pod.example/Files", pictureBase: "https://pod.example/Pictures"}}> |
| 54 | + {({ fileBase, pictureBase }) => { |
| 55 | + const target = document.createElement("div") |
| 56 | + target.style = "padding: 1em; text-align:center; border: 1px solid black; width: 100px; height: 100px" |
| 57 | + target.appendChild(document.createTextNode("Drop a file (document, picture, ...) here")) |
| 58 | + UI.widgets.makeDropTarget(target, action('dropped uri'), (files) => { |
| 59 | + UI.widgets.uploadFiles(UI.store.fetcher, files, fileBase, pictureBase, action('file uploaded successfully')) |
| 60 | + }) |
| 61 | + return target |
| 62 | + }} |
| 63 | + </Story> |
| 64 | +</Canvas> |
| 65 | + |
| 66 | +<Props story="Upload Files" /> |
0 commit comments