|
7 | 7 | * pretty-printer churn. Consumes ParsedGsapAcornForWrite from gsapParserAcorn.ts. |
8 | 8 | */ |
9 | 9 | import MagicString from "magic-string"; |
10 | | -import type { GsapAnimation, GsapPercentageKeyframe } from "./gsapSerialize.js"; |
11 | | -import { resolveConversionProps } from "./gsapSerialize.js"; |
| 10 | +import type { |
| 11 | + GsapAnimation, |
| 12 | + GsapPercentageKeyframe, |
| 13 | + ArcPathConfig, |
| 14 | + ArcPathSegment, |
| 15 | +} from "./gsapSerialize.js"; |
| 16 | +import { |
| 17 | + resolveConversionProps, |
| 18 | + extractArcWaypoints, |
| 19 | + buildMotionPathObjectCode, |
| 20 | +} from "./gsapSerialize.js"; |
12 | 21 | import { |
13 | 22 | parseGsapScriptAcornForWrite, |
14 | 23 | type ParsedGsapAcornForWrite, |
@@ -1304,6 +1313,160 @@ export function removeLabelFromScript(script: string, name: string): string { |
1304 | 1313 | return ms.toString(); |
1305 | 1314 | } |
1306 | 1315 |
|
| 1316 | +// ── Arc path helpers ───────────────────────────────────────────────────────── |
| 1317 | + |
| 1318 | +/** |
| 1319 | + * Remove a set of properties from an ObjectExpression in a single pass. |
| 1320 | + * Groups consecutive marked props into blocks to avoid overlapping remove ranges. |
| 1321 | + */ |
| 1322 | +function removePropsByKey(ms: MagicString, objNode: any, keys: Set<string>): void { |
| 1323 | + if (objNode?.type !== "ObjectExpression") return; |
| 1324 | + const allProps = (objNode.properties ?? []).filter(isObjectProperty); |
| 1325 | + const marked = allProps.map((p: any) => keys.has(propKeyName(p) ?? "")); |
| 1326 | + let i = 0; |
| 1327 | + while (i < allProps.length) { |
| 1328 | + if (!marked[i]) { |
| 1329 | + i++; |
| 1330 | + continue; |
| 1331 | + } |
| 1332 | + const blockStart = i; |
| 1333 | + while (i < allProps.length && marked[i]) i++; |
| 1334 | + ms.remove(...blockRemoveRange(allProps, blockStart, i)); |
| 1335 | + } |
| 1336 | +} |
| 1337 | + |
| 1338 | +function blockRemoveRange(allProps: any[], blockStart: number, blockEnd: number): [number, number] { |
| 1339 | + if (blockStart === 0 && blockEnd === allProps.length) |
| 1340 | + return [allProps[0].start, allProps[allProps.length - 1].end]; |
| 1341 | + if (blockStart === 0) return [allProps[0].start, allProps[blockEnd].start]; |
| 1342 | + return [allProps[blockStart - 1].end, allProps[blockEnd - 1].end]; |
| 1343 | +} |
| 1344 | + |
| 1345 | +// fallow-ignore-next-line complexity |
| 1346 | +function readLastWaypointXY(mpVal: any): { x: number | null; y: number | null } { |
| 1347 | + if (mpVal?.type !== "ObjectExpression") return { x: null, y: null }; |
| 1348 | + const pathProp = findPropertyNode(mpVal, "path"); |
| 1349 | + if (pathProp?.value?.type !== "ArrayExpression") return { x: null, y: null }; |
| 1350 | + const elems: any[] = pathProp.value.elements ?? []; |
| 1351 | + const last = elems[elems.length - 1]; |
| 1352 | + if (last?.type !== "ObjectExpression") return { x: null, y: null }; |
| 1353 | + const xRaw = findPropertyNode(last, "x")?.value?.value; |
| 1354 | + const yRaw = findPropertyNode(last, "y")?.value?.value; |
| 1355 | + return { x: typeof xRaw === "number" ? xRaw : null, y: typeof yRaw === "number" ? yRaw : null }; |
| 1356 | +} |
| 1357 | + |
| 1358 | +function disableArcPath(ms: MagicString, call: TweenCallInfo): boolean { |
| 1359 | + const mpProp = findPropertyNode(call.varsArg, "motionPath"); |
| 1360 | + if (!mpProp) return false; |
| 1361 | + const { x, y } = readLastWaypointXY(mpProp.value); |
| 1362 | + if (x === null && y === null) { |
| 1363 | + const allProps = (call.varsArg.properties ?? []).filter(isObjectProperty); |
| 1364 | + removeProp(ms, mpProp, allProps); |
| 1365 | + return true; |
| 1366 | + } |
| 1367 | + // Overwrite the entire motionPath property with the recovered x/y pair — avoids |
| 1368 | + // the appendLeft+remove range-boundary issue in MagicString. |
| 1369 | + const parts: string[] = []; |
| 1370 | + if (x !== null) parts.push(`x: ${x}`); |
| 1371 | + if (y !== null) parts.push(`y: ${y}`); |
| 1372 | + ms.overwrite(mpProp.start, mpProp.end, parts.join(", ")); |
| 1373 | + return true; |
| 1374 | +} |
| 1375 | + |
| 1376 | +function stripXYFromKeyframes(ms: MagicString, kfPropNode: any): void { |
| 1377 | + if (kfPropNode?.value?.type !== "ObjectExpression") return; |
| 1378 | + const xyKeys = new Set(["x", "y"]); |
| 1379 | + for (const pctProp of (kfPropNode.value.properties ?? []).filter(isObjectProperty)) { |
| 1380 | + const k = propKeyName(pctProp); |
| 1381 | + if (typeof k === "string" && k.endsWith("%") && pctProp.value?.type === "ObjectExpression") { |
| 1382 | + removePropsByKey(ms, pctProp.value, xyKeys); |
| 1383 | + } |
| 1384 | + } |
| 1385 | +} |
| 1386 | + |
| 1387 | +function enableArcPath( |
| 1388 | + ms: MagicString, |
| 1389 | + call: TweenCallInfo, |
| 1390 | + animation: GsapAnimation, |
| 1391 | + config: ArcPathConfig, |
| 1392 | +): boolean { |
| 1393 | + const waypoints = extractArcWaypoints(animation); |
| 1394 | + if (waypoints.length < 2) return false; |
| 1395 | + const segments: ArcPathSegment[] = |
| 1396 | + config.segments.length === waypoints.length - 1 |
| 1397 | + ? config.segments |
| 1398 | + : Array.from({ length: waypoints.length - 1 }, () => ({ curviness: 1 })); |
| 1399 | + const motionPathCode = buildMotionPathObjectCode({ |
| 1400 | + waypoints, |
| 1401 | + segments, |
| 1402 | + autoRotate: config.autoRotate, |
| 1403 | + }); |
| 1404 | + upsertProp(ms, call.varsArg, "motionPath", `__raw:${motionPathCode}`); |
| 1405 | + stripXYFromKeyframes(ms, findPropertyNode(call.varsArg, "keyframes")); |
| 1406 | + removePropsByKey(ms, call.varsArg, new Set(["x", "y"])); |
| 1407 | + return true; |
| 1408 | +} |
| 1409 | + |
| 1410 | +export function setArcPathInScript( |
| 1411 | + script: string, |
| 1412 | + animationId: string, |
| 1413 | + config: ArcPathConfig, |
| 1414 | +): string { |
| 1415 | + const parsed = parseGsapScriptAcornForWrite(script); |
| 1416 | + if (!parsed) return script; |
| 1417 | + const target = parsed.located.find((l) => l.id === animationId); |
| 1418 | + if (!target) return script; |
| 1419 | + const ms = new MagicString(script); |
| 1420 | + const handled = config.enabled |
| 1421 | + ? enableArcPath(ms, target.call, target.animation, config) |
| 1422 | + : disableArcPath(ms, target.call); |
| 1423 | + return handled ? ms.toString() : script; |
| 1424 | +} |
| 1425 | + |
| 1426 | +export function updateArcSegmentInScript( |
| 1427 | + script: string, |
| 1428 | + animationId: string, |
| 1429 | + segmentIndex: number, |
| 1430 | + update: Partial<ArcPathSegment>, |
| 1431 | +): string { |
| 1432 | + const parsed = parseGsapScriptAcornForWrite(script); |
| 1433 | + if (!parsed) return script; |
| 1434 | + const target = parsed.located.find((l) => l.id === animationId); |
| 1435 | + if (!target) return script; |
| 1436 | + |
| 1437 | + const { call, animation } = target; |
| 1438 | + if (!animation.arcPath?.enabled) return script; |
| 1439 | + |
| 1440 | + const segments = [...animation.arcPath.segments]; |
| 1441 | + if (segmentIndex < 0 || segmentIndex >= segments.length) return script; |
| 1442 | + |
| 1443 | + segments[segmentIndex] = { ...segments[segmentIndex]!, ...update }; |
| 1444 | + |
| 1445 | + const waypoints = extractArcWaypoints(animation); |
| 1446 | + if (waypoints.length < 2) return script; |
| 1447 | + |
| 1448 | + const motionPathCode = buildMotionPathObjectCode({ |
| 1449 | + waypoints, |
| 1450 | + segments, |
| 1451 | + autoRotate: animation.arcPath.autoRotate, |
| 1452 | + }); |
| 1453 | + |
| 1454 | + const mpProp = findPropertyNode(call.varsArg, "motionPath"); |
| 1455 | + if (!mpProp) return script; |
| 1456 | + |
| 1457 | + const ms = new MagicString(script); |
| 1458 | + ms.overwrite(mpProp.value.start, mpProp.value.end, motionPathCode); |
| 1459 | + return ms.toString(); |
| 1460 | +} |
| 1461 | + |
| 1462 | +export function removeArcPathFromScript(script: string, animationId: string): string { |
| 1463 | + return setArcPathInScript(script, animationId, { |
| 1464 | + enabled: false, |
| 1465 | + autoRotate: false, |
| 1466 | + segments: [], |
| 1467 | + }); |
| 1468 | +} |
| 1469 | + |
1307 | 1470 | // ── splitAnimationsInScript helpers ────────────────────────────────────────── |
1308 | 1471 |
|
1309 | 1472 | /** Overwrite the selector (first arg) of a tween call. */ |
|
0 commit comments