Browse Source

Add `editor.loadData`

pull/3671/head
Artur Arseniev 5 years ago
parent
commit
05bb5607a0
  1. 11
      src/editor/index.js
  2. 17
      src/editor/model/Editor.js
  3. 32
      src/storage_manager/index.js

11
src/editor/index.js

@ -495,6 +495,17 @@ export default (config = {}) => {
return em.load(clb);
},
/**
* Load data from the JSON data object
* @param {Object} data Data to load
* @return {Object} Loaded object
* @example
* editor.loadData({ pages: [...], styles: [...], ... })
*/
loadData(data) {
return em.loadData(data);
},
/**
* Returns container element. The one which was indicated as 'container'
* on init method

17
src/editor/model/Editor.js

@ -623,14 +623,23 @@ export default Backbone.Model.extend({
*/
load(clb = null) {
this.getCacheLoad(1, res => {
this.get('storables').forEach(module => {
module.load(res);
module.postLoad && module.postLoad(this);
});
this.loadData(res);
clb && clb(res);
});
},
loadData(data = {}) {
const sm = this.get('StorageManager');
const result = sm.__clearKeys(data);
this.get('storables').forEach(module => {
module.load(result);
module.postLoad && module.postLoad(this);
});
return result;
},
/**
* Returns cached load
* @param {Boolean} force Force to reload

32
src/storage_manager/index.js

@ -239,9 +239,9 @@ export default () => {
* });
* */
load(keys, clb) {
var st = this.get(this.getCurrent());
var keysF = [];
var result = {};
const st = this.get(this.getCurrent());
const keysF = [];
let result = {};
if (typeof keys === 'string') keys = [keys];
this.onStart('load', keys);
@ -254,13 +254,7 @@ export default () => {
st.load(
keysF,
res => {
// Restore keys name
var reg = new RegExp('^' + c.id + '');
for (var itemKey in res) {
var itemKeyR = itemKey.replace(reg, '');
result[itemKeyR] = res[itemKey];
}
result = this.__clearKeys(res);
this.onAfter('load', result);
clb && clb(result);
this.onEnd('load', result);
@ -275,6 +269,24 @@ export default () => {
}
},
/**
* Restore key names
* @param {Object} data
* @returns {Object}
* @private
*/
__clearKeys(data = {}) {
const result = {};
const reg = new RegExp('^' + c.id + '');
for (let itemKey in data) {
const itemKeyR = itemKey.replace(reg, '');
result[itemKeyR] = data[itemKey];
}
return result;
},
/**
* Load default storages
* @return {this}

Loading…
Cancel
Save