@hyperframes/parsers 0.7.44 → 0.7.46

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.
@@ -1253,6 +1253,11 @@ function findInsertionPoint(parsed) {
1253
1253
  const tlDecl = findTimelineDeclarationStatement(parsed.ast, parsed.timelineVar);
1254
1254
  return tlDecl?.end ?? parsed.ast.end;
1255
1255
  }
1256
+ function findGlobalSetInsertionPoint(parsed, script) {
1257
+ const tlDecl = findTimelineDeclarationStatement(parsed.ast, parsed.timelineVar);
1258
+ if (!tlDecl) return null;
1259
+ return script.lastIndexOf("\n", tlDecl.start) + 1;
1260
+ }
1256
1261
  function updateAnimationInScript(script, animationId, updates) {
1257
1262
  if (!Object.keys(updates).length) return script;
1258
1263
  const parsed = parseGsapScriptAcornForWrite(script);
@@ -1301,6 +1306,16 @@ function updateAnimationInScript(script, animationId, updates) {
1301
1306
  if (updates.position !== void 0) {
1302
1307
  overwritePosition(ms, call, updates.position);
1303
1308
  }
1309
+ if (target.animation.method === "set" && target.animation.global) {
1310
+ const globalSetPoint = findGlobalSetInsertionPoint(parsed, script);
1311
+ const exprStmt = findEnclosingExpressionStatement(call.ancestors);
1312
+ if (globalSetPoint !== null && exprStmt && exprStmt.start > globalSetPoint) {
1313
+ const lineStart = script.lastIndexOf("\n", exprStmt.start) + 1;
1314
+ const moveStart = /^\s*$/.test(script.slice(lineStart, exprStmt.start)) ? lineStart : exprStmt.start;
1315
+ const moveEnd = exprStmt.end < script.length && script[exprStmt.end] === "\n" ? exprStmt.end + 1 : exprStmt.end;
1316
+ ms.move(moveStart, moveEnd, globalSetPoint);
1317
+ }
1318
+ }
1304
1319
  return ms.toString();
1305
1320
  }
1306
1321
  function overwritePosition(ms, call, position) {
@@ -1350,14 +1365,31 @@ function scalePositionsInScript(script, targetSelector, oldStart, oldDuration, n
1350
1365
  function addAnimationToScript(script, animation) {
1351
1366
  const parsed = parseGsapScriptAcornForWrite(script);
1352
1367
  if (!parsed) return { script, id: "" };
1353
- const insertionPoint = findInsertionPoint(parsed);
1354
- if (insertionPoint === null) return { script, id: "" };
1355
1368
  const ms = new MagicString(script);
1356
1369
  const statementCode = buildTweenStatementCode(parsed.timelineVar, animation);
1357
- ms.appendLeft(insertionPoint, "\n" + statementCode);
1370
+ const globalSetPoint = animation.method === "set" && animation.global ? findGlobalSetInsertionPoint(parsed, script) : null;
1371
+ if (globalSetPoint !== null) {
1372
+ ms.appendLeft(globalSetPoint, statementCode + "\n");
1373
+ } else {
1374
+ const insertionPoint = findInsertionPoint(parsed);
1375
+ if (insertionPoint === null) return { script, id: "" };
1376
+ ms.appendLeft(insertionPoint, "\n" + statementCode);
1377
+ }
1358
1378
  const result = ms.toString();
1359
1379
  const reParsed = parseGsapScriptAcornForWrite(result);
1360
- const newId = reParsed?.located[reParsed.located.length - 1]?.id ?? "";
1380
+ const oldIdCounts = /* @__PURE__ */ new Map();
1381
+ for (const entry of parsed.located) {
1382
+ oldIdCounts.set(entry.id, (oldIdCounts.get(entry.id) ?? 0) + 1);
1383
+ }
1384
+ let newId = "";
1385
+ for (const entry of reParsed?.located ?? []) {
1386
+ const remaining = oldIdCounts.get(entry.id) ?? 0;
1387
+ if (remaining === 0) {
1388
+ newId = entry.id;
1389
+ break;
1390
+ }
1391
+ oldIdCounts.set(entry.id, remaining - 1);
1392
+ }
1361
1393
  return { script: result, id: newId };
1362
1394
  }
1363
1395
  function removeCallFromMagicString(ms, call, script) {