|
1 | 1 | import React, { useState, useRef, useEffect, useReducer } from 'react' // eslint-disable-line |
2 | 2 |
|
3 | 3 | import './remix-ui-home-tab.css' |
| 4 | +import JSZip from 'jszip' |
4 | 5 | import { ModalDialog } from '@remix-ui/modal-dialog' // eslint-disable-line |
5 | 6 | import { Toaster } from '@remix-ui/toaster' // eslint-disable-line |
6 | 7 | import PluginButton from './components/pluginButton' // eslint-disable-line |
@@ -175,6 +176,43 @@ export const RemixUiHomeTab = (props: RemixUiHomeTabProps) => { |
175 | 176 | const startPluginManager = async () => { |
176 | 177 | plugin.verticalIcons.select('pluginManager') |
177 | 178 | } |
| 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 | + } |
178 | 216 |
|
179 | 217 | const showFullMessage = (title: string, loadItem: string, examples: Array<string>) => { |
180 | 218 | setState(prevState => { |
@@ -279,6 +317,10 @@ export const RemixUiHomeTab = (props: RemixUiHomeTabProps) => { |
279 | 317 | <i className="mr-1 far fa-hdd"></i> |
280 | 318 | <label className="ml-1 remixui_home_text" onClick={() => connectToLocalhost()}>Connect to Localhost</label> |
281 | 319 | </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> |
282 | 324 | <p className="mt-3 mb-0"><label>LOAD FROM:</label></p> |
283 | 325 | <div className="btn-group"> |
284 | 326 | <button className="btn mr-1 btn-secondary" data-id="landingPageImportFromGistButton" onClick={() => importFromGist()}>Gist</button> |
|
0 commit comments