Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor file explorer #2348

Merged
merged 1 commit into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions assets/js/browserfs.min.js

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<link rel="stylesheet" id="theme-link"/>
<link rel="stylesheet" href="assets/css/pygment_trac.css">
<link rel="icon" type="x-icon" href="icon.png">
<script src="assets/js/browserfs.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
</head>
<body>
Expand Down Expand Up @@ -76,9 +77,16 @@
document.head.appendChild(app)
}
window.onload = () => {
let app = document.createElement('script')
app.setAttribute('src', versions[versionToLoad])
document.body.appendChild(app)
BrowserFS.install(window)
BrowserFS.configure({
fs: "LocalStorage"
}, function(e) {
if (e) console.log(e)
let app = document.createElement('script')
app.setAttribute('src', versions[versionToLoad])
document.body.appendChild(app)
window.remixFileSystem = require('fs')
})
}
</script>
</body>
Expand Down
31 changes: 7 additions & 24 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ var { OffsetToLineColumnConverter } = require('./lib/offsetToLineColumnConverter
var QueryParams = require('./lib/query-params')
var GistHandler = require('./lib/gist-handler')
var Storage = remixLib.Storage
var LocalStorageProvider = require('./app/files/localStorageProvider')
var RemixDProvider = require('./app/files/remixDProvider')
var Config = require('./config')
var Renderer = require('./app/ui/renderer')
var examples = require('./app/editor/example-contracts')
var modalDialogCustom = require('./app/ui/modal-dialog-custom')
var FileManager = require('./app/files/fileManager')
var ReadonlyProvider = require('./app/files/readonlyProvider')
var BasicFileProvider = require('./app/files/basicFileProvider')
var FileProvider = require('./app/files/fileProvider')
var toolTip = require('./app/ui/tooltip')
var CompilerMetadata = require('./app/files/compiler-metadata')
var CompilerImport = require('./app/compiler/compiler-imports')
Expand Down Expand Up @@ -113,7 +111,7 @@ class App {

// load file system
self._components.filesProviders = {}
self._components.filesProviders['browser'] = new LocalStorageProvider(fileStorage)
self._components.filesProviders['browser'] = new FileProvider('browser', fileStorage)
registry.put({api: self._components.filesProviders['browser'], name: 'fileproviders/browser'})

var remixd = new Remixd(65520)
Expand All @@ -123,17 +121,7 @@ class App {
})

self._components.filesProviders['localhost'] = new RemixDProvider(remixd)
self._components.filesProviders['swarm'] = new ReadonlyProvider('swarm')
self._components.filesProviders['github'] = new ReadonlyProvider('github')
self._components.filesProviders['gist'] = new BasicFileProvider('gist')
self._components.filesProviders['ipfs'] = new ReadonlyProvider('ipfs')
self._components.filesProviders['https'] = new ReadonlyProvider('https')
self._components.filesProviders['http'] = new ReadonlyProvider('http')
registry.put({api: self._components.filesProviders['localhost'], name: 'fileproviders/localhost'})
registry.put({api: self._components.filesProviders['swarm'], name: 'fileproviders/swarm'})
registry.put({api: self._components.filesProviders['github'], name: 'fileproviders/github'})
registry.put({api: self._components.filesProviders['gist'], name: 'fileproviders/gist'})
registry.put({api: self._components.filesProviders['ipfs'], name: 'fileproviders/ipfs'})
registry.put({api: self._components.filesProviders, name: 'fileproviders'})

self._view = {}
Expand Down Expand Up @@ -297,10 +285,8 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
editor,
registry.get('config').api,
new Renderer(),
registry.get('fileproviders/swarm').api,
registry.get('fileproviders/ipfs').api,
registry.get('filemanager').api,
registry.get('fileproviders').api,
registry.get('fileproviders/browser').api,
registry.get('filemanager').api
)
const run = new RunTab(
udapp,
Expand Down Expand Up @@ -352,14 +338,11 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
const loadedFromGist = gistHandler.loadFromGist(queryParams.get(), fileManager)
if (!loadedFromGist) {
// insert ballot contract if there are no files to show
self._components.filesProviders['browser'].resolveDirectory('browser', (error, filesList) => {
self._components.filesProviders['browser'].resolveDirectory('/', (error, filesList) => {
if (error) console.error(error)
if (Object.keys(filesList).length === 0) {
if (!self._components.filesProviders['browser'].set(examples.ballot.name, examples.ballot.content)) {
modalDialogCustom.alert('Failed to store example contract in browser. Remix will not work properly. Please ensure Remix has access to LocalStorage. Safari in Private mode is known not to work.')
} else {
self._components.filesProviders['browser'].set(examples.ballot_test.name, examples.ballot_test.content)
}
fileManager.setFile(examples.ballot.name, examples.ballot.content)
fileManager.setFile(examples.ballot_test.name, examples.ballot_test.content)
}
})
}
Expand Down
119 changes: 0 additions & 119 deletions src/app/files/basicFileProvider.js

This file was deleted.

9 changes: 7 additions & 2 deletions src/app/files/compiler-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class CompilerMetadata extends Plugin {
self.fileManager = fileManager
self.config = config
self.networks = ['VM:-', 'main:1', 'ropsten:3', 'rinkeby:4', 'kovan:42', 'görli:5', 'Custom']
self.innerPath = 'artifacts'
}

_JSONFileName (path, contractName) {
return path + '/' + this.innerPath + '/' + contractName + '.json'
}

onActivation () {
Expand All @@ -31,7 +36,7 @@ class CompilerMetadata extends Plugin {
compiler.visitContracts((contract) => {
if (contract.file !== source.target) return

var fileName = path + '/' + contract.name + '.json'
var fileName = self._JSONFileName(path, contract.name)
provider.get(fileName, (error, content) => {
if (!error) {
content = content || '{}'
Expand Down Expand Up @@ -96,7 +101,7 @@ class CompilerMetadata extends Plugin {
if (err) {
console.log(err)
} else {
var fileName = path + '/' + contractName + '.json'
var fileName = self._JSONFileName(path, contractName)
provider.get(fileName, (error, content) => {
if (error) return reject(error)
if (!content) return resolve()
Expand Down
39 changes: 25 additions & 14 deletions src/app/files/file-explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function fileExplorer (localRegistry, files, menuItems) {
files.event.register('fileRenamed', fileRenamed)
files.event.register('fileRenamedError', fileRenamedError)
files.event.register('fileAdded', fileAdded)
files.event.register('folderAdded', folderAdded)

function fileRenamedError (error) {
modalDialogCustom.alert(error)
Expand Down Expand Up @@ -124,6 +125,18 @@ function fileExplorer (localRegistry, files, menuItems) {
})
}

function folderAdded (folderpath) {
self.ensureRoot(() => {
folderpath = folderpath.split('/').slice(0, -1).join('/')
self.files.resolveDirectory(folderpath, (error, fileTree) => {
if (error) console.error(error)
if (!fileTree) return
fileTree = normalize(folderpath, fileTree)
self.treeView.updateNodeFromJSON(folderpath, fileTree, true)
})
})
}

function fileRemoved (filepath) {
var label = self.treeView.labelAt(filepath)
if (label && label.parentElement) {
Expand Down Expand Up @@ -462,7 +475,7 @@ fileExplorer.prototype.toGist = function (id) {
// return all the files, except the temporary/readonly ones..
fileExplorer.prototype.packageFiles = function (filesProvider, callback) {
var ret = {}
filesProvider.resolveDirectory(filesProvider.type, (error, files) => {
filesProvider.resolveDirectory('/', (error, files) => {
if (error) callback(error)
else {
async.eachSeries(Object.keys(files), (path, cb) => {
Expand Down Expand Up @@ -525,7 +538,7 @@ fileExplorer.prototype.updateGist = function () {

fileExplorer.prototype.createNewFile = function () {
let self = this
modalDialogCustom.prompt('Create new file', 'File Name', 'Untitled.sol', (input) => {
modalDialogCustom.prompt('Create new file', 'File Path (Untitled.sol, Folder1/Untitled.sol)', 'Untitled.sol', (input) => {
helper.createNonClashingName(input, self.files, (error, newName) => {
if (error) return modalDialogCustom.alert('Failed to create file ' + newName + ' ' + error)
if (!self.files.set(newName, '')) {
Expand Down Expand Up @@ -568,18 +581,16 @@ fileExplorer.prototype.ensureRoot = function (cb) {
cb = cb || (() => {})
var self = this
if (self.element) return cb()

self.files.resolveDirectory('/', (error, files) => {
if (error) console.error(error)
var element = self.treeView.render(files, false)
element.classList.add(css.fileexplorer)
element.events = self.events
element.api = self.api
self.container.appendChild(element)
self.element = element
if (cb) cb()
self.treeView.expand(self.files.type)
})
const root = {}
root[this.files.type] = {}
var element = self.treeView.render(root, false)
element.classList.add(css.fileexplorer)
element.events = self.events
element.api = self.api
self.container.appendChild(element)
self.element = element
if (cb) cb()
self.treeView.expand(self.files.type)
}

function normalize (path, filesList) {
Expand Down
17 changes: 3 additions & 14 deletions src/app/files/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ class FileManager extends Plugin {
config: this._components.registry.get('config').api,
browserExplorer: this._components.registry.get('fileproviders/browser').api,
localhostExplorer: this._components.registry.get('fileproviders/localhost').api,
gistExplorer: this._components.registry.get('fileproviders/gist').api,
filesProviders: this._components.registry.get('fileproviders').api
}

this._deps.browserExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this._deps.localhostExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this._deps.gistExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
this._deps.browserExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this._deps.localhostExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this._deps.gistExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
this._deps.localhostExplorer.event.register('errored', (event) => { this.removeTabsOf(this._deps.localhostExplorer) })
this._deps.localhostExplorer.event.register('closed', (event) => { this.removeTabsOf(this._deps.localhostExplorer) })
}
Expand Down Expand Up @@ -309,18 +306,10 @@ class FileManager extends Plugin {
}

fileProviderOf (file) {
if (!file) return null
var provider = file.match(/[^/]*/)
if (provider !== null && this._deps.filesProviders[provider[0]]) {
return this._deps.filesProviders[provider[0]]
} else {
for (var handler of this._components.compilerImport.handlers()) {
if (handler.match.exec(file)) {
return this._deps.filesProviders[handler.type]
}
}
if (file.indexOf('localhost') === 0) {
return this._deps.filesProviders['localhost']
}
return null
return this._deps.filesProviders['browser']
}

saveCurrentFile () {
Expand Down
Loading