@widgetic/canvas 0.5.15 → 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({
@@ -7946,13 +7968,26 @@ function checkAndAddNewShapeToFrame(shape: any) {
7946
7968
  */
7947
7969
  function lockWidgetShapeBodyDrag(obj: any): void {
7948
7970
  if (!obj?._isWidgetImage) return;
7971
+ // A live iframe owns the body so the page inside stays clickable.
7972
+ // Without that iframe the shape is an ordinary object and must move.
7973
+ const locked = obj._allowBodyDrag !== true;
7949
7974
  obj.set({
7950
- lockMovementX: true,
7951
- lockMovementY: true,
7975
+ lockMovementX: locked,
7976
+ lockMovementY: locked,
7952
7977
  });
7953
7978
  obj.dirty = true;
7954
7979
  }
7955
7980
 
7981
+ /** Live overlay present → body stays put. No overlay → the user can drag the shape. */
7982
+ export function setWidgetShapeBodyDragAllowed(widgetId: string, allowed: boolean): void {
7983
+ if (!canvas || !widgetId) return;
7984
+ const obj = canvas.getObjects().find((o: any) => o._isWidgetImage && o._widgetId === widgetId) as any;
7985
+ if (!obj || obj._allowBodyDrag === allowed) return;
7986
+ obj._allowBodyDrag = allowed;
7987
+ lockWidgetShapeBodyDrag(obj);
7988
+ canvas.requestRenderAll();
7989
+ }
7990
+
7956
7991
  /** Translate a WidgetShape by screen-pixel delta (accounts for canvas zoom). */
7957
7992
  export function translateWidgetShapeByScreen(widgetId: string, dxScreen: number, dyScreen: number): boolean {
7958
7993
  if (!canvas || !widgetId) return false;
@@ -9208,13 +9243,16 @@ function checkAndAddNewShapeToFrame(shape: any) {
9208
9243
  * object state, not the camera position.
9209
9244
  */
9210
9245
  function captureHistorySnapshot(): string | null {
9211
- if (!canvas) return null;
9246
+ if (!canvas || isCapturingHistory) return null;
9247
+ isCapturingHistory = true;
9212
9248
  try {
9213
9249
  const json = safelySerializeCanvas(canvas, CANVAS_CUSTOM_PROPS);
9214
9250
  return JSON.stringify(json);
9215
9251
  } catch (err) {
9216
9252
  console.error('Canvas: captureHistorySnapshot failed', err);
9217
9253
  return null;
9254
+ } finally {
9255
+ isCapturingHistory = false;
9218
9256
  }
9219
9257
  }
9220
9258
 
@@ -9226,7 +9264,7 @@ function checkAndAddNewShapeToFrame(shape: any) {
9226
9264
  */
9227
9265
  let _autoSaveTimer: ReturnType<typeof setTimeout> | null = null;
9228
9266
  function takeHistorySnapshot(silent: boolean = false) {
9229
- if (isRestoringHistory || !canvas) return;
9267
+ if (isRestoringHistory || isCapturingHistory || !canvas) return;
9230
9268
  const snapshot = captureHistorySnapshot();
9231
9269
  if (!snapshot) return;
9232
9270
  // Discard any redo entries that are now obsolete
@@ -109,6 +109,7 @@ declare const __propDef: {
109
109
  onSelectFrameTool?: () => void;
110
110
  setPrimaryWidgetShape?: (widgetId: string) => boolean;
111
111
  getPrimaryWidgetId?: () => string | null;
112
+ setWidgetShapeBodyDragAllowed?: (widgetId: string, allowed: boolean) => void;
112
113
  translateWidgetShapeByScreen?: (widgetId: string, dxScreen: number, dyScreen: number) => boolean;
113
114
  commitWidgetShapeTranslate?: (widgetId?: string | null) => void;
114
115
  setWidgetShapeOverlayDragActive?: (active: boolean) => void;
@@ -211,6 +212,7 @@ export default class Canvas extends SvelteComponentTyped<CanvasProps, CanvasEven
211
212
  get onSelectFrameTool(): () => void;
212
213
  get setPrimaryWidgetShape(): (widgetId: string) => boolean;
213
214
  get getPrimaryWidgetId(): () => string | null;
215
+ get setWidgetShapeBodyDragAllowed(): (widgetId: string, allowed: boolean) => void;
214
216
  get translateWidgetShapeByScreen(): (widgetId: string, dxScreen: number, dyScreen: number) => boolean;
215
217
  get commitWidgetShapeTranslate(): (widgetId?: string | null) => void;
216
218
  get setWidgetShapeOverlayDragActive(): (active: boolean) => void;
@@ -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.15",
3
+ "version": "0.5.17",
4
4
  "type": "module",
5
5
  "svelte": "./dist/index.js",
6
6
  "module": "./dist/index.js",