@@ -16,6 +16,7 @@ import {
1616} from "./gsapParserAcorn.js" ;
1717import { classifyPropertyGroup } from "./gsapConstants.js" ;
1818import type { PropertyGroupName } from "./gsapConstants.js" ;
19+ import type { SplitAnimationsOptions , SplitAnimationsResult } from "./gsapParser.js" ;
1920import * as acornWalk from "acorn-walk" ;
2021
2122// ── Code generation helpers ──────────────────────────────────────────────────
@@ -1302,3 +1303,169 @@ export function removeLabelFromScript(script: string, name: string): string {
13021303 }
13031304 return ms . toString ( ) ;
13041305}
1306+
1307+ // ── splitAnimationsInScript helpers ──────────────────────────────────────────
1308+
1309+ /** Overwrite the selector (first arg) of a tween call. */
1310+ function updateAnimationSelectorInScript (
1311+ script : string ,
1312+ animationId : string ,
1313+ newSelector : string ,
1314+ ) : string {
1315+ const parsed = parseGsapScriptAcornForWrite ( script ) ;
1316+ if ( ! parsed ) return script ;
1317+ const target = parsed . located . find ( ( l ) => l . id === animationId ) ;
1318+ if ( ! target ) return script ;
1319+ const selectorArg = target . call . node . arguments ?. [ 0 ] ;
1320+ if ( ! selectorArg ) return script ;
1321+ const ms = new MagicString ( script ) ;
1322+ ms . overwrite ( selectorArg . start , selectorArg . end , JSON . stringify ( newSelector ) ) ;
1323+ return ms . toString ( ) ;
1324+ }
1325+
1326+ /**
1327+ * Insert a `tl.set()` call immediately after the timeline declaration
1328+ * (before existing tweens) to establish inherited state on a new element.
1329+ */
1330+ function insertInheritedStateSetInScript (
1331+ script : string ,
1332+ selector : string ,
1333+ position : number ,
1334+ properties : Record < string , number | string > ,
1335+ ) : string {
1336+ const parsed = parseGsapScriptAcornForWrite ( script ) ;
1337+ if ( ! parsed ) return script ;
1338+ const props = Object . entries ( properties )
1339+ . map ( ( [ k , v ] ) => `${ safeKey ( k ) } : ${ valueToCode ( v ) } ` )
1340+ . join ( ", " ) ;
1341+ const code = `${ parsed . timelineVar } .set(${ JSON . stringify ( selector ) } , { ${ props } }, ${ position } );` ;
1342+ const ms = new MagicString ( script ) ;
1343+ const tlDecl = findTimelineDeclarationStatement ( parsed . ast , parsed . timelineVar ) ;
1344+ if ( tlDecl ) {
1345+ ms . appendLeft ( tlDecl . end , "\n" + code ) ;
1346+ } else if ( parsed . located . length > 0 ) {
1347+ const firstCall = parsed . located [ 0 ] ! . call ;
1348+ const exprStmt = findEnclosingExpressionStatement ( firstCall . ancestors ) ;
1349+ const insertAt = exprStmt ?. start ?? firstCall . node . start ;
1350+ ms . prependLeft ( insertAt , code + "\n" ) ;
1351+ } else {
1352+ ms . append ( "\n" + code ) ;
1353+ }
1354+ return ms . toString ( ) ;
1355+ }
1356+
1357+ // fallow-ignore-next-line complexity
1358+ export function splitAnimationsInScript (
1359+ script : string ,
1360+ opts : SplitAnimationsOptions ,
1361+ ) : SplitAnimationsResult {
1362+ const parsed = parseGsapScriptAcornForWrite ( script ) ;
1363+ if ( ! parsed ) return { script, skippedSelectors : [ ] } ;
1364+
1365+ const originalSelector = `#${ opts . originalId } ` ;
1366+ const newSelector = `#${ opts . newId } ` ;
1367+
1368+ const animations = parsed . located . map ( ( l ) => l . animation ) ;
1369+ const skippedSelectors : string [ ] = [ ] ;
1370+
1371+ for ( const a of animations ) {
1372+ if ( a . targetSelector !== originalSelector && a . targetSelector . includes ( opts . originalId ) ) {
1373+ skippedSelectors . push ( a . targetSelector ) ;
1374+ }
1375+ }
1376+
1377+ const matching = animations . filter ( ( a ) => a . targetSelector === originalSelector ) ;
1378+ if ( matching . length === 0 ) return { script, skippedSelectors } ;
1379+
1380+ let result = script ;
1381+ const newElementStart = opts . splitTime ;
1382+ const inheritedProps : Record < string , number | string > = { } ;
1383+
1384+ // Reverse iteration: updateAnimationSelectorInScript mutates selectors which
1385+ // can shift count-based ID suffixes for later animations.
1386+ for ( let i = matching . length - 1 ; i >= 0 ; i -- ) {
1387+ const anim = matching [ i ] ! ;
1388+ const pos = typeof anim . position === "number" ? anim . position : 0 ;
1389+ const dur = anim . duration ?? 0 ;
1390+ const animEnd = pos + dur ;
1391+
1392+ if ( anim . keyframes ) {
1393+ if ( pos >= opts . splitTime ) {
1394+ result = updateAnimationSelectorInScript ( result , anim . id , newSelector ) ;
1395+ } else if ( animEnd > opts . splitTime ) {
1396+ skippedSelectors . push ( `${ originalSelector } (keyframes spanning split)` ) ;
1397+ const kfs = anim . keyframes . keyframes ;
1398+ for ( const kf of kfs ) {
1399+ const kfTime = pos + ( kf . percentage / 100 ) * dur ;
1400+ if ( kfTime <= opts . splitTime ) {
1401+ for ( const [ k , v ] of Object . entries ( kf . properties ) ) {
1402+ inheritedProps [ k ] = v ;
1403+ }
1404+ }
1405+ }
1406+ } else {
1407+ const kfs = anim . keyframes . keyframes ;
1408+ if ( kfs . length > 0 ) {
1409+ for ( const [ k , v ] of Object . entries ( kfs [ kfs . length - 1 ] ! . properties ) ) {
1410+ inheritedProps [ k ] = v ;
1411+ }
1412+ }
1413+ }
1414+ continue ;
1415+ }
1416+
1417+ if ( animEnd <= opts . splitTime ) {
1418+ for ( const [ k , v ] of Object . entries ( anim . properties ) ) {
1419+ inheritedProps [ k ] = v ;
1420+ }
1421+ continue ;
1422+ }
1423+
1424+ if ( pos >= opts . splitTime ) {
1425+ result = updateAnimationSelectorInScript ( result , anim . id , newSelector ) ;
1426+ continue ;
1427+ }
1428+
1429+ // Spans the split — linear interpolation to compute mid-values.
1430+ const progress = dur > 0 ? ( opts . splitTime - pos ) / dur : 0 ;
1431+ const fromSource = anim . fromProperties ?? inheritedProps ;
1432+ const midProps : Record < string , number | string > = { } ;
1433+ for ( const [ k , v ] of Object . entries ( anim . properties ) ) {
1434+ if ( typeof v !== "number" ) {
1435+ midProps [ k ] = v ;
1436+ continue ;
1437+ }
1438+ const fromVal = typeof fromSource [ k ] === "number" ? ( fromSource [ k ] as number ) : 0 ;
1439+ midProps [ k ] = fromVal + ( v - fromVal ) * progress ;
1440+ }
1441+
1442+ const firstHalfDuration = opts . splitTime - pos ;
1443+ result = updateAnimationInScript ( result , anim . id , {
1444+ duration : firstHalfDuration ,
1445+ properties : midProps ,
1446+ } ) ;
1447+
1448+ const secondHalfDuration = animEnd - opts . splitTime ;
1449+ const addResult = addAnimationToScript ( result , {
1450+ targetSelector : newSelector ,
1451+ method : "fromTo" ,
1452+ position : newElementStart ,
1453+ duration : secondHalfDuration ,
1454+ properties : { ...anim . properties } ,
1455+ fromProperties : { ...midProps } ,
1456+ ease : anim . ease ,
1457+ extras : anim . extras ,
1458+ } ) ;
1459+ result = addResult . script ;
1460+
1461+ for ( const [ k , v ] of Object . entries ( midProps ) ) {
1462+ inheritedProps [ k ] = v ;
1463+ }
1464+ }
1465+
1466+ if ( Object . keys ( inheritedProps ) . length > 0 ) {
1467+ result = insertInheritedStateSetInScript ( result , newSelector , newElementStart , inheritedProps ) ;
1468+ }
1469+
1470+ return { script : result , skippedSelectors } ;
1471+ }
0 commit comments