|
| 1 | +import * as React from 'react'; |
| 2 | +import styles from './DragDropFiles.module.scss'; |
| 3 | +import { Icon } from 'office-ui-fabric-react/lib/Icon'; |
| 4 | +import { IDragDropFilesState, IDragDropFilesProps } from './IDragDropFiles'; |
| 5 | +import * as strings from 'ControlStrings'; |
| 6 | + |
| 7 | +/** |
| 8 | + * DragDropFiles Class Control |
| 9 | + */ |
| 10 | +export class DragDropFiles extends React.Component<IDragDropFilesProps, IDragDropFilesState> { |
| 11 | + private dragCounter = 0; |
| 12 | + private dropRef = React.createRef<HTMLDivElement>(); |
| 13 | + private _LabelMessage; |
| 14 | + private _IconName; |
| 15 | + private _dropEffect; |
| 16 | + private _enable; |
| 17 | + |
| 18 | + constructor(props: IDragDropFilesProps) { |
| 19 | + super(props); |
| 20 | + // Initialize state |
| 21 | + this.state = { |
| 22 | + dragStatus: false |
| 23 | + }; |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Lifecycle hook when component is mounted |
| 29 | + */ |
| 30 | + public componentDidMount(): void { |
| 31 | + const { dropEffect, enable } = this.props; |
| 32 | + if (dropEffect == undefined || dropEffect == "") { |
| 33 | + this._dropEffect = "copy"; |
| 34 | + } else { |
| 35 | + this._dropEffect = dropEffect; |
| 36 | + } |
| 37 | + if (enable == undefined) { |
| 38 | + this._enable = true; |
| 39 | + } else { |
| 40 | + this._enable = enable; |
| 41 | + } |
| 42 | + // Add EventListeners for drag zone area |
| 43 | + let divDropArea = this.dropRef.current; |
| 44 | + if(this._enable == true) |
| 45 | + { |
| 46 | + divDropArea.addEventListener('dragenter', this.handleonDragEnter); |
| 47 | + divDropArea.addEventListener('dragleave', this.handleonDragLeave); |
| 48 | + divDropArea.addEventListener('dragover', this.handleonDragOver); |
| 49 | + divDropArea.addEventListener('drop', this.handleonDrop); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Stop listeners from onDragOver event. |
| 55 | + * @param e |
| 56 | + */ |
| 57 | + private handleonDragOver = (e) => { |
| 58 | + e.preventDefault(); |
| 59 | + e.stopPropagation(); |
| 60 | + if (e.dataTransfer.items && e.dataTransfer.items.length > 0) { |
| 61 | + e.dataTransfer.dropEffect = e.dataTransfer.dropEffect = this.props.dropEffect; |
| 62 | + } |
| 63 | + } |
| 64 | + /** |
| 65 | + * Stop listeners from onDragEnter event, enable drag and drop view. |
| 66 | + * @param e |
| 67 | + */ |
| 68 | + private handleonDragEnter = (e) => { |
| 69 | + e.preventDefault(); |
| 70 | + e.stopPropagation(); |
| 71 | + this.dragCounter++; |
| 72 | + if (e.dataTransfer.items && e.dataTransfer.items.length > 0) { |
| 73 | + e.dataTransfer.dropEffect = this._dropEffect; |
| 74 | + this.setState({ dragStatus: true }); |
| 75 | + } |
| 76 | + } |
| 77 | + /** |
| 78 | + * Stop listeners from ondragenter event, disable drag and drop view. |
| 79 | + * @param e |
| 80 | + */ |
| 81 | + private handleonDragLeave = (e) => { |
| 82 | + e.preventDefault(); |
| 83 | + e.stopPropagation(); |
| 84 | + this.dragCounter--; |
| 85 | + if (this.dragCounter === 0) { |
| 86 | + this.setState({ dragStatus: false }); |
| 87 | + } |
| 88 | + } |
| 89 | + /** |
| 90 | + * Stop listeners from onDrop event and load files to property onDrop. |
| 91 | + * @param e |
| 92 | + */ |
| 93 | + private handleonDrop = async (e) => { |
| 94 | + e.preventDefault(); |
| 95 | + e.stopPropagation(); |
| 96 | + this.setState({ dragStatus: false }); |
| 97 | + if (e.dataTransfer && e.dataTransfer.items) { |
| 98 | + this.props.onDrop(await this.getFilesAsync(e)); |
| 99 | + } |
| 100 | + e.dataTransfer.clearData(); |
| 101 | + this.dragCounter = 0; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Add File to Array Files of type File[] |
| 106 | + * https://www.meziantou.net/upload-files-and-directories-using-an-input-drag-and-drop-or-copy-and-paste-with.htm |
| 107 | + * @param dataTransfer |
| 108 | + */ |
| 109 | + private getFilesAsync = async (e) => { |
| 110 | + const Customfiles = e.dataTransfer; |
| 111 | + const items = Customfiles.items; |
| 112 | + const Directory = []; |
| 113 | + let entry:any; |
| 114 | + const files: File[] = []; |
| 115 | + for (let i = 0; i < items.length; i++) { |
| 116 | + const item = items[i]; |
| 117 | + if (item.kind === "file") { |
| 118 | + /** |
| 119 | + * This method retrieves Files from Folders |
| 120 | + * defensive code to only use method when exist in browser if not only return files. |
| 121 | + * https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry |
| 122 | + */ |
| 123 | + if (item.getAsEntry) { |
| 124 | + entry = item.getAsEntry(); |
| 125 | + if (entry.isDirectory) { |
| 126 | + Directory.push(entry); |
| 127 | + } else { |
| 128 | + const file = item.getAsFile(); |
| 129 | + if (file) { |
| 130 | + file.fullPath = ""; |
| 131 | + files.push(file); |
| 132 | + } |
| 133 | + } |
| 134 | + } else if (item.webkitGetAsEntry) { |
| 135 | + entry = item.webkitGetAsEntry(); |
| 136 | + if (entry.isDirectory) { |
| 137 | + Directory.push(entry); |
| 138 | + } else { |
| 139 | + const file = item.getAsFile(); |
| 140 | + if (file) { |
| 141 | + file.fullPath = ""; |
| 142 | + files.push(file); |
| 143 | + } |
| 144 | + } |
| 145 | + } else if ("function" == typeof item.getAsFile) { |
| 146 | + const file = item.getAsFile(); |
| 147 | + if (file) { |
| 148 | + file.fullPath = ""; |
| 149 | + files.push(file); |
| 150 | + } |
| 151 | + } |
| 152 | + continue; |
| 153 | + } |
| 154 | + } |
| 155 | + if (Directory.length > 0) { |
| 156 | + const entryContent = await this.readEntryContentAsync(Directory); |
| 157 | + files.push(...entryContent); |
| 158 | + } |
| 159 | + return files; |
| 160 | + } |
| 161 | + |
| 162 | + // Returns a promise with all the files of the directory hierarchy |
| 163 | + /** |
| 164 | + * |
| 165 | + * @param entry |
| 166 | + */ |
| 167 | + private readEntryContentAsync = (Directory) => { |
| 168 | + return new Promise<File[]>((resolve, reject) => { |
| 169 | + let reading = 0; |
| 170 | + const contents: File[] = []; |
| 171 | + Directory.forEach(entry => { |
| 172 | + readEntry(entry, ""); |
| 173 | + }); |
| 174 | + |
| 175 | + function readEntry(entry, path) { |
| 176 | + if (entry.isDirectory) { |
| 177 | + readReaderContent(entry.createReader()); |
| 178 | + } else { |
| 179 | + reading++; |
| 180 | + entry.file(file => { |
| 181 | + reading--; |
| 182 | + file.fullPath = path; |
| 183 | + contents.push(file); |
| 184 | + |
| 185 | + if (reading === 0) { |
| 186 | + resolve(contents); |
| 187 | + } |
| 188 | + }); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + function readReaderContent(reader) { |
| 193 | + reading++; |
| 194 | + |
| 195 | + reader.readEntries((entries) => { |
| 196 | + reading--; |
| 197 | + for (const entry of entries) { |
| 198 | + readEntry(entry, entry.fullPath); |
| 199 | + } |
| 200 | + |
| 201 | + if (reading === 0) { |
| 202 | + resolve(contents); |
| 203 | + } |
| 204 | + }); |
| 205 | + } |
| 206 | + }); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Default React component render method |
| 211 | + */ |
| 212 | + public render(): React.ReactElement<IDragDropFilesProps> { |
| 213 | + let { dragStatus } = this.state; |
| 214 | + let { labelMessage, iconName } = this.props; |
| 215 | + |
| 216 | + if (labelMessage == undefined || labelMessage == "") { |
| 217 | + this._LabelMessage = ""; |
| 218 | + } else { |
| 219 | + this._LabelMessage = labelMessage; |
| 220 | + } |
| 221 | + if (iconName == undefined || iconName == "") { |
| 222 | + this._IconName = ""; |
| 223 | + } else { |
| 224 | + this._IconName = iconName; |
| 225 | + } |
| 226 | + return ( |
| 227 | + <div className={(dragStatus && this._enable) ? styles.DragDropFilesArea : styles.DragDropFilesAreaLeave} |
| 228 | + ref={this.dropRef}> |
| 229 | + {(dragStatus && this._enable) && |
| 230 | + <div className={styles.DragDropFilesAreaBorder}> |
| 231 | + <div className={styles.DragDropFilesAreaZone}> |
| 232 | + <Icon iconName={this._IconName} className="ms-IconExample" /> |
| 233 | + <div>{this._LabelMessage}</div> |
| 234 | + </div> |
| 235 | + </div> |
| 236 | + } |
| 237 | + {this.props.children} |
| 238 | + </div> |
| 239 | + ); |
| 240 | + } |
| 241 | +} |
0 commit comments