Skip to content

Commit db3f579

Browse files
authored
Merge pull request #1995 from ethereum/download_zip
add download backup in homepage
2 parents 0845d5f + 3be1a1f commit db3f579

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

libs/remix-ui/home-tab/src/lib/remix-ui-home-tab.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState, useRef, useEffect, useReducer } from 'react' // eslint-disable-line
22

33
import './remix-ui-home-tab.css'
4+
import JSZip from 'jszip'
45
import { ModalDialog } from '@remix-ui/modal-dialog' // eslint-disable-line
56
import { Toaster } from '@remix-ui/toaster' // eslint-disable-line
67
import PluginButton from './components/pluginButton' // eslint-disable-line
@@ -175,6 +176,43 @@ export const RemixUiHomeTab = (props: RemixUiHomeTabProps) => {
175176
const startPluginManager = async () => {
176177
plugin.verticalIcons.select('pluginManager')
177178
}
179+
const saveAs = (blob, name) => {
180+
const node = document.createElement('a')
181+
node.download = name
182+
node.rel = 'noopener'
183+
node.href = URL.createObjectURL(blob)
184+
setTimeout(function () { URL.revokeObjectURL(node.href) }, 4E4) // 40s
185+
setTimeout(function () {
186+
try {
187+
node.dispatchEvent(new MouseEvent('click'))
188+
} catch (e) {
189+
var evt = document.createEvent('MouseEvents')
190+
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80,
191+
20, false, false, false, false, 0, null)
192+
node.dispatchEvent(evt)
193+
}
194+
}, 0) // 40s
195+
}
196+
const downloadFiles = async () => {
197+
try {
198+
plugin.call('notification', 'toast', 'preparing files for download, please wait..')
199+
const zip = new JSZip()
200+
const browserProvider = fileManager.getProvider('browser')
201+
await browserProvider.copyFolderToJson('/', ({ path, content }) => {
202+
zip.file(path, content)
203+
})
204+
zip.generateAsync({ type: 'blob' }).then(function (blob) {
205+
var today = new Date()
206+
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate()
207+
var time = today.getHours() + 'h' + today.getMinutes() + 'min'
208+
saveAs(blob, `remix-backup-at-${time}-${date}.zip`)
209+
}).catch((e) => {
210+
plugin.call('notification', 'toast', e.message)
211+
})
212+
} catch (e) {
213+
plugin.call('notification', 'toast', e.message)
214+
}
215+
}
178216

179217
const showFullMessage = (title: string, loadItem: string, examples: Array<string>) => {
180218
setState(prevState => {
@@ -279,6 +317,10 @@ export const RemixUiHomeTab = (props: RemixUiHomeTabProps) => {
279317
<i className="mr-1 far fa-hdd"></i>
280318
<label className="ml-1 remixui_home_text" onClick={() => connectToLocalhost()}>Connect to Localhost</label>
281319
</p>
320+
<p className="mb-1">
321+
<i className="mr-1 far fa-download"></i>
322+
<label className="ml-1 remixui_home_text" onClick={() => downloadFiles()}>Download Backup</label>
323+
</p>
282324
<p className="mt-3 mb-0"><label>LOAD FROM:</label></p>
283325
<div className="btn-group">
284326
<button className="btn mr-1 btn-secondary" data-id="landingPageImportFromGistButton" onClick={() => importFromGist()}>Gist</button>

0 commit comments

Comments
 (0)