From d8bdc059efc261bf52733287669cd1b1dc394792 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 4 Dec 2017 16:18:31 +0100 Subject: [PATCH] add BasicReadOnlyProvider (swarm, github, gist, ipfs) --- src/app.js | 5 ++ src/app/files/basicReadOnlyExplorer.js | 102 +++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/app/files/basicReadOnlyExplorer.js diff --git a/src/app.js b/src/app.js index 3eb4c96352..278ab5f9c6 100644 --- a/src/app.js +++ b/src/app.js @@ -36,6 +36,7 @@ var handleImports = require('./app/compiler/compiler-imports') var FileManager = require('./app/files/fileManager') var ContextualListener = require('./app/editor/contextualListener') var ContextView = require('./app/editor/contextView') +var BasicReadOnlyExplorer = require('./app/files/basicReadOnlyExplorer') var styleGuide = remixLib.ui.styleGuide var styles = styleGuide() @@ -111,6 +112,10 @@ class App { self._api.filesProviders = {} self._api.filesProviders['browser'] = new Browserfiles(fileStorage) self._api.filesProviders['localhost'] = new SharedFolder(new Remixd()) + self._api.filesProviders['swarm'] = new BasicReadOnlyExplorer('swarm') + self._api.filesProviders['github'] = new BasicReadOnlyExplorer('github') + self._api.filesProviders['gist'] = new BasicReadOnlyExplorer('gist') + self._api.filesProviders['ipfs'] = new BasicReadOnlyExplorer('ipfs') self._view = {} self._components = {} self.data = { diff --git a/src/app/files/basicReadOnlyExplorer.js b/src/app/files/basicReadOnlyExplorer.js new file mode 100644 index 0000000000..5285da3930 --- /dev/null +++ b/src/app/files/basicReadOnlyExplorer.js @@ -0,0 +1,102 @@ +'use strict' +var EventManager = require('remix-lib').EventManager + +class SwarmExplorer { + constructor (type) { + this.event = new EventManager() + this.files = {} + this.type = type + } + + close (cb) { + this.files = {} + cb() + } + + init (cb) { + this.files = {} + } + + exists (path) { + if (!this.files) return false + return this.files[path] !== undefined + } + + get (path, cb) { + var content = this.files[path] + if (cb) { + cb(null, content) + } + return content + } + + set (path, content, cb) { + return true + } + + addReadOnly (path, content) { + this.files[this.type + '/' + path] = content + this.event.trigger('fileAdded', [this.type + '/' + path, true]) + return true + } + + isReadOnly (path) { + return true + } + + remove (path) { + delete this.files[path] + } + + rename (oldPath, newPath, isFolder) { + return true + } + + list () { + return this.files + } + + // + // Tree model for files + // { + // 'a': { }, // empty directory 'a' + // 'b': { + // 'c': {}, // empty directory 'b/c' + // 'd': { '/readonly': true, '/content': 'Hello World' } // files 'b/c/d' + // 'e': { '/readonly': false, '/path': 'b/c/d' } // symlink to 'b/c/d' + // 'f': { '/readonly': false, '/content': '', '/mode': 0755 } + // } + // } + // + listAsTree () { + function hashmapize (obj, path, val) { + var nodes = path.split('/') + var i = 0 + + for (; i < nodes.length - 1; i++) { + var node = nodes[i] + if (obj[node] === undefined) { + obj[node] = {} + } + obj = obj[node] + } + + obj[nodes[i]] = val + } + + var tree = {} + + var self = this + // This does not include '.remix.config', because it is filtered + // inside list(). + Object.keys(this.list()).forEach(function (path) { + hashmapize(tree, path, { + '/readonly': self.isReadOnly(path), + '/content': self.get(path) + }) + }) + return tree + } +} + +module.exports = SwarmExplorer