small fixes

pull/3094/head
Grandschtroumpf 6 years ago
parent 6a05d9ccef
commit b9a8fb3dce
  1. 105
      src/app/files/browser-files-tree.js
  2. 88
      src/app/files/fileManager.js
  3. 16
      src/app/panels/tab-proxy.js

@ -2,21 +2,35 @@
var EventManager = require('../../lib/events') var EventManager = require('../../lib/events')
function FilesTree (name, storage) { import { ApiFactory } from 'remix-plugin'
var self = this
var event = new EventManager() class FilesTree extends ApiFactory {
this.event = event
this.type = name constructor (name, storage) {
this.structFile = '.' + name + '.tree' super()
this.tree = {} this.event = new EventManager()
this.storage = storage
this.exists = function (path, cb) { this.type = name
this.structFile = '.' + name + '.tree'
this.tree = {}
}
get profile () {
// TODO should make them promisable
return {
name: this.type,
methods: ['get', 'set', 'remove'],
description: 'service - read/write file to the `config` explorer without need of additionnal permission.'
}
}
exists (path, cb) {
cb(null, this._exists(path)) cb(null, this._exists(path))
} }
function updateRefs (path, type) { updateRefs (path, type) {
var split = path.split('/') // this should be unprefixed path var split = path.split('/') // this should be unprefixed path
var crawlpath = self.tree var crawlpath = this.tree
var intermediatePath = '' var intermediatePath = ''
split.forEach((pathPart, index) => { split.forEach((pathPart, index) => {
intermediatePath += pathPart intermediatePath += pathPart
@ -30,91 +44,90 @@ function FilesTree (name, storage) {
delete crawlpath[intermediatePath] delete crawlpath[intermediatePath]
} }
}) })
storage.set(self.structFile, JSON.stringify(self.tree)) this.storage.set(this.structFile, JSON.stringify(this.tree))
} }
this._exists = function (path) { _exists (path) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
return storage.exists(unprefixedpath) return this.storage.exists(unprefixedpath)
} }
this.init = function (cb) { init (cb) {
var tree = storage.get(this.structFile) var tree = this.storage.get(this.structFile)
this.tree = tree ? JSON.parse(tree) : {} this.tree = tree ? JSON.parse(tree) : {}
if (cb) cb() if (cb) cb()
} }
this.get = function (path, cb) { get (path, cb) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
var content = storage.get(unprefixedpath) var content = this.storage.get(unprefixedpath)
if (cb) { if (cb) {
cb(null, content) cb(null, content)
} }
return content return content
} }
this.set = function (path, content, cb) { set (path, content, cb) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
updateRefs(unprefixedpath, 'add') this.updateRefs(unprefixedpath, 'add')
var exists = storage.exists(unprefixedpath) var exists = this.storage.exists(unprefixedpath)
if (!storage.set(unprefixedpath, content)) { if (!this.storage.set(unprefixedpath, content)) {
if (cb) cb('error updating ' + path) if (cb) cb('error updating ' + path)
return false return false
} }
if (!exists) { if (!exists) {
event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false]) this.event.trigger('fileAdded', [this.type + '/' + unprefixedpath, false])
} else { } else {
event.trigger('fileChanged', [this.type + '/' + unprefixedpath]) this.event.trigger('fileChanged', [this.type + '/' + unprefixedpath])
} }
if (cb) cb() if (cb) cb()
return true return true
} }
this.addReadOnly = function (path, content) { addReadOnly (path, content) {
return this.set(path, content) return this.set(path, content)
} }
this.isReadOnly = function (path) { isReadOnly (path) {
return false return false
} }
this.remove = function (path) { remove (path) {
var unprefixedpath = this.removePrefix(path) var unprefixedpath = this.removePrefix(path)
updateRefs(unprefixedpath, 'remove') this.updateRefs(unprefixedpath, 'remove')
if (!this._exists(unprefixedpath)) { if (!this._exists(unprefixedpath)) {
return false return false
} }
if (!storage.remove(unprefixedpath)) { if (!this.storage.remove(unprefixedpath)) {
return false return false
} }
event.trigger('fileRemoved', [this.type + '/' + unprefixedpath]) this.event.trigger('fileRemoved', [this.type + '/' + unprefixedpath])
return true return true
} }
this.rename = function (oldPath, newPath, isFolder) { rename (oldPath, newPath, isFolder) {
var unprefixedoldPath = this.removePrefix(oldPath) var unprefixedoldPath = this.removePrefix(oldPath)
var unprefixednewPath = this.removePrefix(newPath) var unprefixednewPath = this.removePrefix(newPath)
updateRefs(unprefixedoldPath, 'remove') this.updateRefs(unprefixedoldPath, 'remove')
updateRefs(unprefixednewPath, 'add') this.updateRefs(unprefixednewPath, 'add')
if (storage.exists(unprefixedoldPath)) { if (this.storage.exists(unprefixedoldPath)) {
if (!storage.rename(unprefixedoldPath, unprefixednewPath)) { if (!this.storage.rename(unprefixedoldPath, unprefixednewPath)) {
return false return false
} }
event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder]) this.event.trigger('fileRenamed', [this.type + '/' + unprefixedoldPath, this.type + '/' + unprefixednewPath, isFolder])
return true return true
} }
return false return false
} }
this.resolveDirectory = function (path, callback) { resolveDirectory (path, callback) {
var self = this
if (path[0] === '/') path = path.substring(1) if (path[0] === '/') path = path.substring(1)
if (!path) return callback(null, { [self.type]: { } }) if (!path) return callback(null, { [this.type]: { } })
var tree = {} var tree = {}
path = self.removePrefix(path) path = this.removePrefix(path)
var split = path.split('/') // this should be unprefixed path var split = path.split('/') // this should be unprefixed path
var crawlpath = self.tree var crawlpath = this.tree
split.forEach((pathPart, index) => { split.forEach((pathPart, index) => {
if (crawlpath[pathPart]) crawlpath = crawlpath[pathPart] if (crawlpath[pathPart]) crawlpath = crawlpath[pathPart]
}) })
@ -125,20 +138,12 @@ function FilesTree (name, storage) {
callback(null, tree) callback(null, tree)
} }
this.removePrefix = function (path) { removePrefix (path) {
path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path path = path.indexOf(this.type) === 0 ? path.replace(this.type, '') : path
if (path[0] === '/') return path.substring(1) if (path[0] === '/') return path.substring(1)
return path return path
} }
this.profile = function () {
// TODO should make them promisable
return {
name: this.type,
methods: ['get', 'set', 'remove'],
description: 'service - read/write file to the `config` explorer without need of additionnal permission.'
}
}
} }
module.exports = FilesTree module.exports = FilesTree

@ -3,14 +3,16 @@
const EventEmitter = require('events') const EventEmitter = require('events')
var globalRegistry = require('../../global/registry') var globalRegistry = require('../../global/registry')
var CompilerImport = require('../compiler/compiler-imports') var CompilerImport = require('../compiler/compiler-imports')
import { ApiFactory } from 'remix-plugin'
/* /*
attach to files event (removed renamed) attach to files event (removed renamed)
trigger: currentFileChanged trigger: currentFileChanged
*/ */
class FileManager { class FileManager extends ApiFactory {
constructor (localRegistry) { constructor (localRegistry) {
super()
this.openedFiles = {} // list all opened files this.openedFiles = {} // list all opened files
this.events = new EventEmitter() this.events = new EventEmitter()
this._components = {} this._components = {}
@ -19,30 +21,29 @@ class FileManager {
} }
init () { init () {
var self = this this._deps = {
self._deps = { editor: this._components.registry.get('editor').api,
editor: self._components.registry.get('editor').api, config: this._components.registry.get('config').api,
config: self._components.registry.get('config').api, browserExplorer: this._components.registry.get('fileproviders/browser').api,
browserExplorer: self._components.registry.get('fileproviders/browser').api, localhostExplorer: this._components.registry.get('fileproviders/localhost').api,
localhostExplorer: self._components.registry.get('fileproviders/localhost').api, configExplorer: this._components.registry.get('fileproviders/config').api,
configExplorer: self._components.registry.get('fileproviders/config').api, gistExplorer: this._components.registry.get('fileproviders/gist').api,
gistExplorer: self._components.registry.get('fileproviders/gist').api, filesProviders: this._components.registry.get('fileproviders').api
filesProviders: self._components.registry.get('fileproviders').api
} }
self._deps.browserExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) this._deps.browserExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
self._deps.localhostExplorer.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) })
self._deps.configExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) }) this._deps.configExplorer.event.register('fileRenamed', (oldName, newName, isFolder) => { this.fileRenamedEvent(oldName, newName, isFolder) })
self._deps.gistExplorer.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) })
self._deps.browserExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) this._deps.browserExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
self._deps.localhostExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) this._deps.localhostExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
self._deps.configExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) this._deps.configExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
self._deps.gistExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) }) this._deps.gistExplorer.event.register('fileRemoved', (path) => { this.fileRemovedEvent(path) })
self._deps.localhostExplorer.event.register('errored', (event) => { this.removeTabsOf(self._deps.localhostExplorer) }) this._deps.localhostExplorer.event.register('errored', (event) => { this.removeTabsOf(this._deps.localhostExplorer) })
self._deps.localhostExplorer.event.register('closed', (event) => { this.removeTabsOf(self._deps.localhostExplorer) }) this._deps.localhostExplorer.event.register('closed', (event) => { this.removeTabsOf(this._deps.localhostExplorer) })
} }
profile () { get profile () {
return { return {
displayName: 'file manager', displayName: 'file manager',
name: 'fileManager', name: 'fileManager',
@ -53,10 +54,9 @@ class FileManager {
} }
fileRenamedEvent (oldName, newName, isFolder) { fileRenamedEvent (oldName, newName, isFolder) {
var self = this
if (!isFolder) { if (!isFolder) {
self._deps.config.set('currentFile', '') this._deps.config.set('currentFile', '')
self._deps.editor.discard(oldName) this._deps.editor.discard(oldName)
if (this.openedFiles[oldName]) { if (this.openedFiles[oldName]) {
delete this.openedFiles[oldName] delete this.openedFiles[oldName]
this.openedFiles[newName] = newName this.openedFiles[newName] = newName
@ -69,7 +69,7 @@ class FileManager {
var newAbsolutePath = k.replace(oldName, newName) var newAbsolutePath = k.replace(oldName, newName)
this.openedFiles[newAbsolutePath] = newAbsolutePath this.openedFiles[newAbsolutePath] = newAbsolutePath
delete this.openedFiles[k] delete this.openedFiles[k]
if (self._deps.config.get('currentFile') === k) { if (this._deps.config.get('currentFile') === k) {
newFocus = newAbsolutePath newFocus = newAbsolutePath
} }
} }
@ -105,8 +105,7 @@ class FileManager {
} }
currentPath () { currentPath () {
var self = this var currentFile = this._deps.config.get('currentFile')
var currentFile = self._deps.config.get('currentFile')
var reg = /(.*)(\/).*/ var reg = /(.*)(\/).*/
var path = reg.exec(currentFile) var path = reg.exec(currentFile)
return path ? path[1] : null return path ? path[1] : null
@ -154,47 +153,45 @@ class FileManager {
} }
fileRemovedEvent (path) { fileRemovedEvent (path) {
var self = this
if (!this.openedFiles[path]) return if (!this.openedFiles[path]) return
if (path === self._deps.config.get('currentFile')) { if (path === this._deps.config.get('currentFile')) {
self._deps.config.set('currentFile', '') this._deps.config.set('currentFile', '')
} }
self._deps.editor.discard(path) this._deps.editor.discard(path)
delete this.openedFiles[path] delete this.openedFiles[path]
this.events.emit('fileRemoved', path) this.events.emit('fileRemoved', path)
this.switchFile() this.switchFile()
} }
switchFile (file) { switchFile (file) {
var self = this
if (file) return _switchFile(file) if (file) return _switchFile(file)
else { else {
var browserProvider = self._deps.filesProviders['browser'] var browserProvider = this._deps.filesProviders['browser']
browserProvider.resolveDirectory('browser', (error, filesTree) => { browserProvider.resolveDirectory('browser', (error, filesTree) => {
if (error) console.error(error) if (error) console.error(error)
var fileList = Object.keys(filesTree) var fileList = Object.keys(filesTree)
if (fileList.length) { if (fileList.length) {
_switchFile(browserProvider.type + '/' + fileList[0]) _switchFile(browserProvider.type + '/' + fileList[0])
} else { } else {
self.events.emit('currentFileChanged') this.events.emit('currentFileChanged')
self._deps.editor.displayEmptyReadOnlySession() this._deps.editor.displayEmptyReadOnlySession()
} }
}) })
} }
function _switchFile (file) { const _switchFile = (file) => {
self.saveCurrentFile() this.saveCurrentFile()
self._deps.config.set('currentFile', file) this._deps.config.set('currentFile', file)
self.openedFiles[file] = file this.openedFiles[file] = file
self.fileProviderOf(file).get(file, (error, content) => { this.fileProviderOf(file).get(file, (error, content) => {
if (error) { if (error) {
console.log(error) console.log(error)
} else { } else {
if (self.fileProviderOf(file).isReadOnly(file)) { if (this.fileProviderOf(file).isReadOnly(file)) {
self._deps.editor.openReadOnly(file, content) this._deps.editor.openReadOnly(file, content)
} else { } else {
self._deps.editor.open(file, content) this._deps.editor.open(file, content)
} }
self.events.emit('currentFileChanged', file) this.events.emit('currentFileChanged', file)
} }
}) })
} }
@ -243,7 +240,6 @@ class FileManager {
} }
syncEditor (path) { syncEditor (path) {
var self = this
var currentFile = this._deps.config.get('currentFile') var currentFile = this._deps.config.get('currentFile')
if (path !== currentFile) return if (path !== currentFile) return
@ -251,7 +247,7 @@ class FileManager {
if (provider) { if (provider) {
provider.get(currentFile, (error, content) => { provider.get(currentFile, (error, content) => {
if (error) console.log(error) if (error) console.log(error)
self._deps.editor.setText(content) this._deps.editor.setText(content)
}) })
} else { } else {
console.log('cannot save ' + currentFile + '. Does not belong to any explorer') console.log('cannot save ' + currentFile + '. Does not belong to any explorer')

@ -52,13 +52,15 @@ export class TabProxy {
appStore.event.on('activate', (name) => { appStore.event.on('activate', (name) => {
const { profile } = appStore.getOne(name) const { profile } = appStore.getOne(name)
if (profile.prefferedLocation === 'mainPanel') { if (profile.location === 'mainPanel') {
this.addTab(name, () => { this.addTab(
this.event.emit('switchApp', name) name,
}, () => { () => this.event.emit('switchApp', name),
this.event.emit('closeApp', name) () => {
this.appManager.deactivateOne(name) this.event.emit('closeApp', name)
}) this.appManager.deactivateOne(name)
}
)
} }
}) })

Loading…
Cancel
Save