From 26d14708bcf3eb4575a69e392eaef1b37c0e84c9 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 12 Sep 2025 23:55:57 +0400 Subject: [PATCH] Improve reset from string (#6607) * Update resetFromString with new options * Fix updateClasses in ComponentView --- .../src/dom_components/model/Components.ts | 39 ++++++++++++++++--- .../src/dom_components/view/ComponentView.ts | 2 +- packages/core/src/selector_manager/index.ts | 2 +- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/core/src/dom_components/model/Components.ts b/packages/core/src/dom_components/model/Components.ts index 23f645f8a..8e6253c02 100644 --- a/packages/core/src/dom_components/model/Components.ts +++ b/packages/core/src/dom_components/model/Components.ts @@ -1,5 +1,5 @@ import { isEmpty, isArray, isString, isFunction, each, includes, extend, flatten, keys } from 'underscore'; -import Component from './Component'; +import Component, { SetAttrOptions } from './Component'; import { AddOptions, Collection } from '../../common'; import { DomComponentsConfig } from '../config/config'; import EditorModel from '../../editor/model/Editor'; @@ -18,6 +18,21 @@ import ComponentWrapper from './ComponentWrapper'; import { ComponentsEvents, ParseStringOptions } from '../types'; import { isSymbolInstance, isSymbolRoot, updateSymbolComps } from './SymbolUtils'; +export interface ResetCommonUpdateProps { + component: Component; + item: ComponentDefinitionDefined; + options: SetAttrOptions; +} + +export interface ResetFromStringOptions { + visitedCmps?: Record; + keepIds?: string[]; + updateOptions?: { + onAttributes?: (props: ResetCommonUpdateProps & { attributes: Record }) => void; + onStyle?: (props: ResetCommonUpdateProps & { style: Record }) => void; + }; +} + export const getComponentIds = (cmp?: Component | Component[] | Components, res: string[] = []) => { if (!cmp) return []; const cmps = (isArray(cmp) || isFunction((cmp as Components).map) ? cmp : [cmp]) as Component[]; @@ -35,6 +50,7 @@ const getComponentsFromDefs = ( ) => { opts.visitedCmps = opts.visitedCmps || {}; const { visitedCmps } = opts; + const updateOptions = (opts.updateOptions as ResetFromStringOptions['updateOptions']) || {}; const itms = isArray(items) ? items : [items]; return itms.map((item) => { @@ -50,10 +66,21 @@ const getComponentsFromDefs = ( // Update the component if exists already if (all[id]) { result = all[id] as any; - const cmp = result as unknown as Component; - tagName && cmp.set({ tagName }, { ...opts, silent: true }); - keys(restAttr).length && cmp.addAttributes(restAttr, { ...opts }); - keys(style).length && cmp.addStyle(style, opts); + const { onAttributes, onStyle } = updateOptions; + const component = result as unknown as Component; + tagName && component.set({ tagName }, { ...opts, silent: true }); + + if (onAttributes) { + onAttributes({ item, component, attributes: restAttr, options: opts }); + } else if (keys(restAttr).length) { + component.addAttributes(restAttr, { ...opts }); + } + + if (onStyle) { + onStyle({ item, component, style, options: opts }); + } else if (keys(style).length) { + component.addStyle(style, opts); + } } } else { // Found another component with the same ID, treat it as a new component @@ -131,7 +158,7 @@ Component> { models.each((model) => this.onAdd(model)); } - resetFromString(input = '', opts: { visitedCmps?: Record; keepIds?: string[] } = {}) { + resetFromString(input = '', opts: ResetFromStringOptions = {}) { opts.keepIds = getComponentIds(this); const { domc, em, parent } = this; const cssc = em?.Css; diff --git a/packages/core/src/dom_components/view/ComponentView.ts b/packages/core/src/dom_components/view/ComponentView.ts index 6658467f1..2e6d2d1a1 100644 --- a/packages/core/src/dom_components/view/ComponentView.ts +++ b/packages/core/src/dom_components/view/ComponentView.ts @@ -315,7 +315,7 @@ TComp> { * @private * */ updateClasses() { - const str = this.model.classes.pluck('name').join(' '); + const str = this.model.classes.pluck?.('name').join(' ') || ''; this.setAttribute('class', str); // Regenerate status class diff --git a/packages/core/src/selector_manager/index.ts b/packages/core/src/selector_manager/index.ts index e8905df40..129cdfec8 100644 --- a/packages/core/src/selector_manager/index.ts +++ b/packages/core/src/selector_manager/index.ts @@ -292,7 +292,7 @@ export default class SelectorManager extends ItemManagerModule added.push(this.addSelector(name) as Selector));