|
|
@ -1,13 +1,59 @@ |
|
|
|
'use strict' |
|
|
|
'use strict' |
|
|
|
|
|
|
|
|
|
|
|
var EventManager = require('../../lib/events') |
|
|
|
const CompilerImport = require('../compiler/compiler-imports') |
|
|
|
|
|
|
|
const EventManager = require('../../lib/events') |
|
|
|
|
|
|
|
const modalDialogCustom = require('../ui/modal-dialog-custom') |
|
|
|
|
|
|
|
const tooltip = require('../ui/tooltip') |
|
|
|
|
|
|
|
const remixLib = require('remix-lib') |
|
|
|
|
|
|
|
const Storage = remixLib.Storage |
|
|
|
|
|
|
|
|
|
|
|
class FileProvider { |
|
|
|
class FileProvider { |
|
|
|
constructor (name) { |
|
|
|
constructor (name) { |
|
|
|
this.event = new EventManager() |
|
|
|
this.event = new EventManager() |
|
|
|
this.type = name |
|
|
|
this.type = name |
|
|
|
this.normalizedNames = {} // contains the raw url associated with the displayed path
|
|
|
|
this.providerExternalsStorage = new Storage('providerExternals:') |
|
|
|
this.readonlyItems = ['browser'] |
|
|
|
this.externalFolders = [this.type + '/swarm', this.type + '/ipfs', this.type + '/github', this.type + '/gist', this.type + '/https'] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addNormalizedName (path, url) { |
|
|
|
|
|
|
|
this.providerExternalsStorage.set(this.type + '/' + path, url) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
removeNormalizedName (path) { |
|
|
|
|
|
|
|
this.providerExternalsStorage.remove(path) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
normalizedNameExists (path) { |
|
|
|
|
|
|
|
return this.providerExternalsStorage.exists(path) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getNormalizedName (path) { |
|
|
|
|
|
|
|
return this.providerExternalsStorage.get(path) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isExternalFolder (path) { |
|
|
|
|
|
|
|
return this.externalFolders.includes(path) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
discardChanges (path) { |
|
|
|
|
|
|
|
this.remove(path) |
|
|
|
|
|
|
|
const compilerImport = new CompilerImport() |
|
|
|
|
|
|
|
this.providerExternalsStorage.keys().map(value => { |
|
|
|
|
|
|
|
if (value.indexOf(path) === 0) { |
|
|
|
|
|
|
|
compilerImport.import( |
|
|
|
|
|
|
|
this.getNormalizedName(value), |
|
|
|
|
|
|
|
true, |
|
|
|
|
|
|
|
(loadingMsg) => { tooltip(loadingMsg) }, |
|
|
|
|
|
|
|
(error, content, cleanUrl, type, url) => { |
|
|
|
|
|
|
|
if (error) { |
|
|
|
|
|
|
|
modalDialogCustom.alert(error) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
this.addExternal(type + '/' + cleanUrl, content, url) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
exists (path, cb) { |
|
|
|
exists (path, cb) { |
|
|
@ -25,7 +71,7 @@ class FileProvider { |
|
|
|
|
|
|
|
|
|
|
|
get (path, cb) { |
|
|
|
get (path, cb) { |
|
|
|
cb = cb || function () {} |
|
|
|
cb = cb || function () {} |
|
|
|
if (this.normalizedNames[path]) path = this.normalizedNames[path] // ensure we actually use the normalized path from here
|
|
|
|
if (this.normalizedNameExists[path]) path = this.getNormalizedName(path) // ensure we actually use the normalized path from here
|
|
|
|
var unprefixedpath = this.removePrefix(path) |
|
|
|
var unprefixedpath = this.removePrefix(path) |
|
|
|
var exists = window.remixFileSystem.existsSync(unprefixedpath) |
|
|
|
var exists = window.remixFileSystem.existsSync(unprefixedpath) |
|
|
|
if (!exists) return cb(null, null) |
|
|
|
if (!exists) return cb(null, null) |
|
|
@ -36,10 +82,6 @@ class FileProvider { |
|
|
|
|
|
|
|
|
|
|
|
set (path, content, cb) { |
|
|
|
set (path, content, cb) { |
|
|
|
cb = cb || function () {} |
|
|
|
cb = cb || function () {} |
|
|
|
if (this.isReadOnly(path)) { |
|
|
|
|
|
|
|
cb(new Error('It is not possible to modify a readonly item')) |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var unprefixedpath = this.removePrefix(path) |
|
|
|
var unprefixedpath = this.removePrefix(path) |
|
|
|
var exists = window.remixFileSystem.existsSync(unprefixedpath) |
|
|
|
var exists = window.remixFileSystem.existsSync(unprefixedpath) |
|
|
|
if (!exists && unprefixedpath.indexOf('/') !== -1) { |
|
|
|
if (!exists && unprefixedpath.indexOf('/') !== -1) { |
|
|
@ -70,26 +112,17 @@ class FileProvider { |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
addReadOnly (path, content, url) { |
|
|
|
// this will not add a folder as readonly but keep the original url to be able to restore it later
|
|
|
|
this.readonlyItems.push(this.type + '/' + path) |
|
|
|
addExternal (path, content, url) { |
|
|
|
if (!url) this.normalizedNames[url] = path |
|
|
|
if (url) this.addNormalizedName(path, url) |
|
|
|
return this.set(path, content) |
|
|
|
return this.set(path, content) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
isReadOnly (path) { |
|
|
|
isReadOnly (path) { |
|
|
|
return this.readonlyItems.includes(path) |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_removeFromReadonlyList (path) { |
|
|
|
|
|
|
|
const indexToRemove = this.readonlyItems.indexOf(path) |
|
|
|
|
|
|
|
if (indexToRemove !== -1) { |
|
|
|
|
|
|
|
this.readonlyItems.splice(indexToRemove, 1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
remove (path) { |
|
|
|
remove (path) { |
|
|
|
this._removeFromReadonlyList(path) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var unprefixedpath = this.removePrefix(path) |
|
|
|
var unprefixedpath = this.removePrefix(path) |
|
|
|
if (!this._exists(unprefixedpath)) { |
|
|
|
if (!this._exists(unprefixedpath)) { |
|
|
|
return false |
|
|
|
return false |
|
|
|