This commit is contained in:
neauoire
2019-11-03 21:23:32 -05:00
parent ccab161421
commit b30996edee
2 changed files with 11 additions and 7 deletions

View File

@@ -35,9 +35,9 @@ function Source () {
this.saveAs(name, content, type, callback)
}
this.saveAs = (name, content, type = 'text/plain', callback) => {
this.saveAs = (name, ext, content, type = 'text/plain', callback) => {
console.log('Source', 'Save new file..')
this.download(name, content, type, callback)
this.download(name, ext, content, type, callback)
}
this.revert = () => {
@@ -55,10 +55,9 @@ function Source () {
reader.readAsText(file, 'UTF-8')
}
this.download = (name, content, type, settings = 'charset=utf-8') => {
console.info('Source', `Downloading ${name}(${type})`)
this.download = (name, ext, content, type, settings = 'charset=utf-8') => {
const link = document.createElement('a')
link.setAttribute('download', name)
link.setAttribute('download', `${name}-${timestamp()}.${ext}`)
if (type === 'image/png' || type === 'image/jpeg') {
link.setAttribute('href', content)
} else {
@@ -66,4 +65,9 @@ function Source () {
}
link.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))
}
function timestamp (d = new Date(), e = new Date(d)) {
const ms = e - d.setHours(0, 0, 0, 0)
return (ms / 8640 / 10000).toFixed(6).substr(2, 6)
}
}