1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-26 10:49:17 +00:00

removed current_preset_index, moved createStore

This commit is contained in:
Steven Hugg 2018-06-27 22:18:16 -06:00
parent 0ca98f5e7f
commit 4772f80e50
2 changed files with 38 additions and 36 deletions

View File

@ -63,12 +63,12 @@ var OldFileStoreDriver = {
localforage.defineDriver(OldFileStoreDriver); localforage.defineDriver(OldFileStoreDriver);
// copy localStorage to new driver // copy localStorage to new driver
function copyFromOldStorageFormat(platform_id, newstore) { function copyFromOldStorageFormat(platformid, newstore) {
var alreadyMigratedKey = "__migrated_" + platform_id; var alreadyMigratedKey = "__migrated_" + platformid;
//localStorage.removeItem(alreadyMigratedKey); //localStorage.removeItem(alreadyMigratedKey);
if (localStorage.getItem(alreadyMigratedKey)) if (localStorage.getItem(alreadyMigratedKey))
return; return;
var oldstore = new OldFileStore(localStorage, platform_id + '/'); var oldstore = new OldFileStore(localStorage, platformid + '/');
var keys = oldstore.getFiles(''); var keys = oldstore.getFiles('');
// no files to convert? // no files to convert?
if (keys.length == 0) { if (keys.length == 0) {
@ -100,3 +100,12 @@ function copyFromOldStorageFormat(platform_id, newstore) {
} }
migrateNext(); // start the conversion migrateNext(); // start the conversion
} }
function createNewPersistentStore(platformid) {
store = localforage.createInstance({
name: platformid,
version: "2.0"
});
copyFromOldStorageFormat(platformid, store);
return store;
}

View File

@ -82,8 +82,8 @@ var TOOL_TO_SOURCE_STYLE = {
var worker = new Worker("./src/worker/workermain.js"); var worker = new Worker("./src/worker/workermain.js");
var editor; var editor;
var current_output; var current_output;
var current_preset_index = -1; var current_preset_entry;
var current_preset_id; var current_file_id;
var assemblyfile; var assemblyfile;
var sourcefile; var sourcefile;
var symbolmap; var symbolmap;
@ -147,10 +147,10 @@ function inspectVariable(ed, name) {
} }
function getCurrentPresetTitle() { function getCurrentPresetTitle() {
if (current_preset_index < 0) if (!current_preset_entry)
return "ROM"; return "ROM";
else else
return PRESETS[current_preset_index].title || PRESETS[current_preset_index].name || "ROM"; return current_preset_entry.title || current_preset_entry.name || "ROM";
} }
function setLastPreset(id) { function setLastPreset(id) {
@ -160,11 +160,11 @@ function setLastPreset(id) {
} }
} }
function updatePreset(current_preset_id, text) { function updatePreset(fileid, text) {
// TODO: do we have to save all Verilog thingies? // TODO: do we have to save all Verilog thingies?
if (text.trim().length && if (text.trim().length &&
(originalFileID != current_preset_id || text != originalText || platform_id=='verilog')) { (originalFileID != fileid || text != originalText || platform_id=='verilog')) {
store.setItem(current_preset_id, text); store.setItem(fileid, text);
} }
} }
@ -179,18 +179,18 @@ function loadCode(text, fileid) {
originalText = text; originalText = text;
} }
function loadFile(fileid, filename, index) { function loadFile(fileid, filename, preset) {
current_preset_id = fileid; current_file_id = fileid;
current_preset_index = index; current_preset_entry = preset;
store.getItem(fileid, function(err, text) { store.getItem(fileid, function(err, text) {
if (err) console.log(err); if (err) console.log(err);
if (!text) text = ''; if (!text) text = '';
if (text) { if (text) {
loadCode(text, fileid); loadCode(text, fileid);
} else if (!text && index >= 0) { } else if (!text && preset) {
if (filename.indexOf('.') <= 0) if (filename.indexOf('.') <= 0)
filename += ".a"; filename += ".a"; // TODO?
console.log("Loading preset", fileid, filename, index, PRESETS[index]); console.log("Loading preset", fileid, filename, preset);
if (text.length == 0) { if (text.length == 0) {
console.log("Fetching", filename); console.log("Fetching", filename);
$.get( filename, function( text ) { $.get( filename, function( text ) {
@ -224,10 +224,10 @@ function loadPreset(preset_id) {
index = (index + PRESETS.length) % PRESETS.length; index = (index + PRESETS.length) % PRESETS.length;
if (index >= 0) { if (index >= 0) {
// load the preset // load the preset
loadFile(preset_id, "presets/" + platform_id + "/" + PRESETS[index].id, index); loadFile(preset_id, "presets/" + platform_id + "/" + PRESETS[index].id, PRESETS[index]);
} else { } else {
// no preset found? load local // no preset found? load local
loadFile(preset_id, "local/" + platform_id + "/" + preset_id, -1); loadFile(preset_id, "local/" + platform_id + "/" + preset_id);
} }
} }
@ -290,7 +290,7 @@ function handleFileUpload(files) {
} }
function getCurrentFilename() { function getCurrentFilename() {
var toks = current_preset_id.split("/"); var toks = current_file_id.split("/");
return toks[toks.length-1]; return toks[toks.length-1];
} }
@ -318,9 +318,9 @@ function _shareFile(e) {
} }
function _resetPreset(e) { function _resetPreset(e) {
if (current_preset_index < 0) { if (!current_preset_entry) {
alert("Can only reset built-in file examples.") alert("Can only reset built-in file examples.")
} else if (confirm("Reset '" + PRESETS[current_preset_index].name + "' to default?")) { } else if (confirm("Reset '" + current_preset_entry.name + "' to default?")) {
qs['reset'] = '1'; qs['reset'] = '1';
gotoNewLocation(); gotoNewLocation();
} }
@ -348,7 +348,7 @@ function populateExamples(sel) {
for (var i=0; i<PRESETS.length; i++) { for (var i=0; i<PRESETS.length; i++) {
var preset = PRESETS[i]; var preset = PRESETS[i];
var name = preset.chapter ? (preset.chapter + ". " + preset.name) : preset.name; var name = preset.chapter ? (preset.chapter + ". " + preset.name) : preset.name;
sel.append($("<option />").val(preset.id).text(name).attr('selected',preset.id==current_preset_id)); sel.append($("<option />").val(preset.id).text(name).attr('selected',preset.id==current_file_id));
} }
}); });
} }
@ -364,12 +364,12 @@ function populateFiles(sel, category, prefix) {
if (numFound++ == 0) if (numFound++ == 0)
sel.append($("<option />").text("------- " + category + " -------").attr('disabled',true)); sel.append($("<option />").text("------- " + category + " -------").attr('disabled',true));
var name = key.substring(prefix.length); var name = key.substring(prefix.length);
sel.append($("<option />").val(key).text(name).attr('selected',key==current_preset_id)); sel.append($("<option />").val(key).text(name).attr('selected',key==current_file_id));
if (key == current_preset_id) foundSelected = true; if (key == current_file_id) foundSelected = true;
} }
} }
if (!foundSelected && current_preset_id && current_preset_id.startsWith(prefix)) { if (!foundSelected && current_file_id && current_file_id.startsWith(prefix)) {
var name = current_preset_id.slice(prefix.length); var name = current_file_id.slice(prefix.length);
var key = prefix + name; var key = prefix + name;
sel.append($("<option />").val(key).text(name).attr('selected',true)); sel.append($("<option />").val(key).text(name).attr('selected',true));
} }
@ -427,7 +427,7 @@ function setCode(text) {
code:text, code:text,
dependencies:depends, dependencies:depends,
platform:platform_id, platform:platform_id,
tool:platform.getToolForFilename(current_preset_id) tool:platform.getToolForFilename(current_file_id)
}); });
}); });
} }
@ -456,7 +456,7 @@ function setCompileOutput(data) {
addr2symbol = invertMap(symbolmap); addr2symbol = invertMap(symbolmap);
addr2symbol[0x10000] = '__END__'; // TODO? addr2symbol[0x10000] = '__END__'; // TODO?
compparams = data.params; compparams = data.params;
updatePreset(current_preset_id, editor.getValue()); // update persisted entry updatePreset(current_file_id, editor.getValue()); // update persisted entry
// errors? // errors?
var lines2errmsg = []; var lines2errmsg = [];
function addErrorMarker(line, msg) { function addErrorMarker(line, msg) {
@ -1257,14 +1257,7 @@ function preloadWorker(fileid) {
} }
function initPlatform() { function initPlatform() {
//store = new FileStore(localStorage, platform_id + '/'); store = createNewPersistentStore(platform_id);
store = localforage.createInstance({
//driver: 'oldFileStoreDriver', //localforage.LOCALSTORAGE,
name: platform_id,
//storeName: platform_id,
version: "2.0"
});
copyFromOldStorageFormat(platform_id, store);
} }
function showBookLink() { function showBookLink() {