Merge pull request #377 from ethereum/storage-reworked

Storage reworked
pull/1/head
chriseth 8 years ago committed by GitHub
commit 2064ac9b6e
  1. 42
      src/app.js
  2. 4
      src/app/config.js
  3. 3
      src/app/debugger.js
  4. 17
      src/app/editor.js
  5. 8
      src/app/renderer.js
  6. 28
      src/app/storage.js
  7. 21
      src/app/utils.js

@ -4,7 +4,6 @@
var $ = require('jquery') var $ = require('jquery')
var base64 = require('js-base64').Base64 var base64 = require('js-base64').Base64
var utils = require('./app/utils')
var QueryParams = require('./app/query-params') var QueryParams = require('./app/query-params')
var queryParams = new QueryParams() var queryParams = new QueryParams()
var GistHandler = require('./app/gist-handler') var GistHandler = require('./app/gist-handler')
@ -45,11 +44,10 @@ var run = function () {
function loadFiles (files) { function loadFiles (files) {
for (var f in files) { for (var f in files) {
var key = utils.fileKey(f) storage.loadFile(f, files[f].content)
var content = files[f].content
storage.loadFile(key, content)
} }
editor.setCacheFile(utils.fileKey(Object.keys(files)[0])) // Set the first file as current tab
editor.setCacheFile(Object.keys(files)[0])
updateFiles() updateFiles()
} }
@ -100,7 +98,7 @@ var run = function () {
function check (key) { function check (key) {
chrome.storage.sync.get(key, function (resp) { chrome.storage.sync.get(key, function (resp) {
console.log('comparing to cloud', key, resp) console.log('comparing to cloud', key, resp)
if (typeof resp[key] !== 'undefined' && obj[key] !== resp[key] && confirm('Overwrite "' + utils.fileNameFromKey(key) + '"? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.')) { if (typeof resp[key] !== 'undefined' && obj[key] !== resp[key] && confirm('Overwrite "' + key + '"? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.')) {
console.log('Overwriting', key) console.log('Overwriting', key)
storage.set(key, resp[key]) storage.set(key, resp[key])
updateFiles() updateFiles()
@ -120,9 +118,6 @@ var run = function () {
for (var y in storage.keys()) { for (var y in storage.keys()) {
console.log('checking', y) console.log('checking', y)
obj[y] = storage.get(y) obj[y] = storage.get(y)
if (!utils.isCachedFile(y)) {
continue
}
count++ count++
check(y) check(y)
} }
@ -246,9 +241,9 @@ var run = function () {
editor.hasFile(newName) editor.hasFile(newName)
? 'Are you sure you want to overwrite: ' + newName + ' with ' + originalName + '?' ? 'Are you sure you want to overwrite: ' + newName + ' with ' + originalName + '?'
: 'Are you sure you want to rename: ' + originalName + ' to ' + newName + '?')) { : 'Are you sure you want to rename: ' + originalName + ' to ' + newName + '?')) {
storage.rename(utils.fileKey(originalName), utils.fileKey(newName)) storage.rename(originalName, newName)
editor.renameSession(utils.fileKey(originalName), utils.fileKey(newName)) editor.renameSession(originalName, newName)
editor.setCacheFile(utils.fileKey(newName)) editor.setCacheFile(newName)
} }
updateFiles() updateFiles()
@ -263,16 +258,16 @@ var run = function () {
var name = $(this).parent().find('.name').text() var name = $(this).parent().find('.name').text()
if (confirm('Are you sure you want to remove: ' + name + ' from local storage?')) { if (confirm('Are you sure you want to remove: ' + name + ' from local storage?')) {
storage.remove(utils.fileKey(name)) storage.remove(name)
editor.removeSession(utils.fileKey(name)) editor.removeSession(name)
editor.setNextFile(utils.fileKey(name)) editor.setNextFile(name)
updateFiles() updateFiles()
} }
return false return false
}) })
function swicthToFile (file) { function swicthToFile (file) {
editor.setCacheFile(utils.fileKey(file)) editor.setCacheFile(file)
updateFiles() updateFiles()
} }
@ -290,12 +285,12 @@ var run = function () {
$('#output').empty() $('#output').empty()
for (var f in files) { for (var f in files) {
var name = utils.fileNameFromKey(files[f]) var name = files[f]
$filesEl.append($('<li class="file"><span class="name">' + name + '</span><span class="remove"><i class="fa fa-close"></i></span></li>')) $filesEl.append($('<li class="file"><span class="name">' + name + '</span><span class="remove"><i class="fa fa-close"></i></span></li>'))
} }
if (editor.cacheFileIsPresent()) { if (editor.cacheFileIsPresent()) {
var currentFileName = utils.fileNameFromKey(editor.getCacheFile()) var currentFileName = editor.getCacheFile()
var active = $('#files .file').filter(function () { return $(this).find('.name').text() === currentFileName }) var active = $('#files .file').filter(function () { return $(this).find('.name').text() === currentFileName })
active.addClass('active') active.addClass('active')
editor.resetSession() editor.resetSession()
@ -369,7 +364,6 @@ var run = function () {
// ----------------- resizeable ui --------------- // ----------------- resizeable ui ---------------
var EDITOR_SIZE_KEY = 'editor-size-cache'
var EDITOR_WINDOW_SIZE = 'editorWindowSize' var EDITOR_WINDOW_SIZE = 'editorWindowSize'
var dragging = false var dragging = false
@ -413,14 +407,6 @@ var run = function () {
} }
}) })
// convert old browser-solidity
if (storage.exists(EDITOR_SIZE_KEY)) {
if (!config.exists(EDITOR_WINDOW_SIZE)) {
config.set(EDITOR_WINDOW_SIZE, storage.get(EDITOR_SIZE_KEY))
}
storage.remove(EDITOR_SIZE_KEY)
}
if (config.exists(EDITOR_WINDOW_SIZE)) { if (config.exists(EDITOR_WINDOW_SIZE)) {
setEditorSize(config.get(EDITOR_WINDOW_SIZE)) setEditorSize(config.get(EDITOR_WINDOW_SIZE))
} else { } else {
@ -536,7 +522,7 @@ var run = function () {
function runCompiler () { function runCompiler () {
var files = {} var files = {}
var target = utils.fileNameFromKey(editor.getCacheFile()) var target = editor.getCacheFile()
files[target] = editor.getValue() files[target] = editor.getValue()

@ -1,8 +1,6 @@
'use strict' 'use strict'
var utils = require('./utils') var CONFIG_FILE = '.browser-solidity.json'
var CONFIG_FILE = utils.fileKey('.browser-solidity.json')
function Config (storage) { function Config (storage) {
this.items = {} this.items = {}

@ -1,7 +1,6 @@
'use strict' 'use strict'
var remix = require('ethereum-remix') var remix = require('ethereum-remix')
var utils = require('./utils')
var ace = require('brace') var ace = require('brace')
var Range = ace.acequire('ace/range').Range var Range = ace.acequire('ace/range').Range
@ -70,7 +69,7 @@ Debugger.prototype.debug = function (txHash) {
* @param {Object} rawLocation - raw position of the source code to hightlight {start, length, file, jump} * @param {Object} rawLocation - raw position of the source code to hightlight {start, length, file, jump}
*/ */
Debugger.prototype.highlight = function (lineColumnPos, rawLocation) { Debugger.prototype.highlight = function (lineColumnPos, rawLocation) {
var name = utils.fileNameFromKey(this.editor.getCacheFile()) // current opened tab var name = this.editor.getCacheFile() // current opened tab
var source = this.compiler.lastCompilationResult.data.sourceList[rawLocation.file] // auto switch to that tab var source = this.compiler.lastCompilationResult.data.sourceList[rawLocation.file] // auto switch to that tab
this.removeCurrentMarker() this.removeCurrentMarker()
if (name !== source) { if (name !== source) {

@ -1,14 +1,13 @@
/* global FileReader */ /* global FileReader */
'use strict' 'use strict'
var utils = require('./utils')
var examples = require('./example-contracts') var examples = require('./example-contracts')
var ace = require('brace') var ace = require('brace')
require('../mode-solidity.js') require('../mode-solidity.js')
function Editor (loadingFromGist, storage) { function Editor (loadingFromGist, storage) {
var SOL_CACHE_UNTITLED = utils.fileKey('Untitled') var SOL_CACHE_UNTITLED = 'Untitled'
var SOL_CACHE_FILE = null var SOL_CACHE_FILE = null
var editor = ace.edit('input') var editor = ace.edit('input')
@ -37,7 +36,7 @@ function Editor (loadingFromGist, storage) {
this.uploadFile = function (file, callback) { this.uploadFile = function (file, callback) {
var fileReader = new FileReader() var fileReader = new FileReader()
var cacheName = utils.fileKey(file.name) var cacheName = file.name
fileReader.onload = function (e) { fileReader.onload = function (e) {
storage.set(cacheName, e.target.result) storage.set(cacheName, e.target.result)
@ -85,18 +84,18 @@ function Editor (loadingFromGist, storage) {
} }
this.hasFile = function (name) { this.hasFile = function (name) {
return this.getFiles().indexOf(utils.fileKey(name)) !== -1 return this.getFiles().indexOf(name) !== -1
} }
this.getFile = function (name) { this.getFile = function (name) {
return storage.get(utils.fileKey(name)) return storage.get(name)
} }
function getFiles () { function getFiles () {
var files = [] var files = []
storage.keys().forEach(function (f) { storage.keys().forEach(function (f) {
// NOTE: as a temporary measure do not show the config file in the editor // NOTE: as a temporary measure do not show the config file in the editor
if (utils.isCachedFile(f) && (f !== (utils.fileKey('.browser-solidity.json')))) { if (f !== '.browser-solidity.json') {
files.push(f) files.push(f)
if (!sessions[f]) sessions[f] = newEditorSession(f) if (!sessions[f]) sessions[f] = newEditorSession(f)
} }
@ -110,7 +109,7 @@ function Editor (loadingFromGist, storage) {
var filesArr = this.getFiles() var filesArr = this.getFiles()
for (var f in filesArr) { for (var f in filesArr) {
files[utils.fileNameFromKey(filesArr[f])] = { files[filesArr[f]] = {
content: storage.get(filesArr[f]) content: storage.get(filesArr[f])
} }
} }
@ -174,8 +173,8 @@ function Editor (loadingFromGist, storage) {
function setupStuff (files) { function setupStuff (files) {
if (files.length === 0) { if (files.length === 0) {
if (loadingFromGist) return if (loadingFromGist) return
files.push(utils.fileKey(examples.ballot.name)) files.push(examples.ballot.name)
storage.set(utils.fileKey(examples.ballot.name), examples.ballot.content) storage.set(examples.ballot.name, examples.ballot.content)
} }
SOL_CACHE_FILE = files[0] SOL_CACHE_FILE = files[0]

@ -55,7 +55,7 @@ Renderer.prototype.error = function (message, container, options) {
var errFile = err[1] var errFile = err[1]
var errLine = parseInt(err[2], 10) - 1 var errLine = parseInt(err[2], 10) - 1
var errCol = err[4] ? parseInt(err[4], 10) : 0 var errCol = err[4] ? parseInt(err[4], 10) : 0
if (!opt.noAnnotations && (errFile === '' || errFile === utils.fileNameFromKey(self.editor.getCacheFile()))) { if (!opt.noAnnotations && (errFile === '' || errFile === self.editor.getCacheFile())) {
self.editor.addAnnotation({ self.editor.addAnnotation({
row: errLine, row: errLine,
column: errCol, column: errCol,
@ -64,9 +64,9 @@ Renderer.prototype.error = function (message, container, options) {
}) })
} }
$error.click(function (ev) { $error.click(function (ev) {
if (errFile !== '' && errFile !== utils.fileNameFromKey(self.editor.getCacheFile()) && self.editor.hasFile(errFile)) { if (errFile !== '' && errFile !== self.editor.getCacheFile() && self.editor.hasFile(errFile)) {
// Switch to file // Switch to file
self.editor.setCacheFile(utils.fileKey(errFile)) self.editor.setCacheFile(errFile)
self.updateFiles() self.updateFiles()
} }
self.editor.handleErrorClick(errLine, errCol) self.editor.handleErrorClick(errLine, errCol)
@ -287,7 +287,7 @@ Renderer.prototype.contracts = function (data, source) {
var self = this var self = this
var getSource = function (contractName, source, data) { var getSource = function (contractName, source, data) {
var currentFile = utils.fileNameFromKey(self.editor.getCacheFile()) var currentFile = self.editor.getCacheFile()
return source.sources[currentFile] return source.sources[currentFile]
} }

@ -6,20 +6,28 @@ function Storage () {
} }
this.get = function (name) { this.get = function (name) {
return window.localStorage.getItem(name) return window.localStorage.getItem('sol:' + name)
} }
this.set = function (name, content) { this.set = function (name, content) {
window.localStorage.setItem(name, content) window.localStorage.setItem('sol:' + name, content)
} }
this.keys = function () { function safeKeys () {
// NOTE: this is a workaround for some browsers // NOTE: this is a workaround for some browsers
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined }) return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
} }
this.keys = function () {
return safeKeys()
// filter any names not including sol:
.filter(function (item) { return item.indexOf('sol:', 0) === 0 })
// remove sol: from filename
.map(function (item) { return item.replace(/^sol:/, '') })
}
this.remove = function (name) { this.remove = function (name) {
window.localStorage.removeItem(name) window.localStorage.removeItem('sol:' + name)
} }
this.rename = function (originalName, newName) { this.rename = function (originalName, newName) {
@ -36,6 +44,18 @@ function Storage () {
} }
this.set(filename, content) this.set(filename, content)
} }
// on startup, upgrade the old storage layout
safeKeys().forEach(function (name) {
if (name.indexOf('sol-cache-file-', 0) === 0) {
var content = window.localStorage.getItem(name)
window.localStorage.setItem(name.replace(/^sol-cache-file-/, 'sol:'), content)
window.localStorage.removeItem(name)
}
})
// remove obsolete key
window.localStorage.removeItem('editor-size-cache')
} }
module.exports = Storage module.exports = Storage

@ -1,30 +1,9 @@
'use strict' 'use strict'
var SOL_CACHE_FILE_PREFIX = 'sol-cache-file-'
function getCacheFilePrefix () {
return SOL_CACHE_FILE_PREFIX
}
function isCachedFile (name) {
return name.indexOf(getCacheFilePrefix(), 0) === 0
}
function fileKey (name) {
return getCacheFilePrefix() + name
}
function fileNameFromKey (key) {
return key.replace(getCacheFilePrefix(), '')
}
function errortype (message) { function errortype (message) {
return message.match(/^(.*:[0-9]*:[0-9]* )?Warning: /) ? 'warning' : 'error' return message.match(/^(.*:[0-9]*:[0-9]* )?Warning: /) ? 'warning' : 'error'
} }
module.exports = { module.exports = {
isCachedFile: isCachedFile,
fileKey: fileKey,
fileNameFromKey: fileNameFromKey,
errortype: errortype errortype: errortype
} }

Loading…
Cancel
Save