@widgetic/canvas 0.5.13 → 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) {
@@ -8887,6 +8889,8 @@ function checkAndAddNewShapeToFrame(shape: any) {
8887
8889
  '_widgetId',
8888
8890
  // Widget Image: converted widget placeholder on canvas
8889
8891
  '_isWidgetImage',
8892
+ // Widget thumbnail kept in the host store, referenced instead of inlined
8893
+ '_imageRef',
8890
8894
  '_groupFill', '_groupStroke', '_groupStrokeWidth',
8891
8895
  '_groupFillOpacity', '_groupStrokeDashArray',
8892
8896
  '_canvasPaintOrder',
@@ -9208,9 +9212,12 @@ function checkAndAddNewShapeToFrame(shape: any) {
9208
9212
  // Light pass: ensure any images without loaded elements are reloaded
9209
9213
  const allObjects = canvas.getObjects();
9210
9214
  const checkUnloadedImages = (obj: any) => {
9211
- if (obj._customType === 'image' || obj._hasImage || obj._imageUrl) {
9215
+ if (obj._customType === 'image' || obj._hasImage || obj._imageUrl || obj._imageRef) {
9212
9216
  if (obj._imageUrl && !obj._imageElement) {
9213
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);
9214
9221
  }
9215
9222
  } else if (String(obj.type || '').toLowerCase() === 'group' || isFrameShape(obj)) {
9216
9223
  (obj.getObjects?.() || []).forEach(checkUnloadedImages);
@@ -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.13",
3
+ "version": "0.5.14",
4
4
  "type": "module",
5
5
  "svelte": "./dist/index.js",
6
6
  "module": "./dist/index.js",