From 97a9776d6ee5dad91a143d04cd59d8db20060d0e Mon Sep 17 00:00:00 2001 From: mohamedsalem401 Date: Thu, 26 Dec 2024 07:45:51 +0200 Subject: [PATCH] fix component.set method --- .../src/dom_components/model/Component.ts | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/packages/core/src/dom_components/model/Component.ts b/packages/core/src/dom_components/model/Component.ts index b2cd45ad5..feceea313 100644 --- a/packages/core/src/dom_components/model/Component.ts +++ b/packages/core/src/dom_components/model/Component.ts @@ -54,12 +54,12 @@ import { import { ComponentDynamicValueListener } from './ComponentDynamicValueListener'; import { DynamicValueWatcher } from './DynamicValueWatcher'; -export interface IComponent extends ExtractMethods {} +export interface IComponent extends ExtractMethods { } export interface DynamicWatchersOptions { skipWatcherUpdates?: boolean; } -export interface SetAttrOptions extends SetOptions, UpdateStyleOptions, DynamicWatchersOptions {} -export interface ComponentSetOptions extends SetOptions, DynamicWatchersOptions {} +export interface SetAttrOptions extends SetOptions, UpdateStyleOptions, DynamicWatchersOptions { } +export interface ComponentSetOptions extends SetOptions, DynamicWatchersOptions { } const escapeRegExp = (str: string) => { return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'); @@ -226,12 +226,12 @@ export default class Component extends StyleableModel { return this.frame?.getPage(); } - preInit() {} + preInit() { } /** * Hook method, called once the model is created */ - init() {} + init() { } /** * Hook method, called when the model has been updated (eg. updated some model's property) @@ -239,12 +239,12 @@ export default class Component extends StyleableModel { * @param {*} value Property value, if triggered after some property update * @param {*} previous Property previous value, if triggered after some property update */ - updated(property: string, value: any, previous: any) {} + updated(property: string, value: any, previous: any) { } /** * Hook method, called once the model has been removed */ - removed() {} + removed() { } em!: EditorModel; opt!: ComponentOptions; @@ -339,26 +339,29 @@ export default class Component extends StyleableModel { set( keyOrAttributes: A | Partial, - valueOrOptions?: ComponentProperties[A] | SetOptions, - options?: ComponentSetOptions, - ): this; - - set( - keyOrAttributes: A | Partial, - valueOrOptions?: ComponentProperties[A] | SetOptions, - options: ComponentSetOptions = { skipWatcherUpdates: false }, + valueOrOptions?: ComponentProperties[A] | ComponentSetOptions, + optionsOrUndefined?: ComponentSetOptions ): this { - const attributes = - typeof keyOrAttributes === 'object' - ? keyOrAttributes - : typeof keyOrAttributes === 'string' - ? { [keyOrAttributes as string]: valueOrOptions } - : {}; + let attributes: Partial; + let options: ComponentSetOptions = { skipWatcherUpdates: false }; + if (typeof keyOrAttributes === 'object') { + attributes = keyOrAttributes; + options = valueOrOptions as ComponentSetOptions; + } else if (typeof keyOrAttributes === 'string') { + attributes = { [keyOrAttributes as string]: valueOrOptions }; + options = optionsOrUndefined || options; + } else { + attributes = {}; + options = optionsOrUndefined || options; + } const areStaticAttributes = DynamicValueWatcher.areStaticValues(attributes); - const evaluatedAttributes = areStaticAttributes - ? attributes - : ComponentDynamicValueListener.evaluateComponentDef(attributes, this.em); + let evaluatedAttributes: Partial; + if (areStaticAttributes) { + evaluatedAttributes = attributes; + } else { + evaluatedAttributes = ComponentDynamicValueListener.evaluateComponentDef(attributes, this.em); + } if (!options.skipWatcherUpdates) { this.componentDVListener?.watchProps(attributes);