@visactor/vrender-core
Version:
```typescript import { xxx } from '@visactor/vrender-core'; ```
78 lines (73 loc) • 4.17 kB
JavaScript
import { DefaultStateAnimateConfig } from "../../animate/config";
import { AttributeUpdateType } from "../../common/enums";
function hasOwnKeys(value) {
return Object.keys(value).length > 0;
}
function normalizeNoAnimateAttrConfig(config) {
return config ? Array.isArray(config) ? config.reduce(((acc, key) => (acc[key] = 1,
acc)), {}) : Object.keys(config).reduce(((acc, key) => (config[key] && (acc[key] = 1),
acc)), {}) : {};
}
export class StateTransitionOrchestrator {
analyzeTransition(targetAttrs, hasAnimation, options = {}) {
var _a, _b;
const plan = {
targetAttrs: Object.assign({}, targetAttrs),
animateAttrs: {},
noAnimateAttrs: {}
};
if (!hasAnimation) return plan;
const noWorkAnimateAttr = Object.assign(Object.assign({}, null !== (_a = options.noWorkAnimateAttr) && void 0 !== _a ? _a : {}), normalizeNoAnimateAttrConfig(null === (_b = options.animateConfig) || void 0 === _b ? void 0 : _b.noAnimateAttrs)), isClear = !0 === options.isClear, readDefaultAttribute = options.getDefaultAttribute, shouldSkipDefaultAttribute = options.shouldSkipDefaultAttribute, assignTransitionAttr = (key, value) => {
if (noWorkAnimateAttr[key]) plan.noAnimateAttrs[key] = value; else if (isClear && void 0 === value) {
if (null == shouldSkipDefaultAttribute ? void 0 : shouldSkipDefaultAttribute(key, targetAttrs)) return;
plan.animateAttrs[key] = readDefaultAttribute(key);
} else plan.animateAttrs[key] = value;
};
for (const key in targetAttrs) {
if (!Object.prototype.hasOwnProperty.call(targetAttrs, key)) continue;
assignTransitionAttr(key, targetAttrs[key]);
}
const extraAnimateAttrs = options.extraAnimateAttrs;
if (extraAnimateAttrs) for (const key in extraAnimateAttrs) {
const hasTargetAttr = Object.prototype.hasOwnProperty.call(targetAttrs, key);
!Object.prototype.hasOwnProperty.call(extraAnimateAttrs, key) || hasTargetAttr && void 0 !== targetAttrs[key] || assignTransitionAttr(key, extraAnimateAttrs[key]);
}
return plan;
}
applyTransition(graphic, plan, hasAnimation, options = {}) {
var _a, _b;
if (hasAnimation) {
const stateAnimateConfig = null !== (_a = options.animateConfig) && void 0 !== _a ? _a : DefaultStateAnimateConfig;
null === (_b = graphic.applyAnimationState) || void 0 === _b || _b.call(graphic, [ "state" ], [ {
name: "state",
animation: {
type: "state",
to: plan.animateAttrs,
duration: stateAnimateConfig.duration,
easing: stateAnimateConfig.easing
}
} ]), hasOwnKeys(plan.noAnimateAttrs) && graphic.setAttributesAndPreventAnimate(plan.noAnimateAttrs, !1, {
type: AttributeUpdateType.STATE
});
} else graphic.stopStateAnimates(), graphic.setAttributesAndPreventAnimate(plan.targetAttrs, !1, {
type: AttributeUpdateType.STATE
});
return graphic.finalAttribute && Object.assign(graphic.finalAttribute, plan.targetAttrs),
graphic._emitCustomEvent("afterStateUpdate", {
type: AttributeUpdateType.STATE
}), plan;
}
applyClearTransition(graphic, targetAttrs, hasAnimation, options = {}) {
var _a;
const plan = this.analyzeTransition(targetAttrs, hasAnimation, {
noWorkAnimateAttr: graphic.getNoWorkAnimateAttr(),
isClear: !0,
getDefaultAttribute: graphic.getDefaultAttribute.bind(graphic),
shouldSkipDefaultAttribute: null !== (_a = options.shouldSkipDefaultAttribute) && void 0 !== _a ? _a : graphic.shouldSkipStateTransitionDefaultAttribute.bind(graphic),
animateConfig: options.animateConfig,
extraAnimateAttrs: options.extraAnimateAttrs
});
return this.applyTransition(graphic, plan, hasAnimation, options);
}
}
//# sourceMappingURL=state-transition-orchestrator.js.map