update-position.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { delay, FreeLayoutPluginContext, startTween } from '@flowgram.ai/free-layout-editor';
  6. import { PositionGroup, positionGroups } from './position-groups';
  7. const updateNodePosition = async (params: {
  8. ctx: FreeLayoutPluginContext;
  9. positionGroup: PositionGroup;
  10. }) => {
  11. const { ctx, positionGroup } = params;
  12. return new Promise<void>((resolve) => {
  13. startTween({
  14. from: { d: 0 },
  15. to: { d: 100 },
  16. duration: 1000,
  17. onUpdate: (v) => {
  18. ctx.document.getAllNodes().forEach((node) => {
  19. const { transform } = node.transform;
  20. const targetPosition = positionGroup[node.id] ?? {
  21. x: transform.position.x,
  22. y: transform.position.y,
  23. };
  24. transform.update({
  25. position: {
  26. x: transform.position.x + ((targetPosition.x - transform.position.x) * v.d) / 100,
  27. y: transform.position.y + ((targetPosition.y - transform.position.y) * v.d) / 100,
  28. },
  29. });
  30. });
  31. },
  32. onComplete: () => {
  33. resolve();
  34. },
  35. });
  36. });
  37. };
  38. export const updatePosition = async (ctx: FreeLayoutPluginContext) => {
  39. // Cycle through position groups every 2 seconds
  40. let currentGroupIndex = 0;
  41. // Use while loop instead of recursion to avoid stack overflow
  42. while (true) {
  43. // Wait for 2 seconds before next update
  44. await delay(2000);
  45. // Update to current position group
  46. await updateNodePosition({
  47. ctx,
  48. positionGroup: positionGroups[currentGroupIndex],
  49. });
  50. // Move to next position group (cycle back to 0 when reaching the end)
  51. currentGroupIndex = (currentGroupIndex + 1) % positionGroups.length;
  52. }
  53. };