@widgetic/creator 0.4.8 → 0.4.10
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.
- package/dist/CreatorApp.svelte +176 -43
- package/dist/components/WidgetDetails.svelte +2 -1
- package/dist/components/WidgetDetails.svelte.d.ts +2 -1
- package/dist/components/WidgetElementSelector.svelte +464 -0
- package/dist/components/WidgetElementSelector.svelte.d.ts +16 -0
- package/dist/components/WidgetShapeLivePreviews.svelte +40 -1
- package/dist/components/WidgetShapeLivePreviews.svelte.d.ts +11 -0
- package/dist/i18n/language.d.ts +9 -1
- package/dist/i18n/messages.d.ts +8 -0
- package/dist/i18n/messages.js +9 -0
- package/dist/utils/widgetElementSelector.d.ts +73 -0
- package/dist/utils/widgetElementSelector.js +316 -0
- package/dist/utils/widgeticChatUpload.js +15 -5
- package/package.json +8 -1
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount, onDestroy } from 'svelte';
|
|
3
|
+
import {
|
|
4
|
+
type SelectedWidgetElementReference,
|
|
5
|
+
deepElementAt,
|
|
6
|
+
pickTarget,
|
|
7
|
+
parentToPick,
|
|
8
|
+
serializePickedElement,
|
|
9
|
+
computeChipPlacement,
|
|
10
|
+
FALLBACK_PICKER_BRIDGE_SCRIPT
|
|
11
|
+
} from '../utils/widgetElementSelector';
|
|
12
|
+
import { useCreatorLanguage } from '../i18n/index.js';
|
|
13
|
+
|
|
14
|
+
const { t } = useCreatorLanguage();
|
|
15
|
+
|
|
16
|
+
interface Props {
|
|
17
|
+
widgetId: string;
|
|
18
|
+
enabled: boolean;
|
|
19
|
+
iframeElement?: HTMLIFrameElement | null;
|
|
20
|
+
iframeSrc?: string;
|
|
21
|
+
viewportWidth: number;
|
|
22
|
+
viewportHeight: number;
|
|
23
|
+
selectedElements?: SelectedWidgetElementReference[];
|
|
24
|
+
onSelectElement?: (element: SelectedWidgetElementReference) => void;
|
|
25
|
+
onRemoveElement?: (id: string) => void;
|
|
26
|
+
onCloseSelector?: () => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let {
|
|
30
|
+
widgetId,
|
|
31
|
+
enabled = false,
|
|
32
|
+
iframeElement = null,
|
|
33
|
+
iframeSrc = '',
|
|
34
|
+
viewportWidth = 800,
|
|
35
|
+
viewportHeight = 500,
|
|
36
|
+
selectedElements = [],
|
|
37
|
+
onSelectElement,
|
|
38
|
+
onRemoveElement,
|
|
39
|
+
onCloseSelector
|
|
40
|
+
}: Props = $props();
|
|
41
|
+
|
|
42
|
+
let hoveredElement = $state<SelectedWidgetElementReference | null>(null);
|
|
43
|
+
let passthroughHeld = $state(false);
|
|
44
|
+
let bridgeReady = $state(false);
|
|
45
|
+
let fallbackSrcdoc = $state<string | null>(null);
|
|
46
|
+
let fallbackIframeRef = $state<HTMLIFrameElement | null>(null);
|
|
47
|
+
|
|
48
|
+
// Direct same-origin DOM state (used when iframe or fallback srcdoc is same-origin)
|
|
49
|
+
let directHoveredNode: Element | null = null;
|
|
50
|
+
let directCurrentNode: Element | null = null;
|
|
51
|
+
let directDescendants: Element[] = [];
|
|
52
|
+
|
|
53
|
+
let pingTimer: ReturnType<typeof setTimeout> | null = null;
|
|
54
|
+
let lastPointerPosition = { x: 0, y: 0 };
|
|
55
|
+
|
|
56
|
+
function getDirectDocument(): Document | null {
|
|
57
|
+
if (fallbackIframeRef?.contentDocument?.body) {
|
|
58
|
+
return fallbackIframeRef.contentDocument;
|
|
59
|
+
}
|
|
60
|
+
if (!iframeElement) return null;
|
|
61
|
+
try {
|
|
62
|
+
const doc = iframeElement.contentDocument;
|
|
63
|
+
if (doc && doc.body && doc.body.children.length > 0) {
|
|
64
|
+
return doc;
|
|
65
|
+
}
|
|
66
|
+
} catch {
|
|
67
|
+
// Cross-origin preview iframe
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function postToTargetIframe(payload: Record<string, unknown>) {
|
|
73
|
+
const targetWindow =
|
|
74
|
+
fallbackIframeRef?.contentWindow ?? iframeElement?.contentWindow;
|
|
75
|
+
if (!targetWindow) return;
|
|
76
|
+
try {
|
|
77
|
+
targetWindow.postMessage({ ...payload, widgetId }, '*');
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.warn('[WidgetElementSelector] Failed to postMessage to preview iframe:', error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function ensurePickerBridge() {
|
|
84
|
+
if (!enabled) return;
|
|
85
|
+
if (getDirectDocument()) {
|
|
86
|
+
bridgeReady = true;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
bridgeReady = false;
|
|
90
|
+
postToTargetIframe({ type: 'widgetic:picker-ping' });
|
|
91
|
+
|
|
92
|
+
if (pingTimer) clearTimeout(pingTimer);
|
|
93
|
+
pingTimer = setTimeout(async () => {
|
|
94
|
+
if (bridgeReady || !enabled || !iframeSrc) return;
|
|
95
|
+
try {
|
|
96
|
+
const response = await fetch(iframeSrc, { mode: 'cors', credentials: 'omit' });
|
|
97
|
+
if (!response.ok) return;
|
|
98
|
+
const rawHtml = await response.text();
|
|
99
|
+
const baseUrl = new URL('.', iframeSrc).href;
|
|
100
|
+
const baseTag = `<base href="${baseUrl}">`;
|
|
101
|
+
const withBase = rawHtml.includes('<head>')
|
|
102
|
+
? rawHtml.replace('<head>', `<head>\n${baseTag}`)
|
|
103
|
+
: `${baseTag}\n${rawHtml}`;
|
|
104
|
+
const withBridge = withBase.includes('</body>')
|
|
105
|
+
? withBase.replace('</body>', `${FALLBACK_PICKER_BRIDGE_SCRIPT}\n</body>`)
|
|
106
|
+
: `${withBase}\n${FALLBACK_PICKER_BRIDGE_SCRIPT}`;
|
|
107
|
+
fallbackSrcdoc = withBridge;
|
|
108
|
+
bridgeReady = true;
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.warn('[WidgetElementSelector] Fallback srcdoc fetch failed:', error);
|
|
111
|
+
}
|
|
112
|
+
}, 180);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function handleWindowMessage(event: MessageEvent) {
|
|
116
|
+
const data = event.data;
|
|
117
|
+
if (!data || typeof data.type !== 'string' || !data.type.startsWith('widgetic:picker-')) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (data.widgetId && data.widgetId !== widgetId) return;
|
|
121
|
+
|
|
122
|
+
if (data.type === 'widgetic:picker-pong') {
|
|
123
|
+
bridgeReady = true;
|
|
124
|
+
if (pingTimer) {
|
|
125
|
+
clearTimeout(pingTimer);
|
|
126
|
+
pingTimer = null;
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (data.type === 'widgetic:picker-hover-result') {
|
|
132
|
+
if (data.element) {
|
|
133
|
+
hoveredElement = {
|
|
134
|
+
...data.element,
|
|
135
|
+
id: `hover_${widgetId}`
|
|
136
|
+
};
|
|
137
|
+
} else {
|
|
138
|
+
hoveredElement = null;
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (data.type === 'widgetic:picker-select-result') {
|
|
144
|
+
if (data.element) {
|
|
145
|
+
const selectedRef: SelectedWidgetElementReference = {
|
|
146
|
+
...data.element,
|
|
147
|
+
id: `sel_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`
|
|
148
|
+
};
|
|
149
|
+
onSelectElement?.(selectedRef);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function runDirectHitTest(localX: number, localY: number) {
|
|
155
|
+
const doc = getDirectDocument();
|
|
156
|
+
if (!doc) return false;
|
|
157
|
+
const rawHit = deepElementAt(doc, localX, localY);
|
|
158
|
+
const picked = rawHit ? pickTarget(rawHit) : null;
|
|
159
|
+
if (picked !== directHoveredNode) {
|
|
160
|
+
directHoveredNode = picked;
|
|
161
|
+
directCurrentNode = picked;
|
|
162
|
+
directDescendants = [];
|
|
163
|
+
}
|
|
164
|
+
hoveredElement = directCurrentNode
|
|
165
|
+
? serializePickedElement(directCurrentNode, `hover_${widgetId}`)
|
|
166
|
+
: null;
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function handlePointerMove(event: PointerEvent) {
|
|
171
|
+
if (!enabled || passthroughHeld) return;
|
|
172
|
+
const surface = event.currentTarget as HTMLElement;
|
|
173
|
+
const bounds = surface.getBoundingClientRect();
|
|
174
|
+
const scaleX = bounds.width > 0 ? viewportWidth / bounds.width : 1;
|
|
175
|
+
const scaleY = bounds.height > 0 ? viewportHeight / bounds.height : 1;
|
|
176
|
+
const localX = (event.clientX - bounds.left) * scaleX;
|
|
177
|
+
const localY = (event.clientY - bounds.top) * scaleY;
|
|
178
|
+
lastPointerPosition = { x: localX, y: localY };
|
|
179
|
+
|
|
180
|
+
if (!runDirectHitTest(localX, localY)) {
|
|
181
|
+
postToTargetIframe({
|
|
182
|
+
type: 'widgetic:picker-hit-test',
|
|
183
|
+
x: localX,
|
|
184
|
+
y: localY
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function handlePointerLeave() {
|
|
190
|
+
hoveredElement = null;
|
|
191
|
+
directHoveredNode = null;
|
|
192
|
+
directCurrentNode = null;
|
|
193
|
+
directDescendants = [];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function handleOverlayClick(event: MouseEvent) {
|
|
197
|
+
if (!enabled || passthroughHeld) return;
|
|
198
|
+
event.preventDefault();
|
|
199
|
+
event.stopPropagation();
|
|
200
|
+
|
|
201
|
+
const doc = getDirectDocument();
|
|
202
|
+
if (doc) {
|
|
203
|
+
let target = directCurrentNode;
|
|
204
|
+
if (!target) {
|
|
205
|
+
const rawHit = deepElementAt(doc, lastPointerPosition.x, lastPointerPosition.y);
|
|
206
|
+
target = rawHit ? pickTarget(rawHit) : null;
|
|
207
|
+
}
|
|
208
|
+
if (target) {
|
|
209
|
+
const selectedRef = serializePickedElement(target);
|
|
210
|
+
onSelectElement?.(selectedRef);
|
|
211
|
+
}
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (hoveredElement) {
|
|
216
|
+
const selectedRef: SelectedWidgetElementReference = {
|
|
217
|
+
...hoveredElement,
|
|
218
|
+
id: `sel_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`
|
|
219
|
+
};
|
|
220
|
+
onSelectElement?.(selectedRef);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
postToTargetIframe({
|
|
225
|
+
type: 'widgetic:picker-select',
|
|
226
|
+
x: lastPointerPosition.x,
|
|
227
|
+
y: lastPointerPosition.y
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function walkHierarchy(direction: 'up' | 'down') {
|
|
232
|
+
const doc = getDirectDocument();
|
|
233
|
+
if (doc && directCurrentNode) {
|
|
234
|
+
if (direction === 'up') {
|
|
235
|
+
const previous = directCurrentNode;
|
|
236
|
+
let candidate = parentToPick(directCurrentNode);
|
|
237
|
+
while (candidate) {
|
|
238
|
+
const rect = candidate.getBoundingClientRect();
|
|
239
|
+
if (rect.width > 0 && rect.height > 0) break;
|
|
240
|
+
candidate = parentToPick(candidate);
|
|
241
|
+
}
|
|
242
|
+
if (candidate) {
|
|
243
|
+
directDescendants.push(previous);
|
|
244
|
+
directCurrentNode = candidate;
|
|
245
|
+
}
|
|
246
|
+
} else if (direction === 'down') {
|
|
247
|
+
const child = directDescendants.pop();
|
|
248
|
+
if (child) {
|
|
249
|
+
directCurrentNode = child;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
hoveredElement = directCurrentNode
|
|
253
|
+
? serializePickedElement(directCurrentNode, `hover_${widgetId}`)
|
|
254
|
+
: null;
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
postToTargetIframe({
|
|
259
|
+
type: 'widgetic:picker-walk',
|
|
260
|
+
direction
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function handleKeyDown(event: KeyboardEvent) {
|
|
265
|
+
if (!enabled) return;
|
|
266
|
+
if (event.key === 'Meta' || event.key === 'Control') {
|
|
267
|
+
passthroughHeld = true;
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Do not hijack typing when focus is inside chat input or another text field
|
|
272
|
+
const activeEl = document.activeElement;
|
|
273
|
+
if (
|
|
274
|
+
activeEl &&
|
|
275
|
+
(activeEl.tagName === 'INPUT' ||
|
|
276
|
+
activeEl.tagName === 'TEXTAREA' ||
|
|
277
|
+
(activeEl as HTMLElement).isContentEditable)
|
|
278
|
+
) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (event.key === 'Escape') {
|
|
283
|
+
event.preventDefault();
|
|
284
|
+
hoveredElement = null;
|
|
285
|
+
onCloseSelector?.();
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (event.key === 'ArrowUp' && hoveredElement) {
|
|
290
|
+
event.preventDefault();
|
|
291
|
+
walkHierarchy('up');
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (event.key === 'ArrowDown' && hoveredElement) {
|
|
296
|
+
event.preventDefault();
|
|
297
|
+
walkHierarchy('down');
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (event.key === 'Enter' && hoveredElement) {
|
|
302
|
+
event.preventDefault();
|
|
303
|
+
const selectedRef: SelectedWidgetElementReference = {
|
|
304
|
+
...hoveredElement,
|
|
305
|
+
id: `sel_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`
|
|
306
|
+
};
|
|
307
|
+
onSelectElement?.(selectedRef);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function handleKeyUp(event: KeyboardEvent) {
|
|
312
|
+
if (event.key === 'Meta' || event.key === 'Control') {
|
|
313
|
+
passthroughHeld = false;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function handleWindowBlur() {
|
|
318
|
+
passthroughHeld = false;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
$effect(() => {
|
|
322
|
+
if (enabled && iframeSrc) {
|
|
323
|
+
ensurePickerBridge();
|
|
324
|
+
} else if (!enabled) {
|
|
325
|
+
hoveredElement = null;
|
|
326
|
+
passthroughHeld = false;
|
|
327
|
+
directHoveredNode = null;
|
|
328
|
+
directCurrentNode = null;
|
|
329
|
+
directDescendants = [];
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
onMount(() => {
|
|
334
|
+
window.addEventListener('message', handleWindowMessage);
|
|
335
|
+
window.addEventListener('keydown', handleKeyDown);
|
|
336
|
+
window.addEventListener('keyup', handleKeyUp);
|
|
337
|
+
window.addEventListener('blur', handleWindowBlur);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
onDestroy(() => {
|
|
341
|
+
if (pingTimer) clearTimeout(pingTimer);
|
|
342
|
+
window.removeEventListener('message', handleWindowMessage);
|
|
343
|
+
window.removeEventListener('keydown', handleKeyDown);
|
|
344
|
+
window.removeEventListener('keyup', handleKeyUp);
|
|
345
|
+
window.removeEventListener('blur', handleWindowBlur);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
const hoverChipPosition = $derived(
|
|
349
|
+
hoveredElement?.rect
|
|
350
|
+
? computeChipPlacement(hoveredElement.rect, viewportWidth, viewportHeight)
|
|
351
|
+
: null
|
|
352
|
+
);
|
|
353
|
+
</script>
|
|
354
|
+
|
|
355
|
+
{#if enabled || selectedElements.length > 0}
|
|
356
|
+
<!-- Transparent mirror iframe for pre-bridge cached widgets (only mounted when needed) -->
|
|
357
|
+
{#if enabled && fallbackSrcdoc}
|
|
358
|
+
<iframe
|
|
359
|
+
bind:this={fallbackIframeRef}
|
|
360
|
+
srcdoc={fallbackSrcdoc}
|
|
361
|
+
title={$t('creator.livePreviews.liveWidgetPreview')}
|
|
362
|
+
class="pointer-events-none absolute inset-0 h-full w-full opacity-0"
|
|
363
|
+
aria-hidden="true"
|
|
364
|
+
tabindex="-1"
|
|
365
|
+
></iframe>
|
|
366
|
+
{/if}
|
|
367
|
+
|
|
368
|
+
<!-- Persistent highlights for already-selected elements -->
|
|
369
|
+
{#each selectedElements as item, index (item.id)}
|
|
370
|
+
{#if item.rect && item.rect.width > 0 && item.rect.height > 0}
|
|
371
|
+
{@const chipPos = computeChipPlacement(item.rect, viewportWidth, viewportHeight)}
|
|
372
|
+
<div
|
|
373
|
+
class="pointer-events-none absolute z-[35] rounded-md border-2 border-dashed border-[#6D9A59] bg-[#7BA668]/10 transition-all duration-75"
|
|
374
|
+
style="left: {item.rect.x}px; top: {item.rect.y}px; width: {item.rect.width}px; height: {item.rect.height}px; /* rtl-ok: DOM element bounding box is measured from the left edge of the widget frame */"
|
|
375
|
+
></div>
|
|
376
|
+
<div
|
|
377
|
+
class="pointer-events-auto absolute z-[38] flex max-w-[340px] items-center gap-1.5 whitespace-nowrap rounded-full border border-[#7BA668]/45 bg-[#0F2010] px-2.5 py-1 font-mono text-[11px] font-medium leading-none text-[#A5CE8F] shadow-md"
|
|
378
|
+
style="left: {chipPos.left}px; top: {chipPos.top}px; /* rtl-ok: chip position is measured from the left edge of the widget frame */"
|
|
379
|
+
>
|
|
380
|
+
<span class="flex h-4 w-4 shrink-0 items-center justify-center rounded-full bg-[#7BA668] font-sans text-[10px] font-bold text-white">
|
|
381
|
+
{index + 1}
|
|
382
|
+
</span>
|
|
383
|
+
<span class="truncate">{item.label}</span>
|
|
384
|
+
{#if onRemoveElement}
|
|
385
|
+
<button
|
|
386
|
+
type="button"
|
|
387
|
+
class="ms-0.5 flex h-4 w-4 shrink-0 items-center justify-center rounded-full text-[#A5CE8F]/80 transition-colors hover:bg-white/15 hover:text-white"
|
|
388
|
+
title={$t('creator.elementSelector.removeSelectedElement')}
|
|
389
|
+
aria-label={$t('creator.elementSelector.removeSelectedElement')}
|
|
390
|
+
onclick={(e) => {
|
|
391
|
+
e.stopPropagation();
|
|
392
|
+
onRemoveElement?.(item.id);
|
|
393
|
+
}}
|
|
394
|
+
>
|
|
395
|
+
✕
|
|
396
|
+
</button>
|
|
397
|
+
{/if}
|
|
398
|
+
</div>
|
|
399
|
+
{/if}
|
|
400
|
+
{/each}
|
|
401
|
+
|
|
402
|
+
<!-- Interactive Picker Hit Surface (active when Selector toggle is ON) -->
|
|
403
|
+
{#if enabled}
|
|
404
|
+
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
405
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
406
|
+
<div
|
|
407
|
+
class=" absolute inset-0 z-[36] select-none {passthroughHeld ? 'pointer-events-none' : 'pointer-events-auto cursor-crosshair'}"
|
|
408
|
+
onpointermove={handlePointerMove}
|
|
409
|
+
onpointerleave={handlePointerLeave}
|
|
410
|
+
onclick={handleOverlayClick}
|
|
411
|
+
data-testid="widget-element-selector-overlay"
|
|
412
|
+
>
|
|
413
|
+
{#if hoveredElement?.rect && hoveredElement.rect.width > 0 && hoveredElement.rect.height > 0 && !passthroughHeld}
|
|
414
|
+
<!-- Dashed Hover Box (matches Chrome Extension & reference screenshot) -->
|
|
415
|
+
<div
|
|
416
|
+
class="pointer-events-none absolute rounded-md border-2 border-dashed border-[#5E8C4A] bg-[#7BA668]/12 transition-all duration-75"
|
|
417
|
+
style="left: {hoveredElement.rect.x}px; top: {hoveredElement.rect.y}px; width: {hoveredElement.rect.width}px; height: {hoveredElement.rect.height}px; /* rtl-ok: DOM element bounding box is measured from the left edge of the widget frame */"
|
|
418
|
+
></div>
|
|
419
|
+
|
|
420
|
+
<!-- Floating Hover Tag + Text Snippet Pill -->
|
|
421
|
+
{#if hoverChipPosition}
|
|
422
|
+
<div
|
|
423
|
+
class="pointer-events-none absolute z-[39] flex max-w-[360px] items-center gap-1.5 whitespace-nowrap rounded-md border border-black/15 bg-white/95 px-2.5 py-1 font-mono text-[11px] font-medium leading-none text-neutral-800 shadow-lg backdrop-blur-sm"
|
|
424
|
+
style="left: {hoverChipPosition.left}px; top: {hoverChipPosition.top}px; /* rtl-ok: chip position is measured from the left edge of the widget frame */"
|
|
425
|
+
>
|
|
426
|
+
<span class="rounded bg-[#7BA668]/20 px-1 py-0.5 font-mono text-[10px] font-semibold text-[#3C632C]">
|
|
427
|
+
<{hoveredElement.tagName}>
|
|
428
|
+
</span>
|
|
429
|
+
{#if hoveredElement.textSnippet}
|
|
430
|
+
<span class="text-neutral-400">·</span>
|
|
431
|
+
<span class="truncate font-sans text-[11px] text-neutral-700">
|
|
432
|
+
{hoveredElement.textSnippet.slice(0, 44)}{hoveredElement.textSnippet.length > 44 ? '…' : ''}
|
|
433
|
+
</span>
|
|
434
|
+
{:else}
|
|
435
|
+
<span class="text-neutral-400">·</span>
|
|
436
|
+
<span class="font-mono text-[10px] text-neutral-500">
|
|
437
|
+
{Math.round(hoveredElement.rect.width)} × {Math.round(hoveredElement.rect.height)}
|
|
438
|
+
</span>
|
|
439
|
+
{/if}
|
|
440
|
+
</div>
|
|
441
|
+
{/if}
|
|
442
|
+
{/if}
|
|
443
|
+
|
|
444
|
+
<!-- Bottom Hint Pill (matches Chrome Extension overlay hint) -->
|
|
445
|
+
<div
|
|
446
|
+
class="pointer-events-none absolute inset-x-0 bottom-3 z-[40] mx-auto flex w-max items-center gap-1.5 whitespace-nowrap rounded-full border border-white/10 bg-neutral-900/90 px-3.5 py-1.5 font-sans text-[11px] font-medium text-neutral-200 shadow-lg backdrop-blur-md"
|
|
447
|
+
>
|
|
448
|
+
<span class="inline-block h-2 w-2 rounded-full bg-[#7BA668]"></span>
|
|
449
|
+
<span>{$t('creator.elementSelector.clickPartToReference')}</span>
|
|
450
|
+
<span class="text-neutral-500">·</span>
|
|
451
|
+
<kbd class="rounded border border-white/15 bg-white/10 px-1.5 py-0.5 font-mono text-[10px] text-white">↑</kbd>
|
|
452
|
+
<kbd class="rounded border border-white/15 bg-white/10 px-1.5 py-0.5 font-mono text-[10px] text-white">↓</kbd>
|
|
453
|
+
<span>{$t('creator.elementSelector.parentChild')}</span>
|
|
454
|
+
<span class="text-neutral-500">·</span>
|
|
455
|
+
<span>{$t('creator.elementSelector.hold')}</span>
|
|
456
|
+
<kbd class="rounded border border-white/15 bg-white/10 px-1.5 py-0.5 font-mono text-[10px] text-white">{$t('creator.elementSelector.cmdCtrl')}</kbd>
|
|
457
|
+
<span>{$t('creator.elementSelector.toInteract')}</span>
|
|
458
|
+
<span class="text-neutral-500">·</span>
|
|
459
|
+
<kbd class="rounded border border-white/15 bg-white/10 px-1.5 py-0.5 font-mono text-[10px] text-white">{$t('creator.elementSelector.esc')}</kbd>
|
|
460
|
+
<span>{$t('creator.elementSelector.done')}</span>
|
|
461
|
+
</div>
|
|
462
|
+
</div>
|
|
463
|
+
{/if}
|
|
464
|
+
{/if}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type SelectedWidgetElementReference } from '../utils/widgetElementSelector';
|
|
2
|
+
interface Props {
|
|
3
|
+
widgetId: string;
|
|
4
|
+
enabled: boolean;
|
|
5
|
+
iframeElement?: HTMLIFrameElement | null;
|
|
6
|
+
iframeSrc?: string;
|
|
7
|
+
viewportWidth: number;
|
|
8
|
+
viewportHeight: number;
|
|
9
|
+
selectedElements?: SelectedWidgetElementReference[];
|
|
10
|
+
onSelectElement?: (element: SelectedWidgetElementReference) => void;
|
|
11
|
+
onRemoveElement?: (id: string) => void;
|
|
12
|
+
onCloseSelector?: () => void;
|
|
13
|
+
}
|
|
14
|
+
declare const WidgetElementSelector: import("svelte").Component<Props, {}, "">;
|
|
15
|
+
type WidgetElementSelector = ReturnType<typeof WidgetElementSelector>;
|
|
16
|
+
export default WidgetElementSelector;
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
import { Portal } from '@widgetic/design-system';
|
|
9
9
|
import HoverTooltip from './HoverTooltip.svelte';
|
|
10
10
|
import VersionDropdownRow from './VersionDropdownRow.svelte';
|
|
11
|
+
import WidgetElementSelector from './WidgetElementSelector.svelte';
|
|
12
|
+
import type { SelectedWidgetElementReference } from '../utils/widgetElementSelector';
|
|
11
13
|
import { errorLabel, useCreatorLanguage } from '../i18n/index.js';
|
|
12
14
|
|
|
13
15
|
const { t } = useCreatorLanguage();
|
|
@@ -93,6 +95,16 @@
|
|
|
93
95
|
resolvePublishDisabledReason?: ((widgetId: string, version: number) => string | null) | null;
|
|
94
96
|
/** Whether code generation is running (suppresses background captures / reloads). */
|
|
95
97
|
isGenerating?: boolean;
|
|
98
|
+
/** Whether the interactive DOM Element Selector is active for this widget. */
|
|
99
|
+
resolveSelectorEnabled?: ((widgetId: string) => boolean) | null;
|
|
100
|
+
/** Selected widget elements currently referenced in the chat panel for this widget. */
|
|
101
|
+
resolveSelectedElements?: ((widgetId: string) => SelectedWidgetElementReference[]) | null;
|
|
102
|
+
/** Fired when the user clicks a widget part in Selector mode. */
|
|
103
|
+
onSelectWidgetElement?: ((widgetId: string, element: SelectedWidgetElementReference) => void) | null;
|
|
104
|
+
/** Fired when the user removes a selected element chip from the canvas overlay. */
|
|
105
|
+
onRemoveWidgetElement?: ((widgetId: string, elementId: string) => void) | null;
|
|
106
|
+
/** Fired when the user presses Escape in Selector mode. */
|
|
107
|
+
onCloseWidgetSelector?: ((widgetId: string) => void) | null;
|
|
96
108
|
}
|
|
97
109
|
|
|
98
110
|
let {
|
|
@@ -130,6 +142,11 @@
|
|
|
130
142
|
/** Per-widget publish-gate reason for a Draft row (tooltip on Publish). */
|
|
131
143
|
resolvePublishDisabledReason = null,
|
|
132
144
|
isGenerating = false,
|
|
145
|
+
resolveSelectorEnabled = null,
|
|
146
|
+
resolveSelectedElements = null,
|
|
147
|
+
onSelectWidgetElement = null,
|
|
148
|
+
onRemoveWidgetElement = null,
|
|
149
|
+
onCloseWidgetSelector = null,
|
|
133
150
|
}: Props = $props();
|
|
134
151
|
|
|
135
152
|
const DRAG_BAR_H = 30;
|
|
@@ -285,6 +302,8 @@
|
|
|
285
302
|
let localSelectedVersionByWidgetId = $state<Record<string, string>>({});
|
|
286
303
|
/** Hide Fabric placeholder flash — show loader until iframe fires load. */
|
|
287
304
|
let iframeLoadedByWidgetId = $state<Record<string, boolean>>({});
|
|
305
|
+
/** Live iframe DOM element references per widget for element selector inspection. */
|
|
306
|
+
let iframeElementByWidgetId = $state<Record<string, HTMLIFrameElement | null>>({});
|
|
288
307
|
|
|
289
308
|
export function sync(): void {
|
|
290
309
|
if (!enabled || !getRects || !resolveUrl) {
|
|
@@ -448,6 +467,7 @@
|
|
|
448
467
|
// An old load event must not mark the replacement loaded or claim its timer.
|
|
449
468
|
if (!matchesFrame()) return;
|
|
450
469
|
iframeLoadedByWidgetId = { ...iframeLoadedByWidgetId, [widgetId]: true };
|
|
470
|
+
iframeElementByWidgetId = { ...iframeElementByWidgetId, [widgetId]: iframe };
|
|
451
471
|
// Suppress auto-refresh / screenshot capture when code generation is running.
|
|
452
472
|
if (isGenerating) {
|
|
453
473
|
cancelPendingRefresh(widgetId);
|
|
@@ -974,7 +994,7 @@
|
|
|
974
994
|
focus, while the iframe keeps receiving the pointer events themselves. -->
|
|
975
995
|
<iframe
|
|
976
996
|
class="widget-shape-live-preview-iframe pointer-events-auto absolute border-0 bg-white"
|
|
977
|
-
class:pointer-events-none={blockIframePointer || selectedPreviewBlocked}
|
|
997
|
+
class:pointer-events-none={blockIframePointer || selectedPreviewBlocked || !!resolveSelectorEnabled?.(overlay.widgetId)}
|
|
978
998
|
class:opacity-0={selectedPreviewBlocked}
|
|
979
999
|
style="left: {IFRAME_INSET_PX}px; top: {IFRAME_INSET_PX}px; right: {IFRAME_INSET_PX}px; /* rtl-ok: the same inset on both sides of the frame */ bottom: {IFRAME_INSET_PX}px; width: calc(100% - {IFRAME_INSET_PX * 2}px); height: calc(100% - {IFRAME_INSET_PX * 2}px);"
|
|
980
1000
|
title={$t('creator.livePreviews.liveWidgetPreview')}
|
|
@@ -986,6 +1006,25 @@
|
|
|
986
1006
|
use:trackIframeSelection={overlay.widgetId}
|
|
987
1007
|
onload={(event) => markIframeLoaded(overlay.widgetId, event.currentTarget)}
|
|
988
1008
|
></iframe>
|
|
1009
|
+
{#if (void titleRevision, resolveSelectorEnabled?.(overlay.widgetId) || (resolveSelectedElements?.(overlay.widgetId)?.length ?? 0) > 0)}
|
|
1010
|
+
<div
|
|
1011
|
+
class="widget-shape-element-selector-host pointer-events-none absolute overflow-visible"
|
|
1012
|
+
style="left: {IFRAME_INSET_PX}px; top: {IFRAME_INSET_PX}px; right: {IFRAME_INSET_PX}px; /* rtl-ok: the same inset on both sides of the frame */ bottom: {IFRAME_INSET_PX}px; width: calc(100% - {IFRAME_INSET_PX * 2}px); height: calc(100% - {IFRAME_INSET_PX * 2}px);"
|
|
1013
|
+
>
|
|
1014
|
+
<WidgetElementSelector
|
|
1015
|
+
widgetId={overlay.widgetId}
|
|
1016
|
+
enabled={!!resolveSelectorEnabled?.(overlay.widgetId)}
|
|
1017
|
+
iframeElement={iframeElementByWidgetId[overlay.widgetId] ?? null}
|
|
1018
|
+
iframeSrc={buildIframeSrc(overlay.src, overlay.widgetId)}
|
|
1019
|
+
viewportWidth={Math.max(1, frameWidth - IFRAME_INSET_PX * 2)}
|
|
1020
|
+
viewportHeight={Math.max(1, frameHeight - IFRAME_INSET_PX * 2)}
|
|
1021
|
+
selectedElements={resolveSelectedElements?.(overlay.widgetId) ?? []}
|
|
1022
|
+
onSelectElement={(element) => onSelectWidgetElement?.(overlay.widgetId, element)}
|
|
1023
|
+
onRemoveElement={(elementId) => onRemoveWidgetElement?.(overlay.widgetId, elementId)}
|
|
1024
|
+
onCloseSelector={() => onCloseWidgetSelector?.(overlay.widgetId)}
|
|
1025
|
+
/>
|
|
1026
|
+
</div>
|
|
1027
|
+
{/if}
|
|
989
1028
|
{/key}
|
|
990
1029
|
{/if}
|
|
991
1030
|
{#if previewState?.error && !previewState.loading}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { SelectedWidgetElementReference } from '../utils/widgetElementSelector';
|
|
1
2
|
interface VersionOption {
|
|
2
3
|
id: string;
|
|
3
4
|
/** Full list label, e.g. "Version 9". */
|
|
@@ -93,6 +94,16 @@ interface Props {
|
|
|
93
94
|
resolvePublishDisabledReason?: ((widgetId: string, version: number) => string | null) | null;
|
|
94
95
|
/** Whether code generation is running (suppresses background captures / reloads). */
|
|
95
96
|
isGenerating?: boolean;
|
|
97
|
+
/** Whether the interactive DOM Element Selector is active for this widget. */
|
|
98
|
+
resolveSelectorEnabled?: ((widgetId: string) => boolean) | null;
|
|
99
|
+
/** Selected widget elements currently referenced in the chat panel for this widget. */
|
|
100
|
+
resolveSelectedElements?: ((widgetId: string) => SelectedWidgetElementReference[]) | null;
|
|
101
|
+
/** Fired when the user clicks a widget part in Selector mode. */
|
|
102
|
+
onSelectWidgetElement?: ((widgetId: string, element: SelectedWidgetElementReference) => void) | null;
|
|
103
|
+
/** Fired when the user removes a selected element chip from the canvas overlay. */
|
|
104
|
+
onRemoveWidgetElement?: ((widgetId: string, elementId: string) => void) | null;
|
|
105
|
+
/** Fired when the user presses Escape in Selector mode. */
|
|
106
|
+
onCloseWidgetSelector?: ((widgetId: string) => void) | null;
|
|
96
107
|
}
|
|
97
108
|
declare const WidgetShapeLivePreviews: import("svelte").Component<Props, {
|
|
98
109
|
requestReload: (widgetId: string, options?: {
|