forked from nytimes/svg-crowbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-crowbar.js
229 lines (196 loc) · 7.02 KB
/
svg-crowbar.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
(function() {
var doctype = '<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
window.URL = window.URL || window.webkitURL;
if (typeof d3 === "undefined") {
if (typeof require === "undefined") {
var script = document.createElement("script");
script.onload = function() { initialize(d3); };
script.src = "//cdnjs.cloudflare.com/ajax/libs/d3/3.4.2/d3.min.js";
document.head.appendChild(script);
} else {
require.config({paths: {d3: "//cdnjs.cloudflare.com/ajax/libs/d3/3.4.2/d3.min"}});
require(["d3"], initialize);
}
} else {
initialize(d3);
}
function initialize(d3) {
var documents = [window.document],
SVGSources = [];
d3.selectAll("iframe").each(function() {
if (this.contentDocument) documents.push(this.contentDocument);
});
documents.forEach(function(document) {
var styles = getStyles(document);
var newSources = getSources(document, styles);
// because of prototype on NYT pages
for (var i = 0; i < newSources.length; i++) {
SVGSources.push(newSources[i]);
};
});
if (SVGSources.length > 1) {
createPopover(SVGSources);
} else if (SVGSources.length > 0) {
download(SVGSources[0]);
} else {
alert("The Crowbar couldn’t find any SVG nodes.");
}
function createPopover(sources) {
cleanup();
var drag = d3.behavior.drag()
.origin(function() {
var el = d3.select(this)
return {
x: el.style("left").replace("px", ""),
y: el.style("top").replace("px", "")
}
})
.on("drag", dragmove);
sources.forEach(function(s1) {
sources.forEach(function(s2) {
if (s1 !== s2) {
if ((Math.abs(s1.top - s2.top) < 38) && (Math.abs(s1.left - s2.left) < 38)) {
s2.top += 38;
s2.left += 38;
}
}
})
})
var body = d3.select("body");
var buttons = body.append("div")
.attr("class", "svg-crowbar")
.style("z-index", 1e7)
.style("position", "absolute")
.style("top", 0)
.style("left", 0);
var button = buttons.selectAll(".crowbar-button")
.data(sources)
.enter().append("div")
.attr("class", "crowbar-button")
.style("position", "absolute")
.style("top", function(d) { return (d.top + document.body.scrollTop) + "px"; })
.style("left", function(d) { return (document.body.scrollLeft + d.left) + "px"; })
.style("padding", "4px")
.style("border-radius", "3px")
.style("color", "white")
.style("text-align", "center")
.style("font-family", "'Helvetica Neue'")
.style("background", "rgba(0, 0, 0, 0.8)")
.style("box-shadow", "0px 4px 18px rgba(0, 0, 0, 0.4)")
.style("cursor", "move")
.text(function(d, i) { return "SVG #" + i + ": " + (d.id ? "#" + d.id : "") + (d.class ? "." + d.class : "")})
.append("button")
.style("width", "150px")
.style("font-size", "12px")
.style("line-height", "1.4em")
.style("margin", "5px 0 0 0")
.text("Download")
.on("click", function(d, i) {
d3.event.preventDefault();
download(d);
});
buttons.selectAll(".crowbar-button").call(drag);
var html = body.append("div")
.attr("class", "svg-crowbar")
.style("background", "rgba(255, 255, 255, 0.7)")
.style("position", "fixed")
.style("left", 0)
.style("top", 0)
.style("width", "100%")
.style("height", "100%");
function dragmove(d) {
d3.select(this)
.style("left", d3.event.x + "px")
.style("top", d3.event.y + "px");
}
}
function cleanup() {
d3.selectAll(".svg-crowbar").remove();
}
function getSources(doc, styles) {
var svgInfo = [],
svgs = d3.select(doc).selectAll("svg");
styles = (styles === undefined) ? "" : styles;
svgs.each(function () {
var svg = d3.select(this);
svg.attr("version", "1.1")
.insert("defs", ":first-child")
.attr("class", "svg-crowbar")
.append("style")
.attr("type", "text/css");
// removing attributes so they aren't doubled up
svg.node().removeAttribute("xmlns");
svg.node().removeAttribute("xlink");
// These are needed for the svg
if (!svg.node().hasAttributeNS(d3.ns.prefix.xmlns, "xmlns")) {
svg.node().setAttributeNS(d3.ns.prefix.xmlns, "xmlns", d3.ns.prefix.svg);
}
if (!svg.node().hasAttributeNS(d3.ns.prefix.xmlns, "xmlns:xlink")) {
svg.node().setAttributeNS(d3.ns.prefix.xmlns, "xmlns:xlink", d3.ns.prefix.xlink);
}
var source = (new XMLSerializer()).serializeToString(svg.node()).replace('</style>', '<![CDATA[' + styles + ']]></style>');
var rect = svg.node().getBoundingClientRect();
svgInfo.push({
top: rect.top,
left: rect.left,
width: rect.width,
height: rect.height,
class: svg.attr("class"),
id: svg.attr("id"),
childElementCount: svg.node().childElementCount,
source: [doctype + source]
});
});
return svgInfo;
}
function download(source) {
var filename = "untitled";
if (source.id) {
filename = source.id;
} else if (source.class) {
filename = source.class;
} else if (window.document.title) {
filename = window.document.title.replace(/[^a-z0-9]/gi, '-').toLowerCase();
}
var url = window.URL.createObjectURL(new Blob(source.source, { "type" : "text\/xml" }));
var a = d3.select("body")
.append('a')
.attr("class", "svg-crowbar")
.attr("download", filename + ".svg")
.attr("href", url)
.style("display", "none");
a.node().click();
setTimeout(function() {
window.URL.revokeObjectURL(url);
}, 10);
}
function getStyles(doc) {
var styles = "",
styleSheets = doc.styleSheets;
if (styleSheets) {
for (var i = 0; i < styleSheets.length; i++) {
processStyleSheet(styleSheets[i]);
}
}
function processStyleSheet(ss) {
if (ss.cssRules) {
for (var i = 0; i < ss.cssRules.length; i++) {
var rule = ss.cssRules[i];
if (rule.type === 3) {
// Import Rule
processStyleSheet(rule.styleSheet);
} else {
// hack for illustrator crashing on descendent selectors
if (rule.selectorText) {
if (rule.selectorText.indexOf(">") === -1) {
styles += "\n" + rule.cssText;
}
}
}
}
}
}
return styles;
}
}
})();