@bian-womp/spark-workbench 0.2.84 → 0.2.85

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.
package/lib/cjs/index.cjs CHANGED
@@ -159,9 +159,11 @@ class InMemoryWorkbench extends AbstractWorkbench {
159
159
  const defNodeIds = new Set(this._def.nodes.map((n) => n.nodeId));
160
160
  const defEdgeIds = new Set(this._def.edges.map((e) => e.id));
161
161
  const filteredPositions = Object.fromEntries(Object.entries(this.positions).filter(([id]) => defNodeIds.has(id)));
162
+ const filteredSizes = Object.fromEntries(Object.entries(this.sizes).filter(([id]) => defNodeIds.has(id)));
162
163
  const filteredNodes = this.selection.nodes.filter((id) => defNodeIds.has(id));
163
164
  const filteredEdges = this.selection.edges.filter((id) => defEdgeIds.has(id));
164
165
  this.positions = filteredPositions;
166
+ this.sizes = filteredSizes;
165
167
  this.selection = { nodes: filteredNodes, edges: filteredEdges };
166
168
  this.emit("graphChanged", { def: this._def });
167
169
  this.refreshValidation();
@@ -218,8 +220,10 @@ class InMemoryWorkbench extends AbstractWorkbench {
218
220
  params: node.params,
219
221
  resolvedHandles: node.resolvedHandles,
220
222
  });
221
- if (node.position)
222
- this.positions[id] = node.position;
223
+ if (options?.position)
224
+ this.positions[id] = options.position;
225
+ if (options?.size)
226
+ this.sizes[id] = options.size;
223
227
  this.emit("graphChanged", {
224
228
  def: this._def,
225
229
  change: {
@@ -237,6 +241,7 @@ class InMemoryWorkbench extends AbstractWorkbench {
237
241
  this._def.nodes = this._def.nodes.filter((n) => n.nodeId !== nodeId);
238
242
  this._def.edges = this._def.edges.filter((e) => e.source.nodeId !== nodeId && e.target.nodeId !== nodeId);
239
243
  delete this.positions[nodeId];
244
+ delete this.sizes[nodeId];
240
245
  delete this.nodeNames[nodeId];
241
246
  this.emit("graphChanged", {
242
247
  def: this._def,
@@ -303,14 +308,16 @@ class InMemoryWorkbench extends AbstractWorkbench {
303
308
  return { ...this.positions };
304
309
  }
305
310
  setSizes(sizes, options) {
311
+ const updatedSizes = { ...this.sizes };
306
312
  for (const [nodeId, size] of Object.entries(sizes)) {
307
313
  if (size) {
308
- this.sizes = { ...this.sizes, [nodeId]: size };
314
+ updatedSizes[nodeId] = size;
309
315
  }
310
316
  else {
311
- this.sizes = lod.omit(this.sizes, nodeId);
317
+ delete updatedSizes[nodeId];
312
318
  }
313
319
  }
320
+ this.sizes = updatedSizes;
314
321
  this.emit("graphUiChanged", {
315
322
  change: { type: "resizeNodes" },
316
323
  ...options,
@@ -468,6 +475,7 @@ class InMemoryWorkbench extends AbstractWorkbench {
468
475
  if (selection.nodes.length === 0)
469
476
  return null;
470
477
  const positions = this.getPositions();
478
+ const sizes = this.getSizes();
471
479
  const selectedNodeSet = new Set(selection.nodes);
472
480
  // Collect nodes to copy
473
481
  const nodesToCopy = this.def.nodes.filter((n) => selectedNodeSet.has(n.nodeId));
@@ -479,15 +487,16 @@ class InMemoryWorkbench extends AbstractWorkbench {
479
487
  let maxX = -Infinity;
480
488
  let maxY = -Infinity;
481
489
  nodesToCopy.forEach((node) => {
482
- const pos = positions[node.nodeId] || { x: 0, y: 0 };
483
- const size = getNodeSize?.(node.nodeId, node.typeId) || {
484
- width: 200,
485
- height: 100,
486
- };
487
- minX = Math.min(minX, pos.x);
488
- minY = Math.min(minY, pos.y);
489
- maxX = Math.max(maxX, pos.x + size.width);
490
- maxY = Math.max(maxY, pos.y + size.height);
490
+ const pos = positions[node.nodeId];
491
+ const size = sizes[node.nodeId] || getNodeSize?.(node.nodeId, node.typeId);
492
+ if (pos) {
493
+ minX = Math.min(minX, pos.x);
494
+ minY = Math.min(minY, pos.y);
495
+ if (size) {
496
+ maxX = Math.max(maxX, pos.x + size.width);
497
+ maxY = Math.max(maxY, pos.y + size.height);
498
+ }
499
+ }
491
500
  });
492
501
  const bounds = { minX, minY, maxX, maxY };
493
502
  const centerX = (bounds.minX + bounds.maxX) / 2;
@@ -497,7 +506,8 @@ class InMemoryWorkbench extends AbstractWorkbench {
497
506
  const allInputs = runner.getInputs(this.def);
498
507
  const selectedEdgeSet = new Set(selection.edges);
499
508
  const copiedNodes = nodesToCopy.map((node) => {
500
- const pos = positions[node.nodeId] || { x: 0, y: 0 };
509
+ const pos = positions[node.nodeId];
510
+ const size = sizes[node.nodeId];
501
511
  // Get all inbound edges for this node
502
512
  const inboundEdges = this.def.edges.filter((e) => e.target.nodeId === node.nodeId);
503
513
  // Build set of handles that have inbound edges
@@ -514,10 +524,13 @@ class InMemoryWorkbench extends AbstractWorkbench {
514
524
  typeId: node.typeId,
515
525
  params: node.params,
516
526
  resolvedHandles: node.resolvedHandles,
517
- position: {
518
- x: pos.x - centerX,
519
- y: pos.y - centerY,
520
- },
527
+ position: pos
528
+ ? {
529
+ x: pos.x - centerX,
530
+ y: pos.y - centerY,
531
+ }
532
+ : undefined,
533
+ size,
521
534
  inputs: inputsToCopy,
522
535
  originalNodeId: node.nodeId,
523
536
  };
@@ -552,6 +565,7 @@ class InMemoryWorkbench extends AbstractWorkbench {
552
565
  if (selection.nodes.length === 0)
553
566
  return [];
554
567
  const positions = this.getPositions();
568
+ const sizes = this.getSizes();
555
569
  const newNodes = [];
556
570
  // Get inputs without bindings (literal values only)
557
571
  const allInputs = runner.getInputs(this.def) || {};
@@ -560,7 +574,8 @@ class InMemoryWorkbench extends AbstractWorkbench {
560
574
  const n = this.def.nodes.find((n) => n.nodeId === nodeId);
561
575
  if (!n)
562
576
  continue;
563
- const pos = positions[nodeId] || { x: 0, y: 0 };
577
+ const pos = positions[nodeId];
578
+ const size = sizes[nodeId];
564
579
  const inboundHandles = new Set(this.def.edges
565
580
  .filter((e) => e.target.nodeId === nodeId)
566
581
  .map((e) => e.target.handle));
@@ -568,10 +583,11 @@ class InMemoryWorkbench extends AbstractWorkbench {
568
583
  const newNodeId = this.addNode({
569
584
  typeId: n.typeId,
570
585
  params: n.params,
571
- position: { x: pos.x + 24, y: pos.y + 24 },
572
586
  resolvedHandles: n.resolvedHandles,
573
587
  }, {
574
588
  inputs: inputsWithoutBindings,
589
+ position: pos ? { x: pos.x + 24, y: pos.y + 24 } : undefined,
590
+ size,
575
591
  copyOutputsFrom: nodeId,
576
592
  dry: true,
577
593
  });
@@ -618,8 +634,10 @@ class InMemoryWorkbench extends AbstractWorkbench {
618
634
  const coerced = await coerceIfNeeded(outputTypeId, inType, unwrap(outputValue));
619
635
  newNodeId = this.addNode({
620
636
  typeId: singleTarget.nodeTypeId,
637
+ }, {
638
+ inputs: { [singleTarget.inputHandle]: coerced },
621
639
  position: { x: pos.x + 180, y: pos.y },
622
- }, { inputs: { [singleTarget.inputHandle]: coerced } });
640
+ });
623
641
  }
624
642
  else if (isArray && arrTarget) {
625
643
  const nodeDesc = registry.nodes.get(arrTarget.nodeTypeId);
@@ -627,8 +645,10 @@ class InMemoryWorkbench extends AbstractWorkbench {
627
645
  const coerced = await coerceIfNeeded(outputTypeId, inType, unwrap(outputValue));
628
646
  newNodeId = this.addNode({
629
647
  typeId: arrTarget.nodeTypeId,
648
+ }, {
630
649
  position: { x: pos.x + 180, y: pos.y },
631
- }, { inputs: { [arrTarget.inputHandle]: coerced } });
650
+ inputs: { [arrTarget.inputHandle]: coerced },
651
+ });
632
652
  }
633
653
  else if (isArray && elemTarget) {
634
654
  const nodeDesc = registry.nodes.get(elemTarget.nodeTypeId);
@@ -644,8 +664,10 @@ class InMemoryWorkbench extends AbstractWorkbench {
644
664
  const row = Math.floor(idx / COLS);
645
665
  newNodeId = this.addNode({
646
666
  typeId: elemTarget.nodeTypeId,
667
+ }, {
647
668
  position: { x: pos.x + (col + 1) * DX, y: pos.y + row * DY },
648
- }, { inputs: { [elemTarget.inputHandle]: coercedItems[idx] } });
669
+ inputs: { [elemTarget.inputHandle]: coercedItems[idx] },
670
+ });
649
671
  }
650
672
  }
651
673
  if (newNodeId) {
@@ -667,7 +689,8 @@ class InMemoryWorkbench extends AbstractWorkbench {
667
689
  const n = this.def.nodes.find((n) => n.nodeId === nodeId);
668
690
  if (!n)
669
691
  return undefined;
670
- const pos = this.getPositions()[nodeId] || { x: 0, y: 0 };
692
+ const pos = this.getPositions()[nodeId];
693
+ const size = this.getSizes()[nodeId];
671
694
  // Get inputs without bindings (literal values only)
672
695
  const allInputs = runner.getInputs(this.def)[nodeId] || {};
673
696
  const inboundHandles = new Set(this.def.edges
@@ -677,10 +700,11 @@ class InMemoryWorkbench extends AbstractWorkbench {
677
700
  const newNodeId = this.addNode({
678
701
  typeId: n.typeId,
679
702
  params: n.params,
680
- position: { x: pos.x + 24, y: pos.y + 24 },
681
703
  resolvedHandles: n.resolvedHandles,
682
704
  }, {
683
705
  inputs: inputsWithoutBindings,
706
+ position: pos ? { x: pos.x + 24, y: pos.y + 24 } : undefined,
707
+ size,
684
708
  copyOutputsFrom: nodeId,
685
709
  dry: true,
686
710
  });
@@ -698,17 +722,19 @@ class InMemoryWorkbench extends AbstractWorkbench {
698
722
  const n = this.def.nodes.find((n) => n.nodeId === nodeId);
699
723
  if (!n)
700
724
  return undefined;
701
- const pos = this.getPositions()[nodeId] || { x: 0, y: 0 };
725
+ const pos = this.getPositions()[nodeId];
726
+ const size = this.getSizes()[nodeId];
702
727
  // Get all inputs (including those with bindings, since edges will be duplicated)
703
728
  const inputs = runner.getInputs(this.def)[nodeId] || {};
704
729
  // Add the duplicated node
705
730
  const newNodeId = this.addNode({
706
731
  typeId: n.typeId,
707
732
  params: n.params,
708
- position: { x: pos.x + 24, y: pos.y + 24 },
709
733
  resolvedHandles: n.resolvedHandles,
710
734
  }, {
711
735
  inputs,
736
+ position: pos ? { x: pos.x + 24, y: pos.y + 24 } : undefined,
737
+ size,
712
738
  copyOutputsFrom: nodeId,
713
739
  dry: true,
714
740
  });
@@ -742,12 +768,15 @@ class InMemoryWorkbench extends AbstractWorkbench {
742
768
  typeId: nodeData.typeId,
743
769
  params: nodeData.params,
744
770
  resolvedHandles: nodeData.resolvedHandles,
745
- position: {
746
- x: nodeData.position.x + center.x,
747
- y: nodeData.position.y + center.y,
748
- },
749
771
  }, {
750
772
  inputs: nodeData.inputs,
773
+ position: nodeData.position
774
+ ? {
775
+ x: nodeData.position.x + center.x,
776
+ y: nodeData.position.y + center.y,
777
+ }
778
+ : undefined,
779
+ size: nodeData.size,
751
780
  copyOutputsFrom: nodeData.originalNodeId,
752
781
  dry: true,
753
782
  });
@@ -5160,7 +5189,7 @@ const WorkbenchCanvas = React.forwardRef(({ showValues, toString, toElement, get
5160
5189
  setNodeMenuOpen(false);
5161
5190
  setSelectionMenuOpen(false);
5162
5191
  };
5163
- const addNodeAt = React.useCallback(async (typeId, opts) => wb.addNode({ typeId, position: opts.position }, { inputs: opts.inputs, commit: true }), [wb]);
5192
+ const addNodeAt = React.useCallback(async (typeId, opts) => wb.addNode({ typeId }, { inputs: opts.inputs, position: opts.position, commit: true }), [wb]);
5164
5193
  const onCloseMenu = React.useCallback(() => {
5165
5194
  setMenuOpen(false);
5166
5195
  }, []);