@widgetic/creator 0.3.78 → 0.3.80

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.
@@ -1709,29 +1709,32 @@
1709
1709
  /** One-widget lookup for the persisted thumbnail, cached per widget. */
1710
1710
  const thumbnailUrlRequests = new Map<string, Promise<string | null>>();
1711
1711
 
1712
+ /**
1713
+ * The generated widget model drops thumbnailUrl. Read the list as JSON so
1714
+ * the canvas shape still receives the saved image.
1715
+ */
1716
+ async function readWidgetList(canvasIdForList?: string | null): Promise<any[]> {
1717
+ if (!widgetsClient || typeof (widgetsClient as any).getWidgetsRaw !== 'function') return [];
1718
+ const response = await (widgetsClient as any).getWidgetsRaw(
1719
+ canvasIdForList ? { canvasId: canvasIdForList } : {},
1720
+ );
1721
+ const body = await response.raw.json();
1722
+ const envelopeData = body?.data;
1723
+ const nested = envelopeData?.data;
1724
+ if (Array.isArray(body)) return body;
1725
+ if (Array.isArray(nested)) return nested;
1726
+ if (Array.isArray(envelopeData)) return envelopeData;
1727
+ return [];
1728
+ }
1729
+
1712
1730
  function fetchWidgetThumbnailUrl(widgetId: string, widget?: any): Promise<string | null> {
1713
1731
  const pending = thumbnailUrlRequests.get(widgetId);
1714
1732
  if (pending) return pending;
1715
1733
  const request = (async () => {
1716
1734
  if (!widgetsClient) return null;
1717
1735
  try {
1718
- // The canvas list already carries thumbnail_url. Do not use
1719
- // POST /widgets/batch: that route is the public embed gate and
1720
- // drops the row when the trial has ended, so the sketch never paints.
1721
1736
  const canvasForWidget = widget?.canvasId || widget?.canvas_id || canvasId;
1722
- const res = await widgetsClient.getWidgets(
1723
- canvasForWidget ? { canvasId: canvasForWidget } : {},
1724
- );
1725
- const envelope = res as { data?: unknown };
1726
- const envelopeData = envelope?.data;
1727
- const nested = (envelopeData as { data?: unknown } | undefined)?.data;
1728
- const list = Array.isArray(res)
1729
- ? (res as any[])
1730
- : Array.isArray(nested)
1731
- ? (nested as any[])
1732
- : Array.isArray(envelopeData)
1733
- ? (envelopeData as any[])
1734
- : [];
1737
+ const list = await readWidgetList(canvasForWidget);
1735
1738
  const fetched = list.find((row) => row?.id === widgetId);
1736
1739
  return fetched?.thumbnailUrl ?? fetched?.thumbnail_url ?? null;
1737
1740
  } catch (error) {
@@ -6460,34 +6463,9 @@
6460
6463
  console.warn('WidgetCreator.loadWidgetsForCanvas: missing JWT before calling widgets API');
6461
6464
  }
6462
6465
 
6463
- // Fetch widgets directly by canvasId
6464
- const res = await widgetsClient.getWidgets({ canvasId: canvasId ?? undefined });
6465
-
6466
- // normalize error shape from apiRequest
6467
- const envelope = res as unknown as {
6468
- error?: string;
6469
- message?: string;
6470
- data?: unknown;
6471
- };
6472
- if (envelope && (envelope.error || envelope.message)) {
6473
- // console.error('Widget Creator: loadWidgetsForCanvas: response not ok:', res);
6474
- widgets = [];
6475
- selectedWidgetId = null;
6476
- isLoadingWidgets = false;
6477
- widgetsError = envelope.message || envelope.error || 'Failed to load widgets';
6478
- throw new Error(widgetsError);
6479
- }
6480
-
6481
- // if the response is ok, parse the data (bare list, {data: list} or {data: {data: list}})
6482
- const envelopeData = envelope?.data;
6483
- const envelopeNestedData = (envelopeData as { data?: unknown } | undefined)?.data;
6484
- const fetched = Array.isArray(res)
6485
- ? (res as unknown as WidgetSummary[])
6486
- : Array.isArray(envelopeNestedData)
6487
- ? (envelopeNestedData as unknown as WidgetSummary[])
6488
- : Array.isArray(envelopeData)
6489
- ? (envelopeData as unknown as WidgetSummary[])
6490
- : [];
6466
+ // Fetch widgets directly by canvasId. Raw JSON keeps thumbnailUrl,
6467
+ // which the generated model drops.
6468
+ const fetched = await readWidgetList(canvasId);
6491
6469
 
6492
6470
  const previousById = new Map(
6493
6471
  widgets.filter((w) => w.id).map((w) => [w.id as string, w]),
@@ -8536,6 +8514,67 @@
8536
8514
  }, IDLE_THUMBNAIL_DELAY_MS);
8537
8515
  idleThumbnailTimers.set(widgetId, timer);
8538
8516
  }
8517
+ /**
8518
+ * Screenshot of whatever the preview is showing now, stored as the list thumbnail.
8519
+ * A published CDN file often has no capture script, so the Worker preview is the fallback.
8520
+ * That fallback still uses the main thread, so publish schedules it after the click returns.
8521
+ */
8522
+ async function captureListThumbnail(widgetId: string, interactive: boolean): Promise<void> {
8523
+ if (!widgetId || isDraftConvertId(widgetId)) return;
8524
+ if (codegenCaptureBlocked() || generatingWidgetIds[widgetId]) {
8525
+ if (interactive) {
8526
+ showToast('info', 'Preview image waits until generation finishes', { duration: 3000 });
8527
+ }
8528
+ return;
8529
+ }
8530
+ if (interactive) {
8531
+ showToast('info', 'Saving preview image...', { duration: 2500 });
8532
+ }
8533
+ try {
8534
+ const screenshot = await handleChatScreenshot(widgetId, null, false);
8535
+ if (!screenshot) {
8536
+ if (interactive) {
8537
+ showToast('error', 'Could not save the preview image', {
8538
+ description: 'The preview did not return an image. Refresh it, then try again.',
8539
+ duration: 5000,
8540
+ });
8541
+ }
8542
+ return;
8543
+ }
8544
+ canvasRef?.updateWidgetImage(widgetId, screenshot);
8545
+ await persistWidgetListThumbnail(widgetId, screenshot);
8546
+ if (interactive) {
8547
+ showToast('success', 'Preview image saved', { duration: 2000 });
8548
+ } else {
8549
+ console.log('[Chat→Screenshot] List thumbnail saved', widgetId.substring(0, 8));
8550
+ }
8551
+ } catch (error) {
8552
+ console.warn('[Chat→Screenshot] List thumbnail failed', widgetId.substring(0, 8), error);
8553
+ if (interactive) {
8554
+ showToast('error', 'Could not save the preview image', { duration: 4000 });
8555
+ }
8556
+ }
8557
+ }
8558
+
8559
+ const listThumbnailTimers = new Map<string, ReturnType<typeof setTimeout>>();
8560
+ function scheduleListThumbnailCapture(widgetId: string): void {
8561
+ if (!widgetId) return;
8562
+ const existing = listThumbnailTimers.get(widgetId);
8563
+ if (existing) clearTimeout(existing);
8564
+ const timer = setTimeout(() => {
8565
+ listThumbnailTimers.delete(widgetId);
8566
+ const run = () => {
8567
+ void captureListThumbnail(widgetId, false);
8568
+ };
8569
+ if (typeof requestIdleCallback === 'function') {
8570
+ requestIdleCallback(() => run(), { timeout: 8000 });
8571
+ } else {
8572
+ run();
8573
+ }
8574
+ }, 8000);
8575
+ listThumbnailTimers.set(widgetId, timer);
8576
+ }
8577
+
8539
8578
  async function persistIdleWidgetThumbnail(widgetId: string): Promise<void> {
8540
8579
  if (codegenCaptureBlocked() || generatingWidgetIds[widgetId]) return;
8541
8580
  const iframe = resolveLiveWidgetShapeIframe(widgetId);
@@ -10312,6 +10351,7 @@ Requirements:
10312
10351
  console.log('[Publish] Refreshing preview after publish (step:', widgetDetailsStep, ')');
10313
10352
  publishedPanelRef?.invalidatePublishedArtifactCache?.();
10314
10353
  await publishedPanelRef?.refreshPreview({ withRetry: true });
10354
+ scheduleListThumbnailCapture(capturedWidgetId);
10315
10355
 
10316
10356
  await loadPublishHistory();
10317
10357
  void refreshTrialCounter();
@@ -16001,7 +16041,9 @@ $: if (codeGenerationPrompt && selectedWidgetId && promptLoadedForWidgetId === s
16001
16041
  onWidgetShapeDuplicated={handleWidgetShapeDuplicated}
16002
16042
  onWidgetShapeDeleting={handleWidgetShapeDeleting}
16003
16043
  onWidgetRename={handleWidgetRename}
16004
- onRefreshScreenshot={null}
16044
+ onRefreshScreenshot={(widgetId) => {
16045
+ void captureListThumbnail(widgetId, true);
16046
+ }}
16005
16047
  onSetPrimaryWidget={handleSetPrimaryWidget}
16006
16048
  onAfterRender={syncWidgetShapeLivePreviews}
16007
16049
  getWidgetName={getWidgetNameById}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/creator",
3
- "version": "0.3.78",
3
+ "version": "0.3.80",
4
4
  "type": "module",
5
5
  "peerDependencies": {
6
6
  "@sveltejs/kit": "^2.20.0",