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 = {
// 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 : {},

50
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);
},
});

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){
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;
},
/**

4
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);
});
});

Loading…
Cancel
Save