Browse Source

Add processor option to DomComponents module

refactor-traits
Artur Arseniev 7 years ago
parent
commit
dbb7916059
  1. 21
      src/dom_components/config/config.js
  2. 14
      src/dom_components/model/Components.js

21
src/dom_components/config/config.js

@ -36,6 +36,27 @@ export default {
// as we need to store inlined style.
storeWrapper: 0,
/**
* You can setup a custom component definiton processor before adding it into the editor.
* It might be useful to transform custom objects (es. some framework specific JSX) to GrapesJS component one.
* This custom function will be executed on ANY new added component to the editor so make smart checks/conditions
* to avoid doing useless executions
* By default, GrapesJS supports already React Element
* @example
* processor: (obj) => {
* if (obj.$$typeof) { // eg. this is a React Element
* const gjsComponent = {
* type: obj.type,
* components: obj.props.children,
* ...
* };
* ...
* return gjsComponent;
* }
* }
*/
processor: 0,
// List of void elements
voidElements: [
'area',

14
src/dom_components/model/Components.js

@ -1,5 +1,5 @@
import Backbone from 'backbone';
import { isEmpty, isArray, isString, each, includes } from 'underscore';
import { isEmpty, isArray, isString, each, includes, extend } from 'underscore';
let Component;
@ -82,9 +82,17 @@ export default Backbone.Collection.extend({
* Process component definition.
*/
processDef(model) {
const { em, config } = this;
const { processor } = config;
const modelPr = processor && processor(model);
if (modelPr) {
each(model, (val, key) => delete model[key]);
extend(model, modelPr);
}
// React JSX
if (model.$$typeof && typeof model.props == 'object') {
const { em } = this;
const domc = em.get('DomComponents');
const parser = em.get('Parser');
const { parserHtml } = parser;
@ -109,7 +117,7 @@ export default Backbone.Collection.extend({
delete model.type;
}
each(res.props, (val, key) => (model[key] = val));
extend(model, res.props);
}
return model;

Loading…
Cancel
Save