@widgetic/canvas 0.5.10 → 0.5.11

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.
@@ -233,6 +233,9 @@
233
233
  /** Host persists canvases.primary_widget_id after Canvas paints the red stroke. */
234
234
  export let onSetPrimaryWidget: ((widgetId: string) => void) | null = null;
235
235
 
236
+ /** Throttled paint hook for HTML overlays (e.g. live WidgetShape iframes). */
237
+ export let onAfterRender: (() => void) | null = null;
238
+
236
239
  // Widget name resolver: given a widgetId, returns the widget name (or null).
237
240
  // Used by PropsPanel to display the linked widget's name.
238
241
  export let getWidgetName: ((widgetId: string) => string | null) | null = null;
@@ -1066,6 +1069,7 @@
1066
1069
  // Upgrade ImageShape → WidgetShape (swaps prototype, sets _isWidgetImage)
1067
1070
  upgradeToWidgetShape(widgetImage);
1068
1071
  clearWidgetShapeDefaultBorder(widgetImage);
1072
+ lockWidgetShapeBodyDrag(widgetImage);
1069
1073
 
1070
1074
  const newObjectId = `widget_img_${Date.now()}`;
1071
1075
  widgetImage._objectId = newObjectId;
@@ -1216,6 +1220,40 @@
1216
1220
  };
1217
1221
  }
1218
1222
 
1223
+ /**
1224
+ * Screen-space boxes for every WidgetShape currently on the canvas.
1225
+ * Used by the host to sync live iframe overlays (W3).
1226
+ */
1227
+ export function listWidgetShapeScreenRects(): Array<{
1228
+ widgetId: string;
1229
+ left: number;
1230
+ top: number;
1231
+ width: number;
1232
+ height: number;
1233
+ }> {
1234
+ if (!canvas || !canvasEl) return [];
1235
+ const results: Array<{
1236
+ widgetId: string;
1237
+ left: number;
1238
+ top: number;
1239
+ width: number;
1240
+ height: number;
1241
+ }> = [];
1242
+ for (const obj of canvas.getObjects() as any[]) {
1243
+ if (!obj?._isWidgetImage || !obj?._widgetId || obj._widgetOrphaned) continue;
1244
+ const rect = objectSceneToScreenRect(obj);
1245
+ if (!rect || rect.width < 4 || rect.height < 4) continue;
1246
+ results.push({
1247
+ widgetId: String(obj._widgetId),
1248
+ left: rect.left,
1249
+ top: rect.top,
1250
+ width: rect.width,
1251
+ height: rect.height,
1252
+ });
1253
+ }
1254
+ return results;
1255
+ }
1256
+
1219
1257
  /**
1220
1258
  * Screen-space box of a widget shape plus the canvas element viewport.
1221
1259
  * Used to park Widget Details left/right of the shape.
@@ -1318,6 +1356,7 @@
1318
1356
 
1319
1357
  upgradeToWidgetShape(placeholder);
1320
1358
  clearWidgetShapeDefaultBorder(placeholder);
1359
+ lockWidgetShapeBodyDrag(placeholder);
1321
1360
 
1322
1361
  const newObjectId = `widget_img_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
1323
1362
  placeholder._objectId = newObjectId;
@@ -2416,6 +2455,7 @@
2416
2455
  // Upgrade old Rect-based image placeholders to ImageShape (backward compat)
2417
2456
  upgradeToImageShape(obj);
2418
2457
  if ((obj as any)._isWidgetImage) {
2458
+ lockWidgetShapeBodyDrag(obj);
2419
2459
  if ((obj as any)._isPrimaryWidget) {
2420
2460
  obj.set({
2421
2461
  stroke: PRIMARY_WIDGET_STROKE,
@@ -4750,6 +4790,21 @@
4750
4790
  canvas.on('selection:created', handleSelectionCreated);
4751
4791
  canvas.on('selection:updated', handleSelectionUpdated);
4752
4792
  canvas.on('selection:cleared', handleSelectionCleared);
4793
+
4794
+ // Live HTML overlays (WidgetShape iframes) — one callback per animation frame max
4795
+ let afterRenderRaf = 0;
4796
+ canvas.on('after:render', () => {
4797
+ if (!onAfterRender) return;
4798
+ if (afterRenderRaf) return;
4799
+ afterRenderRaf = requestAnimationFrame(() => {
4800
+ afterRenderRaf = 0;
4801
+ try {
4802
+ onAfterRender?.();
4803
+ } catch (err) {
4804
+ canvasWarn('Canvas: onAfterRender failed', err);
4805
+ }
4806
+ });
4807
+ });
4753
4808
 
4754
4809
  // Double-click handler:
4755
4810
  // - Textbox: enter editing mode with all text selected
@@ -7556,6 +7611,56 @@ function checkAndAddNewShapeToFrame(shape: any) {
7556
7611
  obj.dirty = true;
7557
7612
  }
7558
7613
 
7614
+ /**
7615
+ * WidgetShapes move only via the HTML chrome drag bar (live iframe stays interactive).
7616
+ * Other shapes keep normal Fabric body-drag.
7617
+ */
7618
+ function lockWidgetShapeBodyDrag(obj: any): void {
7619
+ if (!obj?._isWidgetImage) return;
7620
+ obj.set({
7621
+ lockMovementX: true,
7622
+ lockMovementY: true,
7623
+ });
7624
+ obj.dirty = true;
7625
+ }
7626
+
7627
+ /** Translate a WidgetShape by screen-pixel delta (accounts for canvas zoom). */
7628
+ export function translateWidgetShapeByScreen(widgetId: string, dxScreen: number, dyScreen: number): boolean {
7629
+ if (!canvas || !widgetId) return false;
7630
+ const obj = canvas.getObjects().find((o: any) => o._isWidgetImage && o._widgetId === widgetId) as any;
7631
+ if (!obj) return false;
7632
+ const zoom = canvas.getZoom?.() || 1;
7633
+ const dx = dxScreen / zoom;
7634
+ const dy = dyScreen / zoom;
7635
+ // Temporarily unlock so Fabric applies left/top without fighting locks
7636
+ obj.set({
7637
+ lockMovementX: false,
7638
+ lockMovementY: false,
7639
+ left: (obj.left ?? 0) + dx,
7640
+ top: (obj.top ?? 0) + dy,
7641
+ });
7642
+ obj.setCoords?.();
7643
+ lockWidgetShapeBodyDrag(obj);
7644
+ canvas.requestRenderAll();
7645
+ return true;
7646
+ }
7647
+
7648
+ export function selectWidgetShapeById(widgetId: string): boolean {
7649
+ if (!canvas || !widgetId) return false;
7650
+ const obj = canvas.getObjects().find((o: any) => o._isWidgetImage && o._widgetId === widgetId);
7651
+ if (!obj) return false;
7652
+ canvas.setActiveObject(obj);
7653
+ canvas.requestRenderAll();
7654
+ return true;
7655
+ }
7656
+
7657
+ export function getSelectedWidgetShapeId(): string | null {
7658
+ if (!canvas) return null;
7659
+ const active = canvas.getActiveObject() as any;
7660
+ if (active?._isWidgetImage && active?._widgetId) return String(active._widgetId);
7661
+ return null;
7662
+ }
7663
+
7559
7664
  /**
7560
7665
  * Create an WidgetShape object (unified Rect that can display placeholder UI OR an image)
7561
7666
  * This is a custom Rect that:
@@ -41,6 +41,7 @@ declare const __propDef: {
41
41
  onWidgetRename?: ((widgetId: string, newName: string) => void) | null;
42
42
  onRefreshScreenshot?: ((widgetId: string) => void) | null;
43
43
  /** Host persists canvases.primary_widget_id after Canvas paints the red stroke. */ onSetPrimaryWidget?: ((widgetId: string) => void) | null;
44
+ /** Throttled paint hook for HTML overlays (e.g. live WidgetShape iframes). */ onAfterRender?: (() => void) | null;
44
45
  getWidgetName?: ((widgetId: string) => string | null) | null;
45
46
  /** Returns true when widgetId still exists in the host widget list. */ isWidgetKnown?: ((widgetId: string) => boolean) | null;
46
47
  showLogs?: boolean;
@@ -54,6 +55,13 @@ declare const __propDef: {
54
55
  replaceObjectWithWidgetImage?: (objectId: string, widgetId: string, screenshotDataUrl: string) => Promise<string | null>;
55
56
  updateWidgetImage?: (widgetId: string, newScreenshotDataUrl: string) => void;
56
57
  selectWidgetShape?: (widgetId: string) => boolean;
58
+ listWidgetShapeScreenRects?: () => Array<{
59
+ widgetId: string;
60
+ left: number;
61
+ top: number;
62
+ width: number;
63
+ height: number;
64
+ }>;
57
65
  getWidgetShapeScreenRect?: (widgetId: string) => {
58
66
  left: number;
59
67
  top: number;
@@ -93,6 +101,9 @@ declare const __propDef: {
93
101
  onSelectFrameTool?: () => void;
94
102
  setPrimaryWidgetShape?: (widgetId: string) => boolean;
95
103
  getPrimaryWidgetId?: () => string | null;
104
+ translateWidgetShapeByScreen?: (widgetId: string, dxScreen: number, dyScreen: number) => boolean;
105
+ selectWidgetShapeById?: (widgetId: string) => boolean;
106
+ getSelectedWidgetShapeId?: () => string | null;
96
107
  onSelectImageTool?: () => void;
97
108
  onSelectVideoTool?: () => void;
98
109
  onSelectPanTool?: () => void;
@@ -137,6 +148,13 @@ export default class Canvas extends SvelteComponentTyped<CanvasProps, CanvasEven
137
148
  get replaceObjectWithWidgetImage(): (objectId: string, widgetId: string, screenshotDataUrl: string) => Promise<string | null>;
138
149
  get updateWidgetImage(): (widgetId: string, newScreenshotDataUrl: string) => void;
139
150
  get selectWidgetShape(): (widgetId: string) => boolean;
151
+ get listWidgetShapeScreenRects(): () => Array<{
152
+ widgetId: string;
153
+ left: number;
154
+ top: number;
155
+ width: number;
156
+ height: number;
157
+ }>;
140
158
  get getWidgetShapeScreenRect(): (widgetId: string) => {
141
159
  left: number;
142
160
  top: number;
@@ -176,6 +194,9 @@ export default class Canvas extends SvelteComponentTyped<CanvasProps, CanvasEven
176
194
  get onSelectFrameTool(): () => void;
177
195
  get setPrimaryWidgetShape(): (widgetId: string) => boolean;
178
196
  get getPrimaryWidgetId(): () => string | null;
197
+ get translateWidgetShapeByScreen(): (widgetId: string, dxScreen: number, dyScreen: number) => boolean;
198
+ get selectWidgetShapeById(): (widgetId: string) => boolean;
199
+ get getSelectedWidgetShapeId(): () => string | null;
179
200
  get onSelectImageTool(): () => void;
180
201
  get onSelectVideoTool(): () => void;
181
202
  get onSelectPanTool(): () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/canvas",
3
- "version": "0.5.10",
3
+ "version": "0.5.11",
4
4
  "type": "module",
5
5
  "svelte": "./dist/index.js",
6
6
  "module": "./dist/index.js",