-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrop.js
69 lines (62 loc) · 1.82 KB
/
drop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// read named graph files that have been dropped by event
import {Graph} from './graph.js'
const inst = ({nodes,rels}) => {return new Graph(nodes, rels)}
export async function drop (event,sufix) {
const want = files(event).filter(file =>
file.name.endsWith(sufix) &&
file.type === 'application/json')
const concepts = []
for (const file of want) {
const name = file.name.replace(sufix,'')
const graph = await file.text()
.then(text => JSON.parse(text))
.then(inst)
concepts.push({name, graph})
}
return concepts
}
export async function dropl (event,sufix) {
const want = files(event).filter(file =>
file.name.endsWith(sufix))
const concepts = []
for (const file of want) {
const text = await file.text()
text.trim().split(/\n/).forEach(line => {
const {name,graph} = JSON.parse(line)
concepts.push({name,graph:inst(graph)})
})
}
return concepts
}
export async function dropu (event) {
const want = strings(event)
const concepts = []
for (const file of want) {
const filename = await file
const name = filename.split(/\//).reverse()[0].split(/\./)[0]
const graph = await fetch(filename)
.then(res => res.json())
.then(inst)
concepts.push({name,graph})
}
return concepts
}
function files(event) {
if (event.dataTransfer.items) {
return [...event.dataTransfer.items]
.filter(item => item.kind === 'file')
.map(item => item.getAsFile())
} else {
return [...event.dataTransfer.files]
}
}
function strings(event) {
if (event.dataTransfer.items) {
return [...event.dataTransfer.items]
.filter(item => item.kind === 'string')
.filter(item => item.type === 'text/uri-list')
.map(item => {return new Promise(res => item.getAsString(res))})
} else {
return [...event.dataTransfer.files]
}
}