Browse Source

Update StorageManager

pull/36/head
Artur Arseniev 10 years ago
parent
commit
ef80c10b3f
  1. 26
      src/editor/config/config.js
  2. 50
      src/editor/model/Editor.js
  3. 34
      src/grapesjs/config/config.js
  4. 0
      src/grapesjs/main.js
  5. 12
      src/storage_manager/main.js
  6. 4
      test/specs/storage_manager/main.js

26
src/editor/config/config.js

@ -2,31 +2,31 @@ define(function () {
var config = { var config = {
// Style prefix // Style prefix
stylePrefix: 'wte-', stylePrefix: 'wte-',
// Prefix to use inside local storage name // Prefix to use inside local storage name (!)
storagePrefix: 'wte-', storagePrefix: 'wte-',
// Editor ID. Useful in case of multiple editors on the same page // Editor ID. Useful in case of multiple editors on the same page (!)
id : '', id: '',
// Where render the editor // 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 // Enable/Disable possibility to copy(ctrl + c) & paste(ctrl + v) components
copyPaste : true, copyPaste: true,
// Enable/Disable undo manager // Enable/Disable undo manager
undoManager : true, undoManager: true,
//Indicates which storage to use. Available: local | remote | none //Indicates which storage to use. Available: local | remote | none (!)
storageType : 'local', storageType: 'local',
//Configurations for Asset Manager //Configurations for Asset Manager
assetManager : {}, assetManager : {},

50
src/editor/model/Editor.js

@ -35,15 +35,14 @@ define([
){ ){
return Backbone.Model.extend({ return Backbone.Model.extend({
defaults:{ defaults: {
clipboard : null, clipboard: null,
selectedComponent : null, selectedComponent: null,
previousModel : null, previousModel: null,
changesCount : 0, changesCount: 0,
}, },
initialize: function(c) initialize: function(c) {
{
this.config = c; this.config = c;
this.pfx = this.config.storagePrefix; this.pfx = this.config.storagePrefix;
this.compName = this.pfx + 'components' + this.config.id; this.compName = this.pfx + 'components' + this.config.id;
@ -159,9 +158,9 @@ define([
/** /**
* Initialize components * Initialize components
* @private
* */ * */
initComponents: function() initComponents: function() {
{
var cfg = this.config.components, var cfg = this.config.components,
comp = this.loadComponents(), comp = this.loadComponents(),
cmpStylePfx = cfg.stylePrefix || 'comp-'; cmpStylePfx = cfg.stylePrefix || 'comp-';
@ -405,7 +404,7 @@ define([
if(r) if(r)
result = JSON.parse(r); result = JSON.parse(r);
}catch(err){ }catch(err){
console.warn("Error encountered while parsing JSON response"); //console.warn("Error encountered while parsing JSON response");
} }
return result; return result;
}, },
@ -435,7 +434,7 @@ define([
if(r) if(r)
result = JSON.parse(r); result = JSON.parse(r);
}catch(err){ }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; return result;
}, },
@ -594,19 +593,19 @@ define([
if(!sm) if(!sm)
return; return;
var smConfig = sm.getConfig(); var smc = sm.getConfig();
var store = {}; var store = {};
if(smConfig.storeHtml) if(smc.storeHtml)
store.html = this.getHtml(); store.html = this.getHtml();
if(smConfig.storeComponents) if(smc.storeComponents)
store.components = JSON.stringify(this.getComponents()); store.components = JSON.stringify(this.getComponents());
if(smConfig.storeCss) if(smc.storeCss)
store.css = this.getCss(); store.css = this.getCss();
if(smConfig.storeStyles) if(smc.storeStyles)
store.styles = JSON.stringify(this.getStyle()); store.styles = JSON.stringify(this.getStyle());
sm.store(store); sm.store(store);
@ -622,31 +621,34 @@ define([
if(!sm) if(!sm)
return; return;
var smConfig = sm.getConfig(); var smc = sm.getConfig();
if(smConfig.storeHtml) if(smc.storeHtml)
load.push('html'); load.push('html');
if(smConfig.storeComponents) if(smc.storeComponents)
load.push('components'); load.push('components');
if(smConfig.storeCss) if(smc.storeCss)
load.push('css'); load.push('css');
if(smConfig.storeStyles) if(smc.storeStyles)
load.push('styles'); load.push('styles');
var result = sm.load(load); var result = sm.load(load);
var comps = [];
if(result.components){ if(result.components){
try{ try{
var comps = JSON.parse(result.components); comps = JSON.parse(result.components);
this.setComponents(comps);
}catch(err){} }catch(err){}
}else if(result.html){ }else if(result.html){
this.setComponents(result.html); comps = result.html;
} }
console.log(result);
//this.setComponents(comps);
}, },
}); });

34
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: [],
};
});

0
src/grapesjs/main.js

12
src/storage_manager/main.js

@ -186,6 +186,7 @@ define(function(require) {
load: function(keys){ load: function(keys){
var st = this.get(this.getCurrent()); var st = this.get(this.getCurrent());
var keysF = []; var keysF = [];
var result = {};
if(typeof keys === 'string') if(typeof keys === 'string')
keys = [keys]; keys = [keys];
@ -193,7 +194,16 @@ define(function(require) {
for (var i = 0, len = keys.length; i < len; i++) for (var i = 0, len = keys.length; i < len; i++)
keysF.push(c.id + keys[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;
}, },
/** /**

4
test/specs/storage_manager/main.js

@ -68,7 +68,7 @@ define([
}); });
it('Load do not execute if empty', function() { 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() { it('Load default storages ', function() {
@ -116,7 +116,7 @@ define([
obj.store(data); obj.store(data);
var load = obj.load(['item', 'item2']); var load = obj.load(['item', 'item2']);
storeValue.should.deep.equal(data2); storeValue.should.deep.equal(data2);
load.should.deep.equal(data2); load.should.deep.equal(data);
}); });
}); });

Loading…
Cancel
Save