Browse Source

Fix unset a dynamic value for a component

pull/6351/head
mohamedsalem401 1 year ago
parent
commit
ba5e50965e
  1. 13
      packages/core/src/common/index.ts
  2. 20
      packages/core/src/dom_components/model/Component.ts
  3. 4
      packages/core/src/dom_components/model/ComponentDynamicValueListener.ts

13
packages/core/src/common/index.ts

@ -2,13 +2,14 @@ import Backbone from 'backbone';
import { HTMLParserOptions } from '../parser/config/config'; import { HTMLParserOptions } from '../parser/config/config';
export { default as $ } from '../utils/cash-dom'; export { default as $ } from '../utils/cash-dom';
interface NOOP {} interface NOOP { }
export const collectionEvents = 'add remove reset change'; export const collectionEvents = 'add remove reset change';
export type Debounced = Function & { cancel(): void }; export type Debounced = Function & { cancel(): void };
export type SetOptions = Backbone.ModelSetOptions & { export type SetOptions = Backbone.ModelSetOptions & {
unset?: boolean;
avoidStore?: boolean; avoidStore?: boolean;
avoidTransformers?: boolean; avoidTransformers?: boolean;
partial?: boolean; partial?: boolean;
@ -61,7 +62,7 @@ export interface Dimensions {
width: number; width: number;
} }
export interface BoxRect extends Coordinates, Dimensions {} export interface BoxRect extends Coordinates, Dimensions { }
export type ElementRect = { export type ElementRect = {
top: number; top: number;
@ -76,13 +77,13 @@ export type CombinedModelConstructorOptions<
> = Backbone.ModelConstructorOptions<M> & E; > = Backbone.ModelConstructorOptions<M> & E;
export interface ViewOptions<TModel extends Model | undefined = Model, TElement extends Element = HTMLElement> export interface ViewOptions<TModel extends Model | undefined = Model, TElement extends Element = HTMLElement>
extends Backbone.ViewOptions<TModel, TElement> {} extends Backbone.ViewOptions<TModel, TElement> { }
export class Model<T extends ObjectHash = any, S = SetOptions, E = any> extends Backbone.Model<T, S, E> {} export class Model<T extends ObjectHash = any, S = SetOptions, E = any> extends Backbone.Model<T, S, E> { }
export class Collection<T extends Model = Model> extends Backbone.Collection<T> {} export class Collection<T extends Model = Model> extends Backbone.Collection<T> { }
export class View<T extends Model | undefined = Model, E extends Element = HTMLElement> extends Backbone.View<T, E> {} export class View<T extends Model | undefined = Model, E extends Element = HTMLElement> extends Backbone.View<T, E> { }
export type PickMatching<T, V> = { [K in keyof T as T[K] extends V ? K : never]: T[K] }; export type PickMatching<T, V> = { [K in keyof T as T[K] extends V ? K : never]: T[K] };

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

@ -56,7 +56,7 @@ import { DynamicValueWatcher } from './DynamicValueWatcher';
export interface IComponent extends ExtractMethods<Component> { } export interface IComponent extends ExtractMethods<Component> { }
export interface DynamicWatchersOptions { export interface DynamicWatchersOptions {
updateDynamicWatchers?: 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 { }
@ -347,7 +347,7 @@ export default class Component extends StyleableModel<ComponentProperties> {
set<A extends string>( set<A extends string>(
attributeName: Partial<ComponentProperties> | A, attributeName: Partial<ComponentProperties> | A,
value?: SetOptions | ComponentProperties[A], value?: SetOptions | ComponentProperties[A],
options: ComponentSetOptions = { updateDynamicWatchers: true }, options: ComponentSetOptions = { skipWatcherUpdates: false },
): this { ): this {
const props = const props =
typeof attributeName === 'object' typeof attributeName === 'object'
@ -359,7 +359,7 @@ export default class Component extends StyleableModel<ComponentProperties> {
const evaluatedProps = areStaticAttributes const evaluatedProps = areStaticAttributes
? props ? props
: ComponentDynamicValueListener.evaluateComponentDef(props, this.em); : ComponentDynamicValueListener.evaluateComponentDef(props, this.em);
if (options.updateDynamicWatchers) { if (!options.skipWatcherUpdates) {
this.componentDVListener?.watchProps(props); this.componentDVListener?.watchProps(props);
} }
@ -683,10 +683,9 @@ export default class Component extends StyleableModel<ComponentProperties> {
* @example * @example
* component.setAttributes({ id: 'test', 'data-key': 'value' }); * component.setAttributes({ id: 'test', 'data-key': 'value' });
*/ */
setAttributes(attrs: ObjectAny, opts: SetAttrOptions = { updateDynamicWatchers: true }) { setAttributes(attrs: ObjectAny, opts: SetAttrOptions = { skipWatcherUpdates: false }) {
const areStaticAttributes = DynamicValueWatcher.areStaticValues(attrs); const evaluatedAttributes = DynamicValueWatcher.getStaticValues(attrs, this.em);
const evaluatedAttributes = areStaticAttributes ? attrs : DynamicValueWatcher.getStaticValues(attrs, this.em); if (!opts.skipWatcherUpdates) {
if (opts.updateDynamicWatchers) {
this.componentDVListener.setAttributes(attrs); this.componentDVListener.setAttributes(attrs);
} }
this.set('attributes', { ...evaluatedAttributes }, opts); this.set('attributes', { ...evaluatedAttributes }, opts);
@ -703,15 +702,10 @@ export default class Component extends StyleableModel<ComponentProperties> {
* component.addAttributes({ 'data-key': 'value' }); * component.addAttributes({ 'data-key': 'value' });
*/ */
addAttributes(attrs: ObjectAny, opts: SetAttrOptions = {}) { addAttributes(attrs: ObjectAny, opts: SetAttrOptions = {}) {
const areStaticAttributes = DynamicValueWatcher.areStaticValues(attrs);
this.componentDVListener.removeAttributes(Object.keys(attrs));
const evaluatedAttributes = areStaticAttributes ? attrs : DynamicValueWatcher.getStaticValues(attrs, this.em);
this.componentDVListener.watchAttributes(attrs);
return this.setAttributes( return this.setAttributes(
{ {
...this.getAttributes({ noClass: true }), ...this.getAttributes({ noClass: true }),
...evaluatedAttributes, ...attrs,
}, },
opts, opts,
); );

4
packages/core/src/dom_components/model/ComponentDynamicValueListener.ts

@ -13,11 +13,11 @@ export class ComponentDynamicValueListener {
em: EditorModel, em: EditorModel,
) { ) {
this.propertyWatchClass = new DynamicValueWatcher((key: string, value: any) => { this.propertyWatchClass = new DynamicValueWatcher((key: string, value: any) => {
this.component.set(key, value, { updateDynamicWatchers: false }); this.component.set(key, value, { skipWatcherUpdates: false });
}, em); }, em);
this.attributeWatchClass = new DynamicValueWatcher((key: string, value: any) => { this.attributeWatchClass = new DynamicValueWatcher((key: string, value: any) => {
this.component.setAttributes({ [key]: value }, { updateDynamicWatchers: false }); this.component.setAttributes({ [key]: value }, { skipWatcherUpdates: false });
}, em); }, em);
this.traitsWatchClass = new DynamicValueWatcher((key: string, value: any) => { this.traitsWatchClass = new DynamicValueWatcher((key: string, value: any) => {

Loading…
Cancel
Save