|
| 1 | + |
| 2 | +function dragEvent(event) { |
| 3 | + event.stopPropagation(); |
| 4 | + event.preventDefault(); |
| 5 | +} |
| 6 | +const DRAGEVENTS = ['dragstart', 'dragenter', 'dragend', 'dragleave', 'dragover']; |
| 7 | + |
| 8 | +let uid = 0; |
| 9 | + |
| 10 | +const uuid = () => { |
| 11 | + uid++; |
| 12 | + return uid; |
| 13 | +}; |
| 14 | + |
| 15 | +function readFileItems(fileItems, callback) { |
| 16 | + let dirs = []; |
| 17 | + const files = []; |
| 18 | + for (let i = 0, len = fileItems.length; i < len; i++) { |
| 19 | + const item = fileItems[i]; |
| 20 | + const fileEntry = item.webkitGetAsEntry ? item.webkitGetAsEntry() : item; |
| 21 | + fileEntry.id = uuid(); |
| 22 | + fileEntry.path = fileEntry.path || '/'; |
| 23 | + if (fileEntry.isDirectory) { |
| 24 | + dirs.push(fileEntry); |
| 25 | + } |
| 26 | + files.push(fileEntry); |
| 27 | + } |
| 28 | + let fileEntryList = [], isReading = false, idx = 0; |
| 29 | + const readFiles = () => { |
| 30 | + idx = 0; |
| 31 | + const readFile = () => { |
| 32 | + if (idx < files.length) { |
| 33 | + const fileEntry = files[idx]; |
| 34 | + if (fileEntry.isDirectory) { |
| 35 | + idx++; |
| 36 | + readFile(); |
| 37 | + } else { |
| 38 | + fileEntry.file((file) => { |
| 39 | + file.path = fileEntry.path || '/'; |
| 40 | + file.parentName = fileEntry.parentName; |
| 41 | + file.id = fileEntry.id; |
| 42 | + file.pid = fileEntry.pid; |
| 43 | + file.isDirectory = !!file.isDirectory; |
| 44 | + files[idx] = file; |
| 45 | + idx++; |
| 46 | + readFile(); |
| 47 | + }); |
| 48 | + } |
| 49 | + } else { |
| 50 | + callback(files); |
| 51 | + } |
| 52 | + }; |
| 53 | + readFile(); |
| 54 | + }; |
| 55 | + |
| 56 | + const read = () => { |
| 57 | + if (idx < dirs.length) { |
| 58 | + const dirEntry = dirs[idx]; |
| 59 | + const dirRender = dirEntry.createReader(); |
| 60 | + const readDir = () => { |
| 61 | + dirRender.readEntries((results) => { |
| 62 | + if (results.length) { |
| 63 | + results.forEach(fileEntry => { |
| 64 | + fileEntry.id = uuid(); |
| 65 | + fileEntry.pid = dirEntry.id; |
| 66 | + fileEntry.path = fileEntry.path || ''; |
| 67 | + fileEntry.parentName = dirEntry.name; |
| 68 | + if (dirEntry.path) { |
| 69 | + fileEntry.path += dirEntry.path; |
| 70 | + } |
| 71 | + fileEntry.path += (dirEntry.name + '/'); |
| 72 | + }); |
| 73 | + fileEntryList = fileEntryList.concat(results); |
| 74 | + readDir(); |
| 75 | + } else { |
| 76 | + idx++; |
| 77 | + read(); |
| 78 | + } |
| 79 | + }); |
| 80 | + }; |
| 81 | + readDir(); |
| 82 | + } else { |
| 83 | + const tempDirs = []; |
| 84 | + fileEntryList.forEach(fileEntry => { |
| 85 | + files.push(fileEntry); |
| 86 | + if (fileEntry.isDirectory) { |
| 87 | + tempDirs.push(fileEntry); |
| 88 | + } |
| 89 | + }); |
| 90 | + dirs = tempDirs; |
| 91 | + isReading = false; |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + const id = setInterval(() => { |
| 96 | + if (dirs.length === 0) { |
| 97 | + clearInterval(id); |
| 98 | + readFiles(); |
| 99 | + } else if (!isReading) { |
| 100 | + isReading = true; |
| 101 | + fileEntryList = []; |
| 102 | + idx = 0; |
| 103 | + read(); |
| 104 | + } |
| 105 | + }, 1); |
| 106 | +} |
| 107 | + |
| 108 | +export class FileDND { |
| 109 | + constructor(ele) { |
| 110 | + if (!ele || !(ele instanceof HTMLElement)) { |
| 111 | + console.error('ele is error,It should be HTMLElement instance'); |
| 112 | + return; |
| 113 | + } |
| 114 | + this.ele = ele; |
| 115 | + this.files = []; |
| 116 | + this._bindEvents = false; |
| 117 | + } |
| 118 | + |
| 119 | + dnd(callback) { |
| 120 | + if (!this.ele) { |
| 121 | + console.error('not find ele'); |
| 122 | + return; |
| 123 | + } |
| 124 | + if (!callback) { |
| 125 | + console.error('callback is null'); |
| 126 | + return; |
| 127 | + } |
| 128 | + if (!this._bindEvents) { |
| 129 | + DRAGEVENTS.forEach(eventName => { |
| 130 | + this.ele.addEventListener(eventName, dragEvent); |
| 131 | + }); |
| 132 | + const dropEvent = (event) => { |
| 133 | + event.preventDefault(); |
| 134 | + const df = event.dataTransfer; |
| 135 | + const items = df.items; |
| 136 | + readFileItems(items, (files) => { |
| 137 | + const callback = this.dndBackCall.bind(this); |
| 138 | + this.files = files; |
| 139 | + callback(files.filter(file => { |
| 140 | + return file instanceof File; |
| 141 | + })); |
| 142 | + }); |
| 143 | + }; |
| 144 | + this.dropEvent = dropEvent; |
| 145 | + this._bindEvents = true; |
| 146 | + this.ele.addEventListener('drop', dropEvent); |
| 147 | + } |
| 148 | + this.dndBackCall = callback; |
| 149 | + } |
| 150 | + |
| 151 | + dispose() { |
| 152 | + if (this._bindEvents) { |
| 153 | + DRAGEVENTS.forEach(eventName => { |
| 154 | + this.ele.removeEventListener(eventName, dragEvent); |
| 155 | + }); |
| 156 | + this.ele.removeEventListener('drop', this.dropEvent); |
| 157 | + } |
| 158 | + this.ele = null; |
| 159 | + this.files = null; |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + toTree() { |
| 164 | + const files = this.files; |
| 165 | + const fileMap = {}; |
| 166 | + files.forEach(file => { |
| 167 | + const { id, path, name, parentName } = file; |
| 168 | + if (!fileMap[id]) { |
| 169 | + fileMap[id] = { |
| 170 | + id, |
| 171 | + name, |
| 172 | + label: name, |
| 173 | + path, |
| 174 | + parentName: parentName, |
| 175 | + children: [], |
| 176 | + isDirectory: file.isDirectory |
| 177 | + }; |
| 178 | + } |
| 179 | + }); |
| 180 | + files.forEach(file => { |
| 181 | + const { pid, id } = file; |
| 182 | + if (fileMap[pid]) { |
| 183 | + fileMap[pid].children.push(fileMap[id]); |
| 184 | + } |
| 185 | + }); |
| 186 | + for (const id in fileMap) { |
| 187 | + const children = fileMap[id].children; |
| 188 | + const dirs = [], files = []; |
| 189 | + children.forEach(child => { |
| 190 | + if (child.isDirectory) { |
| 191 | + dirs.push(child); |
| 192 | + } else { |
| 193 | + files.push(child); |
| 194 | + } |
| 195 | + }); |
| 196 | + fileMap[id].children = dirs.concat(files); |
| 197 | + } |
| 198 | + return Object.values(fileMap).filter(d => { |
| 199 | + return !d.parentName; |
| 200 | + }); |
| 201 | + |
| 202 | + } |
| 203 | +} |
0 commit comments