Skip to content

Commit e2ca90e

Browse files
committed
init
1 parent b54e5a2 commit e2ca90e

7 files changed

+2051
-0
lines changed

.eslintignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
src/vendor
2+
dist
3+
src/libs
4+
build.js
5+
ftp.js
6+
clean.js
7+
siteclean.js
8+
gulpfile.js
9+
src/lib
10+
src/data
11+

.eslintrc.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
"env": {
3+
"browser": true,
4+
"es6": true,
5+
"node": true
6+
},
7+
"extends": "standard",
8+
"globals": {
9+
"Atomics": "readonly",
10+
"SharedArrayBuffer": "readonly",
11+
"AMap": true,
12+
"mapboxgl": true,
13+
"TMap": true
14+
},
15+
"parserOptions": {
16+
"ecmaVersion": 2018,
17+
"sourceType": "module"
18+
},
19+
"rules": {
20+
"no-console": "off",
21+
"no-inner-declarations": "off",
22+
"semi": ["error", "always"],
23+
"indent": 0,
24+
"padded-blocks": 0,
25+
"space-before-function-paren": 0,
26+
"no-var": 1,
27+
"dot-notation": 0,
28+
"one-var": 0,
29+
"new-cap": 0,
30+
"quote-props": 0,
31+
"camelcase": 0,
32+
"wrap-iife": 0
33+
}
34+
};

index.js

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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+
}

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "filednd",
3+
"version": "0.1.0",
4+
"description": "",
5+
"module": "./index.js",
6+
"main": "dist/filednd.js",
7+
"scripts": {
8+
"lint": "eslint ./*.js",
9+
"build": "npm run lint && cross-env NODE_ENV=dev rollup -c",
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/deyihu/filednd.git"
15+
},
16+
"author": "deyihu",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/deyihu/filednd/issues"
20+
},
21+
"homepage": "https://github.com/deyihu/filednd#readme",
22+
"devDependencies": {
23+
"cross-env": "^5.1.4",
24+
"eslint": "^6.2.2",
25+
"eslint-config-standard": "^14.1.0",
26+
"eslint-plugin-import": "^2.18.2",
27+
"eslint-plugin-node": "^10.0.0",
28+
"eslint-plugin-promise": "^4.2.1",
29+
"eslint-plugin-standard": "^4.0.1",
30+
"rollup": "^0.68.2",
31+
"rollup-plugin-commonjs": "^8.2.1",
32+
"rollup-plugin-json": "^3.1.0",
33+
"rollup-plugin-node-resolve": "^3.3.0",
34+
"rollup-plugin-terser": "^7.0.2"
35+
}
36+
}

0 commit comments

Comments
 (0)