From c953633b2ab33e67532c291a2eecdd7325e73606 Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 1 May 2018 16:10:00 +0200 Subject: [PATCH] Save the current gist instead of creating a new one over and over --- src/app.js | 7 ++- src/app/panels/file-panel.js | 82 +++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/app.js b/src/app.js index 1d948b6c35..ffe04d296a 100644 --- a/src/app.js +++ b/src/app.js @@ -556,7 +556,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org }) // Add files received from remote instance (i.e. another remix-ide) - function loadFiles (filesSet, fileProvider) { + function loadFiles (filesSet, fileProvider, callback) { if (!fileProvider) fileProvider = 'browser' async.each(Object.keys(filesSet), (file, callback) => { @@ -573,6 +573,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org }) }, (error) => { if (!error) fileManager.switchFile() + if (callback) callback(error) }) } @@ -597,7 +598,9 @@ Please make a backup of your contracts and start using http://remix.ethereum.org modalDialogCustom.alert(`Gist load error: ${error || data.message}`) return } - loadFiles(data.files, 'gist') + loadFiles(data.files, 'gist', (errorLoadingFile) => { + if (!errorLoadingFile) filesProviders['gist'].id = gistId + }) }) }) } diff --git a/src/app/panels/file-panel.js b/src/app/panels/file-panel.js index 3ac3b56b48..cea4250d9a 100644 --- a/src/app/panels/file-panel.js +++ b/src/app/panels/file-panel.js @@ -8,6 +8,7 @@ var EventManager = remixLib.EventManager var FileExplorer = require('../files/file-explorer') var modalDialog = require('../ui/modaldialog') var modalDialogCustom = require('../ui/modal-dialog-custom') +var tooltip = require('../ui/tooltip') var QueryParams = require('../../lib/query-params') var queryParams = new QueryParams() var helper = require('../../lib/helper') @@ -80,10 +81,10 @@ function filepanel (appAPI, filesProvider) { ` : ''} - publishToGist('browser')}> + publishToGist('browser')}> - publishToGist('gist')}> + updateGist()}> @@ -273,35 +274,47 @@ function filepanel (appAPI, filesProvider) { // ------------------ gist publish -------------- + function updateGist () { + var gistId = filesProvider['gist'].id + if (!gistId) { + tooltip('no gist content is currently loaded.') + } else { + toGist('gist', gistId) + } + } + function publishToGist (fileProviderName) { - function cb (error, data) { + modalDialogCustom.confirm(null, 'Are you very sure you want to publish all your files anonymously as a public gist on github.com?', () => { + toGist(fileProviderName) + }) + } + + function toGist (fileProviderName, id) { + packageFiles(filesProvider[fileProviderName], (error, packaged) => { if (error) { + console.log(error) modalDialogCustom.alert('Failed to create gist: ' + error) } else { - if (data.html_url) { - modalDialogCustom.confirm(null, `Created a gist at ${data.html_url}. Would you like to open it in a new window?`, () => { - window.open(data.html_url, '_blank') - }) + var tokenAccess = appAPI.config.get('settings/gist-access-token') + if (!tokenAccess) { + modalDialogCustom.alert('Remix requires an access token (which includes gists creation permission). Please go to the settings tab for more information.') } else { - modalDialogCustom.alert(data.message + ' ' + data.documentation_url + ' ' + JSON.stringify(data.errors, null, '\t')) - } - } - } - - function toGist () { - packageFiles(filesProvider[fileProviderName], (error, packaged) => { - if (error) { - console.log(error) - modalDialogCustom.alert('Failed to create gist: ' + error) - } else { - var tokenAccess = appAPI.config.get('settings/gist-access-token') - if (!tokenAccess) { - modalDialogCustom.alert('Remix requires an access token (which includes gists creation permission). Please go to the settings tab for more information.') - } else { - var description = 'Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. \n Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=' + queryParams.get().version + '&optimize=' + queryParams.get().optimize + '&gist=' - var gists = new Gists({ - token: tokenAccess + var description = 'Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. \n Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=' + queryParams.get().version + '&optimize=' + queryParams.get().optimize + '&gist=' + var gists = new Gists({ + token: tokenAccess + }) + if (id) { + tooltip('Saving gist (' + id + ') ...') + gists.edit({ + description: description, + public: true, + files: packaged, + id: id + }, (error, result) => { + cb(error, result) }) + } else { + tooltip('Creating a new gist ...') gists.create({ description: description, public: true, @@ -311,13 +324,24 @@ function filepanel (appAPI, filesProvider) { }) } } - }) - } - modalDialogCustom.confirm(null, 'Are you very sure you want to publish all your files anonymously as a public gist on github.com?', () => { - toGist() + } }) } + function cb (error, data) { + if (error) { + modalDialogCustom.alert('Failed to manage gist: ' + error) + } else { + if (data.html_url) { + modalDialogCustom.confirm(null, `The gist is at ${data.html_url}. Would you like to open it in a new window?`, () => { + window.open(data.html_url, '_blank') + }) + } else { + modalDialogCustom.alert(data.message + ' ' + data.documentation_url + ' ' + JSON.stringify(data.errors, null, '\t')) + } + } + } + // ------------------ copy files -------------- function copyFiles () {