@widgetic/canvas 0.5.20 → 0.5.22

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # This is a regular Widgetic Frontend project created with SvelteKit.
2
2
 
3
+ > **Internal building block.** This package is part of Widgetic's own apps and is not a supported integration surface. Partners: use [`@widgetic/ui-sdk`](https://www.npmjs.com/package/@widgetic/ui-sdk) instead.
4
+
3
5
  Everything you need to build a Svelte project, powered by [`sv`](https://github.com/sveltejs/cli).
4
6
 
5
7
  ## Creating a project
@@ -216,6 +216,10 @@
216
216
  // Lower = more frequent saves, higher = less API pressure. Default: 3000ms.
217
217
  export let autosaveDelayMs: number = 3000;
218
218
 
219
+ // While set, a due autosave waits another delay instead of serializing.
220
+ // Serializing during code generation blocks the page thread for the whole save.
221
+ export let suspendAutosave: boolean = false;
222
+
219
223
  // Show a save button in the bottom-right corner of the canvas.
220
224
  // Displays a dirty indicator (●) when unsaved changes exist.
221
225
  export let showSaveButton: boolean = false;
@@ -306,6 +310,12 @@
306
310
  * suffixes below encode that lifecycle contract (init → teardown).
307
311
  */
308
312
  let canvas: Canvas | null = null;
313
+ // One overlay move per turn. Hiding the Fabric image asks for another paint;
314
+ // that paint must not move the overlay again, or the turn never ends.
315
+ let overlaySyncQueued = false;
316
+ let applyingOverlaySync = false;
317
+ let ignoreOverlayAfterRender = false;
318
+ let overlayPaintAfterSync = false;
309
319
 
310
320
  // The language of everything on the canvas: the toolbar, the panels and the menu read their words from it, and the
311
321
  // shapes read the words they draw onto the canvas itself (see i18n/shapeWords.ts).
@@ -1423,6 +1433,33 @@
1423
1433
  }
1424
1434
  }
1425
1435
 
1436
+ /**
1437
+ * Move the live iframes on a later frame, at most once per turn.
1438
+ * A paint that this move itself requested is ignored, so after:render
1439
+ * cannot call back into the move before the browser gets another turn.
1440
+ */
1441
+ function queueWidgetPreviewOverlaySync(): void {
1442
+ if (!onAfterRender || overlaySyncQueued || applyingOverlaySync) return;
1443
+ overlaySyncQueued = true;
1444
+ requestAnimationFrame(() => {
1445
+ overlaySyncQueued = false;
1446
+ if (!onAfterRender || applyingOverlaySync) return;
1447
+ applyingOverlaySync = true;
1448
+ try {
1449
+ onAfterRender();
1450
+ } catch (err) {
1451
+ canvasWarn('Canvas: onAfterRender failed', err);
1452
+ } finally {
1453
+ applyingOverlaySync = false;
1454
+ }
1455
+ if (overlayPaintAfterSync && canvas) {
1456
+ overlayPaintAfterSync = false;
1457
+ ignoreOverlayAfterRender = true;
1458
+ canvas.requestRenderAll();
1459
+ }
1460
+ });
1461
+ }
1462
+
1426
1463
  /**
1427
1464
  * Hide (or restore) the Fabric screenshot under a live HTML WidgetShape overlay
1428
1465
  * so the old image does not ghost in the iframe padding.
@@ -1439,6 +1476,13 @@
1439
1476
  const nextHidden = !!hidden;
1440
1477
  if ((obj as any)._hideImageForLivePreview === nextHidden) return;
1441
1478
  (obj as any)._hideImageForLivePreview = nextHidden;
1479
+ (obj as any).dirty = true;
1480
+ // The overlay sync runs because a paint just finished. Painting again
1481
+ // from that same turn fires after:render, which moves the overlay again.
1482
+ if (applyingOverlaySync) {
1483
+ overlayPaintAfterSync = true;
1484
+ return;
1485
+ }
1442
1486
  canvas.requestRenderAll();
1443
1487
  canvasLog('Canvas: setWidgetShapeLivePreviewHiddenImage', widgetId.substring(0, 8), nextHidden);
1444
1488
  }
@@ -2496,15 +2540,9 @@
2496
2540
  }
2497
2541
  });
2498
2542
 
2499
- // Listen for scaling updates during drag
2500
- const flushWidgetPreviewOverlay = () => {
2501
- if (!onAfterRender) return;
2502
- try {
2503
- onAfterRender();
2504
- } catch (err) {
2505
- canvasWarn('Canvas: onAfterRender failed', err);
2506
- }
2507
- };
2543
+ // Listen for scaling updates during drag.
2544
+ // The overlay sync lives in queueWidgetPreviewOverlaySync, outside
2545
+ // this handler, so a paint started by the sync cannot re-enter it.
2508
2546
 
2509
2547
  canvas.on('object:scaling', (opt: any) => {
2510
2548
  isCurrentlyScaling = true;
@@ -2581,11 +2619,14 @@
2581
2619
  currentScalingCorner = null;
2582
2620
  });
2583
2621
 
2584
- // Also reset on mouse:up in case object:scaled doesn't fire
2622
+ // Also reset on mouse:up in case object:scaled doesn't fire.
2623
+ // Clear the flag in this turn. A deferred clear left after:render
2624
+ // flushing the overlay synchronously, and that flush never returned.
2585
2625
  canvas.on('mouse:up', () => {
2586
- // Small delay to allow object:scaled to process first
2626
+ const wasScaling = isCurrentlyScaling;
2627
+ isCurrentlyScaling = false;
2587
2628
  setTimeout(() => {
2588
- if (isCurrentlyScaling) {
2629
+ if (wasScaling) {
2589
2630
  // Clean up _originalCorners on the target
2590
2631
  if (scalingTarget?._originalCorners) {
2591
2632
  delete scalingTarget._originalCorners;
@@ -3197,7 +3238,7 @@
3197
3238
  // object:modified fires once at mouseup — ideal for undo snapshots.
3198
3239
  // Also fires after arrow-key nudges.
3199
3240
  canvas.on('object:modified', () => {
3200
- flushWidgetPreviewOverlay();
3241
+ queueWidgetPreviewOverlaySync();
3201
3242
  takeHistorySnapshot();
3202
3243
  });
3203
3244
 
@@ -5012,20 +5053,15 @@
5012
5053
  canvas.on('selection:updated', handleSelectionUpdated);
5013
5054
  canvas.on('selection:cleared', handleSelectionCleared);
5014
5055
 
5015
- // Live HTML overlays (WidgetShape iframes) — one callback per animation frame max
5016
- let afterRenderRaf = 0;
5056
+ // Live HTML overlays (WidgetShape iframes). Never in this paint's turn.
5017
5057
  canvas.on('after:render', () => {
5018
5058
  if (!onAfterRender) return;
5019
- // While the pointer is resizing, the iframe has to match this paint.
5020
- if (isCurrentlyScaling) {
5021
- flushWidgetPreviewOverlay();
5059
+ if (ignoreOverlayAfterRender) {
5060
+ ignoreOverlayAfterRender = false;
5022
5061
  return;
5023
5062
  }
5024
- if (afterRenderRaf) return;
5025
- afterRenderRaf = requestAnimationFrame(() => {
5026
- afterRenderRaf = 0;
5027
- flushWidgetPreviewOverlay();
5028
- });
5063
+ if (applyingOverlaySync) return;
5064
+ queueWidgetPreviewOverlaySync();
5029
5065
  });
5030
5066
 
5031
5067
  // Double-click handler:
@@ -5103,7 +5139,7 @@
5103
5139
  x: opt.e?.clientX ?? 0,
5104
5140
  y: opt.e?.clientY ?? 0,
5105
5141
  target,
5106
- scene: canvas.getPointer(opt.e),
5142
+ scene: canvas.getScenePoint(opt.e),
5107
5143
  };
5108
5144
  } else {
5109
5145
  videoPointerDown = null;
@@ -7020,7 +7056,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7020
7056
  pointerDownHandler: (event: any) => {
7021
7057
  if (!canvas || !pendingLibraryShapePath || !pendingLibraryShapeKey) return;
7022
7058
 
7023
- const pointer = canvas.getPointer(event.e);
7059
+ const pointer = canvas.getScenePoint(event.e);
7024
7060
  drawingStartPoint = { x: pointer.x, y: pointer.y };
7025
7061
  previewOverlayObjects = [];
7026
7062
 
@@ -7054,7 +7090,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7054
7090
  pointerMoveHandler: (event: any) => {
7055
7091
  if (!canvas || !drawingStartPoint || !previewObject) return;
7056
7092
 
7057
- const pointer = canvas.getPointer(event.e);
7093
+ const pointer = canvas.getScenePoint(event.e);
7058
7094
  const shiftHeld = Boolean(event.e?.shiftKey) || isShiftModifierPressed;
7059
7095
  const { lockAspectRatio } = getLibraryShapeMeta(pendingLibraryShapeKey ?? '');
7060
7096
 
@@ -7096,7 +7132,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7096
7132
  return;
7097
7133
  }
7098
7134
 
7099
- const pointer = canvas.getPointer(event.e);
7135
+ const pointer = canvas.getScenePoint(event.e);
7100
7136
  const shiftHeld = Boolean(event.e?.shiftKey) || isShiftModifierPressed;
7101
7137
  const { lockAspectRatio, cornerRadiusMode } = getLibraryShapeMeta(pendingLibraryShapeKey);
7102
7138
 
@@ -7446,7 +7482,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7446
7482
  // Invalidate any pending deferred revert from previous interactions
7447
7483
  ++_revertGeneration;
7448
7484
 
7449
- const pointer = canvas.getPointer(event.e);
7485
+ const pointer = canvas.getScenePoint(event.e);
7450
7486
  drawingStartPoint = { x: pointer.x, y: pointer.y };
7451
7487
  // Don't create preview yet — defer to first pointerMove
7452
7488
  // that exceeds MIN_DRAW_DISTANCE to avoid flicker on quick click.
@@ -7457,7 +7493,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7457
7493
  return;
7458
7494
  }
7459
7495
 
7460
- const pointer = canvas.getPointer(event.e);
7496
+ const pointer = canvas.getScenePoint(event.e);
7461
7497
 
7462
7498
  const width = Math.abs(pointer.x - drawingStartPoint.x);
7463
7499
  const height = Math.abs(pointer.y - drawingStartPoint.y);
@@ -7517,7 +7553,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7517
7553
  return;
7518
7554
  }
7519
7555
 
7520
- const pointer = canvas.getPointer(event.e);
7556
+ const pointer = canvas.getScenePoint(event.e);
7521
7557
 
7522
7558
  // Calculate final dimensions based on drag.
7523
7559
  // On quick click (no drag), use the estimated placeholder width
@@ -7598,7 +7634,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7598
7634
  return;
7599
7635
  }
7600
7636
 
7601
- const pointer = canvas.getPointer(event.e);
7637
+ const pointer = canvas.getScenePoint(event.e);
7602
7638
  drawingStartPoint = { x: pointer.x, y: pointer.y };
7603
7639
  previewOverlayObjects = [];
7604
7640
 
@@ -7721,7 +7757,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7721
7757
  return;
7722
7758
  }
7723
7759
 
7724
- const pointer = canvas.getPointer(event.e);
7760
+ const pointer = canvas.getScenePoint(event.e);
7725
7761
  const shiftFromEvent = Boolean(event.e?.shiftKey);
7726
7762
  const bounds = applyShapePreviewForPointer(pointer, shapeType, shiftFromEvent || isShiftModifierPressed);
7727
7763
  if (!bounds) {
@@ -7739,7 +7775,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7739
7775
  return;
7740
7776
  }
7741
7777
 
7742
- const pointer = canvas.getPointer(event.e);
7778
+ const pointer = canvas.getScenePoint(event.e);
7743
7779
  const shiftFinal = Boolean(event.e?.shiftKey) || isShiftModifierPressed;
7744
7780
 
7745
7781
  if (shapeType === 'arrow') {
@@ -7897,7 +7933,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7897
7933
  return;
7898
7934
  }
7899
7935
 
7900
- const pointer = canvas.getPointer(event.e);
7936
+ const pointer = canvas.getScenePoint(event.e);
7901
7937
  drawingStartPoint = { x: pointer.x, y: pointer.y };
7902
7938
 
7903
7939
  try {
@@ -7930,7 +7966,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7930
7966
  return;
7931
7967
  }
7932
7968
 
7933
- const pointer = canvas.getPointer(event.e);
7969
+ const pointer = canvas.getScenePoint(event.e);
7934
7970
  const bounds = applyShapePreviewForPointer(pointer, 'rect', Boolean(event.e?.shiftKey));
7935
7971
  if (!bounds) {
7936
7972
  return;
@@ -7947,7 +7983,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
7947
7983
  return;
7948
7984
  }
7949
7985
 
7950
- const pointer = canvas.getPointer(event.e);
7986
+ const pointer = canvas.getScenePoint(event.e);
7951
7987
  const bounds =
7952
7988
  applyShapePreviewForPointer(pointer, 'rect', Boolean(event.e?.shiftKey)) ?? lastAppliedBounds;
7953
7989
 
@@ -8102,6 +8138,10 @@ function checkAndAddNewShapeToFrame(shape: any) {
8102
8138
  if (!obj || obj._allowBodyDrag === allowed) return;
8103
8139
  obj._allowBodyDrag = allowed;
8104
8140
  lockWidgetShapeBodyDrag(obj);
8141
+ if (applyingOverlaySync) {
8142
+ overlayPaintAfterSync = true;
8143
+ return;
8144
+ }
8105
8145
  canvas.requestRenderAll();
8106
8146
  }
8107
8147
 
@@ -8176,8 +8216,9 @@ function checkAndAddNewShapeToFrame(shape: any) {
8176
8216
  function setHoveredWidgetShape(widgetId: string | null): void {
8177
8217
  if (hoveredWidgetShapeId === widgetId) return;
8178
8218
  hoveredWidgetShapeId = widgetId;
8179
- // Overlays sync on Fabric after:render; hover does not repaint, so force one.
8180
- canvas?.requestRenderAll();
8219
+ // Hover only changes the HTML bar. A canvas paint here moved the iframe
8220
+ // in the paint that the move itself had requested.
8221
+ queueWidgetPreviewOverlaySync();
8181
8222
  }
8182
8223
 
8183
8224
  export function selectWidgetShapeById(widgetId: string): boolean {
@@ -8499,7 +8540,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8499
8540
  return;
8500
8541
  }
8501
8542
 
8502
- const pointer = canvas.getPointer(event.e);
8543
+ const pointer = canvas.getScenePoint(event.e);
8503
8544
  drawingStartPoint = { x: pointer.x, y: pointer.y };
8504
8545
 
8505
8546
  try {
@@ -8534,7 +8575,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8534
8575
  return;
8535
8576
  }
8536
8577
 
8537
- const pointer = canvas.getPointer(event.e);
8578
+ const pointer = canvas.getScenePoint(event.e);
8538
8579
  const bounds = applyShapePreviewForPointer(pointer, 'rect', Boolean(event.e?.shiftKey));
8539
8580
  if (!bounds) {
8540
8581
  return;
@@ -8551,7 +8592,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
8551
8592
  return;
8552
8593
  }
8553
8594
 
8554
- const pointer = canvas.getPointer(event.e);
8595
+ const pointer = canvas.getScenePoint(event.e);
8555
8596
  const bounds =
8556
8597
  applyShapePreviewForPointer(pointer, 'rect', Boolean(event.e?.shiftKey)) ?? lastAppliedBounds;
8557
8598
 
@@ -9423,6 +9464,16 @@ function checkAndAddNewShapeToFrame(shape: any) {
9423
9464
  if (_autoSaveTimer) clearTimeout(_autoSaveTimer);
9424
9465
  _autoSaveTimer = setTimeout(() => {
9425
9466
  _autoSaveTimer = null;
9467
+ if (suspendAutosave) {
9468
+ if (import.meta.env.DEV) {
9469
+ fetch('http://localhost:3000/health?probe=autosave&status=deferred').catch(() => {});
9470
+ }
9471
+ scheduleAutoSave();
9472
+ return;
9473
+ }
9474
+ if (import.meta.env.DEV) {
9475
+ fetch('http://localhost:3000/health?probe=autosave&status=run').catch(() => {});
9476
+ }
9426
9477
  const json = saveCanvas();
9427
9478
  onAutoSave?.(json);
9428
9479
  _hasUnsavedChanges = false;
@@ -31,6 +31,7 @@ declare const __propDef: {
31
31
  onBeforeUnmount?: ((json: string | null) => void) | null;
32
32
  onAutoSave?: ((json: string | null) => void) | null;
33
33
  autosaveDelayMs?: number;
34
+ suspendAutosave?: boolean;
34
35
  showSaveButton?: boolean;
35
36
  onCanvasDirty?: (() => void) | null;
36
37
  onCanvasCleared?: (() => void) | null;
@@ -28,10 +28,12 @@ export function getWidgetPreviewGeometry(object, canvas, canvasElement) {
28
28
  // and finish smaller or larger than the shape.
29
29
  const halfWidth = (object.width || 1) / 2;
30
30
  const halfHeight = (object.height || 1) / 2;
31
+ /** @param {number} x @param {number} y */
31
32
  const scenePoint = (x, y) => ({
32
33
  x: sceneMatrix[0] * x + sceneMatrix[2] * y + sceneMatrix[4],
33
34
  y: sceneMatrix[1] * x + sceneMatrix[3] * y + sceneMatrix[5],
34
35
  });
36
+ /** @param {{x: number, y: number}} point */
35
37
  const project = (point) => ({
36
38
  x: bounds.left + (viewport[0] * point.x + viewport[2] * point.y + viewport[4]) * cssScaleX,
37
39
  y: bounds.top + (viewport[1] * point.x + viewport[3] * point.y + viewport[5]) * cssScaleY,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/canvas",
3
- "version": "0.5.20",
3
+ "version": "0.5.22",
4
4
  "type": "module",
5
5
  "svelte": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -89,7 +89,7 @@
89
89
  },
90
90
  "dependencies": {
91
91
  "@tailwindcss/typography": "^0.5.19",
92
- "fabric": "^6.6.5",
92
+ "fabric": "^7.4.0",
93
93
  "tailwindcss-animate": "^1.0.7"
94
94
  },
95
95
  "overrides": {