@widgetic/canvas 0.5.16 → 0.5.18

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.
@@ -381,6 +381,9 @@
381
381
  let historyIndex = -1;
382
382
  /** Set to true while loading a history snapshot so events don't push new entries. */
383
383
  let isRestoringHistory = false;
384
+ // discardActiveObject during a snapshot fires object:modified, which takes
385
+ // another snapshot. Without this, that loop overflows the stack.
386
+ let isCapturingHistory = false;
384
387
 
385
388
  /** Whether there is a prior state to undo to. */
386
389
  $: canUndo = historyIndex > 0;
@@ -1257,11 +1260,30 @@
1257
1260
  * Screen-space boxes for every WidgetShape currently on the canvas.
1258
1261
  * Used by the host to sync live iframe overlays (W3).
1259
1262
  */
1263
+ function widgetShapesForLivePreview(): any[] {
1264
+ const shapes: any[] = [];
1265
+ const seen = new Set<any>();
1266
+ const consider = (obj: any) => {
1267
+ if (!obj || seen.has(obj)) return;
1268
+ seen.add(obj);
1269
+ if (obj._isWidgetImage && obj._widgetId && !obj._widgetOrphaned && obj.visible !== false) {
1270
+ shapes.push(obj);
1271
+ }
1272
+ const children = typeof obj.getObjects === 'function' ? obj.getObjects() : null;
1273
+ if (!children) return;
1274
+ for (const child of children) consider(child);
1275
+ };
1276
+ for (const obj of canvas!.getObjects() as any[]) consider(obj);
1277
+ // A multi-selection takes its shapes off the canvas list.
1278
+ const active = typeof canvas!.getActiveObject === 'function' ? canvas!.getActiveObject() : null;
1279
+ if (active) consider(active);
1280
+ return shapes;
1281
+ }
1282
+
1260
1283
  export function listWidgetShapeScreenRects() {
1261
1284
  if (!canvas || !canvasEl) return [];
1262
1285
  const results = [];
1263
- for (const obj of canvas.getObjects() as any[]) {
1264
- if (!obj?._isWidgetImage || !obj?._widgetId || obj._widgetOrphaned || !obj.visible) continue;
1286
+ for (const obj of widgetShapesForLivePreview()) {
1265
1287
  const geometry = getWidgetPreviewGeometry(obj, canvas, canvasEl);
1266
1288
  if (!geometry || !geometry.matrix.every(Number.isFinite) || geometry.width < 4 || geometry.height < 4) continue;
1267
1289
  results.push({
@@ -2405,6 +2427,15 @@
2405
2427
  });
2406
2428
 
2407
2429
  // Listen for scaling updates during drag
2430
+ const flushWidgetPreviewOverlay = () => {
2431
+ if (!onAfterRender) return;
2432
+ try {
2433
+ onAfterRender();
2434
+ } catch (err) {
2435
+ canvasWarn('Canvas: onAfterRender failed', err);
2436
+ }
2437
+ };
2438
+
2408
2439
  canvas.on('object:scaling', (opt: any) => {
2409
2440
  isCurrentlyScaling = true;
2410
2441
  scalingTarget = opt.target;
@@ -3096,6 +3127,7 @@
3096
3127
  // object:modified fires once at mouseup — ideal for undo snapshots.
3097
3128
  // Also fires after arrow-key nudges.
3098
3129
  canvas.on('object:modified', () => {
3130
+ flushWidgetPreviewOverlay();
3099
3131
  takeHistorySnapshot();
3100
3132
  });
3101
3133
 
@@ -4882,14 +4914,15 @@
4882
4914
  let afterRenderRaf = 0;
4883
4915
  canvas.on('after:render', () => {
4884
4916
  if (!onAfterRender) return;
4917
+ // While the pointer is resizing, the iframe has to match this paint.
4918
+ if (isCurrentlyScaling) {
4919
+ flushWidgetPreviewOverlay();
4920
+ return;
4921
+ }
4885
4922
  if (afterRenderRaf) return;
4886
4923
  afterRenderRaf = requestAnimationFrame(() => {
4887
4924
  afterRenderRaf = 0;
4888
- try {
4889
- onAfterRender?.();
4890
- } catch (err) {
4891
- canvasWarn('Canvas: onAfterRender failed', err);
4892
- }
4925
+ flushWidgetPreviewOverlay();
4893
4926
  });
4894
4927
  });
4895
4928
 
@@ -9221,13 +9254,16 @@ function checkAndAddNewShapeToFrame(shape: any) {
9221
9254
  * object state, not the camera position.
9222
9255
  */
9223
9256
  function captureHistorySnapshot(): string | null {
9224
- if (!canvas) return null;
9257
+ if (!canvas || isCapturingHistory) return null;
9258
+ isCapturingHistory = true;
9225
9259
  try {
9226
9260
  const json = safelySerializeCanvas(canvas, CANVAS_CUSTOM_PROPS);
9227
9261
  return JSON.stringify(json);
9228
9262
  } catch (err) {
9229
9263
  console.error('Canvas: captureHistorySnapshot failed', err);
9230
9264
  return null;
9265
+ } finally {
9266
+ isCapturingHistory = false;
9231
9267
  }
9232
9268
  }
9233
9269
 
@@ -9239,7 +9275,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
9239
9275
  */
9240
9276
  let _autoSaveTimer: ReturnType<typeof setTimeout> | null = null;
9241
9277
  function takeHistorySnapshot(silent: boolean = false) {
9242
- if (isRestoringHistory || !canvas) return;
9278
+ if (isRestoringHistory || isCapturingHistory || !canvas) return;
9243
9279
  const snapshot = captureHistorySnapshot();
9244
9280
  if (!snapshot) return;
9245
9281
  // Discard any redo entries that are now obsolete
@@ -577,7 +577,7 @@
577
577
  {#if onRefreshScreenshot && widgetId}
578
578
  <button
579
579
  class="refresh-screenshot-btn"
580
- title="Refresh widget preview"
580
+ title="Refresh preview image"
581
581
  onclick={() => onRefreshScreenshot && widgetId && onRefreshScreenshot(widgetId)}
582
582
  >
583
583
  <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
@@ -585,7 +585,7 @@
585
585
  <polyline points="1 20 1 14 7 14"/>
586
586
  <path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/>
587
587
  </svg>
588
- Refresh Widget Preview
588
+ Refresh Preview Image
589
589
  </button>
590
590
  {/if}
591
591
  {#if onSetAsPrimary && widgetId}
@@ -16,20 +16,30 @@ export function getWidgetPreviewGeometry(object, canvas, canvasElement) {
16
16
  const cssScaleX = bounds.width / canvasWidth;
17
17
  const cssScaleY = bounds.height / canvasHeight;
18
18
  const viewport = canvas.viewportTransform || [1, 0, 0, 1, 0, 0];
19
- // aCoords are the painted corners in scene space. The old center-matrix
20
- // path drifted from those corners once a selection drag updated the
21
- // bitmap without the same translation the iframe was using.
22
- object.setCoords?.();
23
- const scene = object.aCoords;
24
- if (!scene?.tl || !scene?.tr || !scene?.bl || !scene?.br) return null;
19
+ // calcACoords() is relative to the parent. Inside a multi-selection that
20
+ // parent is the selection, so those corners are not where the shape is
21
+ // painted. calcTransformMatrix() includes the selection.
22
+ const sceneMatrix = typeof object.calcTransformMatrix === 'function'
23
+ ? object.calcTransformMatrix()
24
+ : null;
25
+ if (!sceneMatrix) return null;
26
+ // Local half-size only. calcTransformMatrix already applies scaleX/scaleY,
27
+ // so feeding it the scaled size makes the iframe lag the selection stroke
28
+ // and finish smaller or larger than the shape.
29
+ const halfWidth = (object.width || 1) / 2;
30
+ const halfHeight = (object.height || 1) / 2;
31
+ const scenePoint = (x, y) => ({
32
+ x: sceneMatrix[0] * x + sceneMatrix[2] * y + sceneMatrix[4],
33
+ y: sceneMatrix[1] * x + sceneMatrix[3] * y + sceneMatrix[5],
34
+ });
25
35
  const project = (point) => ({
26
36
  x: bounds.left + (viewport[0] * point.x + viewport[2] * point.y + viewport[4]) * cssScaleX,
27
37
  y: bounds.top + (viewport[1] * point.x + viewport[3] * point.y + viewport[5]) * cssScaleY,
28
38
  });
29
- const tl = project(scene.tl);
30
- const tr = project(scene.tr);
31
- const bl = project(scene.bl);
32
- const br = project(scene.br);
39
+ const tl = project(scenePoint(-halfWidth, -halfHeight));
40
+ const tr = project(scenePoint(halfWidth, -halfHeight));
41
+ const bl = project(scenePoint(-halfWidth, halfHeight));
42
+ const br = project(scenePoint(halfWidth, halfHeight));
33
43
  const xs = [tl.x, tr.x, bl.x, br.x];
34
44
  const ys = [tl.y, tr.y, bl.y, br.y];
35
45
  const left = Math.min(...xs);
@@ -47,7 +47,7 @@
47
47
  // Base tooltip styles
48
48
  Object.assign(tooltipBodyEl.style, {
49
49
  position: 'fixed',
50
- zIndex: '99999',
50
+ zIndex: '100000010',
51
51
  pointerEvents: 'none',
52
52
  display: 'none',
53
53
  background: '#1f2937',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/canvas",
3
- "version": "0.5.16",
3
+ "version": "0.5.18",
4
4
  "type": "module",
5
5
  "svelte": "./dist/index.js",
6
6
  "module": "./dist/index.js",