@widgetic/canvas 0.5.12 → 0.5.14

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.
@@ -28,7 +28,7 @@
28
28
  MIN_FRAME_WIDTH, MIN_FRAME_HEIGHT, DEFAULT_STROKE, FRAME_HOVER_STROKE
29
29
  } from './shapes/FrameShape';
30
30
  import { ImageShape, IMAGE_SHAPE_CUSTOM_PROPS, upgradeToImageShape } from './shapes/ImageShape';
31
- import { WidgetShape, WIDGET_SHAPE_CUSTOM_PROPS, upgradeToWidgetShape } from './shapes/WidgetShape';
31
+ import { WidgetShape, WIDGET_SHAPE_CUSTOM_PROPS, upgradeToWidgetShape, restoreThumbnailFromStore } from './shapes/WidgetShape';
32
32
  import { SHAPE_LIBRARY, getShapeByKey, generatePathForShape } from './shapes/ShapeLibrary';
33
33
  import { canvasLog, canvasWarn, setCanvasLogsEnabled } from './canvasLogger';
34
34
  import type { WidgetConversionPayload } from './types';
@@ -121,6 +121,8 @@
121
121
  '_widgetId',
122
122
  // Widget Image: marks an image shape as a converted widget placeholder
123
123
  '_isWidgetImage',
124
+ // Widget thumbnail held by the host store instead of inline in the JSON
125
+ '_imageRef',
124
126
  ];
125
127
  const existing = new Set((FabricObject as any).customProperties as string[]);
126
128
  for (const prop of propsToRegister) {
@@ -5706,6 +5708,8 @@
5706
5708
  // decide drag-bar visibility. Hover also selects a WidgetShape that
5707
5709
  // has no live iframe yet (same selection path as click) — pointer
5708
5710
  // then selects the iframe itself via the overlay's focus bridge.
5711
+ canvas.on('mouse:down', () => { canvasPointerInteractionActive = true; });
5712
+ canvas.on('mouse:up', () => { canvasPointerInteractionActive = false; });
5709
5713
  canvas.on('mouse:over', (hoverEvt: any) => {
5710
5714
  const target = hoverEvt?.target as any;
5711
5715
  if (!target?._isWidgetImage || !target._widgetId || target._widgetOrphaned) return;
@@ -7743,15 +7747,31 @@ function checkAndAddNewShapeToFrame(shape: any) {
7743
7747
 
7744
7748
  /** Hover tracking for WidgetShape overlays (W3 drag bar) + hover-select. */
7745
7749
  let hoveredWidgetShapeId: string | null = null;
7750
+ /**
7751
+ * True from `mouse down` on the Fabric canvas until `mouse:up` — covers plain
7752
+ * object drags, pans and FreeTransform. Any hover-select must stay off while
7753
+ * this is set, otherwise dragging one shape across another selects the shape
7754
+ * the pointer happens to pass over.
7755
+ */
7756
+ let canvasPointerInteractionActive = false;
7757
+ /** Hover-select must also yield while the overlay drag bar moves a WidgetShape. */
7758
+ let widgetShapeOverlayDragActive = false;
7746
7759
  /** Suppress hover-select while a Fabric transform (drag/resize/rotate) is active. */
7747
7760
  function isWidgetShapeHoverSelectBlocked(): boolean {
7748
7761
  if (!canvas) return true;
7749
7762
  if (activeTool !== 'select') return true;
7750
7763
  if ((canvas as any)._currentTransform) return true;
7764
+ if (canvasPointerInteractionActive) return true;
7765
+ if (widgetShapeOverlayDragActive) return true;
7751
7766
  if (activeEditingTextbox?.isEditing) return true;
7752
7767
  return false;
7753
7768
  }
7754
7769
 
7770
+ /** Exported for the host overlay: report its chrome drag-bar drags. */
7771
+ export function setWidgetShapeOverlayDragActive(active: boolean): void {
7772
+ widgetShapeOverlayDragActive = active;
7773
+ }
7774
+
7755
7775
  /** Apply hover state change + notify the host overlay layer. */
7756
7776
  function setHoveredWidgetShape(widgetId: string | null): void {
7757
7777
  if (hoveredWidgetShapeId === widgetId) return;
@@ -8869,6 +8889,8 @@ function checkAndAddNewShapeToFrame(shape: any) {
8869
8889
  '_widgetId',
8870
8890
  // Widget Image: converted widget placeholder on canvas
8871
8891
  '_isWidgetImage',
8892
+ // Widget thumbnail kept in the host store, referenced instead of inlined
8893
+ '_imageRef',
8872
8894
  '_groupFill', '_groupStroke', '_groupStrokeWidth',
8873
8895
  '_groupFillOpacity', '_groupStrokeDashArray',
8874
8896
  '_canvasPaintOrder',
@@ -9190,9 +9212,12 @@ function checkAndAddNewShapeToFrame(shape: any) {
9190
9212
  // Light pass: ensure any images without loaded elements are reloaded
9191
9213
  const allObjects = canvas.getObjects();
9192
9214
  const checkUnloadedImages = (obj: any) => {
9193
- if (obj._customType === 'image' || obj._hasImage || obj._imageUrl) {
9215
+ if (obj._customType === 'image' || obj._hasImage || obj._imageUrl || obj._imageRef) {
9194
9216
  if (obj._imageUrl && !obj._imageElement) {
9195
9217
  ImageShape.reloadImage(obj);
9218
+ } else if (!obj._imageUrl && obj._imageRef) {
9219
+ // Thumbnail lives in the host store, not in the JSON
9220
+ void restoreThumbnailFromStore(obj);
9196
9221
  }
9197
9222
  } else if (String(obj.type || '').toLowerCase() === 'group' || isFrameShape(obj)) {
9198
9223
  (obj.getObjects?.() || []).forEach(checkUnloadedImages);
@@ -110,6 +110,7 @@ declare const __propDef: {
110
110
  getPrimaryWidgetId?: () => string | null;
111
111
  translateWidgetShapeByScreen?: (widgetId: string, dxScreen: number, dyScreen: number) => boolean;
112
112
  commitWidgetShapeTranslate?: (widgetId?: string | null) => void;
113
+ setWidgetShapeOverlayDragActive?: (active: boolean) => void;
113
114
  selectWidgetShapeById?: (widgetId: string) => boolean;
114
115
  getSelectedWidgetShapeId?: () => string | null;
115
116
  getHoveredWidgetShapeId?: () => string | null;
@@ -210,6 +211,7 @@ export default class Canvas extends SvelteComponentTyped<CanvasProps, CanvasEven
210
211
  get getPrimaryWidgetId(): () => string | null;
211
212
  get translateWidgetShapeByScreen(): (widgetId: string, dxScreen: number, dyScreen: number) => boolean;
212
213
  get commitWidgetShapeTranslate(): (widgetId?: string | null) => void;
214
+ get setWidgetShapeOverlayDragActive(): (active: boolean) => void;
213
215
  get selectWidgetShapeById(): (widgetId: string) => boolean;
214
216
  get getSelectedWidgetShapeId(): () => string | null;
215
217
  get getHoveredWidgetShapeId(): () => string | null;
@@ -11,3 +11,5 @@ export { default as PanZoomPanel } from './PanZoomPanel.svelte';
11
11
  export { default as PropsPanel } from './props-panel/PropsPanel.svelte';
12
12
  export type { WidgetConversionPayload } from './types';
13
13
  export { canvasLog, canvasWarn, canvasDebug, setCanvasLogsEnabled, isCanvasLogsEnabled } from './canvasLogger';
14
+ export { setShapeImageStore, getShapeImageStore } from './shapes/shapeImageStore';
15
+ export type { ShapeImageStore } from './shapes/shapeImageStore';
@@ -10,3 +10,4 @@ export { default as ContextMenu } from './ContextMenu.svelte';
10
10
  export { default as PanZoomPanel } from './PanZoomPanel.svelte';
11
11
  export { default as PropsPanel } from './props-panel/PropsPanel.svelte';
12
12
  export { canvasLog, canvasWarn, canvasDebug, setCanvasLogsEnabled, isCanvasLogsEnabled } from './canvasLogger';
13
+ export { setShapeImageStore, getShapeImageStore } from './shapes/shapeImageStore';
@@ -1,6 +1,8 @@
1
1
  import { ImageShape } from './ImageShape';
2
2
  export declare const WIDGET_SHAPE_EXTRA_PROPS: string[];
3
3
  export declare const WIDGET_SHAPE_CUSTOM_PROPS: string[];
4
+ /** Pulls a shape's thumbnail back from the host store and repaints it. */
5
+ export declare function restoreThumbnailFromStore(shape: WidgetShape): Promise<void>;
4
6
  export declare class WidgetShape extends ImageShape {
5
7
  static type: string;
6
8
  _isWidgetImage: boolean;
@@ -8,6 +10,8 @@ export declare class WidgetShape extends ImageShape {
8
10
  _widgetName: string | null;
9
11
  /** True when _widgetId no longer exists in the host widget list (deleted or moved). */
10
12
  _widgetOrphaned: boolean;
13
+ /** Reference to the thumbnail bytes held by the host store, when externalized. */
14
+ _imageRef: string | null;
11
15
  /**
12
16
  * Runtime-only: when live HTML iframe overlays the shape, hide the screenshot
13
17
  * (draw a solid fill instead) so it does not ghost in the padding. Not serialized.
@@ -15,18 +15,69 @@
15
15
  import { Rect, classRegistry } from 'fabric';
16
16
  import { ImageShape, IMAGE_SHAPE_CUSTOM_PROPS } from './ImageShape';
17
17
  import { canvasLog, canvasWarn } from '../canvasLogger';
18
+ import { getShapeImageStore, isInlineImageData, shapeImageRef } from './shapeImageStore';
18
19
  // Widget-specific custom properties (on top of IMAGE_SHAPE_CUSTOM_PROPS)
19
20
  export const WIDGET_SHAPE_EXTRA_PROPS = [
20
21
  '_isWidgetImage',
21
22
  '_widgetId',
22
23
  '_widgetName',
23
24
  '_widgetOrphaned',
25
+ /** Stands in for the thumbnail bytes when a host store holds them. */
26
+ '_imageRef',
24
27
  ];
25
28
  // Full list: base image props + widget props
26
29
  export const WIDGET_SHAPE_CUSTOM_PROPS = [
27
30
  ...IMAGE_SHAPE_CUSTOM_PROPS,
28
31
  ...WIDGET_SHAPE_EXTRA_PROPS,
29
32
  ];
33
+ /** What the store already holds per reference, so repeat saves are skipped. */
34
+ const lastStoredThumbnailByRef = new Map();
35
+ /**
36
+ * Hands the thumbnail bytes to the host store and returns the fields that keep
37
+ * them out of the serialized shape. Returns nothing to change when there is no
38
+ * store, no inline bytes, or no stable reference to file them under — those
39
+ * shapes keep serializing as before.
40
+ */
41
+ function externalizeThumbnail(shape) {
42
+ const store = getShapeImageStore();
43
+ if (!store || !isInlineImageData(shape._imageUrl))
44
+ return {};
45
+ const ref = shapeImageRef(shape._widgetId, shape._objectId);
46
+ if (!ref)
47
+ return {};
48
+ try {
49
+ // Undo history serializes on every edit, so only a changed screenshot is
50
+ // worth another write.
51
+ if (lastStoredThumbnailByRef.get(ref) !== shape._imageUrl) {
52
+ store.save(ref, shape._imageUrl);
53
+ lastStoredThumbnailByRef.set(ref, shape._imageUrl);
54
+ }
55
+ }
56
+ catch (err) {
57
+ canvasWarn('WidgetShape: thumbnail store rejected the save, keeping it inline', err);
58
+ return {};
59
+ }
60
+ shape._imageRef = ref;
61
+ return { _imageUrl: undefined, _imageRef: ref };
62
+ }
63
+ /** Pulls a shape's thumbnail back from the host store and repaints it. */
64
+ export async function restoreThumbnailFromStore(shape) {
65
+ const store = getShapeImageStore();
66
+ if (!store || !shape._imageRef || shape._imageUrl)
67
+ return;
68
+ try {
69
+ const url = await store.load(shape._imageRef);
70
+ if (!url || shape._imageUrl)
71
+ return;
72
+ if (isInlineImageData(url))
73
+ lastStoredThumbnailByRef.set(shape._imageRef, url);
74
+ shape._imageUrl = url;
75
+ ImageShape.reloadImage(shape);
76
+ }
77
+ catch (err) {
78
+ canvasWarn('WidgetShape: could not restore thumbnail for', shape._imageRef, err);
79
+ }
80
+ }
30
81
  export class WidgetShape extends ImageShape {
31
82
  static type = 'WidgetShape';
32
83
  _isWidgetImage = false;
@@ -34,6 +85,8 @@ export class WidgetShape extends ImageShape {
34
85
  _widgetName = null;
35
86
  /** True when _widgetId no longer exists in the host widget list (deleted or moved). */
36
87
  _widgetOrphaned = false;
88
+ /** Reference to the thumbnail bytes held by the host store, when externalized. */
89
+ _imageRef = null;
37
90
  /**
38
91
  * Runtime-only: when live HTML iframe overlays the shape, hide the screenshot
39
92
  * (draw a solid fill instead) so it does not ghost in the padding. Not serialized.
@@ -90,6 +143,7 @@ export class WidgetShape extends ImageShape {
90
143
  const base = super.toObject(extras);
91
144
  return {
92
145
  ...base,
146
+ ...externalizeThumbnail(this),
93
147
  type: WidgetShape.type,
94
148
  _imageElement: undefined,
95
149
  };
@@ -107,6 +161,9 @@ export class WidgetShape extends ImageShape {
107
161
  if (shape._imageUrl) {
108
162
  ImageShape.reloadImage(shape);
109
163
  }
164
+ else if (shape._imageRef) {
165
+ void restoreThumbnailFromStore(shape);
166
+ }
110
167
  canvasLog('WidgetShape: fromObject — restored', shape._objectId || 'shape', 'hasImage:', shape._hasImage, 'isWidget:', shape._isWidgetImage, 'widgetId:', shape._widgetId);
111
168
  return shape;
112
169
  });
@@ -127,6 +184,9 @@ export function upgradeToWidgetShape(obj) {
127
184
  Object.setPrototypeOf(obj, WidgetShape.prototype);
128
185
  const shape = obj;
129
186
  shape._isWidgetImage = true;
187
+ if (shape._imageRef && !shape._imageUrl) {
188
+ void restoreThumbnailFromStore(shape);
189
+ }
130
190
  canvasLog('WidgetShape: upgraded ImageShape → WidgetShape for', shape._objectId || 'shape');
131
191
  return shape;
132
192
  }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Where a widget shape's thumbnail bytes live when they are not in the canvas
3
+ * JSON.
4
+ *
5
+ * A widget shape's image is a screenshot of the widget, and it was serialized
6
+ * inline as a `data:` URL on every save. Canvas JSON is stringified on each
7
+ * autosave and written three times over (durable store, legacy mirror, API), so
8
+ * a handful of shapes turned every save into megabytes of synchronous string
9
+ * work on the main thread — the freeze reports named it directly.
10
+ *
11
+ * The bytes are reproducible and already persisted per widget server-side, so
12
+ * the canvas only needs to record *which* image a shape had. The host supplies
13
+ * the store: it saves the bytes where it likes and resolves a reference back to
14
+ * something an `Image` can load (its own copy, or the widget's thumbnail URL).
15
+ *
16
+ * With no store installed nothing changes — shapes keep serializing inline, so
17
+ * a consumer that has not wired one up is never left with blank shapes.
18
+ */
19
+ export interface ShapeImageStore {
20
+ /**
21
+ * Persist the bytes for a reference. Called during serialization, so it must
22
+ * return immediately — write in the background and swallow failures.
23
+ */
24
+ save(ref: string, dataUrl: string): void;
25
+ /** Resolve a reference to a data URL or any URL an `Image` can load. */
26
+ load(ref: string): Promise<string | null>;
27
+ }
28
+ export declare function setShapeImageStore(store: ShapeImageStore | null): void;
29
+ export declare function getShapeImageStore(): ShapeImageStore | null;
30
+ /** Inline bytes — the thing that must not reach the canvas JSON. */
31
+ export declare function isInlineImageData(url: unknown): url is string;
32
+ /**
33
+ * Reference for a shape's thumbnail. Keyed on the widget when there is one, so
34
+ * the same reference resolves after a reload, a duplicate, or a fresh browser
35
+ * where only the server copy exists.
36
+ */
37
+ export declare function shapeImageRef(widgetId?: string | null, objectId?: string | null): string | null;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Where a widget shape's thumbnail bytes live when they are not in the canvas
3
+ * JSON.
4
+ *
5
+ * A widget shape's image is a screenshot of the widget, and it was serialized
6
+ * inline as a `data:` URL on every save. Canvas JSON is stringified on each
7
+ * autosave and written three times over (durable store, legacy mirror, API), so
8
+ * a handful of shapes turned every save into megabytes of synchronous string
9
+ * work on the main thread — the freeze reports named it directly.
10
+ *
11
+ * The bytes are reproducible and already persisted per widget server-side, so
12
+ * the canvas only needs to record *which* image a shape had. The host supplies
13
+ * the store: it saves the bytes where it likes and resolves a reference back to
14
+ * something an `Image` can load (its own copy, or the widget's thumbnail URL).
15
+ *
16
+ * With no store installed nothing changes — shapes keep serializing inline, so
17
+ * a consumer that has not wired one up is never left with blank shapes.
18
+ */
19
+ let installedStore = null;
20
+ export function setShapeImageStore(store) {
21
+ installedStore = store;
22
+ }
23
+ export function getShapeImageStore() {
24
+ return installedStore;
25
+ }
26
+ /** Inline bytes — the thing that must not reach the canvas JSON. */
27
+ export function isInlineImageData(url) {
28
+ return typeof url === 'string' && url.startsWith('data:');
29
+ }
30
+ /**
31
+ * Reference for a shape's thumbnail. Keyed on the widget when there is one, so
32
+ * the same reference resolves after a reload, a duplicate, or a fresh browser
33
+ * where only the server copy exists.
34
+ */
35
+ export function shapeImageRef(widgetId, objectId) {
36
+ if (widgetId)
37
+ return `widget:${widgetId}`;
38
+ if (objectId)
39
+ return `shape:${objectId}`;
40
+ return null;
41
+ }
package/dist/index.d.ts CHANGED
@@ -8,4 +8,6 @@
8
8
  */
9
9
  export { Canvas, CanvasToolbar, ContextMenu, PanZoomPanel, PropsPanel } from './canvas';
10
10
  export type { WidgetConversionPayload } from './canvas';
11
+ export { setShapeImageStore, getShapeImageStore } from './canvas';
12
+ export type { ShapeImageStore } from './canvas';
11
13
  export { Tooltip, ColorIC, FontSelectorIC, SliderUnitIC, TextAlignIC, TextStyleIC } from './components';
package/dist/index.js CHANGED
@@ -8,5 +8,7 @@
8
8
  */
9
9
  // Canvas (heavy — dynamic import recommended)
10
10
  export { Canvas, CanvasToolbar, ContextMenu, PanZoomPanel, PropsPanel } from './canvas';
11
+ // Widget thumbnails live outside the canvas JSON when the host installs a store
12
+ export { setShapeImageStore, getShapeImageStore } from './canvas';
11
13
  // Lightweight components (safe for static import)
12
14
  export { Tooltip, ColorIC, FontSelectorIC, SliderUnitIC, TextAlignIC, TextStyleIC } from './components';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/canvas",
3
- "version": "0.5.12",
3
+ "version": "0.5.14",
4
4
  "type": "module",
5
5
  "svelte": "./dist/index.js",
6
6
  "module": "./dist/index.js",