From f54dc7505c6136f060fbc4d7207ac3076efb2e0b Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 30 Aug 2017 17:19:50 +0200 Subject: [PATCH] Migrate indentation settings to a settings subobject --- www/code2/inner.js | 4 +- www/common/fsStore.js | 5 ++- www/common/migrate-user-object.js | 68 +++++++++++++++++++++++++++++++ www/common/userObject.js | 1 + 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 www/common/migrate-user-object.js diff --git a/www/code2/inner.js b/www/code2/inner.js index 137ef4b31..f7e045792 100644 --- a/www/code2/inner.js +++ b/www/code2/inner.js @@ -81,7 +81,7 @@ define([ }; var andThen = function (editor, CodeMirror, common) { - var readOnly = false; // TODO + var readOnly = false; var cpNfInner; var metadataMgr; var onLocal; @@ -113,8 +113,6 @@ define([ var indentKey = 'cryptpad.indentUnit'; var useTabsKey = 'cryptpad.indentWithTabs'; - //var proxy = Cryptpad.getProxy(); - var updateIndentSettings = function () { var indentUnit = proxy[indentKey]; var useTabs = proxy[useTabsKey]; diff --git a/www/common/fsStore.js b/www/common/fsStore.js index ce2c8f0da..1599c46d3 100644 --- a/www/common/fsStore.js +++ b/www/common/fsStore.js @@ -4,7 +4,8 @@ define([ '/bower_components/chainpad-crypto/crypto.js?v=0.1.5', '/bower_components/textpatcher/TextPatcher.amd.js', '/common/userObject.js', -], function ($, Listmap, Crypto, TextPatcher, FO) { + '/common/migrate-user-object.js' +], function ($, Listmap, Crypto, TextPatcher, FO, Migrate) { /* This module uses localStorage, which is synchronous, but exposes an asyncronous API. This is so that we can substitute other storage @@ -154,6 +155,8 @@ define([ var todo = function () { fo.fixFiles(); + Migrate(proxy, Cryptpad); + //storeObj = proxy; store = initStore(fo, proxy, exp); if (typeof(f) === 'function') { diff --git a/www/common/migrate-user-object.js b/www/common/migrate-user-object.js new file mode 100644 index 000000000..5cccd22b9 --- /dev/null +++ b/www/common/migrate-user-object.js @@ -0,0 +1,68 @@ +define([], function () { + // Start migration check + // Versions: + // 1: migrate pad attributes + // 2: migrate indent settings (codemirror) + + return function (userObject, Cryptpad) { + var version = userObject.version || 0; + + // Migration 1: pad attributes moved to filesData + var migrateAttributes = function () { + var files = userObject && userObject.drive; + if (!files) { return; } + + var migratePadAttributes = function (el, id, parsed) { + // Migrate old pad attributes + ['userid', 'previewMode'].forEach(function (attr) { + var key = parsed.hash + '.' + attr; + var key2 = parsed.hash.slice(0,-1) + '.' + attr;// old pads not ending with / + if (typeof(files[key]) !== "undefined" || typeof(files[key2]) !== "undefined") { + debug("Migrating pad attribute", attr, "for pad", id); + el[attr] = files[key] || files[key2]; + delete files[key]; + delete files[key2]; + } + }); + }; + var filesData = files.filesData; + if (!filesData) { return; } + + var el, id, parsed; + for (var id in filesData) { + id = Number(id); + el = filesData[id]; + parsed = el.href && Cryptpad.parsePadUrl(el.href); + if (!parsed) { continue; } + migratePadAttributes(el, id, parsed); + } + // Migration done + }; + if (version < 1) { + migrateAttributes(); + Cryptpad.feedback('Migrate-1', true); + userObject.version = version = 1; + } + + + // Migration 2: indentation settings for CodeMirror moved from root to 'settings' + var migrateIndent = function () { + var indentKey = 'cryptpad.indentUnit'; + var useTabsKey = 'cryptpad.indentWithTabs'; + userObject.settings = userObject.settings || {}; + if (userObject[indentKey]) { + userObject.settings.indentUnit = userObject[indentKey]; + delete userObject[indentKey]; + } + if (userObject[useTabsKey]) { + userObject.settings.indentWithTabs = userObject[useTabsKey]; + delete userObject[useTabsKey]; + } + }; + if (version < 2) { + migrateIndent(); + Cryptpad.feedback('Migrate-2', true); + userObject.version = version = 2; + } + }; +}); diff --git a/www/common/userObject.js b/www/common/userObject.js index 431e9c9dc..2d9f0680d 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -816,6 +816,7 @@ define([ } try { debug("Migrating file system..."); + Cryptpad.feedback('Migrate-oldFilesData', true); files.migrate = 1; var next = function () { var oldData = files[OLD_FILES_DATA].slice();