@statelyai/flow-dom 3.5.2 → 3.6.0

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.
@@ -103,6 +103,22 @@ declare function renderableIdsEqual(a: RenderableIds, b: RenderableIds): boolean
103
103
  * when called inside a store-subscription selector.
104
104
  */
105
105
  declare function computeRenderableIds(engine: FlowEngine, context: FlowStoreContext, enabled: boolean | undefined, options?: ComputeRenderableIdsOptions): RenderableIds;
106
+ /** A cull and the inputs it was computed for. See `computeSettledRenderableIds`. */
107
+ type SettledRenderableIds = {
108
+ value: RenderableIds;
109
+ graph: FlowStoreContext["graph"];
110
+ enabled: boolean | undefined;
111
+ settledZoom: number | undefined;
112
+ };
113
+ /**
114
+ * `computeRenderableIds` that holds the previous cull during a zoom gesture.
115
+ * While the live zoom differs from `settledZoom`, it returns `previous` as long
116
+ * as the graph, `enabled` and `settledZoom` are unchanged: mounting what a zoom
117
+ * frame reveals costs far more than the transform. Any other case recomputes.
118
+ * Omitting `settledZoom` always recomputes. Pass the returned value back as
119
+ * `previous` on the next call.
120
+ */
121
+ declare function computeSettledRenderableIds(engine: FlowEngine, context: FlowStoreContext, enabled: boolean | undefined, settledZoom: number | undefined, previous: SettledRenderableIds | null, options?: ComputeRenderableIdsOptions): SettledRenderableIds;
106
122
  //#endregion
107
123
  //#region src/render-styles.d.ts
108
124
  type NodePositionStyle = {
@@ -216,7 +232,20 @@ declare function attachContainer(store: FlowStore, element: Element, options?: {
216
232
  type ViewportTransformOptions = {
217
233
  /** Idle delay (ms) before dropping the GPU-layer hint after interaction stops. */
218
234
  promoteIdleMs?: number;
235
+ /**
236
+ * The zoom written to `--flow-zoom` and `--flow-inverse-zoom`, when it
237
+ * should differ from the live zoom. Every element below the viewport
238
+ * inherits these variables, so changing them restyles the whole canvas.
239
+ * Returning a settled zoom during a zoom gesture keeps each frame to a
240
+ * transform change. Defaults to the live zoom.
241
+ */
242
+ getVariableZoom?: () => number | undefined;
219
243
  };
244
+ /**
245
+ * Write `--flow-zoom` and `--flow-inverse-zoom`. Every element below inherits
246
+ * them, so a change restyles the whole canvas; an unchanged value is skipped.
247
+ */
248
+ declare function writeZoomVariables(element: HTMLElement, zoom: number): void;
220
249
  declare function attachViewportTransform(store: FlowStore, element: HTMLElement, options?: ViewportTransformOptions): () => void;
221
250
  type AttachInputOptions = {
222
251
  container: HTMLElement;
@@ -249,4 +278,4 @@ type AttachInputOptions = {
249
278
  };
250
279
  declare function attachInput(options: AttachInputOptions): () => void;
251
280
  //#endregion
252
- export { AttachInputOptions, type ClipboardTransport, type ComputeRenderableIdsOptions, type EntityBoundsTarget, type EntityEdgeTarget, type EntityGeometryObserver, type EntityGeometryObserverOptions, type EntityRectTarget, FLOW_CONTENT_MIME, type FlowClipboard, type FlowImageExportOptions, type FlowImageExportResult, type FlowView, MeasurementObserver, MeasurementObserverOptions, MeasurementTarget, type NodePositionStyle, type RenderableIds, ResolvedFlowColorMode, type ViewportStyle, ViewportTransformOptions, applyFlowColorMode, attachContainer, attachInput, attachViewportTransform, canvasToClient, clientToCanvas, computeRenderableIds, createEntityGeometryObserver, createFlowClipboard, createFlowView, createMeasurementObserver, exportFlowImage, getAttachedContainer, nodePositionStyle, observeAppearanceBounds, renderableIdsEqual, resolveFlowColorMode, viewportStyle };
281
+ export { AttachInputOptions, type ClipboardTransport, type ComputeRenderableIdsOptions, type EntityBoundsTarget, type EntityEdgeTarget, type EntityGeometryObserver, type EntityGeometryObserverOptions, type EntityRectTarget, FLOW_CONTENT_MIME, type FlowClipboard, type FlowImageExportOptions, type FlowImageExportResult, type FlowView, MeasurementObserver, MeasurementObserverOptions, MeasurementTarget, type NodePositionStyle, type RenderableIds, ResolvedFlowColorMode, type SettledRenderableIds, type ViewportStyle, ViewportTransformOptions, applyFlowColorMode, attachContainer, attachInput, attachViewportTransform, canvasToClient, clientToCanvas, computeRenderableIds, computeSettledRenderableIds, createEntityGeometryObserver, createFlowClipboard, createFlowView, createMeasurementObserver, exportFlowImage, getAttachedContainer, nodePositionStyle, observeAppearanceBounds, renderableIdsEqual, resolveFlowColorMode, viewportStyle, writeZoomVariables };
package/dist/esm/index.js CHANGED
@@ -355,6 +355,23 @@ function computeRenderableIds(engine, context, enabled, options = {}) {
355
355
  edgeIds: new Set(renderable.edges.map((edge) => edge.id))
356
356
  };
357
357
  }
358
+ /**
359
+ * `computeRenderableIds` that holds the previous cull during a zoom gesture.
360
+ * While the live zoom differs from `settledZoom`, it returns `previous` as long
361
+ * as the graph, `enabled` and `settledZoom` are unchanged: mounting what a zoom
362
+ * frame reveals costs far more than the transform. Any other case recomputes.
363
+ * Omitting `settledZoom` always recomputes. Pass the returned value back as
364
+ * `previous` on the next call.
365
+ */
366
+ function computeSettledRenderableIds(engine, context, enabled, settledZoom, previous, options = {}) {
367
+ if (settledZoom !== void 0 && context.viewport.zoom !== settledZoom && previous && previous.graph === context.graph && previous.enabled === enabled && previous.settledZoom === settledZoom) return previous;
368
+ return {
369
+ value: computeRenderableIds(engine, context, enabled, options),
370
+ graph: context.graph,
371
+ enabled,
372
+ settledZoom
373
+ };
374
+ }
358
375
  //#endregion
359
376
  //#region src/render-styles.ts
360
377
  /**
@@ -541,15 +558,15 @@ function createMeasurementObserver(store, options = {}) {
541
558
  let rafId = null;
542
559
  function flush() {
543
560
  rafId = null;
544
- if (pendingMeasurements.length > 0) {
545
- store.trigger.updateMeasurements({ measurements: pendingMeasurements });
546
- pendingMeasurements = [];
547
- }
561
+ const measurements = pendingMeasurements.filter(({ element, registration }) => elementMap.get(element) === registration).map(({ measurement }) => measurement);
562
+ pendingMeasurements = [];
563
+ if (measurements.length > 0) store.trigger.updateMeasurements({ measurements });
548
564
  }
549
565
  function scheduleFlush() {
550
566
  if (rafId === null) rafId = requestAnimationFrame(flush);
551
567
  }
552
- function pushMeasurement(element, target, layoutSize) {
568
+ function pushMeasurement(element, registration, layoutSize) {
569
+ const { target } = registration;
553
570
  const viewport = store.getSnapshot().context.viewport;
554
571
  const zoom = viewport.zoom || 1;
555
572
  const rect = element.getBoundingClientRect();
@@ -565,15 +582,20 @@ function createMeasurementObserver(store, options = {}) {
565
582
  const existing = store.getSnapshot().context.measurements.get(id);
566
583
  if (existing && Math.abs(existing.x - measurement.x) < equalityThreshold && Math.abs(existing.y - measurement.y) < equalityThreshold && Math.abs(existing.width - measurement.width) < equalityThreshold && Math.abs(existing.height - measurement.height) < equalityThreshold) return;
567
584
  pendingMeasurements.push({
568
- id,
569
- rect: measurement
585
+ element,
586
+ registration,
587
+ measurement: {
588
+ id,
589
+ rect: measurement
590
+ }
570
591
  });
571
592
  scheduleFlush();
572
593
  }
573
- const handleResize = (entries) => {
594
+ const handleResize = (entries, source) => {
595
+ if (source !== observer) return;
574
596
  for (const entry of entries) {
575
- const target = elementMap.get(entry.target);
576
- if (!target) continue;
597
+ const registration = elementMap.get(entry.target);
598
+ if (!registration) continue;
577
599
  const box = entry.borderBoxSize?.[0];
578
600
  const layoutSize = box ? {
579
601
  width: box.inlineSize,
@@ -582,7 +604,7 @@ function createMeasurementObserver(store, options = {}) {
582
604
  width: entry.contentRect.width,
583
605
  height: entry.contentRect.height
584
606
  } : void 0;
585
- pushMeasurement(entry.target, target, layoutSize);
607
+ pushMeasurement(entry.target, registration, layoutSize);
586
608
  }
587
609
  };
588
610
  let observer = null;
@@ -593,8 +615,11 @@ function createMeasurementObserver(store, options = {}) {
593
615
  }
594
616
  return {
595
617
  observe(element, target) {
596
- elementMap.set(element, target);
618
+ const replacing = elementMap.has(element);
619
+ const registration = { target };
620
+ elementMap.set(element, registration);
597
621
  getObserver(element).observe(element);
622
+ if (replacing) pushMeasurement(element, registration);
598
623
  },
599
624
  /**
600
625
  * Re-measure an observed element immediately. ResizeObserver only fires
@@ -602,20 +627,19 @@ function createMeasurementObserver(store, options = {}) {
602
627
  * a port moving within its node) need an explicit re-measure.
603
628
  */
604
629
  measure(element) {
605
- const target = elementMap.get(element);
606
- if (target) pushMeasurement(element, target);
630
+ const registration = elementMap.get(element);
631
+ if (registration) pushMeasurement(element, registration);
607
632
  },
608
633
  refresh(targets) {
609
634
  const requested = targets ? new Set(targets) : null;
610
- for (const [element, target] of elementMap) {
635
+ for (const [element, registration] of elementMap) {
636
+ const { target } = registration;
611
637
  const id = "appearanceId" in target ? target.appearanceId : "portName" in target ? `${target.nodeId}:${target.portName}` : "nodeId" in target ? target.nodeId : target.edgeId;
612
638
  if (requested && !requested.has(element) && !requested.has(id)) continue;
613
- pushMeasurement(element, target);
639
+ pushMeasurement(element, registration);
614
640
  }
615
641
  },
616
642
  unobserve(element) {
617
- const target = elementMap.get(element);
618
- if (target && "appearanceId" in target) pendingMeasurements = pendingMeasurements.filter(({ id }) => id !== target.appearanceId);
619
643
  elementMap.delete(element);
620
644
  observer?.unobserve(element);
621
645
  },
@@ -686,6 +710,16 @@ function attachContainer(store, element, options = {}) {
686
710
  observer.disconnect();
687
711
  };
688
712
  }
713
+ /**
714
+ * Write `--flow-zoom` and `--flow-inverse-zoom`. Every element below inherits
715
+ * them, so a change restyles the whole canvas; an unchanged value is skipped.
716
+ */
717
+ function writeZoomVariables(element, zoom) {
718
+ const value = String(zoom);
719
+ if (element.style.getPropertyValue("--flow-zoom") === value) return;
720
+ element.style.setProperty("--flow-zoom", value);
721
+ element.style.setProperty("--flow-inverse-zoom", String(1 / zoom));
722
+ }
689
723
  function attachViewportTransform(store, element, options = {}) {
690
724
  const promoteIdleMs = options.promoteIdleMs ?? 200;
691
725
  let prevViewport = store.getSnapshot().context.viewport;
@@ -693,8 +727,7 @@ function attachViewportTransform(store, element, options = {}) {
693
727
  let idle;
694
728
  const writeTransform = (viewport) => {
695
729
  element.style.transform = `translate(${viewport.x}px, ${viewport.y}px) scale(${viewport.zoom})`;
696
- element.style.setProperty("--flow-zoom", String(viewport.zoom));
697
- element.style.setProperty("--flow-inverse-zoom", String(1 / viewport.zoom));
730
+ writeZoomVariables(element, options.getVariableZoom?.() ?? viewport.zoom);
698
731
  };
699
732
  const promote = () => {
700
733
  element.style.willChange = "transform";
@@ -1130,4 +1163,4 @@ function attachInput(options) {
1130
1163
  };
1131
1164
  }
1132
1165
  //#endregion
1133
- export { FLOW_CONTENT_MIME, applyFlowColorMode, attachContainer, attachInput, attachViewportTransform, canvasToClient, clientToCanvas, computeRenderableIds, createEntityGeometryObserver, createFlowClipboard, createFlowView, createMeasurementObserver, exportFlowImage, getAttachedContainer, nodePositionStyle, observeAppearanceBounds, renderableIdsEqual, resolveFlowColorMode, viewportStyle };
1166
+ export { FLOW_CONTENT_MIME, applyFlowColorMode, attachContainer, attachInput, attachViewportTransform, canvasToClient, clientToCanvas, computeRenderableIds, computeSettledRenderableIds, createEntityGeometryObserver, createFlowClipboard, createFlowView, createMeasurementObserver, exportFlowImage, getAttachedContainer, nodePositionStyle, observeAppearanceBounds, renderableIdsEqual, resolveFlowColorMode, viewportStyle, writeZoomVariables };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statelyai/flow-dom",
3
- "version": "3.5.2",
3
+ "version": "3.6.0",
4
4
  "description": "DOM-specific helpers for @statelyai/flow with core re-exports.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "license": "MIT",
29
29
  "dependencies": {
30
- "@statelyai/flow": "0.13.0"
30
+ "@statelyai/flow": "0.13.2"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@vitest/coverage-v8": "^4.1.0",