Unverified Commit 73658927 authored by Liana Husikyan's avatar Liana Husikyan Committed by GitHub

Merge pull request #2384 from ethereum/kuku

fixed editing gists from last session
parents fd6ddd6c 2bd02ef3
/* global FileReader */ /* global FileReader */
var async = require('async') /* global fetch */
var Gists = require('gists') const async = require('async')
var modalDialogCustom = require('../ui/modal-dialog-custom') const Gists = require('gists')
var tooltip = require('../ui/tooltip') const modalDialogCustom = require('../ui/modal-dialog-custom')
var QueryParams = require('../../lib/query-params') const tooltip = require('../ui/tooltip')
var helper = require('../../lib/helper') const QueryParams = require('../../lib/query-params')
var yo = require('yo-yo') const helper = require('../../lib/helper')
var Treeview = require('../ui/TreeView') const yo = require('yo-yo')
var modalDialog = require('../ui/modaldialog') const Treeview = require('../ui/TreeView')
var EventManager = require('../../lib/events') const modalDialog = require('../ui/modaldialog')
var contextMenu = require('../ui/contextMenu') const EventManager = require('../../lib/events')
var css = require('./styles/file-explorer-styles') const contextMenu = require('../ui/contextMenu')
var globalRegistry = require('../../global/registry') const css = require('./styles/file-explorer-styles')
var queryParams = new QueryParams() const globalRegistry = require('../../global/registry')
const queryParams = new QueryParams()
let MENU_HANDLE let MENU_HANDLE
function fileExplorer (localRegistry, files, menuItems) { function fileExplorer (localRegistry, files, menuItems) {
...@@ -441,35 +442,47 @@ fileExplorer.prototype.toGist = function (id) { ...@@ -441,35 +442,47 @@ fileExplorer.prototype.toGist = function (id) {
} }
} }
/**
* This function is to get the original content of given gist
* @params id is the gist id to fetch
*/
async function getOriginalFiles (id) {
if (!id) {
return []
}
const url = `https://api.github.com/gists/${id}`
const res = await fetch(url)
const data = await res.json()
return data.files || []
}
this.packageFiles(this.files, 'browser/gists/' + id, (error, packaged) => { this.packageFiles(this.files, 'browser/gists/' + id, (error, packaged) => {
if (error) { if (error) {
console.log(error) console.log(error)
modalDialogCustom.alert('Failed to create gist: ' + error) modalDialogCustom.alert('Failed to create gist: ' + error)
} else { } else {
// check for token
var tokenAccess = this._deps.config.get('settings/gist-access-token') var tokenAccess = this._deps.config.get('settings/gist-access-token')
if (!tokenAccess) { if (!tokenAccess) {
modalDialogCustom.alert( modalDialogCustom.alert(
'Remix requires an access token (which includes gists creation permission). Please go to the settings tab to create one.' 'Remix requires an access token (which includes gists creation permission). Please go to the settings tab to create one.'
) )
} else { } 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=' + const 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 + queryParams.get().version + '&optimize=' + queryParams.get().optimize + '&gist='
'&optimize=' + const gists = new Gists({ token: tokenAccess })
queryParams.get().optimize +
'&gist='
var gists = new Gists({
token: tokenAccess
})
if (id) { if (id) {
const fileList = Object.keys(this.files.origGistFiles) const originalFileList = getOriginalFiles(id)
const updatedFileList = Object.keys(packaged)
// Telling the GIST API to remove files // Telling the GIST API to remove files
const allItems = fileList const updatedFileList = Object.keys(packaged)
const allItems = Object.keys(originalFileList)
.filter(fileName => updatedFileList.indexOf(fileName) === -1) .filter(fileName => updatedFileList.indexOf(fileName) === -1)
.reduce((acc, deleteFileName) => ({ .reduce((acc, deleteFileName) => ({
...acc, ...acc,
[deleteFileName]: null [deleteFileName]: null
}), this.files.origGistFiles) }), originalFileList)
// adding new files // adding new files
updatedFileList.forEach((file) => { updatedFileList.forEach((file) => {
const _items = file.split('/') const _items = file.split('/')
...@@ -489,10 +502,10 @@ fileExplorer.prototype.toGist = function (id) { ...@@ -489,10 +502,10 @@ fileExplorer.prototype.toGist = function (id) {
for (const key in allItems) { for (const key in allItems) {
if (allItems[key] === null) delete allItems[key] if (allItems[key] === null) delete allItems[key]
} }
this.files.origGistFiles = allItems
} }
}) })
} else { } else {
// id is not existing, need to create a new gist
tooltip('Creating a new gist ...') tooltip('Creating a new gist ...')
gists.create({ gists.create({
description: description, description: description,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment