|
|
|
@ -25,7 +25,7 @@ export interface ResetCommonUpdateProps { |
|
|
|
} |
|
|
|
|
|
|
|
export interface ResetFromStringOptions { |
|
|
|
visitedCmps?: Record<string, Component[]>; |
|
|
|
visitedCmps?: Record<string, ComponentDefinitionDefined[]>; |
|
|
|
keepIds?: string[]; |
|
|
|
updateOptions?: { |
|
|
|
onAttributes?: (props: ResetCommonUpdateProps & { attributes: Record<string, any> }) => void; |
|
|
|
@ -161,32 +161,11 @@ Component> { |
|
|
|
resetFromString(input = '', opts: ResetFromStringOptions = {}) { |
|
|
|
opts.keepIds = getComponentIds(this); |
|
|
|
const { domc, em, parent } = this; |
|
|
|
const cssc = em?.Css; |
|
|
|
const allByID = domc?.allById() || {}; |
|
|
|
const parsed = this.parseString(input, opts); |
|
|
|
const parsed = this.parseString(input, { ...opts, cloneRules: true }); |
|
|
|
const fromDefOpts = { skipViewUpdate: true, ...opts }; |
|
|
|
const newCmps = getComponentsFromDefs(parsed, allByID, fromDefOpts); |
|
|
|
const { visitedCmps = {} } = fromDefOpts; |
|
|
|
|
|
|
|
// Clone styles for duplicated components
|
|
|
|
Object.keys(visitedCmps).forEach((id) => { |
|
|
|
const cmps = visitedCmps[id]; |
|
|
|
if (cmps.length) { |
|
|
|
// Get all available rules of the component
|
|
|
|
const rulesToClone = cssc?.getRules(`#${id}`) || []; |
|
|
|
|
|
|
|
if (rulesToClone.length) { |
|
|
|
cmps.forEach((cmp) => { |
|
|
|
rulesToClone.forEach((rule) => { |
|
|
|
const newRule = rule.clone(); |
|
|
|
// @ts-ignore
|
|
|
|
newRule.set('selectors', [`#${cmp.attributes.id}`]); |
|
|
|
cssc!.getAll().add(newRule); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
Components.cloneCssRules(em, fromDefOpts.visitedCmps); |
|
|
|
|
|
|
|
this.reset(newCmps, opts as any); |
|
|
|
em?.trigger('component:content', parent, opts, input); |
|
|
|
@ -318,7 +297,8 @@ Component> { |
|
|
|
} |
|
|
|
|
|
|
|
// We need this to avoid duplicate IDs
|
|
|
|
Component.checkId(components, parsed.css, domc!.componentsById, opt); |
|
|
|
const result = Component.checkId(components, parsed.css, domc!.componentsById, opt); |
|
|
|
opt.cloneRules && Components.cloneCssRules(em, result.updatedIds); |
|
|
|
|
|
|
|
if (parsed.css && cssc && !opt.temporary) { |
|
|
|
const { at, ...optsToPass } = opt; |
|
|
|
@ -447,4 +427,26 @@ Component> { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static cloneCssRules(em: EditorModel, cmpsMap: Record<string, ComponentDefinitionDefined[]> = {}) { |
|
|
|
const { Css } = em; |
|
|
|
Object.keys(cmpsMap).forEach((id) => { |
|
|
|
const cmps = cmpsMap[id]; |
|
|
|
if (cmps.length) { |
|
|
|
// Get all available rules of the component
|
|
|
|
const rulesToClone = (Css.getRules(`#${id}`) || []).filter((rule) => !isEmpty(rule.attributes.style)); |
|
|
|
|
|
|
|
if (rulesToClone.length) { |
|
|
|
const rules = Css.getAll(); |
|
|
|
cmps.forEach((cmp) => { |
|
|
|
rulesToClone.forEach((rule) => { |
|
|
|
const newRule = rule.clone(); |
|
|
|
newRule.set('selectors', [`#${cmp.attributes.id}`] as any); |
|
|
|
rules.add(newRule); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|