@lego-build/plugins 0.0.13 → 0.0.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.
@@ -0,0 +1,568 @@
1
+ // src/index.ts
2
+ function sendToParent(type, payload) {
3
+ window.parent.postMessage({ type, payload }, "*");
4
+ }
5
+ function setupIframeBridge() {
6
+ const pushState = window.history.pushState.bind(window.history);
7
+ const originalPushState = (...args) => {
8
+ pushState(...args);
9
+ sendToParent("IFRAME_URL_CHANGED", window.location.href);
10
+ };
11
+ window.history.pushState = originalPushState;
12
+ const replaceState = window.history.replaceState.bind(window.history);
13
+ const originalReplaceState = (...args) => {
14
+ replaceState(...args);
15
+ sendToParent("IFRAME_URL_CHANGED", window.location.href);
16
+ };
17
+ window.history.replaceState = originalReplaceState;
18
+ window.addEventListener("popstate", () => {
19
+ sendToParent("IFRAME_URL_CHANGED", window.location.href);
20
+ });
21
+ window.addEventListener("message", (e) => {
22
+ const msg = e.data;
23
+ if (!msg?.type) return;
24
+ switch (msg.type) {
25
+ case "NAVIGATE_BACK":
26
+ window.history.back();
27
+ break;
28
+ case "NAVIGATE_FORWARD":
29
+ window.history.forward();
30
+ break;
31
+ case "NAVIGATE_URL":
32
+ if (msg.payload) {
33
+ window.history.pushState({}, "", msg.payload);
34
+ sendToParent("IFRAME_URL_CHANGED", window.location.href);
35
+ }
36
+ break;
37
+ case "REFRESH":
38
+ window.location.reload();
39
+ break;
40
+ }
41
+ });
42
+ }
43
+ function createElementSelectorState() {
44
+ return {
45
+ isActive: false,
46
+ isEditing: false,
47
+ isSelected: false,
48
+ hoveredElement: null,
49
+ selectedElement: null,
50
+ overlay: null,
51
+ selectionOverlay: null,
52
+ tooltip: null,
53
+ inlineEditor: null,
54
+ depth: 0,
55
+ originalText: "",
56
+ originalHTML: ""
57
+ };
58
+ }
59
+ function createOverlay() {
60
+ const overlay = document.createElement("div");
61
+ overlay.id = "lego-element-selector-overlay";
62
+ overlay.style.cssText = `
63
+ position: fixed;
64
+ pointer-events: none;
65
+ z-index: 2147483645;
66
+ background: rgba(59, 130, 246, 0.1);
67
+ border: 2px solid #3b82f6;
68
+ border-radius: 4px;
69
+ transition: all 0.1s ease-out;
70
+ `;
71
+ document.body.appendChild(overlay);
72
+ return overlay;
73
+ }
74
+ function createSelectionOverlay() {
75
+ const overlay = document.createElement("div");
76
+ overlay.id = "lego-element-selector-selection";
77
+ overlay.style.cssText = `
78
+ position: fixed;
79
+ pointer-events: none;
80
+ z-index: 2147483646;
81
+ background: rgba(34, 197, 94, 0.15);
82
+ border: 2px solid #22c55e;
83
+ border-radius: 4px;
84
+ box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2);
85
+ transition: all 0.15s ease-out;
86
+ `;
87
+ document.body.appendChild(overlay);
88
+ return overlay;
89
+ }
90
+ function createTooltip() {
91
+ const tooltip = document.createElement("div");
92
+ tooltip.id = "lego-element-selector-tooltip";
93
+ tooltip.style.cssText = `
94
+ position: fixed;
95
+ pointer-events: none;
96
+ z-index: 2147483647;
97
+ background: #3b82f6;
98
+ color: white;
99
+ padding: 4px 8px;
100
+ border-radius: 4px;
101
+ font-family: ui-monospace, monospace;
102
+ font-size: 12px;
103
+ white-space: nowrap;
104
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
105
+ transition: all 0.1s ease-out;
106
+ `;
107
+ document.body.appendChild(tooltip);
108
+ return tooltip;
109
+ }
110
+ function enableInlineEditing(state, element, onSave, onCancel) {
111
+ const originalOutline = element.style.outline;
112
+ const originalBoxShadow = element.style.boxShadow;
113
+ const originalBackground = element.style.background;
114
+ element.contentEditable = "true";
115
+ element.style.outline = "2px solid #3b82f6";
116
+ element.style.boxShadow = "0 0 0 4px rgba(59, 130, 246, 0.2)";
117
+ element.style.background = "rgba(59, 130, 246, 0.05)";
118
+ element.style.borderRadius = "4px";
119
+ const marker = document.createElement("div");
120
+ marker.id = "lego-inline-editor";
121
+ marker.style.display = "none";
122
+ marker.dataset.originalOutline = originalOutline;
123
+ marker.dataset.originalBoxShadow = originalBoxShadow;
124
+ marker.dataset.originalBackground = originalBackground;
125
+ document.body.appendChild(marker);
126
+ element.focus();
127
+ const range = document.createRange();
128
+ range.selectNodeContents(element);
129
+ range.collapse(false);
130
+ const selection = window.getSelection();
131
+ selection?.removeAllRanges();
132
+ selection?.addRange(range);
133
+ const handleKeyDown = (e) => {
134
+ if (e.key === "Escape") {
135
+ e.preventDefault();
136
+ e.stopPropagation();
137
+ onCancel();
138
+ } else if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
139
+ e.preventDefault();
140
+ e.stopPropagation();
141
+ onSave();
142
+ }
143
+ };
144
+ const handleBlur = (e) => {
145
+ setTimeout(() => {
146
+ if (document.activeElement !== element && state.isEditing) {
147
+ onSave();
148
+ }
149
+ }, 100);
150
+ };
151
+ element.addEventListener("keydown", handleKeyDown);
152
+ element.addEventListener("blur", handleBlur);
153
+ marker.__cleanup = () => {
154
+ element.removeEventListener("keydown", handleKeyDown);
155
+ element.removeEventListener("blur", handleBlur);
156
+ element.contentEditable = "false";
157
+ element.style.outline = originalOutline;
158
+ element.style.boxShadow = originalBoxShadow;
159
+ element.style.background = originalBackground;
160
+ element.style.borderRadius = "";
161
+ window.getSelection()?.removeAllRanges();
162
+ };
163
+ return marker;
164
+ }
165
+ function getElementTextContent(element) {
166
+ return element.innerText?.trim() || element.textContent?.trim() || "";
167
+ }
168
+ function getElementDepth(element) {
169
+ let depth = 0;
170
+ let current = element;
171
+ while (current && current !== document.body) {
172
+ depth++;
173
+ current = current.parentElement;
174
+ }
175
+ return depth;
176
+ }
177
+ function updateOverlayPosition(state, element, overlay) {
178
+ const targetElement = element || state.hoveredElement;
179
+ const targetOverlay = overlay || state.overlay;
180
+ if (!targetElement || !targetOverlay) return;
181
+ const rect = targetElement.getBoundingClientRect();
182
+ targetOverlay.style.top = `${rect.top}px`;
183
+ targetOverlay.style.left = `${rect.left}px`;
184
+ targetOverlay.style.width = `${rect.width}px`;
185
+ targetOverlay.style.height = `${rect.height}px`;
186
+ }
187
+ function updateTooltipPosition(state) {
188
+ if (!state.hoveredElement || !state.tooltip) return;
189
+ const rect = state.hoveredElement.getBoundingClientRect();
190
+ const viewportWidth = window.innerWidth;
191
+ let tooltipTop = rect.top - 32;
192
+ let tooltipLeft = rect.left;
193
+ if (tooltipTop < 8) {
194
+ tooltipTop = rect.bottom + 8;
195
+ }
196
+ const estimatedTooltipWidth = state.tooltip.offsetWidth || 150;
197
+ if (tooltipLeft + estimatedTooltipWidth > viewportWidth - 8) {
198
+ tooltipLeft = viewportWidth - estimatedTooltipWidth - 8;
199
+ }
200
+ if (tooltipLeft < 8) {
201
+ tooltipLeft = 8;
202
+ }
203
+ state.tooltip.style.top = `${tooltipTop}px`;
204
+ state.tooltip.style.left = `${tooltipLeft}px`;
205
+ }
206
+ function updateSelectionOverlayPosition(state) {
207
+ if (!state.selectedElement || !state.selectionOverlay) return;
208
+ updateOverlayPosition(state, state.selectedElement, state.selectionOverlay);
209
+ }
210
+ function getElementInfo(element) {
211
+ const filePath = element.getAttribute("data-locator-path") || "";
212
+ const line = element.getAttribute("data-locator-line");
213
+ const depth = getElementDepth(element);
214
+ const id = element.id ? `#${element.id}` : "";
215
+ const classes = element.className && typeof element.className === "string" ? element.className.split(" ").filter((c) => c).map((c) => `.${c}`).join("") : "";
216
+ return {
217
+ tagName: element.tagName.toLowerCase(),
218
+ selector: element.tagName.toLowerCase() + id + classes.slice(0, 50),
219
+ filePath,
220
+ line: line ? parseInt(line) : void 0,
221
+ depth,
222
+ rect: element.getBoundingClientRect()
223
+ };
224
+ }
225
+ function highlightElement(state, element) {
226
+ state.hoveredElement = element;
227
+ if (!state.overlay) {
228
+ state.overlay = createOverlay();
229
+ }
230
+ if (!state.tooltip) {
231
+ state.tooltip = createTooltip();
232
+ }
233
+ state.overlay.style.display = "block";
234
+ state.tooltip.style.display = "block";
235
+ const info = getElementInfo(element);
236
+ state.depth = info.depth;
237
+ const isSelectedElement = state.isSelected && state.selectedElement === element;
238
+ let actionHint = "";
239
+ if (isSelectedElement) {
240
+ actionHint = `<span style="margin-left: 8px; color: #22c55e; font-size: 10px;">\u2713 Selected \xB7 Click to edit</span>`;
241
+ } else if (state.isSelected) {
242
+ actionHint = `<span style="margin-left: 8px; opacity: 0.5; font-size: 10px;">Click to select</span>`;
243
+ } else {
244
+ actionHint = `<span style="margin-left: 8px; opacity: 0.5; font-size: 10px;">Click to select \xB7 Double-click to edit</span>`;
245
+ }
246
+ state.tooltip.innerHTML = `
247
+ <span style="opacity: 0.8">${info.selector}</span>
248
+ ${info.filePath ? `<span style="margin-left: 8px; opacity: 0.6">\u{1F4C4} ${info.filePath.split("/").pop()}</span>` : ""}
249
+ ${actionHint}
250
+ `;
251
+ updateOverlayPosition(state);
252
+ updateTooltipPosition(state);
253
+ sendToParent("HOVER_ELEMENT", {
254
+ tagName: info.tagName,
255
+ rect: info.rect,
256
+ filePath: info.filePath || void 0,
257
+ line: info.line,
258
+ depth: info.depth
259
+ });
260
+ }
261
+ function navigateDOM(state, direction) {
262
+ if (!state.hoveredElement) return;
263
+ let target = null;
264
+ switch (direction) {
265
+ case "parent":
266
+ target = state.hoveredElement.parentElement;
267
+ if (target && target === document.body) target = null;
268
+ break;
269
+ case "child":
270
+ target = state.hoveredElement.firstElementChild;
271
+ break;
272
+ case "next":
273
+ target = state.hoveredElement.nextElementSibling;
274
+ break;
275
+ case "prev":
276
+ target = state.hoveredElement.previousElementSibling;
277
+ break;
278
+ }
279
+ if (target && target !== document.documentElement && target !== document.body) {
280
+ highlightElement(state, target);
281
+ }
282
+ }
283
+ function setupElementSelector() {
284
+ const state = createElementSelectorState();
285
+ function saveInlineEdit() {
286
+ if (!state.inlineEditor || !state.selectedElement) return;
287
+ const newText = getElementTextContent(state.selectedElement);
288
+ const info = getElementInfo(state.selectedElement);
289
+ if (newText !== state.originalText && info.filePath) {
290
+ sendToParent("SAVE_INLINE_EDIT", {
291
+ filePath: info.filePath,
292
+ originalContent: state.originalText,
293
+ newContent: newText,
294
+ line: info.line
295
+ });
296
+ }
297
+ exitEditMode();
298
+ }
299
+ function cancelInlineEdit() {
300
+ if (state.selectedElement && state.originalHTML !== void 0) {
301
+ state.selectedElement.innerHTML = state.originalHTML;
302
+ }
303
+ sendToParent("CANCEL_INLINE_EDIT");
304
+ exitEditMode();
305
+ }
306
+ function exitEditMode() {
307
+ state.isEditing = false;
308
+ if (state.inlineEditor) {
309
+ const cleanup = state.inlineEditor.__cleanup;
310
+ if (cleanup) cleanup();
311
+ state.inlineEditor.remove();
312
+ state.inlineEditor = null;
313
+ }
314
+ clearSelection();
315
+ if (state.isActive && state.hoveredElement) {
316
+ if (state.overlay) state.overlay.style.display = "block";
317
+ if (state.tooltip) state.tooltip.style.display = "block";
318
+ }
319
+ }
320
+ function enterEditMode() {
321
+ if (!state.selectedElement) return;
322
+ state.isEditing = true;
323
+ state.originalHTML = state.selectedElement.innerHTML;
324
+ if (state.overlay) state.overlay.style.display = "none";
325
+ if (state.tooltip) state.tooltip.style.display = "none";
326
+ if (state.selectionOverlay) state.selectionOverlay.style.display = "none";
327
+ state.inlineEditor = enableInlineEditing(state, state.selectedElement, saveInlineEdit, cancelInlineEdit);
328
+ const info = getElementInfo(state.selectedElement);
329
+ sendToParent("START_INLINE_EDIT", {
330
+ tagName: info.tagName,
331
+ filePath: info.filePath,
332
+ line: info.line,
333
+ text: state.originalText
334
+ });
335
+ }
336
+ function clearSelection() {
337
+ state.isSelected = false;
338
+ state.selectedElement = null;
339
+ state.originalText = "";
340
+ state.originalHTML = "";
341
+ if (state.selectionOverlay) {
342
+ state.selectionOverlay.style.display = "none";
343
+ }
344
+ }
345
+ function selectElement() {
346
+ if (!state.hoveredElement) return;
347
+ if (state.isSelected && state.selectedElement === state.hoveredElement) {
348
+ enterEditMode();
349
+ return;
350
+ }
351
+ if (state.isSelected) {
352
+ clearSelection();
353
+ }
354
+ state.isSelected = true;
355
+ state.selectedElement = state.hoveredElement;
356
+ const info = getElementInfo(state.hoveredElement);
357
+ const text = getElementTextContent(state.hoveredElement);
358
+ state.originalText = text;
359
+ if (!state.selectionOverlay) {
360
+ state.selectionOverlay = createSelectionOverlay();
361
+ }
362
+ state.selectionOverlay.style.display = "block";
363
+ updateSelectionOverlayPosition(state);
364
+ if (state.tooltip) {
365
+ state.tooltip.innerHTML = `
366
+ <span style="opacity: 0.8">${info.selector}</span>
367
+ ${info.filePath ? `<span style="margin-left: 8px; opacity: 0.6">\u{1F4C4} ${info.filePath.split("/").pop()}</span>` : ""}
368
+ <span style="margin-left: 8px; color: #22c55e; font-size: 10px;">\u2713 Selected \xB7 Click again to edit</span>
369
+ `;
370
+ }
371
+ sendToParent("CLICK_ELEMENT", {
372
+ tagName: info.tagName,
373
+ filePath: info.filePath,
374
+ line: info.line,
375
+ depth: info.depth,
376
+ text
377
+ });
378
+ }
379
+ function startEditing() {
380
+ if (!state.hoveredElement) return;
381
+ state.selectedElement = state.hoveredElement;
382
+ const info = getElementInfo(state.hoveredElement);
383
+ const text = getElementTextContent(state.hoveredElement);
384
+ state.originalText = text;
385
+ sendToParent("CLICK_ELEMENT", {
386
+ tagName: info.tagName,
387
+ filePath: info.filePath,
388
+ line: info.line,
389
+ depth: info.depth,
390
+ text
391
+ });
392
+ enterEditMode();
393
+ }
394
+ const handleMouseMove = (e) => {
395
+ if (!state.isActive) return;
396
+ const target = e.target;
397
+ if (target.id === "lego-element-selector-overlay" || target.id === "lego-element-selector-tooltip" || target.id === "lego-element-selector-selection") {
398
+ return;
399
+ }
400
+ if (target === state.hoveredElement) return;
401
+ highlightElement(state, target);
402
+ };
403
+ const handleScroll = () => {
404
+ if (state.isActive && state.hoveredElement) {
405
+ updateOverlayPosition(state);
406
+ updateTooltipPosition(state);
407
+ }
408
+ if (state.isSelected && state.selectedElement) {
409
+ updateSelectionOverlayPosition(state);
410
+ }
411
+ };
412
+ const handleResize = () => {
413
+ if (state.isActive && state.hoveredElement) {
414
+ updateOverlayPosition(state);
415
+ updateTooltipPosition(state);
416
+ }
417
+ if (state.isSelected && state.selectedElement) {
418
+ updateSelectionOverlayPosition(state);
419
+ }
420
+ };
421
+ const handleKeyDown = (e) => {
422
+ if (!state.isActive) return;
423
+ if (state.isEditing) {
424
+ if (e.key === "Escape") {
425
+ e.preventDefault();
426
+ cancelInlineEdit();
427
+ return;
428
+ }
429
+ return;
430
+ }
431
+ switch (e.key) {
432
+ case "Escape":
433
+ if (state.isSelected) {
434
+ e.preventDefault();
435
+ clearSelection();
436
+ } else {
437
+ sendToParent("SELECTOR_EXIT", true);
438
+ }
439
+ break;
440
+ case "ArrowUp":
441
+ e.preventDefault();
442
+ navigateDOM(state, "parent");
443
+ break;
444
+ case "ArrowDown":
445
+ e.preventDefault();
446
+ navigateDOM(state, "child");
447
+ break;
448
+ case "ArrowLeft":
449
+ e.preventDefault();
450
+ navigateDOM(state, "prev");
451
+ break;
452
+ case "ArrowRight":
453
+ e.preventDefault();
454
+ navigateDOM(state, "next");
455
+ break;
456
+ case "Enter":
457
+ e.preventDefault();
458
+ if (state.isSelected && state.selectedElement === state.hoveredElement) {
459
+ enterEditMode();
460
+ } else {
461
+ selectElement();
462
+ }
463
+ break;
464
+ case "e":
465
+ case "E":
466
+ if (state.hoveredElement) {
467
+ e.preventDefault();
468
+ startEditing();
469
+ }
470
+ break;
471
+ }
472
+ };
473
+ const handleClick = (e) => {
474
+ if (!state.isActive) return;
475
+ if (state.isEditing) {
476
+ const target = e.target;
477
+ if (state.selectedElement?.contains(target) || target === state.selectedElement) {
478
+ return;
479
+ }
480
+ e.preventDefault();
481
+ e.stopPropagation();
482
+ e.stopImmediatePropagation();
483
+ saveInlineEdit();
484
+ return;
485
+ }
486
+ e.preventDefault();
487
+ e.stopPropagation();
488
+ e.stopImmediatePropagation();
489
+ selectElement();
490
+ };
491
+ const handleDoubleClick = (e) => {
492
+ if (!state.isActive || state.isEditing) return;
493
+ e.preventDefault();
494
+ e.stopPropagation();
495
+ e.stopImmediatePropagation();
496
+ startEditing();
497
+ };
498
+ const activateSelector = () => {
499
+ state.isActive = true;
500
+ document.addEventListener("mousemove", handleMouseMove, true);
501
+ document.addEventListener("click", handleClick, true);
502
+ document.addEventListener("dblclick", handleDoubleClick, true);
503
+ document.addEventListener("keydown", handleKeyDown, true);
504
+ window.addEventListener("scroll", handleScroll, true);
505
+ window.addEventListener("resize", handleResize);
506
+ document.body.style.cursor = "crosshair";
507
+ state.overlay = createOverlay();
508
+ state.tooltip = createTooltip();
509
+ };
510
+ const deactivateSelector = () => {
511
+ state.isActive = false;
512
+ state.isEditing = false;
513
+ state.isSelected = false;
514
+ document.removeEventListener("mousemove", handleMouseMove, true);
515
+ document.removeEventListener("click", handleClick, true);
516
+ document.removeEventListener("dblclick", handleDoubleClick, true);
517
+ document.removeEventListener("keydown", handleKeyDown, true);
518
+ window.removeEventListener("scroll", handleScroll, true);
519
+ window.removeEventListener("resize", handleResize);
520
+ if (state.overlay) {
521
+ state.overlay.remove();
522
+ state.overlay = null;
523
+ }
524
+ if (state.selectionOverlay) {
525
+ state.selectionOverlay.remove();
526
+ state.selectionOverlay = null;
527
+ }
528
+ if (state.tooltip) {
529
+ state.tooltip.remove();
530
+ state.tooltip = null;
531
+ }
532
+ if (state.inlineEditor) {
533
+ state.inlineEditor.remove();
534
+ state.inlineEditor = null;
535
+ }
536
+ state.hoveredElement = null;
537
+ state.selectedElement = null;
538
+ state.originalText = "";
539
+ document.body.style.cursor = "";
540
+ state.depth = 0;
541
+ };
542
+ window.addEventListener("message", (e) => {
543
+ const msg = e.data;
544
+ if (msg.type === "TOGGLE_ELEMENT_SELECTOR") {
545
+ if (msg.payload) {
546
+ activateSelector();
547
+ } else {
548
+ deactivateSelector();
549
+ }
550
+ }
551
+ });
552
+ }
553
+ function initIframeBridge() {
554
+ try {
555
+ if (window.parent !== window) {
556
+ setupIframeBridge();
557
+ setupElementSelector();
558
+ }
559
+ } catch {
560
+ }
561
+ }
562
+
563
+ export {
564
+ sendToParent,
565
+ setupIframeBridge,
566
+ setupElementSelector,
567
+ initIframeBridge
568
+ };