Skip to content

Commit d0d7bb4

Browse files
committed
fix: fix update animation of group-mark
1 parent 990b63a commit d0d7bb4

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

packages/vchart/src/mark/base/base-mark.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import type {
5151
IMarkStateManager,
5252
StateValueType
5353
} from '../../compile/mark/interface';
54-
import { array, degreeToRadian, has, isArray, isBoolean, isFunction, isNil, isObject, isValid } from '@visactor/vutils';
54+
import { array, degreeToRadian, isArray, isBoolean, isFunction, isNil, isObject, isValid } from '@visactor/vutils';
5555
import { curveTypeTransform, groupData, runEncoder } from '../utils/common';
5656
import type { ICompilableInitOption } from '../../compile/interface';
5757
import { LayoutState } from '../../compile/interface';
@@ -67,7 +67,8 @@ import type { ICompilableData } from '../../compile/data/interface';
6767
import type { IAnimationConfig } from '../../animation/interface';
6868
import { AnimationStateEnum, type MarkAnimationSpec } from '../../animation/interface';
6969
import { CompilableData } from '../../compile/data/compilable-data';
70-
import { log } from '../../util';
70+
import { getDiffAttributesOfGraphic } from '../../util/mark';
71+
import { log } from '../../util/debug';
7172
import { morph as runMorph } from '../../compile/morph';
7273

7374
export type ExChannelCall = (
@@ -1263,7 +1264,7 @@ export class BaseMark<T extends ICommonSpec> extends GrammarItem implements IMar
12631264
}
12641265
}
12651266

1266-
private _setAnimationState(g: IMarkGraphic) {
1267+
protected _setAnimationState(g: IMarkGraphic) {
12671268
const customizedState = this._aniamtionStateCallback ? this._aniamtionStateCallback(g) : undefined;
12681269

12691270
g.context.animationState = customizedState ?? g.context.diffState;
@@ -1552,14 +1553,7 @@ export class BaseMark<T extends ICommonSpec> extends GrammarItem implements IMar
15521553
this._graphicMap.set(g.context.uniqueKey, g);
15531554
}
15541555
} else {
1555-
// diff一下,获取差异的属性
1556-
const prevAttrs: Record<string, any> = g.getAttributes(true);
1557-
const diffAttrs: Record<string, any> = {};
1558-
Object.keys(finalAttrs).forEach(key => {
1559-
if (prevAttrs[key] !== finalAttrs[key]) {
1560-
diffAttrs[key] = finalAttrs[key];
1561-
}
1562-
});
1556+
const diffAttrs = getDiffAttributesOfGraphic(g, finalAttrs);
15631557
g.context.diffAttrs = diffAttrs;
15641558
if (g.context.reusing) {
15651559
// 表示正在被复用,需要重设属性的
@@ -1982,11 +1976,11 @@ export class BaseMark<T extends ICommonSpec> extends GrammarItem implements IMar
19821976
this._aniamtionStateCallback = callback;
19831977
}
19841978

1985-
hasAnimationByState(state: keyof MarkAnimationSpec) {
1986-
if (!state || !this._animationConfig || !this._animationConfig[state]) {
1979+
hasAnimationByState(state: AnimationStateValues) {
1980+
if (!state || !this._animationConfig || !(this._animationConfig as any)[state]) {
19871981
return false;
19881982
}
1989-
const stateAnimationConfig = this._animationConfig[state];
1983+
const stateAnimationConfig = (this._animationConfig as any)[state];
19901984
return (stateAnimationConfig as IAnimationConfig[]).length > 0 || isObject(stateAnimationConfig);
19911985
}
19921986

packages/vchart/src/mark/group.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ import type { Maybe } from '../typings';
44
import { log, warn } from '../util/debug';
55
import type { IGroupMarkSpec } from '../typings/visual';
66
import { BaseMark } from './base/base-mark';
7-
import type { AnimationStateValues, IGroupMark, IMark, IMarkGraphic, MarkType } from './interface';
7+
import {
8+
DiffState,
9+
type AnimationStateValues,
10+
type IGroupMark,
11+
type IMark,
12+
type IMarkGraphic,
13+
type MarkType
14+
} from './interface';
815
// eslint-disable-next-line no-duplicate-imports
916
import { MarkTypeEnum } from './interface/type';
1017
import { type IMarkCompileOption } from '../compile/mark';
1118
import type { IGroup, IGroupGraphicAttribute } from '@visactor/vrender-core';
1219
import { registerGroup, registerShadowRoot } from '@visactor/vrender-kits';
1320
import { isNil } from '@visactor/vutils';
1421
import { traverseGroupMark } from '../compile/util';
15-
import { LayoutState } from '../compile/interface';
22+
import { getDiffAttributesOfGraphic } from '../util/mark';
1623

1724
export class GroupMark extends BaseMark<IGroupMarkSpec> implements IGroupMark {
1825
static readonly type = MarkTypeEnum.group;
@@ -110,9 +117,33 @@ export class GroupMark extends BaseMark<IGroupMarkSpec> implements IGroupMark {
110117
}
111118

112119
const style = this._simpleStyle ?? this.getAttributesOfState({});
120+
const prevState = this._product.context?.diffState;
121+
122+
this._product.context = {
123+
...this._product.context,
124+
...this._getCommonContext(),
125+
diffState: prevState ? DiffState.update : DiffState.enter
126+
};
127+
this._setAnimationState(this._product as unknown as IMarkGraphic);
128+
const newAttrs = this._getAttrsFromConfig(style);
129+
130+
// TODO: 需要优化,现在group mark 走了一些特殊逻辑
131+
if (this._product.context.diffState === DiffState.update) {
132+
const hasAnimation = this.hasAnimation();
133+
const diffAttrs = getDiffAttributesOfGraphic(this._product as unknown as IMarkGraphic, newAttrs);
134+
this._product.context.diffAttrs = diffAttrs;
135+
136+
if (!this.hasAnimationByState(this._product.context.animationState)) {
137+
hasAnimation ? this._product.setAttributesAndPreventAnimate(diffAttrs) : this._product.setAttributes(diffAttrs);
138+
}
139+
140+
if (hasAnimation) {
141+
this._product.setFinalAttributes(newAttrs);
142+
}
143+
} else {
144+
this._product.setAttributes(newAttrs);
145+
}
113146

114-
this._product.context = { ...this._product.context, ...this._getCommonContext() };
115-
this._product.setAttributes(this._getAttrsFromConfig(style));
116147
this.needClear = true;
117148
}
118149

packages/vchart/src/mark/interface/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type IMarkStyle<T extends ICommonSpec> = {
5959

6060
export type DiffStateValues = 'update' | 'enter' | 'exit';
6161

62-
export type AnimationStateValues = 'appear' | 'enter' | 'update' | 'exit' | 'disappear' | 'none';
62+
export type AnimationStateValues = 'appear' | 'enter' | 'update' | 'exit' | 'disappear' | 'none' | 'state';
6363

6464
export interface IGraphicContext {
6565
markType: MarkTypeEnum;

packages/vchart/src/util/mark.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,16 @@ export const findMarkGraphic = (rootGroup: IGraphic, target: IGraphic) => {
3232

3333
return null;
3434
};
35+
36+
export const getDiffAttributesOfGraphic = (g: IMarkGraphic, newAttrs: any) => {
37+
// diff一下,获取差异的属性
38+
const prevAttrs: Record<string, any> = g.getAttributes(true);
39+
const diffAttrs: Record<string, any> = {};
40+
Object.keys(newAttrs).forEach(key => {
41+
if (prevAttrs[key] !== newAttrs[key]) {
42+
diffAttrs[key] = newAttrs[key];
43+
}
44+
});
45+
46+
return diffAttrs;
47+
};

0 commit comments

Comments
 (0)