d3-save-svg
d3-save-svg copied to clipboard
Suggestions for save method
Hi, Thanks for the great work - it's been a awesome start to a problem I'm working on. I'm adding this issue as I didn't want to push these changes incase I've scr3ed-the-pooch but I noticed some interesting code in the save method and had a crack at updating it - please see below.
Changes
- renamed input var config to avoid 'redefinition' in subsequent code
- preprocess does not take a 2nd parameter
- preprocess was called twice - removed 1st call
- only call the getDefaultFileName method if required
Before
export function save(svgElement, config) {
if (svgElement.nodeName !== 'svg' || svgElement.nodeType !== 1) {
throw 'Need an svg element input';
}
var config = config || {};
var svgInfo = preprocess(svgElement, config);
var defaultFileName = getDefaultFileName(svgInfo);
var filename = config.filename || defaultFileName;
var svgInfo = preprocess(svgElement);
download(svgInfo, filename);
}
After
function save(svgElement, pConfig) {
if (!svgElement || svgElement.nodeName !== 'svg' || svgElement.nodeType !== 1)
throw 'Need an svg element input';
var config = pConfig || {};
if (!config.hasOwnProperty("filename") || (config.filename || "").trim() == "")
config["filename"] = getDefaultFileName(svgInfo);
var svgInfo = preprocess(svgElement);
download(svgInfo, config.filename);
}
Cheers