add file saver lib

This commit is contained in:
Alfred Melch 2019-08-07 03:30:02 +02:00
parent b4b1aac857
commit 3c636b2edf
2 changed files with 2 additions and 18 deletions

View File

@ -2,8 +2,7 @@ import React from 'react'
import { Line, Bar } from 'react-chartjs-2' import { Line, Bar } from 'react-chartjs-2'
import debounceRender from 'react-debounce-render' import debounceRender from 'react-debounce-render'
import platform from 'platform' import platform from 'platform'
import { saveAs } from 'file-saver'
import { downloadAsJson } from '../../../lib/json-download/index.js'
import { datasetNames } from '../data/index.js' import { datasetNames } from '../data/index.js'
import { datasetTemplates, genOptions, beautifyLabels } from './chartOptions.js' import { datasetTemplates, genOptions, beautifyLabels } from './chartOptions.js'
@ -202,7 +201,7 @@ function saveChart(chartRef) {
let config = JSON.stringify(myChart, (key, val) => let config = JSON.stringify(myChart, (key, val) =>
key === '_meta' ? undefined : val key === '_meta' ? undefined : val
) )
downloadAsJson(config, 'config.json') saveAs(new Blob([config]), 'config.json', { type: 'text/json;charset=utf8' })
} }
/** /**

View File

@ -1,15 +0,0 @@
/*
Taken from: https://stackoverflow.com/questions/19721439/download-json-object-as-a-file-from-browser
*/
export function downloadAsJson(exportObj, exportName) {
if (typeof exportObj !== 'string') exportObj = JSON.stringify(exportObj)
var dataStr =
'data:application/json;charset=utf-8,' + encodeURIComponent(exportObj)
var downloadAnchorNode = document.createElement('a')
downloadAnchorNode.setAttribute('href', dataStr)
downloadAnchorNode.setAttribute('download', exportName)
document.body.appendChild(downloadAnchorNode) // required for firefox
downloadAnchorNode.click()
downloadAnchorNode.remove()
}