@lego-build/plugins 0.0.3 → 0.0.6

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