-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
109 lines (99 loc) · 2.44 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const d3 = require('d3')
const {SVGConverter} = require('svg-dataurl')
const css = `.download-menu {
position: absolute;
top: 100%;
left: 0;
z-index: 1000;
display: inline-block;
float: left;
min-width: 160px;
padding: 5px 0;
margin: 2px 0 0;
list-style: none;
font-size: 14px;
background-color: #fff;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,.15);
border-radius: 4px;
-webkit-box-shadow: 0 6px 12px rgba(0,0,0,.175);
box-shadow: 0 6px 12px rgba(0,0,0,.175);
background-clip: padding-box;
}
.download-menu>li>a {
display: block;
padding: 3px 20px;
clear: both;
font-weight: 400;
line-height: 1.42857143;
color: #333;
white-space: nowrap;
text-decoration: none;
background: 0 0;
}
.download-menu>li>a:hover, .download-menu>li>a:focus {
text-decoration: none;
color: #262626;
background-color: #f5f5f5;
}`
const createMenu = (pos, filename, converter) => {
const menu = d3.select('body')
.append('ul')
.classed('download-menu', true)
.style('left', `${pos[0]}px`)
.style('top', `${pos[1]}px`)
.on('mouseleave', () => {
menu.remove()
})
const list = menu
.append('li')
list
.append('a')
.text('Save as SVG')
.attr('download', filename + '.svg')
.attr('href', converter.svgDataURL())
list
.append('a')
.text('Save as PNG')
.attr('download', filename + '.png')
.attr('href', converter.pngDataURL())
list
.append('a')
.text('Save as JPG')
.attr('download', filename + '.jpeg')
.attr('href', converter.jpegDataURL())
}
const downloadable = () => {
let filename = 'image'
const downloadableImpl = (selection) => {
if (d3.select('#downloadable-css').empty()) {
d3.select('head')
.append('style')
.attr('id', 'downloadable-css')
.text(css)
}
selection.on('contextmenu', () => {
const f = () => {
const pos = d3.mouse(document.body)
SVGConverter.loadFromElement(selection.node()).then((converter) => {
createMenu(pos, filename, converter)
})
d3.event.preventDefault()
}
if (d3.event == null) {
d3.customEvent(window.event, f) // Hack for React
} else {
f()
}
})
}
downloadableImpl.filename = function () {
if (arguments.length === 0) {
return filename
}
filename = arguments[0]
return downloadableImpl
}
return downloadableImpl
}
exports.downloadable = downloadable