From ef80c10b3f278f9a751e870e49f1268b99290d7a Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 26 May 2016 14:23:24 +0200 Subject: [PATCH] Update StorageManager --- src/editor/config/config.js | 26 ++++++++-------- src/editor/model/Editor.js | 50 ++++++++++++++++-------------- src/grapesjs/config/config.js | 34 ++++++++++++++++++++ src/grapesjs/main.js | 0 src/storage_manager/main.js | 12 ++++++- test/specs/storage_manager/main.js | 4 +-- 6 files changed, 86 insertions(+), 40 deletions(-) create mode 100644 src/grapesjs/config/config.js create mode 100644 src/grapesjs/main.js diff --git a/src/editor/config/config.js b/src/editor/config/config.js index d5094f37f..67eca657f 100644 --- a/src/editor/config/config.js +++ b/src/editor/config/config.js @@ -2,31 +2,31 @@ define(function () { var config = { // Style prefix - stylePrefix: 'wte-', + stylePrefix: 'wte-', - // Prefix to use inside local storage name - storagePrefix: 'wte-', + // Prefix to use inside local storage name (!) + storagePrefix: 'wte-', - // Editor ID. Useful in case of multiple editors on the same page - id : '', + // Editor ID. Useful in case of multiple editors on the same page (!) + id: '', // Where render the editor - container : '', + container: '', - idCanvas : 'canvas', + idCanvas : 'canvas', //(!) - idCanvasOverlay : 'canvas-overlay', + idCanvasOverlay : 'canvas-overlay', //(!) - idWrapper : 'wrapper', + idWrapper : 'wrapper', //(!) // Enable/Disable possibility to copy(ctrl + c) & paste(ctrl + v) components - copyPaste : true, + copyPaste: true, // Enable/Disable undo manager - undoManager : true, + undoManager: true, - //Indicates which storage to use. Available: local | remote | none - storageType : 'local', + //Indicates which storage to use. Available: local | remote | none (!) + storageType: 'local', //Configurations for Asset Manager assetManager : {}, diff --git a/src/editor/model/Editor.js b/src/editor/model/Editor.js index 5adb70551..feca8caac 100644 --- a/src/editor/model/Editor.js +++ b/src/editor/model/Editor.js @@ -35,15 +35,14 @@ define([ ){ return Backbone.Model.extend({ - defaults:{ - clipboard : null, - selectedComponent : null, - previousModel : null, - changesCount : 0, + defaults: { + clipboard: null, + selectedComponent: null, + previousModel: null, + changesCount: 0, }, - initialize: function(c) - { + initialize: function(c) { this.config = c; this.pfx = this.config.storagePrefix; this.compName = this.pfx + 'components' + this.config.id; @@ -159,9 +158,9 @@ define([ /** * Initialize components + * @private * */ - initComponents: function() - { + initComponents: function() { var cfg = this.config.components, comp = this.loadComponents(), cmpStylePfx = cfg.stylePrefix || 'comp-'; @@ -405,7 +404,7 @@ define([ if(r) result = JSON.parse(r); }catch(err){ - console.warn("Error encountered while parsing JSON response"); + //console.warn("Error encountered while parsing JSON response"); } return result; }, @@ -435,7 +434,7 @@ define([ if(r) result = JSON.parse(r); }catch(err){ - console.warn("Load '" + this.rulesName + "':Error encountered while parsing JSON response"); + //console.warn("Load '" + this.rulesName + "':Error encountered while parsing JSON response"); } return result; }, @@ -594,19 +593,19 @@ define([ if(!sm) return; - var smConfig = sm.getConfig(); + var smc = sm.getConfig(); var store = {}; - if(smConfig.storeHtml) + if(smc.storeHtml) store.html = this.getHtml(); - if(smConfig.storeComponents) + if(smc.storeComponents) store.components = JSON.stringify(this.getComponents()); - if(smConfig.storeCss) + if(smc.storeCss) store.css = this.getCss(); - if(smConfig.storeStyles) + if(smc.storeStyles) store.styles = JSON.stringify(this.getStyle()); sm.store(store); @@ -622,31 +621,34 @@ define([ if(!sm) return; - var smConfig = sm.getConfig(); + var smc = sm.getConfig(); - if(smConfig.storeHtml) + if(smc.storeHtml) load.push('html'); - if(smConfig.storeComponents) + if(smc.storeComponents) load.push('components'); - if(smConfig.storeCss) + if(smc.storeCss) load.push('css'); - if(smConfig.storeStyles) + if(smc.storeStyles) load.push('styles'); var result = sm.load(load); + var comps = []; if(result.components){ try{ - var comps = JSON.parse(result.components); - this.setComponents(comps); + comps = JSON.parse(result.components); }catch(err){} }else if(result.html){ - this.setComponents(result.html); + comps = result.html; } + console.log(result); + //this.setComponents(comps); + }, }); diff --git a/src/grapesjs/config/config.js b/src/grapesjs/config/config.js new file mode 100644 index 000000000..d5b60b957 --- /dev/null +++ b/src/grapesjs/config/config.js @@ -0,0 +1,34 @@ +define(function () { + return { + // Where init the editor + container: '', + + // HTML string or object of components + components: '', + + // CSS string or object of rules + style: '', + + // If true, will fetch HTML and CSS from selected element + fromElement: false, + + // Enable/Disable the possibility to copy(ctrl + c) & paste(ctrl + v) components + copyPaste: true, + + // Enable/Disable undo manager + undoManager: true, + + storageId: '', // (!) + + //Indicates which storage to use. Available: local | remote | none + storageType: 'local', // (!) + + storage:{ + id: '', + type: '', + }, + + // Array of plugins to init + plugins: [], + }; +}); \ No newline at end of file diff --git a/src/grapesjs/main.js b/src/grapesjs/main.js new file mode 100644 index 000000000..e69de29bb diff --git a/src/storage_manager/main.js b/src/storage_manager/main.js index bcc45ec74..9d638abc3 100644 --- a/src/storage_manager/main.js +++ b/src/storage_manager/main.js @@ -186,6 +186,7 @@ define(function(require) { load: function(keys){ var st = this.get(this.getCurrent()); var keysF = []; + var result = {}; if(typeof keys === 'string') keys = [keys]; @@ -193,7 +194,16 @@ define(function(require) { for (var i = 0, len = keys.length; i < len; i++) keysF.push(c.id + keys[i]); - return st ? st.load(keysF) : null; + var loaded = st ? st.load(keysF) : {}; + + // Restore keys name + for (var itemKey in loaded){ + var reg = new RegExp('^' + c.id + ''); + var itemKeyR = itemKey.replace(reg, ''); + result[itemKeyR] = loaded[itemKey]; + } + + return result; }, /** diff --git a/test/specs/storage_manager/main.js b/test/specs/storage_manager/main.js index 99578612e..1825fe552 100644 --- a/test/specs/storage_manager/main.js +++ b/test/specs/storage_manager/main.js @@ -68,7 +68,7 @@ define([ }); it('Load do not execute if empty', function() { - (obj.load(['item']) === null).should.equal(true); + obj.load(['item']).should.be.empty; }); it('Load default storages ', function() { @@ -116,7 +116,7 @@ define([ obj.store(data); var load = obj.load(['item', 'item2']); storeValue.should.deep.equal(data2); - load.should.deep.equal(data2); + load.should.deep.equal(data); }); });