Commit ddf06bd2 authored by Alex Beregszaszi's avatar Alex Beregszaszi

Rewrite file handling logic using the new files API

parent d55a6dc4
This diff is collapsed.
/* global FileReader */
'use strict' 'use strict'
var EventManager = require('../lib/eventManager') var EventManager = require('../lib/eventManager')
var examples = require('./example-contracts')
var ace = require('brace') var ace = require('brace')
var Range = ace.acequire('ace/range').Range var Range = ace.acequire('ace/range').Range
require('../mode-solidity.js') require('../mode-solidity.js')
function Editor (doNotLoadStorage, storage) { function Editor () {
var SOL_CACHE_FILE = null
var editor = ace.edit('input') var editor = ace.edit('input')
document.getElementById('input').editor = editor // required to access the editor during tests document.getElementById('input').editor = editor // required to access the editor during tests
var event = new EventManager() var event = new EventManager()
this.event = event this.event = event
var sessions = {} var sessions = {}
var sourceAnnotations = [] var sourceAnnotations = []
var readOnlySessions = {}
var currentSession
this.addMarker = function (lineColumnPos, cssClass) { var emptySession = createSession('')
var currentRange = new Range(lineColumnPos.start.line, lineColumnPos.start.column, lineColumnPos.end.line, lineColumnPos.end.column)
return editor.session.addMarker(currentRange, cssClass)
}
this.removeMarker = function (markerId) { function createSession (content) {
editor.session.removeMarker(markerId) var s = new ace.EditSession(content, 'ace/mode/javascript')
} s.setUndoManager(new ace.UndoManager())
s.setTabSize(4)
this.newFile = function () { s.setUseSoftTabs(true)
var untitledCount = '' return s
while (storage.exists('Untitled' + untitledCount)) {
untitledCount = (untitledCount - 0) + 1
}
this.setCacheFile('Untitled' + untitledCount)
this.setCacheFileContent('')
}
this.uploadFile = function (file, callback) {
var fileReader = new FileReader()
var name = file.name
var self = this
fileReader.onload = function (e) {
self.setCacheFile(name)
self.setCacheFileContent(e.target.result)
callback()
}
fileReader.readAsText(file)
}
this.setCacheFileContent = function (content) {
storage.set(SOL_CACHE_FILE, content)
}
this.setCacheFile = function (cacheFile) {
SOL_CACHE_FILE = cacheFile
}
this.getCacheFile = function () {
return SOL_CACHE_FILE
}
this.cacheFileIsPresent = function () {
return !!SOL_CACHE_FILE
}
this.setNextFile = function (fileKey) {
var index = this.getFiles().indexOf(fileKey)
this.setCacheFile(this.getFiles()[ Math.max(0, index - 1) ])
} }
this.resetSession = function () { function switchSession (path) {
editor.setSession(sessions[this.getCacheFile()]) currentSession = path
editor.setSession(sessions[currentSession])
editor.setReadOnly(readOnlySessions[currentSession])
editor.focus() editor.focus()
} }
this.removeSession = function (fileKey) { this.open = function (path, content) {
delete sessions[fileKey] if (!sessions[path]) {
var session = createSession(content)
sessions[path] = session
readOnlySessions[path] = false
}
switchSession(path)
} }
this.renameSession = function (oldFileKey, newFileKey) { this.openReadOnly = function (path, content) {
if (oldFileKey !== newFileKey) { if (!sessions[path]) {
sessions[newFileKey] = sessions[oldFileKey] var session = createSession(content)
this.removeSession(oldFileKey) sessions[path] = session
readOnlySessions[path] = true
} }
switchSession(path)
} }
this.hasFile = function (name) { this.get = function (path) {
return this.getFiles().indexOf(name) !== -1 if (currentSession === path) {
return editor.getValue()
} }
this.getFile = function (name) {
return storage.get(name)
} }
function getFiles () { this.current = function (path) {
var files = [] if (editor.getSession() === emptySession) {
storage.keys().forEach(function (f) { return
// NOTE: as a temporary measure do not show the config file in the editor
if (f !== '.browser-solidity.json') {
files.push(f)
if (!sessions[f]) sessions[f] = newEditorSession(f)
} }
}) return currentSession
return files
} }
this.getFiles = getFiles
this.packageFiles = function () { this.discard = function (path) {
var files = {} if (currentSession !== path) {
var filesArr = this.getFiles() delete sessions[path]
for (var f in filesArr) {
files[filesArr[f]] = {
content: storage.get(filesArr[f])
}
} }
return files
} }
this.resize = function () { this.resize = function () {
...@@ -133,8 +84,13 @@ function Editor (doNotLoadStorage, storage) { ...@@ -133,8 +84,13 @@ function Editor (doNotLoadStorage, storage) {
} }
} }
this.getValue = function () { this.addMarker = function (lineColumnPos, cssClass) {
return editor.getValue() var currentRange = new Range(lineColumnPos.start.line, lineColumnPos.start.column, lineColumnPos.end.line, lineColumnPos.end.column)
return editor.session.addMarker(currentRange, cssClass)
}
this.removeMarker = function (markerId) {
editor.session.removeMarker(markerId)
} }
this.clearAnnotations = function () { this.clearAnnotations = function () {
...@@ -156,15 +112,6 @@ function Editor (doNotLoadStorage, storage) { ...@@ -156,15 +112,6 @@ function Editor (doNotLoadStorage, storage) {
editor.gotoLine(line + 1, col - 1, true) editor.gotoLine(line + 1, col - 1, true)
} }
function newEditorSession (filekey) {
var s = new ace.EditSession(storage.get(filekey), 'ace/mode/javascript')
s.setUndoManager(new ace.UndoManager())
s.setTabSize(4)
s.setUseSoftTabs(true)
sessions[filekey] = s
return s
}
// Do setup on initialisation here // Do setup on initialisation here
editor.on('changeSession', function () { editor.on('changeSession', function () {
event.trigger('sessionSwitched', []) event.trigger('sessionSwitched', [])
...@@ -178,24 +125,6 @@ function Editor (doNotLoadStorage, storage) { ...@@ -178,24 +125,6 @@ function Editor (doNotLoadStorage, storage) {
editor.commands.bindKeys({ 'ctrl-t': null }) editor.commands.bindKeys({ 'ctrl-t': null })
editor.commands.bindKeys({ 'ctrl-f': null }) editor.commands.bindKeys({ 'ctrl-f': null })
if (doNotLoadStorage) {
return
}
var files = getFiles()
if (files.length === 0) {
files.push(examples.ballot.name)
storage.set(examples.ballot.name, examples.ballot.content)
}
this.setCacheFile(files[0])
for (var x in files) {
sessions[files[x]] = newEditorSession(files[x])
}
editor.setSession(sessions[this.getCacheFile()])
editor.resize(true) editor.resize(true)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment