@lego-build/plugins 0.0.1

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