From 8524458ab7dc2ca87c3b3188ecdbd07c33aa766d Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 23 Feb 2021 10:01:29 +0100 Subject: [PATCH] fix loading code sample --- apps/remix-ide/src/app.js | 10 ----- apps/remix-ide/src/app/panels/file-panel.js | 42 +++++++++++++-------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/apps/remix-ide/src/app.js b/apps/remix-ide/src/app.js index f851cd2bb6..a6fe62b0eb 100644 --- a/apps/remix-ide/src/app.js +++ b/apps/remix-ide/src/app.js @@ -443,15 +443,5 @@ Please make a backup of your contracts and start using http://remix.ethereum.org migrateToWorkspace(fileManager) filePanel.initWorkspace() - - if (params.code) { - try { - const path = 'browser/.code-sample/contract.sol' - await fileManager.writeFile(path, atob(params.code)) - await fileManager.openFile(path) - } catch (e) { - console.error(e) - } - } if (params.embed) framingService.embed() } diff --git a/apps/remix-ide/src/app/panels/file-panel.js b/apps/remix-ide/src/app/panels/file-panel.js index f30011ea34..46a550a3e5 100644 --- a/apps/remix-ide/src/app/panels/file-panel.js +++ b/apps/remix-ide/src/app/panels/file-panel.js @@ -5,6 +5,7 @@ import React from 'react' // eslint-disable-line import ReactDOM from 'react-dom' import { FileExplorer } from '@remix-ui/file-explorer' // eslint-disable-line import './styles/file-panel-styles.css' +var ethutil = require('ethereumjs-util') var yo = require('yo-yo') var EventManager = require('../../lib/events') var { RemixdHandle } = require('../files/remixd-handle.js') @@ -104,27 +105,38 @@ module.exports = class Filepanel extends ViewPlugin { this.renderComponent() } - initWorkspace () { + async initWorkspace () { + const workspacesPath = this._deps.fileProviders.workspace.workspacesPath const queryParams = new QueryParams() const params = queryParams.get() // get the file from gist const gistHandler = new GistHandler() const loadedFromGist = gistHandler.loadFromGist(params, this._deps.fileManager) - if (!loadedFromGist) { - // insert example contracts if there are no files to show - this._deps.fileProviders.browser.resolveDirectory('/', async (error, filesList) => { - if (error) console.error(error) - if (Object.keys(filesList).length === 0) { - const workspacesPath = this._deps.fileProviders.workspace.workspacesPath - for (const file in examples) { - await this._deps.fileManager.writeFile('browser/' + workspacesPath + '/default_workspace/' + examples[file].name, examples[file].content) - } - this.setWorkspace('default_workspace') - } - }) - } else { - this.setWorkspace('gists') + if (loadedFromGist) return + if (params.code) { + try { + await this._deps.fileManager.createWorkspace('code-sample') + var hash = ethutil.bufferToHex(ethutil.keccak(params.code)) + const fileName = 'contract-' + hash.replace('0x', '').substring(0, 10) + '.sol' + const path = 'browser/' + workspacesPath + '/code-sample/' + fileName + await this._deps.fileManager.writeFile(path, atob(params.code)) + this.setWorkspace('code-sample') + await this._deps.fileManager.openFile(path) + } catch (e) { + console.error(e) + } + return } + // insert example contracts if there are no files to show + this._deps.fileProviders.browser.resolveDirectory('/', async (error, filesList) => { + if (error) console.error(error) + if (Object.keys(filesList).length === 0) { + for (const file in examples) { + await this._deps.fileManager.writeFile('browser/' + workspacesPath + '/default_workspace/' + examples[file].name, examples[file].content) + } + this.setWorkspace('default_workspace') + } + }) } refreshWorkspacesList () {