Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/modern-queens-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@antv/g-lite': minor
---

feat: add experimental api `setAttributes`
4 changes: 4 additions & 0 deletions packages/g-lite/src/css/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export interface PropertyParseOptions {
forceUpdateGeometry: boolean;
usedAttributes: string[];
memoize: boolean;
/**
* @experimental
*/
skipDispatchAttrModifiedEvent?: boolean;
}

export interface StyleValueRegistry {
Expand Down
48 changes: 48 additions & 0 deletions packages/g-lite/src/display-objects/DisplayObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,43 @@ export class DisplayObject<
}
}

/**
* batch update attributes without attributeChangedCallback, for performance
* use with caution
* @param attributes
* @param parseOptions
* @experimental
*/
setAttributes(
attributes: Partial<StyleProps>,
parseOptions: Partial<PropertyParseOptions> = {},
) {
const { skipDispatchAttrModifiedEvent = false } = parseOptions;
let oldAttributes;
let oldParsedValues;
if (!skipDispatchAttrModifiedEvent) {
oldAttributes = { ...this.attributes };
oldParsedValues = { ...this.parsedStyle };
}
runtime.styleValueRegistry.processProperties(
this as unknown as DisplayObject,
attributes,
parseOptions,
);
// redraw at next frame
this.dirty();
if (!skipDispatchAttrModifiedEvent) {
for (const key in attributes) {
this.dispatchAttrModifiedEvent(
key,
oldAttributes[key],
attributes[key],
oldParsedValues[key as string],
);
}
}
}

/**
* called when attributes get changed or initialized
*/
Expand All @@ -297,6 +334,17 @@ export class DisplayObject<
// redraw at next frame
this.dirty();

// return;

this.dispatchAttrModifiedEvent(name, oldValue, value, oldParsedValue);
}

private dispatchAttrModifiedEvent<Key extends keyof StyleProps>(
name: Key,
oldValue: StyleProps[Key],
value: StyleProps[Key],
oldParsedValue: ParsedStyleProps[keyof ParsedStyleProps],
) {
const newParsedValue = this.parsedStyle[name as string];
if (this.isConnected) {
attrModifiedEvent.relatedNode = this as IElement;
Expand Down
Loading