Stackedit/public/res/storage.js

290 lines
12 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
// Setup an empty localStorage or upgrade an existing one
define([
2013-11-05 23:03:38 +00:00
"underscore"
], function(_) {
2014-03-20 00:24:56 +00:00
2013-11-05 23:03:38 +00:00
function retrieveIndexArray(storeIndex) {
try {
return _.compact(localStorage[storeIndex].split(";"));
}
catch(e) {
localStorage[storeIndex] = ";";
return [];
}
}
2013-05-29 19:55:23 +00:00
2013-11-05 23:03:38 +00:00
var fileIndexList = retrieveIndexArray("file.list");
var currentFileIndex, settings;
2013-05-29 19:55:23 +00:00
// localStorage versioning
2013-11-05 23:03:38 +00:00
var version = localStorage.version;
2013-05-29 19:55:23 +00:00
if(version === undefined) {
// Not used anymore
localStorage.removeItem("sync.queue");
localStorage.removeItem("sync.current");
localStorage.removeItem("file.counter");
_.each(fileIndexList, function(fileIndex) {
localStorage[fileIndex + ".publish"] = ";";
2013-11-05 23:03:38 +00:00
var syncIndexList = retrieveIndexArray(fileIndex + ".sync");
2013-05-29 19:55:23 +00:00
_.each(syncIndexList, function(syncIndex) {
localStorage[syncIndex + ".contentCRC"] = "0";
// We store title CRC only for Google Drive synchronization
if(localStorage[syncIndex + ".etag"] !== undefined) {
localStorage[syncIndex + ".titleCRC"] = "0";
}
});
});
version = "v1";
}
if(version == "v1") {
var gdriveLastChangeId = localStorage["sync.gdrive.lastChangeId"];
if(gdriveLastChangeId) {
localStorage["gdrive.lastChangeId"] = gdriveLastChangeId;
localStorage.removeItem("sync.gdrive.lastChangeId");
}
var dropboxLastChangeId = localStorage["sync.dropbox.lastChangeId"];
if(dropboxLastChangeId) {
localStorage["dropbox.lastChangeId"] = dropboxLastChangeId;
localStorage.removeItem("sync.dropbox.lastChangeId");
}
var PROVIDER_GDRIVE = "gdrive";
var PROVIDER_DROPBOX = "dropbox";
var SYNC_PROVIDER_GDRIVE = "sync." + PROVIDER_GDRIVE + ".";
var SYNC_PROVIDER_DROPBOX = "sync." + PROVIDER_DROPBOX + ".";
_.each(fileIndexList, function(fileIndex) {
2013-11-05 23:03:38 +00:00
var syncIndexList = retrieveIndexArray(fileIndex + ".sync");
2013-05-29 19:55:23 +00:00
_.each(syncIndexList, function(syncIndex) {
var syncAttributes = {};
if(syncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
syncAttributes.provider = PROVIDER_GDRIVE;
syncAttributes.id = syncIndex.substring(SYNC_PROVIDER_GDRIVE.length);
syncAttributes.etag = localStorage[syncIndex + ".etag"];
syncAttributes.contentCRC = localStorage[syncIndex + ".contentCRC"];
syncAttributes.titleCRC = localStorage[syncIndex + ".titleCRC"];
}
else if(syncIndex.indexOf(SYNC_PROVIDER_DROPBOX) === 0) {
syncAttributes.provider = PROVIDER_DROPBOX;
syncAttributes.path = decodeURIComponent(syncIndex.substring(SYNC_PROVIDER_DROPBOX.length));
syncAttributes.version = localStorage[syncIndex + ".version"];
syncAttributes.contentCRC = localStorage[syncIndex + ".contentCRC"];
}
localStorage[syncIndex] = JSON.stringify(syncAttributes);
localStorage.removeItem(syncIndex + ".etag");
localStorage.removeItem(syncIndex + ".version");
localStorage.removeItem(syncIndex + ".contentCRC");
localStorage.removeItem(syncIndex + ".titleCRC");
});
});
version = "v2";
}
if(version == "v2") {
_.each(fileIndexList, function(fileIndex) {
if(!_.has(localStorage, fileIndex + ".sync")) {
localStorage.removeItem(fileIndex + ".title");
localStorage.removeItem(fileIndex + ".publish");
localStorage.removeItem(fileIndex + ".content");
2013-11-05 23:03:38 +00:00
localStorage["file.list"].replace(";" + fileIndex + ";", ";");
2013-05-29 19:55:23 +00:00
}
});
version = "v3";
}
if(version == "v3") {
2013-11-05 23:03:38 +00:00
currentFileIndex = localStorage["file.current"];
2013-05-29 19:55:23 +00:00
if(currentFileIndex !== undefined && localStorage["file.list"].indexOf(";" + currentFileIndex + ";") === -1) {
localStorage.removeItem("file.current");
}
version = "v4";
}
if(version == "v4") {
// Recreate GitHub token
localStorage.removeItem("githubToken");
version = "v5";
}
if(version == "v5") {
_.each(fileIndexList, function(fileIndex) {
2013-11-05 23:03:38 +00:00
var publishIndexList = retrieveIndexArray(fileIndex + ".publish");
2013-05-29 19:55:23 +00:00
_.each(publishIndexList, function(publishIndex) {
var publishAttributes = JSON.parse(localStorage[publishIndex]);
if(publishAttributes.provider == "gdrive") {
// Change fileId to Id to be consistent with syncAttributes
publishAttributes.id = publishAttributes.fileId;
publishAttributes.fileId = undefined;
localStorage[publishIndex] = JSON.stringify(publishAttributes);
}
});
});
version = "v6";
}
2013-09-09 00:08:55 +00:00
2013-06-16 10:47:35 +00:00
if(version == "v6") {
2013-11-05 23:03:38 +00:00
currentFileIndex = localStorage["file.current"];
2013-06-16 10:47:35 +00:00
if(currentFileIndex !== undefined) {
localStorage[currentFileIndex + ".selectTime"] = new Date().getTime();
localStorage.removeItem("file.current");
}
version = "v7";
}
2013-09-09 00:08:55 +00:00
if(version == "v7" || version == "v8" || version == "v9") {
2013-09-15 14:14:42 +00:00
if(_.has(localStorage, 'settings')) {
2013-11-05 23:03:38 +00:00
settings = JSON.parse(localStorage.settings);
2013-09-15 14:14:42 +00:00
delete settings.editorFontFamily;
delete settings.editorFontSize;
settings.template && (settings.template = settings.template.replace('http://benweet.github.io/stackedit/css/main-min.css', 'http://benweet.github.io/stackedit/res-min/themes/default.css'));
localStorage.settings = JSON.stringify(settings);
}
version = "v10";
}
2013-10-05 18:18:38 +00:00
if(version == "v10") {
if(_.has(localStorage, 'settings')) {
2013-11-05 23:03:38 +00:00
settings = JSON.parse(localStorage.settings);
((settings.extensionSettings || {}).markdownExtra || {}).extensions && settings.extensionSettings.markdownExtra.extensions.push('smartypants');
2013-10-08 00:34:15 +00:00
settings.sshProxy == 'http://stackedit-ssh-proxy.herokuapp.com/' && (settings.sshProxy = 'https://stackedit-ssh-proxy.herokuapp.com/');
settings.template && (settings.template = settings.template.replace('http://benweet.github.io/stackedit/lib/', 'https://stackedit.io/libs/'));
settings.template && (settings.template = settings.template.replace('http://benweet.github.io/stackedit/', 'https://stackedit.io/'));
settings.pdfTemplate && (settings.pdfTemplate = settings.pdfTemplate.replace('http://benweet.github.io/stackedit/lib/', 'https://stackedit.io/libs/'));
settings.pdfTemplate && (settings.pdfTemplate = settings.pdfTemplate.replace('http://benweet.github.io/stackedit/', 'https://stackedit.io/'));
settings.defaultContent && (settings.defaultContent = settings.defaultContent.replace('http://benweet.github.io/stackedit/', 'https://stackedit.io/'));
settings.commitMsg && (settings.commitMsg = settings.commitMsg.replace('http://benweet.github.io/stackedit/', 'https://stackedit.io/'));
2013-10-05 18:18:38 +00:00
localStorage.settings = JSON.stringify(settings);
}
version = "v11";
}
2013-12-02 23:29:48 +00:00
if(version == "v11") {
// Force new theme by using themeV3 variable
localStorage.removeItem("theme");
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
// Force new font
delete settings.editorFontFamily;
delete settings.editorFontSize;
settings.template && (settings.template = settings.template.replace('https://stackedit.io/res-min/themes/default.css', 'https://stackedit.io/res-min/themes/base.css'));
settings.pdfTemplate && (settings.pdfTemplate = settings.pdfTemplate.replace('https://stackedit.io/res-min/themes/default.css', 'https://stackedit.io/res-min/themes/base.css'));
localStorage.settings = JSON.stringify(settings);
}
version = "v12";
}
2013-12-05 00:25:17 +00:00
if(version == "v12" || version == "v13") {
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
// Have to reset the font because of Monaco issue with ACE
delete settings.editorFontFamily;
localStorage.settings = JSON.stringify(settings);
}
2013-12-05 00:25:17 +00:00
version = "v14";
}
2013-12-06 22:15:51 +00:00
if(version == "v14") {
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
settings.template && (settings.template = settings.template.replace('https://stackedit.io/res-min/themes/default.css', 'https://stackedit.io/res-min/themes/base.css'));
settings.pdfTemplate && (settings.pdfTemplate = settings.pdfTemplate.replace('https://stackedit.io/res-min/themes/default.css', 'https://stackedit.io/res-min/themes/base.css'));
localStorage.settings = JSON.stringify(settings);
}
version = "v15";
}
2013-12-15 17:26:17 +00:00
if(version == "v15") {
2013-12-23 22:46:49 +00:00
localStorage.removeItem('gdrivePermissions');
2013-12-15 17:26:17 +00:00
if(_.has(localStorage, 'gdrive.lastChangeId')) {
2013-12-23 22:46:49 +00:00
localStorage['google.gdrive0.gdrive.lastChangeId'] = localStorage['gdrive.lastChangeId'];
2013-12-15 17:26:17 +00:00
localStorage.removeItem('gdrive.lastChangeId');
}
2013-12-23 22:46:49 +00:00
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
if(((settings.extensionSettings || {}).markdownExtra || {}).extensions) {
settings.extensionSettings.markdownExtra.extensions.push('newlines');
settings.extensionSettings.markdownExtra.extensions.push('strikethrough');
}
localStorage.settings = JSON.stringify(settings);
}
2013-12-15 17:26:17 +00:00
version = "v16";
}
if(version == "v16" || version == "v17") {
2014-03-17 02:01:46 +00:00
localStorage.removeItem('focusMode');
2014-03-20 00:24:56 +00:00
localStorage.removeItem('mode');
localStorage.removeItem('gdrive.state');
2014-04-21 15:36:22 +00:00
localStorage.removeItem('google.picasa0.permissions');
localStorage.removeItem('google.picasa0.userId');
2014-04-17 23:41:35 +00:00
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
delete settings.shortcuts;
delete settings.editorFontFamily;
delete settings.editorFontSize;
delete settings.maxWidth;
2014-04-17 23:41:35 +00:00
localStorage.settings = JSON.stringify(settings);
}
2014-03-17 02:01:46 +00:00
version = "v18";
}
2014-03-20 00:24:56 +00:00
2014-04-26 00:53:06 +00:00
if(version == 'v18') {
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
((settings.extensionSettings || {}).markdownExtra || {}).diagrams = true;
localStorage.settings = JSON.stringify(settings);
}
version = "v19";
}
2014-08-19 08:17:27 +00:00
if(version == 'v19') {
// Force new theme by using themeV4 variable
localStorage.removeItem("themeV3");
// Force welcome tour
localStorage.removeItem("welcomeTour");
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
2014-08-19 23:20:51 +00:00
// New web services
2014-08-19 08:17:27 +00:00
delete settings.pdfTemplate;
delete settings.pdfPageSize;
2014-08-19 23:20:51 +00:00
delete settings.sshProxy;
2014-08-19 08:17:27 +00:00
localStorage.settings = JSON.stringify(settings);
}
version = "v20";
}
2014-08-27 23:11:08 +00:00
if(version == 'v20') {
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
// Force use of text/plain
delete settings.markdownMimeType;
localStorage.settings = JSON.stringify(settings);
}
version = "v21";
}
2015-02-03 19:58:27 +00:00
if(version == "v21") {
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
settings.template && (settings.template = settings.template.replace('https://stackedit.io/libs/MathJax/', 'https://cdn.mathjax.org/mathjax/latest/'));
settings.pdfTemplate && (settings.pdfTemplate = settings.pdfTemplate.replace('/libs/MathJax/', '/res/bower-libs/MathJax/'));
localStorage.settings = JSON.stringify(settings);
}
version = "v22";
}
if(version == "v22") {
2014-08-31 20:35:18 +00:00
if(_.has(localStorage, 'settings')) {
settings = JSON.parse(localStorage.settings);
2015-02-03 19:58:27 +00:00
settings.couchdbUrl && (settings.couchdbUrl = settings.couchdbUrl.replace('https://stackedit.couchappy.com/documents', 'https://stackedit.smileupps.com/documents'));
2014-08-31 20:35:18 +00:00
localStorage.settings = JSON.stringify(settings);
}
2015-02-03 19:58:27 +00:00
version = "v23";
2014-08-31 20:35:18 +00:00
}
localStorage.version = version;
2013-11-05 23:03:38 +00:00
return localStorage;
2014-03-20 00:24:56 +00:00
});