@widgetic/canvas 0.5.16 → 0.5.17

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({
@@ -9221,13 +9243,16 @@ function checkAndAddNewShapeToFrame(shape: any) {
9221
9243
  * object state, not the camera position.
9222
9244
  */
9223
9245
  function captureHistorySnapshot(): string | null {
9224
- if (!canvas) return null;
9246
+ if (!canvas || isCapturingHistory) return null;
9247
+ isCapturingHistory = true;
9225
9248
  try {
9226
9249
  const json = safelySerializeCanvas(canvas, CANVAS_CUSTOM_PROPS);
9227
9250
  return JSON.stringify(json);
9228
9251
  } catch (err) {
9229
9252
  console.error('Canvas: captureHistorySnapshot failed', err);
9230
9253
  return null;
9254
+ } finally {
9255
+ isCapturingHistory = false;
9231
9256
  }
9232
9257
  }
9233
9258
 
@@ -9239,7 +9264,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
9239
9264
  */
9240
9265
  let _autoSaveTimer: ReturnType<typeof setTimeout> | null = null;
9241
9266
  function takeHistorySnapshot(silent: boolean = false) {
9242
- if (isRestoringHistory || !canvas) return;
9267
+ if (isRestoringHistory || isCapturingHistory || !canvas) return;
9243
9268
  const snapshot = captureHistorySnapshot();
9244
9269
  if (!snapshot) return;
9245
9270
  // 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,33 @@ 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
+ const dimensions = typeof object._getTransformedDimensions === 'function'
27
+ ? object._getTransformedDimensions()
28
+ : {
29
+ x: (object.width || 1) * (object.scaleX || 1),
30
+ y: (object.height || 1) * (object.scaleY || 1),
31
+ };
32
+ const halfWidth = dimensions.x / 2;
33
+ const halfHeight = dimensions.y / 2;
34
+ const scenePoint = (x, y) => ({
35
+ x: sceneMatrix[0] * x + sceneMatrix[2] * y + sceneMatrix[4],
36
+ y: sceneMatrix[1] * x + sceneMatrix[3] * y + sceneMatrix[5],
37
+ });
25
38
  const project = (point) => ({
26
39
  x: bounds.left + (viewport[0] * point.x + viewport[2] * point.y + viewport[4]) * cssScaleX,
27
40
  y: bounds.top + (viewport[1] * point.x + viewport[3] * point.y + viewport[5]) * cssScaleY,
28
41
  });
29
- const tl = project(scene.tl);
30
- const tr = project(scene.tr);
31
- const bl = project(scene.bl);
32
- const br = project(scene.br);
42
+ const tl = project(scenePoint(-halfWidth, -halfHeight));
43
+ const tr = project(scenePoint(halfWidth, -halfHeight));
44
+ const bl = project(scenePoint(-halfWidth, halfHeight));
45
+ const br = project(scenePoint(halfWidth, halfHeight));
33
46
  const xs = [tl.x, tr.x, bl.x, br.x];
34
47
  const ys = [tl.y, tr.y, bl.y, br.y];
35
48
  const left = Math.min(...xs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/canvas",
3
- "version": "0.5.16",
3
+ "version": "0.5.17",
4
4
  "type": "module",
5
5
  "svelte": "./dist/index.js",
6
6
  "module": "./dist/index.js",