Browse Source

Add getTypes method and improve the parserHtml method

improve-component-api
Artur Arseniev 8 years ago
parent
commit
e73f08fd1d
  1. 46
      src/dom_components/index.js
  2. 4
      src/parser/index.js
  3. 11
      src/parser/model/ParserHtml.js
  4. 18
      test/specs/dom_components/index.js

46
src/dom_components/index.js

@ -20,6 +20,9 @@
* * [clear](#clear) * * [clear](#clear)
* * [load](#load) * * [load](#load)
* * [store](#store) * * [store](#store)
* * [addType](#addtype)
* * [getType](#gettype)
* * [getTypes](#gettypes)
* * [render](#render) * * [render](#render)
* *
* @module DomComponents * @module DomComponents
@ -506,9 +509,11 @@ module.exports = () => {
}, },
/** /**
* Add new component type * Add new component type.
* @param {string} type * Read more about this in [Define New Component](https://grapesjs.com/docs/modules/Components.html#define-new-component)
* @param {Object} methods * @param {string} type Component ID
* @param {Object} methods Component methods
* @return {this}
*/ */
addType(type, methods) { addType(type, methods) {
const compType = this.getType(type); const compType = this.getType(type);
@ -519,15 +524,18 @@ module.exports = () => {
// If the model/view is a simple object I need to extend it // If the model/view is a simple object I need to extend it
if (typeof model === 'object') { if (typeof model === 'object') {
methods.model = modelToExt.extend({ methods.model = modelToExt.extend(
...model, {
defaults: { ...model,
...modelToExt.prototype.defaults, defaults: {
...(model.defaults || {}), ...modelToExt.prototype.defaults,
...(model.defaults || {})
}
}, },
}, { {
isComponent: isComponent || (() => 0), isComponent: isComponent || (() => 0)
}); }
);
} }
if (typeof view === 'object') { if (typeof view === 'object') {
@ -541,11 +549,15 @@ module.exports = () => {
methods.id = type; methods.id = type;
componentTypes.unshift(methods); componentTypes.unshift(methods);
} }
return this;
}, },
/** /**
* Get component type * Get component type.
* @param {string} type * Read more about this in [Define New Component](https://grapesjs.com/docs/modules/Components.html#define-new-component)
* @param {string} type Component ID
* @return {Object} Component type defintion, eg. `{ model: ..., view: ... }`
*/ */
getType(type) { getType(type) {
var df = componentTypes; var df = componentTypes;
@ -559,6 +571,14 @@ module.exports = () => {
return; return;
}, },
/**
* Return the array of all types
* @return {Array}
*/
getTypes() {
return componentTypes;
},
selectAdd(component, opts = {}) { selectAdd(component, opts = {}) {
if (component) { if (component) {
component.set({ component.set({

4
src/parser/index.js

@ -47,6 +47,7 @@ module.exports = () => {
conf.Parser = this; conf.Parser = this;
pHtml = new parserHtml(conf); pHtml = new parserHtml(conf);
pCss = new parserCss(conf); pCss = new parserCss(conf);
this.em = conf.em;
return this; return this;
}, },
@ -56,7 +57,8 @@ module.exports = () => {
* @return {Object} * @return {Object}
*/ */
parseHtml(str) { parseHtml(str) {
pHtml.compTypes = this.compTypes; const { em, compTypes } = this;
pHtml.compTypes = em ? em.get('DomComponents').getTypes() : compTypes;
return pHtml.parse(str, pCss); return pHtml.parse(str, pCss);
}, },

11
src/parser/model/ParserHtml.js

@ -81,8 +81,15 @@ module.exports = config => {
// Iterate over all available Component Types and // Iterate over all available Component Types and
// the first with a valid result will be that component // the first with a valid result will be that component
for (let it = 0; it < ct.length; it++) { for (let it = 0; it < ct.length; it++) {
obj = ct[it].model.isComponent(node); const compType = ct[it];
if (obj) break; obj = compType.model.isComponent(node);
if (obj) {
if (typeof obj !== 'object') {
obj = { type: compType.id };
}
break;
}
} }
model = obj; model = obj;

18
test/specs/dom_components/index.js

@ -179,7 +179,7 @@ describe('DOM Components', () => {
obj.addType(id, { obj.addType(id, {
model: { model: {
defaults: { defaults: {
testProp, testProp
} }
} }
}); });
@ -189,6 +189,22 @@ describe('DOM Components', () => {
expect(comp.get('type')).toEqual(id); expect(comp.get('type')).toEqual(id);
expect(comp.get('testProp')).toEqual(testProp); expect(comp.get('testProp')).toEqual(testProp);
}); });
test('Add new component type with custom isComponent', () => {
obj = em.get('DomComponents');
const id = 'test-type';
const testProp = 'testValue';
obj.addType(id, {
isComponent: el => {
return el.getAttribute('test-prop') === testProp;
}
});
expect(obj.componentTypes[0].id).toEqual(id);
obj.addComponent(`<div test-prop="${testProp}"></div>`);
const comp = obj.getComponents().at(0);
expect(comp.get('type')).toEqual(id);
expect(comp.getAttributes()['test-prop']).toEqual(testProp);
});
}); });
ComponentModels.run(); ComponentModels.run();

Loading…
Cancel
Save