@visactor/vrender-core 1.1.0-alpha.17 → 1.1.0-alpha.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import type { ICustomPath2D } from './../interface/path';
2
2
  import { Matrix, Point, type Dict, type IAABBBounds, type IOBBBounds, type IPointLike } from '@visactor/vutils';
3
- import type { GraphicType, IAnimateConfig, IGraphicAttribute, IGraphic, IGraphicJson, ISetAttributeContext, ITransform, GraphicReleaseStatus } from '../interface/graphic';
3
+ import type { GraphicType, IAnimateConfig, IGraphicAttribute, IGraphic, IGraphicJson, ISetAttributeContext, ISetStatesOptions, ITransform, GraphicReleaseStatus } from '../interface/graphic';
4
4
  import { Node } from './node-tree';
5
5
  import type { IAnimate, IAnimateTarget, IGlyphGraphicAttribute, ILayer, IPickerService, IShadowRoot, IStage, IStep, ISymbolClass } from '../interface';
6
6
  import { IContainPointMode } from '../common/enums';
@@ -18,6 +18,16 @@ type AttributeDelta = Map<string, {
18
18
  prev: unknown;
19
19
  next: unknown;
20
20
  }>;
21
+ type ResolvedGraphicStateTransition<T> = {
22
+ transition: {
23
+ changed: boolean;
24
+ states: string[];
25
+ effectiveStates?: string[];
26
+ };
27
+ effectiveStates: string[];
28
+ resolvedStateAttrs: Partial<T>;
29
+ isSimpleLocalTransition: boolean;
30
+ };
21
31
  export declare const NOWORK_ANIMATE_ATTR: {
22
32
  strokeSeg: number;
23
33
  boundsPadding: number;
@@ -235,13 +245,23 @@ export declare abstract class Graphic<T extends Partial<IGraphicAttribute> = Par
235
245
  }): void;
236
246
  hasState(stateName?: string): boolean;
237
247
  getState(stateName: string): Partial<T> | StateDefinition<T> | undefined;
238
- protected createStateModel(): StateModel<T>;
248
+ protected getStateResolveBaseAttrs(): Partial<T>;
249
+ protected syncStateResolveContext(stateResolveBaseAttrs?: Partial<T>): Partial<T>;
250
+ protected createStateModel(stateResolveBaseAttrs?: Partial<T>): StateModel<T>;
239
251
  protected resolveSimpleLocalStateTransition(states: string[], previousStates: readonly string[]): {
240
252
  changed: boolean;
241
253
  states: string[];
242
254
  effectiveStates: string[];
243
255
  resolvedStateAttrs: Partial<T>;
244
256
  } | null;
257
+ protected resolveGraphicStateTransition(states: string[], previousStates: readonly string[], forceResolverRefresh?: boolean): ResolvedGraphicStateTransition<T>;
258
+ protected normalizeSetStatesOptions(options?: boolean | ISetStatesOptions): {
259
+ hasAnimation?: boolean;
260
+ animateSameStatePatchChange: boolean;
261
+ shouldRefreshSameStatePatch: boolean;
262
+ };
263
+ protected sameStatePatches(left?: Partial<T>, right?: Partial<T>): boolean;
264
+ protected commitSameStatePatchRefresh(states: string[], hasAnimation?: boolean, animateSameStatePatchChange?: boolean): void;
245
265
  protected resolveStateAnimateConfig(animateConfig?: IAnimateConfig): any;
246
266
  applyStateAttrs(attrs: Partial<T>, stateNames: string[], hasAnimation?: boolean, isClear?: boolean, animateConfig?: IAnimateConfig, extraAnimateAttrs?: Partial<T>): void;
247
267
  updateNormalAttrs(_stateAttrs: Partial<T>): void;
@@ -254,6 +274,7 @@ export declare abstract class Graphic<T extends Partial<IGraphicAttribute> = Par
254
274
  toggleState(stateName: string, hasAnimation?: boolean): void;
255
275
  addState(stateName: string, keepCurrentStates?: boolean, hasAnimation?: boolean): void;
256
276
  setStates(states?: string[] | null, hasAnimation?: boolean): void;
277
+ setStates(states?: string[] | null, options?: ISetStatesOptions): void;
257
278
  useStates(states: string[], hasAnimation?: boolean): void;
258
279
  invalidateResolver(): void;
259
280
  private sameStateNames;
@@ -248,12 +248,10 @@ class Graphic extends node_tree_1.Node {
248
248
  };
249
249
  }
250
250
  recomputeCurrentStatePatch() {
251
- var _a, _b, _c, _d;
251
+ var _a, _b;
252
252
  if (!(null === (_a = this.currentStates) || void 0 === _a ? void 0 : _a.length)) return this.effectiveStates = [],
253
253
  this.resolvedStatePatch = void 0, void this.syncSharedStateActiveRegistrations();
254
- const stateResolveBaseAttrs = null !== (_b = this.baseAttributes) && void 0 !== _b ? _b : this.attribute, stateModel = this.createStateModel();
255
- null === (_c = this.stateEngine) || void 0 === _c || _c.setResolveContext(this, stateResolveBaseAttrs);
256
- const transition = stateModel.useStates(this.currentStates), effectiveStates = null !== (_d = transition.effectiveStates) && void 0 !== _d ? _d : transition.states, resolvedStateAttrs = this.stateEngine && this.compiledStateDefinitions ? Object.assign({}, this.stateEngine.resolvedPatch) : this.getStateStyleResolver(this.stateMergeMode).resolve(stateResolveBaseAttrs, this.states, this.stateProxy, transition.states, this.stateSort);
254
+ const stateResolveBaseAttrs = this.getStateResolveBaseAttrs(), transition = this.createStateModel(stateResolveBaseAttrs).useStates(this.currentStates), effectiveStates = null !== (_b = transition.effectiveStates) && void 0 !== _b ? _b : transition.states, resolvedStateAttrs = this.stateEngine && this.compiledStateDefinitions ? Object.assign({}, this.stateEngine.resolvedPatch) : this.getStateStyleResolver(this.stateMergeMode).resolve(stateResolveBaseAttrs, this.states, this.stateProxy, transition.states, this.stateSort);
257
255
  this.currentStates = transition.states, this.effectiveStates = [ ...effectiveStates ],
258
256
  this.resolvedStatePatch = resolvedStateAttrs, this.syncSharedStateActiveRegistrations();
259
257
  }
@@ -843,7 +841,16 @@ class Graphic extends node_tree_1.Node {
843
841
  var _a;
844
842
  return null === (_a = this.states) || void 0 === _a ? void 0 : _a[stateName];
845
843
  }
846
- createStateModel() {
844
+ getStateResolveBaseAttrs() {
845
+ var _a;
846
+ return null !== (_a = this.baseAttributes) && void 0 !== _a ? _a : this.attribute;
847
+ }
848
+ syncStateResolveContext(stateResolveBaseAttrs = this.getStateResolveBaseAttrs()) {
849
+ var _a;
850
+ return null === (_a = this.stateEngine) || void 0 === _a || _a.setResolveContext(this, stateResolveBaseAttrs),
851
+ stateResolveBaseAttrs;
852
+ }
853
+ createStateModel(stateResolveBaseAttrs = this.getStateResolveBaseAttrs()) {
847
854
  const {compiledDefinitions: compiledDefinitions, stateProxyEligibility: stateProxyEligibility, stateProxyModeKey: stateProxyModeKey} = this.resolveEffectiveCompiledDefinitions();
848
855
  return this.compiledStateDefinitions = compiledDefinitions, compiledDefinitions ? this.stateEngine && this.stateEngineCompiledDefinitions === compiledDefinitions && this.stateEngineStateProxy === this.stateProxy && this.stateEngineStateSort === this.stateSort && this.stateEngineMergeMode === this.stateMergeMode && this.stateEngineStateProxyModeKey === stateProxyModeKey || (this.stateEngine = new state_engine_1.StateEngine({
849
856
  compiledDefinitions: compiledDefinitions,
@@ -856,7 +863,7 @@ class Graphic extends node_tree_1.Node {
856
863
  this.stateEngineStateSort = this.stateSort, this.stateEngineMergeMode = this.stateMergeMode,
857
864
  this.stateEngineStateProxyModeKey = stateProxyModeKey) : (this.stateEngine = void 0,
858
865
  this.stateEngineCompiledDefinitions = void 0, this.stateEngineStateProxyModeKey = void 0),
859
- new state_model_1.StateModel({
866
+ this.syncStateResolveContext(stateResolveBaseAttrs), new state_model_1.StateModel({
860
867
  states: this.states,
861
868
  currentStates: this.currentStates,
862
869
  stateSort: this.stateSort,
@@ -917,6 +924,65 @@ class Graphic extends node_tree_1.Node {
917
924
  resolvedStateAttrs: resolvedStateAttrs
918
925
  };
919
926
  }
927
+ resolveGraphicStateTransition(states, previousStates, forceResolverRefresh = !1) {
928
+ var _a, _b;
929
+ let transition = this.resolveSimpleLocalStateTransition(states, previousStates);
930
+ const isSimpleLocalTransition = !!transition;
931
+ let resolvedStateAttrs;
932
+ if (transition) resolvedStateAttrs = transition.resolvedStateAttrs; else {
933
+ const stateResolveBaseAttrs = this.getStateResolveBaseAttrs(), stateModel = this.createStateModel(stateResolveBaseAttrs);
934
+ forceResolverRefresh && (null === (_a = this.stateEngine) || void 0 === _a || _a.invalidateResolverCache()),
935
+ transition = stateModel.useStates(states), resolvedStateAttrs = this.stateEngine && this.compiledStateDefinitions ? Object.assign({}, this.stateEngine.resolvedPatch) : this.getStateStyleResolver(this.stateMergeMode).resolve(stateResolveBaseAttrs, this.states, this.stateProxy, transition.states, this.stateSort);
936
+ }
937
+ return {
938
+ transition: transition,
939
+ effectiveStates: null !== (_b = transition.effectiveStates) && void 0 !== _b ? _b : transition.states,
940
+ resolvedStateAttrs: resolvedStateAttrs,
941
+ isSimpleLocalTransition: isSimpleLocalTransition
942
+ };
943
+ }
944
+ normalizeSetStatesOptions(options) {
945
+ return options && "object" == typeof options ? {
946
+ hasAnimation: options.animate,
947
+ animateSameStatePatchChange: !0 === options.animateSameStatePatchChange,
948
+ shouldRefreshSameStatePatch: !0
949
+ } : {
950
+ hasAnimation: "boolean" == typeof options ? options : void 0,
951
+ animateSameStatePatchChange: !1,
952
+ shouldRefreshSameStatePatch: !1
953
+ };
954
+ }
955
+ sameStatePatches(left, right) {
956
+ const leftRecord = null != left ? left : {}, rightRecord = null != right ? right : {}, keys = new Set([ ...Object.keys(leftRecord), ...Object.keys(rightRecord) ]);
957
+ for (const key of keys) {
958
+ if (Object.prototype.hasOwnProperty.call(leftRecord, key) !== Object.prototype.hasOwnProperty.call(rightRecord, key)) return !1;
959
+ if (!areAttributeValuesEqual(leftRecord[key], rightRecord[key])) return !1;
960
+ }
961
+ return !0;
962
+ }
963
+ commitSameStatePatchRefresh(states, hasAnimation, animateSameStatePatchChange = !1) {
964
+ var _a;
965
+ const previousStates = null !== (_a = this.currentStates) && void 0 !== _a ? _a : EMPTY_STATE_NAMES, previousResolvedStatePatch = this.resolvedStatePatch, {transition: transition, effectiveStates: effectiveStates, resolvedStateAttrs: resolvedStateAttrs, isSimpleLocalTransition: isSimpleLocalTransition} = this.resolveGraphicStateTransition(states, previousStates, !0), patchChanged = !this.sameStatePatches(previousResolvedStatePatch, resolvedStateAttrs);
966
+ if ((!patchChanged || this.beforeStateUpdate(resolvedStateAttrs, previousStates, transition.states, hasAnimation, !1)) && (this.currentStates = transition.states,
967
+ this.effectiveStates = isSimpleLocalTransition ? effectiveStates : [ ...effectiveStates ],
968
+ this.resolvedStatePatch = resolvedStateAttrs, this.sharedStateDirty = !1, this.syncSharedStateActiveRegistrations(),
969
+ patchChanged)) {
970
+ if (this.stage) {
971
+ const perfMonitor = (0, state_perf_monitor_1.getActiveStageStatePerfMonitor)(this.stage);
972
+ null == perfMonitor || perfMonitor.incrementCounter("stateCommits"), null == perfMonitor || perfMonitor.recordEvent("state-commit", {
973
+ graphicId: this._uid,
974
+ targetStates: [ ...transition.states ]
975
+ });
976
+ }
977
+ hasAnimation && animateSameStatePatchChange ? (this._syncFinalAttributeFromStaticTruth(),
978
+ this.applyStateAttrs(resolvedStateAttrs, transition.states, hasAnimation, !1, void 0, this.buildRemovedStateAnimationAttrs(resolvedStateAttrs, previousResolvedStatePatch))) : (this.stopStateAnimates(),
979
+ this.attributeMayContainTransientAttrs ? this._restoreAttributeFromStaticTruth({
980
+ type: enums_1.AttributeUpdateType.STATE
981
+ }) : this.restoreAttributeFromStatePatchDelta(previousResolvedStatePatch, this.resolvedStatePatch, {
982
+ type: enums_1.AttributeUpdateType.STATE
983
+ }), this.emitStateUpdateEvent());
984
+ }
985
+ }
920
986
  resolveStateAnimateConfig(animateConfig) {
921
987
  var _a, _b, _c;
922
988
  return null !== (_c = null !== (_a = null != animateConfig ? animateConfig : this.stateAnimateConfig) && void 0 !== _a ? _a : null === (_b = this.context) || void 0 === _b ? void 0 : _b.stateAnimateConfig) && void 0 !== _c ? _c : config_2.DefaultStateAnimateConfig;
@@ -993,32 +1059,22 @@ class Graphic extends node_tree_1.Node {
993
1059
  const transition = this.createStateModel().addState(stateName, keepCurrentStates);
994
1060
  transition.changed && this.useStates(transition.states, hasAnimation);
995
1061
  }
996
- setStates(states, hasAnimation) {
1062
+ setStates(states, options) {
997
1063
  var _a, _b, _c;
998
- const nextStates = (null == states ? void 0 : states.length) ? states : EMPTY_STATE_NAMES, hasCurrentState = !!((null === (_a = this.currentStates) || void 0 === _a ? void 0 : _a.length) || (null === (_b = this.effectiveStates) || void 0 === _b ? void 0 : _b.length) || this.resolvedStatePatch || (null === (_c = this.registeredActiveScopes) || void 0 === _c ? void 0 : _c.size));
999
- if (nextStates.length) this.sameStateNames(this.currentStates, nextStates) ? this.sharedStateDirty && this.refreshSharedStateBeforeRender() : this.useStates(nextStates, hasAnimation); else {
1064
+ const {hasAnimation: hasAnimation, animateSameStatePatchChange: animateSameStatePatchChange, shouldRefreshSameStatePatch: shouldRefreshSameStatePatch} = this.normalizeSetStatesOptions(options), nextStates = (null == states ? void 0 : states.length) ? states : EMPTY_STATE_NAMES, hasCurrentState = !!((null === (_a = this.currentStates) || void 0 === _a ? void 0 : _a.length) || (null === (_b = this.effectiveStates) || void 0 === _b ? void 0 : _b.length) || this.resolvedStatePatch || (null === (_c = this.registeredActiveScopes) || void 0 === _c ? void 0 : _c.size));
1065
+ if (nextStates.length) {
1066
+ if (this.sameStateNames(this.currentStates, nextStates)) return shouldRefreshSameStatePatch ? void this.commitSameStatePatchRefresh(nextStates, hasAnimation, animateSameStatePatchChange) : void (this.sharedStateDirty && this.refreshSharedStateBeforeRender());
1067
+ this.useStates(nextStates, hasAnimation);
1068
+ } else {
1000
1069
  if (!hasCurrentState && !this.sharedStateDirty) return;
1001
1070
  this.clearStates(hasAnimation);
1002
1071
  }
1003
1072
  }
1004
1073
  useStates(states, hasAnimation) {
1005
- var _a, _b, _c, _d;
1074
+ var _a;
1006
1075
  if (!states.length) return void this.clearStates(hasAnimation);
1007
- const previousStates = null !== (_a = this.currentStates) && void 0 !== _a ? _a : EMPTY_STATE_NAMES, previousResolvedStatePatch = this.resolvedStatePatch;
1008
- let transition = this.resolveSimpleLocalStateTransition(states, previousStates);
1009
- const isSimpleLocalTransition = !!transition;
1010
- let resolvedStateAttrs;
1011
- if (transition) {
1012
- if (!transition.changed) return;
1013
- resolvedStateAttrs = transition.resolvedStateAttrs;
1014
- } else {
1015
- const stateResolveBaseAttrs = null !== (_b = this.baseAttributes) && void 0 !== _b ? _b : this.attribute, stateModel = this.createStateModel();
1016
- if (null === (_c = this.stateEngine) || void 0 === _c || _c.setResolveContext(this, stateResolveBaseAttrs),
1017
- transition = stateModel.useStates(states), !transition.changed && this.sameStateNames(previousStates, transition.states)) return;
1018
- resolvedStateAttrs = this.stateEngine && this.compiledStateDefinitions ? Object.assign({}, this.stateEngine.resolvedPatch) : this.getStateStyleResolver(this.stateMergeMode).resolve(stateResolveBaseAttrs, this.states, this.stateProxy, transition.states, this.stateSort);
1019
- }
1020
- const effectiveStates = null !== (_d = transition.effectiveStates) && void 0 !== _d ? _d : transition.states;
1021
- if (this.beforeStateUpdate(resolvedStateAttrs, previousStates, transition.states, hasAnimation, !1)) {
1076
+ const previousStates = null !== (_a = this.currentStates) && void 0 !== _a ? _a : EMPTY_STATE_NAMES, previousResolvedStatePatch = this.resolvedStatePatch, {transition: transition, effectiveStates: effectiveStates, resolvedStateAttrs: resolvedStateAttrs, isSimpleLocalTransition: isSimpleLocalTransition} = this.resolveGraphicStateTransition(states, previousStates);
1077
+ if ((transition.changed || !this.sameStateNames(previousStates, transition.states)) && this.beforeStateUpdate(resolvedStateAttrs, previousStates, transition.states, hasAnimation, !1)) {
1022
1078
  if (this.currentStates = transition.states, this.effectiveStates = isSimpleLocalTransition ? effectiveStates : [ ...effectiveStates ],
1023
1079
  this.resolvedStatePatch = resolvedStateAttrs, this.sharedStateDirty = !1, this.syncSharedStateActiveRegistrations(),
1024
1080
  this.stage) {
@@ -1037,10 +1093,10 @@ class Graphic extends node_tree_1.Node {
1037
1093
  }
1038
1094
  }
1039
1095
  invalidateResolver() {
1040
- var _a, _b, _c;
1096
+ var _a, _b;
1041
1097
  if (!this.stateEngine || !(null === (_a = this.currentStates) || void 0 === _a ? void 0 : _a.length) || !this.compiledStateDefinitions) return;
1042
- const stateResolveBaseAttrs = null !== (_b = this.baseAttributes) && void 0 !== _b ? _b : this.attribute;
1043
- this.stateEngine.setResolveContext(this, stateResolveBaseAttrs), this.resolverEpoch = (null !== (_c = this.resolverEpoch) && void 0 !== _c ? _c : 0) + 1,
1098
+ this.syncStateResolveContext();
1099
+ this.resolverEpoch = (null !== (_b = this.resolverEpoch) && void 0 !== _b ? _b : 0) + 1,
1044
1100
  this.stateEngine.invalidateResolverCache();
1045
1101
  const transition = this.stateEngine.applyStates(this.currentStates), resolvedStateAttrs = Object.assign({}, this.stateEngine.resolvedPatch);
1046
1102
  this.effectiveStates = [ ...transition.effectiveStates ], this.resolvedStatePatch = resolvedStateAttrs,