Browse Source

fix component.set method

pull/6351/head
mohamedsalem401 2 years ago
parent
commit
97a9776d6e
  1. 51
      packages/core/src/dom_components/model/Component.ts

51
packages/core/src/dom_components/model/Component.ts

@ -54,12 +54,12 @@ import {
import { ComponentDynamicValueListener } from './ComponentDynamicValueListener'; import { ComponentDynamicValueListener } from './ComponentDynamicValueListener';
import { DynamicValueWatcher } from './DynamicValueWatcher'; import { DynamicValueWatcher } from './DynamicValueWatcher';
export interface IComponent extends ExtractMethods<Component> {} export interface IComponent extends ExtractMethods<Component> { }
export interface DynamicWatchersOptions { export interface DynamicWatchersOptions {
skipWatcherUpdates?: boolean; skipWatcherUpdates?: boolean;
} }
export interface SetAttrOptions extends SetOptions, UpdateStyleOptions, DynamicWatchersOptions {} export interface SetAttrOptions extends SetOptions, UpdateStyleOptions, DynamicWatchersOptions { }
export interface ComponentSetOptions extends SetOptions, DynamicWatchersOptions {} export interface ComponentSetOptions extends SetOptions, DynamicWatchersOptions { }
const escapeRegExp = (str: string) => { const escapeRegExp = (str: string) => {
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'); return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
@ -226,12 +226,12 @@ export default class Component extends StyleableModel<ComponentProperties> {
return this.frame?.getPage(); return this.frame?.getPage();
} }
preInit() {} preInit() { }
/** /**
* Hook method, called once the model is created * 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) * 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<ComponentProperties> {
* @param {*} value Property value, if triggered after some property update * @param {*} value Property value, if triggered after some property update
* @param {*} previous Property previous 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 * Hook method, called once the model has been removed
*/ */
removed() {} removed() { }
em!: EditorModel; em!: EditorModel;
opt!: ComponentOptions; opt!: ComponentOptions;
@ -339,26 +339,29 @@ export default class Component extends StyleableModel<ComponentProperties> {
set<A extends string>( set<A extends string>(
keyOrAttributes: A | Partial<ComponentProperties>, keyOrAttributes: A | Partial<ComponentProperties>,
valueOrOptions?: ComponentProperties[A] | SetOptions, valueOrOptions?: ComponentProperties[A] | ComponentSetOptions,
options?: ComponentSetOptions, optionsOrUndefined?: ComponentSetOptions
): this;
set<A extends string>(
keyOrAttributes: A | Partial<ComponentProperties>,
valueOrOptions?: ComponentProperties[A] | SetOptions,
options: ComponentSetOptions = { skipWatcherUpdates: false },
): this { ): this {
const attributes = let attributes: Partial<ComponentProperties>;
typeof keyOrAttributes === 'object' let options: ComponentSetOptions = { skipWatcherUpdates: false };
? keyOrAttributes if (typeof keyOrAttributes === 'object') {
: typeof keyOrAttributes === 'string' attributes = keyOrAttributes;
? { [keyOrAttributes as string]: valueOrOptions } 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 areStaticAttributes = DynamicValueWatcher.areStaticValues(attributes);
const evaluatedAttributes = areStaticAttributes let evaluatedAttributes: Partial<ComponentProperties>;
? attributes if (areStaticAttributes) {
: ComponentDynamicValueListener.evaluateComponentDef(attributes, this.em); evaluatedAttributes = attributes;
} else {
evaluatedAttributes = ComponentDynamicValueListener.evaluateComponentDef(attributes, this.em);
}
if (!options.skipWatcherUpdates) { if (!options.skipWatcherUpdates) {
this.componentDVListener?.watchProps(attributes); this.componentDVListener?.watchProps(attributes);

Loading…
Cancel
Save