|
|
@ -1,11 +1,12 @@ |
|
|
import { isEmpty, isArray, isString, isFunction, each, includes, extend, flatten, keys } from 'underscore'; |
|
|
import { isEmpty, isArray, isString, isFunction, each, includes, extend, flatten, keys } from 'underscore'; |
|
|
import Component from './Component'; |
|
|
import Component from './Component'; |
|
|
import { AddOptions, Collection, ObjectAny, OptionAsDocument } from '../../common'; |
|
|
import { AddOptions, Collection, OptionAsDocument } from '../../common'; |
|
|
import { DomComponentsConfig } from '../config/config'; |
|
|
import { DomComponentsConfig } from '../config/config'; |
|
|
import EditorModel from '../../editor/model/Editor'; |
|
|
import EditorModel from '../../editor/model/Editor'; |
|
|
import ComponentManager from '..'; |
|
|
import ComponentManager from '..'; |
|
|
import CssRule from '../../css_composer/model/CssRule'; |
|
|
import CssRule from '../../css_composer/model/CssRule'; |
|
|
import { ComponentAdd, ComponentDefinitionDefined, ComponentProperties } from './types'; |
|
|
|
|
|
|
|
|
import { ComponentAdd, ComponentAddType, ComponentDefinition, ComponentDefinitionDefined, ComponentProperties } from './types'; |
|
|
import ComponentText from './ComponentText'; |
|
|
import ComponentText from './ComponentText'; |
|
|
import ComponentWrapper from './ComponentWrapper'; |
|
|
import ComponentWrapper from './ComponentWrapper'; |
|
|
import { ComponentsEvents } from '../types'; |
|
|
import { ComponentsEvents } from '../types'; |
|
|
@ -71,7 +72,7 @@ const getComponentsFromDefs = ( |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
export interface ComponentsOptions { |
|
|
export interface ComponentsOptions { |
|
|
em?: EditorModel; |
|
|
em: EditorModel; |
|
|
config?: DomComponentsConfig; |
|
|
config?: DomComponentsConfig; |
|
|
domc?: ComponentManager; |
|
|
domc?: ComponentManager; |
|
|
} |
|
|
} |
|
|
@ -82,19 +83,19 @@ export default class Components extends Collection</** |
|
|
Component> { |
|
|
Component> { |
|
|
opt!: ComponentsOptions; |
|
|
opt!: ComponentsOptions; |
|
|
config?: DomComponentsConfig; |
|
|
config?: DomComponentsConfig; |
|
|
em!: EditorModel; |
|
|
em: EditorModel; |
|
|
domc?: ComponentManager; |
|
|
domc?: ComponentManager; |
|
|
parent?: Component; |
|
|
parent?: Component; |
|
|
__firstAdd?: any; |
|
|
|
|
|
|
|
|
|
|
|
initialize(models: any, opt: ComponentsOptions = {}) { |
|
|
constructor(models: any, opt: ComponentsOptions) { |
|
|
|
|
|
super(models, opt); |
|
|
this.opt = opt; |
|
|
this.opt = opt; |
|
|
this.listenTo(this, 'add', this.onAdd); |
|
|
this.listenTo(this, 'add', this.onAdd); |
|
|
this.listenTo(this, 'remove', this.removeChildren); |
|
|
this.listenTo(this, 'remove', this.removeChildren); |
|
|
this.listenTo(this, 'reset', this.resetChildren); |
|
|
this.listenTo(this, 'reset', this.resetChildren); |
|
|
const { em, config } = opt; |
|
|
const { em, config } = opt; |
|
|
this.config = config; |
|
|
this.config = config; |
|
|
this.em = em!; |
|
|
this.em = em; |
|
|
this.domc = opt.domc || em?.Components; |
|
|
this.domc = opt.domc || em?.Components; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -263,14 +264,17 @@ Component> { |
|
|
return components; |
|
|
return components; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** @ts-ignore */ |
|
|
add(model: Exclude<ComponentAddType,string>, opt?: AddOptions & { previousModels?: Component[]; keepIds?: string[] }): Component; |
|
|
add(models: ComponentAdd, opt: AddOptions & { previousModels?: Component[]; keepIds?: string[] } = {}) { |
|
|
add(models: ComponentAddType[], opt?: AddOptions & { previousModels?: Component[]; keepIds?: string[] }): Component[]; |
|
|
|
|
|
add(models: ComponentAdd, opt?: AddOptions & { previousModels?: Component[]; keepIds?: string[] }): Component|Component[]; |
|
|
|
|
|
add(models: unknown, opt: AddOptions & { previousModels?: Component[]; keepIds?: string[] } = {}): unknown { |
|
|
|
|
|
if (models == undefined) return; |
|
|
|
|
|
|
|
|
opt.keepIds = [...(opt.keepIds || []), ...getComponentIds(opt.previousModels)]; |
|
|
opt.keepIds = [...(opt.keepIds || []), ...getComponentIds(opt.previousModels)]; |
|
|
|
|
|
|
|
|
if (isString(models)) { |
|
|
if (isString(models)) { |
|
|
models = this.parseString(models, opt)!; |
|
|
models = this.parseString(models, opt)!; |
|
|
} else if (isArray(models)) { |
|
|
} else if (isArray(models)) { |
|
|
models = [...models]; |
|
|
|
|
|
models.forEach((item: string, index: number) => { |
|
|
models.forEach((item: string, index: number) => { |
|
|
if (isString(item)) { |
|
|
if (isString(item)) { |
|
|
const nodes = this.parseString(item, opt); |
|
|
const nodes = this.parseString(item, opt); |
|
|
@ -279,21 +283,19 @@ Component> { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const isMult = isArray(models); |
|
|
const processedModels = (isArray(models) ? models : [models]) |
|
|
// @ts-ignore
|
|
|
.filter(Boolean) |
|
|
models = (isMult ? models : [models]).filter(Boolean).map((model: any) => this.processDef(model)); |
|
|
.map((model: any) => this.processDef(model)); |
|
|
// @ts-ignore
|
|
|
|
|
|
models = isMult ? flatten(models as any, 1) : models[0]; |
|
|
|
|
|
|
|
|
|
|
|
const result = Collection.prototype.add.apply(this, [models as any, opt]); |
|
|
models = isArray(models) ? flatten(processedModels as any, 1) : processedModels[0]; |
|
|
this.__firstAdd = result; |
|
|
|
|
|
return result; |
|
|
return super.add(models as any, opt); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Process component definition. |
|
|
* Process component definition. |
|
|
*/ |
|
|
*/ |
|
|
processDef(mdl: any) { |
|
|
processDef(mdl: Component | ComponentDefinition | ComponentDefinitionDefined) { |
|
|
// Avoid processing Models
|
|
|
// Avoid processing Models
|
|
|
if (mdl.cid && mdl.ccid) return mdl; |
|
|
if (mdl.cid && mdl.ccid) return mdl; |
|
|
const { em, config = {} } = this; |
|
|
const { em, config = {} } = this; |
|
|
@ -304,12 +306,14 @@ Component> { |
|
|
model = { ...model }; // Avoid 'Cannot delete property ...'
|
|
|
model = { ...model }; // Avoid 'Cannot delete property ...'
|
|
|
const modelPr = processor(model); |
|
|
const modelPr = processor(model); |
|
|
if (modelPr) { |
|
|
if (modelPr) { |
|
|
|
|
|
//@ts-ignore
|
|
|
each(model, (val, key) => delete model[key]); |
|
|
each(model, (val, key) => delete model[key]); |
|
|
extend(model, modelPr); |
|
|
extend(model, modelPr); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// React JSX preset
|
|
|
// React JSX preset
|
|
|
|
|
|
//@ts-ignore
|
|
|
if (model.$$typeof && typeof model.props == 'object') { |
|
|
if (model.$$typeof && typeof model.props == 'object') { |
|
|
model = { ...model }; |
|
|
model = { ...model }; |
|
|
model.props = { ...model.props }; |
|
|
model.props = { ...model.props }; |
|
|
@ -318,6 +322,7 @@ Component> { |
|
|
const { parserHtml } = parser; |
|
|
const { parserHtml } = parser; |
|
|
|
|
|
|
|
|
each(model, (value, key) => { |
|
|
each(model, (value, key) => { |
|
|
|
|
|
//@ts-ignore
|
|
|
if (!includes(['props', 'type'], key)) delete model[key]; |
|
|
if (!includes(['props', 'type'], key)) delete model[key]; |
|
|
}); |
|
|
}); |
|
|
const { props } = model; |
|
|
const { props } = model; |
|
|
@ -349,8 +354,7 @@ Component> { |
|
|
const avoidInline = em && em.getConfig().avoidInlineStyle; |
|
|
const avoidInline = em && em.getConfig().avoidInlineStyle; |
|
|
domc && domc.Component.ensureInList(model); |
|
|
domc && domc.Component.ensureInList(model); |
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
if (!isEmpty(style) && !avoidInline && em && em.getConfig().forceClass && !opts.temporary) { |
|
|
if (!isEmpty(style) && !avoidInline && em && em.get && em.getConfig().forceClass && !opts.temporary) { |
|
|
|
|
|
const name = model.cid; |
|
|
const name = model.cid; |
|
|
em.Css.setClassRule(name, style); |
|
|
em.Css.setClassRule(name, style); |
|
|
model.setStyle({}); |
|
|
model.setStyle({}); |
|
|
@ -366,34 +370,5 @@ Component> { |
|
|
}; |
|
|
}; |
|
|
triggerAdd(model); |
|
|
triggerAdd(model); |
|
|
} |
|
|
} |
|
|
// this.__onAddEnd();
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// __onAddEnd = debounce(function () {
|
|
|
|
|
|
// // TODO to check symbols on load, probably this might be removed as symbols
|
|
|
|
|
|
// // are always recovered from the model
|
|
|
|
|
|
// // const { domc } = this;
|
|
|
|
|
|
// // const allComp = (domc && domc.allById()) || {};
|
|
|
|
|
|
// // const firstAdd = this.__firstAdd;
|
|
|
|
|
|
// // const toCheck = isArray(firstAdd) ? firstAdd : [firstAdd];
|
|
|
|
|
|
// // const silent = { silent: true };
|
|
|
|
|
|
// // const onAll = comps => {
|
|
|
|
|
|
// // comps.forEach(comp => {
|
|
|
|
|
|
// // const symbol = comp.get(keySymbols);
|
|
|
|
|
|
// // const symbolOf = comp.get(keySymbol);
|
|
|
|
|
|
// // if (symbol && isArray(symbol) && isString(symbol[0])) {
|
|
|
|
|
|
// // comp.set(
|
|
|
|
|
|
// // keySymbols,
|
|
|
|
|
|
// // symbol.map(smb => allComp[smb]).filter(i => i),
|
|
|
|
|
|
// // silent
|
|
|
|
|
|
// // );
|
|
|
|
|
|
// // }
|
|
|
|
|
|
// // if (isString(symbolOf)) {
|
|
|
|
|
|
// // comp.set(keySymbol, allComp[symbolOf], silent);
|
|
|
|
|
|
// // }
|
|
|
|
|
|
// // onAll(comp.components());
|
|
|
|
|
|
// // });
|
|
|
|
|
|
// // };
|
|
|
|
|
|
// // onAll(toCheck);
|
|
|
|
|
|
// });
|
|
|
|
|
|
} |
|
|
} |
|
|
|