( script: string, animationId: string, config: ArcPathConfig, )
| 2771 | } |
| 2772 | |
| 2773 | export function setArcPathInScript( |
| 2774 | script: string, |
| 2775 | animationId: string, |
| 2776 | config: ArcPathConfig, |
| 2777 | ): string { |
| 2778 | const loc = locateAnimation(script, animationId); |
| 2779 | if (!loc) return script; |
| 2780 | |
| 2781 | const varsArg = loc.target.call.varsArg; |
| 2782 | const anim = loc.target.animation; |
| 2783 | |
| 2784 | if (!config.enabled) { |
| 2785 | // Disable arc: restore x/y from motionPath's last waypoint, then remove motionPath |
| 2786 | const motionPathProp = varsArg.properties.find( |
| 2787 | (p: AstNode) => isObjectProperty(p) && propKeyName(p) === "motionPath", |
| 2788 | ); |
| 2789 | if (motionPathProp) { |
| 2790 | const mpVal = motionPathProp.value; |
| 2791 | let pathArr: AstNode[] | undefined; |
| 2792 | if (mpVal?.type === "ObjectExpression") { |
| 2793 | const pathProp = mpVal.properties.find( |
| 2794 | (p: AstNode) => isObjectProperty(p) && propKeyName(p) === "path", |
| 2795 | ); |
| 2796 | if (pathProp?.value?.type === "ArrayExpression") pathArr = pathProp.value.elements; |
| 2797 | } |
| 2798 | if (pathArr && pathArr.length > 0) { |
| 2799 | const last = pathArr[pathArr.length - 1]; |
| 2800 | if (last?.type === "ObjectExpression") { |
| 2801 | for (const p of last.properties) { |
| 2802 | const k = propKeyName(p); |
| 2803 | if (k === "x" || k === "y") { |
| 2804 | const v = p.value?.value; |
| 2805 | if (typeof v === "number") setVarsKey(varsArg, k, v); |
| 2806 | } |
| 2807 | } |
| 2808 | } |
| 2809 | } |
| 2810 | } |
| 2811 | removeVarsKey(varsArg, "motionPath"); |
| 2812 | return recast.print(loc.parsed.ast).code; |
| 2813 | } |
| 2814 | |
| 2815 | const waypoints = extractArcWaypoints(anim); |
| 2816 | if (waypoints.length < 2) return script; |
| 2817 | |
| 2818 | // Build segments — use provided segments or create defaults |
| 2819 | const segments: ArcPathSegment[] = |
| 2820 | config.segments.length === waypoints.length - 1 |
| 2821 | ? config.segments |
| 2822 | : Array.from({ length: waypoints.length - 1 }, () => ({ curviness: 1 })); |
| 2823 | |
| 2824 | const motionPathCode = buildMotionPathObjectCode({ |
| 2825 | waypoints, |
| 2826 | segments, |
| 2827 | autoRotate: config.autoRotate, |
| 2828 | }); |
| 2829 | |
| 2830 | // Set motionPath on the vars |
no test coverage detected