@lego-build/plugins 0.0.10 → 0.0.11

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/auto.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  initIframeBridge
3
- } from "./chunk-FJKEG5SO.js";
3
+ } from "./chunk-SYZFFTAJ.js";
4
4
 
5
5
  // src/auto.ts
6
6
  initIframeBridge();
@@ -0,0 +1,494 @@
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 enableInlineEditing(state, element, onSave, onCancel) {
92
+ const originalOutline = element.style.outline;
93
+ const originalBoxShadow = element.style.boxShadow;
94
+ const originalBackground = element.style.background;
95
+ element.contentEditable = "true";
96
+ element.style.outline = "2px solid #3b82f6";
97
+ element.style.boxShadow = "0 0 0 4px rgba(59, 130, 246, 0.2)";
98
+ element.style.background = "rgba(59, 130, 246, 0.05)";
99
+ element.style.borderRadius = "4px";
100
+ const marker = document.createElement("div");
101
+ marker.id = "lego-inline-editor";
102
+ marker.style.display = "none";
103
+ marker.dataset.originalOutline = originalOutline;
104
+ marker.dataset.originalBoxShadow = originalBoxShadow;
105
+ marker.dataset.originalBackground = originalBackground;
106
+ document.body.appendChild(marker);
107
+ element.focus();
108
+ const range = document.createRange();
109
+ range.selectNodeContents(element);
110
+ range.collapse(false);
111
+ const selection = window.getSelection();
112
+ selection?.removeAllRanges();
113
+ selection?.addRange(range);
114
+ const handleKeyDown = (e) => {
115
+ if (e.key === "Escape") {
116
+ e.preventDefault();
117
+ e.stopPropagation();
118
+ onCancel();
119
+ } else if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
120
+ e.preventDefault();
121
+ e.stopPropagation();
122
+ onSave();
123
+ }
124
+ };
125
+ const handleBlur = (e) => {
126
+ setTimeout(() => {
127
+ if (document.activeElement !== element && state.isEditing) {
128
+ onSave();
129
+ }
130
+ }, 100);
131
+ };
132
+ element.addEventListener("keydown", handleKeyDown);
133
+ element.addEventListener("blur", handleBlur);
134
+ marker.__cleanup = () => {
135
+ element.removeEventListener("keydown", handleKeyDown);
136
+ element.removeEventListener("blur", handleBlur);
137
+ element.contentEditable = "false";
138
+ element.style.outline = originalOutline;
139
+ element.style.boxShadow = originalBoxShadow;
140
+ element.style.background = originalBackground;
141
+ element.style.borderRadius = "";
142
+ window.getSelection()?.removeAllRanges();
143
+ };
144
+ return marker;
145
+ }
146
+ function getElementTextContent(element) {
147
+ let text = "";
148
+ for (const node of element.childNodes) {
149
+ if (node.nodeType === Node.TEXT_NODE) {
150
+ text += node.textContent?.trim() || "";
151
+ }
152
+ }
153
+ if (!text && element.children.length === 0) {
154
+ text = element.textContent?.trim() || "";
155
+ }
156
+ return text;
157
+ }
158
+ function getElementDepth(element) {
159
+ let depth = 0;
160
+ let current = element;
161
+ while (current && current !== document.body) {
162
+ depth++;
163
+ current = current.parentElement;
164
+ }
165
+ return depth;
166
+ }
167
+ function updateOverlayPosition(state) {
168
+ if (!state.hoveredElement || !state.overlay || !state.tooltip) return;
169
+ const rect = state.hoveredElement.getBoundingClientRect();
170
+ const viewportWidth = window.innerWidth;
171
+ state.overlay.style.top = `${rect.top}px`;
172
+ state.overlay.style.left = `${rect.left}px`;
173
+ state.overlay.style.width = `${rect.width}px`;
174
+ state.overlay.style.height = `${rect.height}px`;
175
+ let tooltipTop = rect.top - 32;
176
+ let tooltipLeft = rect.left;
177
+ if (tooltipTop < 8) {
178
+ tooltipTop = rect.bottom + 8;
179
+ }
180
+ const estimatedTooltipWidth = state.tooltip.offsetWidth || 150;
181
+ if (tooltipLeft + estimatedTooltipWidth > viewportWidth - 8) {
182
+ tooltipLeft = viewportWidth - estimatedTooltipWidth - 8;
183
+ }
184
+ if (tooltipLeft < 8) {
185
+ tooltipLeft = 8;
186
+ }
187
+ state.tooltip.style.top = `${tooltipTop}px`;
188
+ state.tooltip.style.left = `${tooltipLeft}px`;
189
+ }
190
+ function getElementInfo(element) {
191
+ const filePath = element.getAttribute("data-locator-path") || "";
192
+ const line = element.getAttribute("data-locator-line");
193
+ const depth = getElementDepth(element);
194
+ const id = element.id ? `#${element.id}` : "";
195
+ const classes = element.className && typeof element.className === "string" ? element.className.split(" ").filter((c) => c).map((c) => `.${c}`).join("") : "";
196
+ return {
197
+ tagName: element.tagName.toLowerCase(),
198
+ selector: element.tagName.toLowerCase() + id + classes.slice(0, 50),
199
+ filePath,
200
+ line: line ? parseInt(line) : void 0,
201
+ depth,
202
+ rect: element.getBoundingClientRect()
203
+ };
204
+ }
205
+ function highlightElement(state, element) {
206
+ state.hoveredElement = element;
207
+ if (!state.overlay) {
208
+ state.overlay = createOverlay();
209
+ }
210
+ if (!state.tooltip) {
211
+ state.tooltip = createTooltip();
212
+ }
213
+ state.overlay.style.display = "block";
214
+ state.tooltip.style.display = "block";
215
+ const info = getElementInfo(element);
216
+ state.depth = info.depth;
217
+ state.tooltip.innerHTML = `
218
+ <span style="opacity: 0.8">${info.selector}</span>
219
+ ${info.filePath ? `<span style="margin-left: 8px; opacity: 0.6">\u{1F4C4} ${info.filePath.split("/").pop()}</span>` : ""}
220
+ <span style="margin-left: 8px; opacity: 0.5; font-size: 10px;">Double-click to edit</span>
221
+ `;
222
+ updateOverlayPosition(state);
223
+ sendToParent("HOVER_ELEMENT", {
224
+ tagName: info.tagName,
225
+ rect: info.rect,
226
+ filePath: info.filePath || void 0,
227
+ line: info.line,
228
+ depth: info.depth
229
+ });
230
+ }
231
+ function navigateDOM(state, direction) {
232
+ if (!state.hoveredElement) return;
233
+ let target = null;
234
+ switch (direction) {
235
+ case "parent":
236
+ target = state.hoveredElement.parentElement;
237
+ if (target && target === document.body) target = null;
238
+ break;
239
+ case "child":
240
+ target = state.hoveredElement.firstElementChild;
241
+ break;
242
+ case "next":
243
+ target = state.hoveredElement.nextElementSibling;
244
+ break;
245
+ case "prev":
246
+ target = state.hoveredElement.previousElementSibling;
247
+ break;
248
+ }
249
+ if (target && target !== document.documentElement && target !== document.body) {
250
+ highlightElement(state, target);
251
+ }
252
+ }
253
+ function setupElementSelector() {
254
+ const state = createElementSelectorState();
255
+ function saveInlineEdit() {
256
+ if (!state.inlineEditor || !state.selectedElement) return;
257
+ const newText = getElementTextContent(state.selectedElement);
258
+ const info = getElementInfo(state.selectedElement);
259
+ if (newText !== state.originalText && info.filePath) {
260
+ sendToParent("SAVE_INLINE_EDIT", {
261
+ filePath: info.filePath,
262
+ originalContent: state.originalText,
263
+ newContent: newText,
264
+ line: info.line
265
+ });
266
+ }
267
+ exitEditMode();
268
+ }
269
+ function cancelInlineEdit() {
270
+ if (state.selectedElement) {
271
+ for (const node of state.selectedElement.childNodes) {
272
+ if (node.nodeType === Node.TEXT_NODE) {
273
+ node.textContent = state.originalText;
274
+ break;
275
+ }
276
+ }
277
+ if (state.selectedElement.children.length === 0) {
278
+ state.selectedElement.textContent = state.originalText;
279
+ }
280
+ }
281
+ sendToParent("CANCEL_INLINE_EDIT");
282
+ exitEditMode();
283
+ }
284
+ function exitEditMode() {
285
+ state.isEditing = false;
286
+ if (state.inlineEditor) {
287
+ const cleanup = state.inlineEditor.__cleanup;
288
+ if (cleanup) cleanup();
289
+ state.inlineEditor.remove();
290
+ state.inlineEditor = null;
291
+ }
292
+ if (state.isActive && state.hoveredElement) {
293
+ if (state.overlay) state.overlay.style.display = "block";
294
+ if (state.tooltip) state.tooltip.style.display = "block";
295
+ }
296
+ state.selectedElement = null;
297
+ state.originalText = "";
298
+ }
299
+ function enterEditMode() {
300
+ if (!state.selectedElement) return;
301
+ state.isEditing = true;
302
+ if (state.overlay) state.overlay.style.display = "none";
303
+ if (state.tooltip) state.tooltip.style.display = "none";
304
+ state.inlineEditor = enableInlineEditing(state, state.selectedElement, saveInlineEdit, cancelInlineEdit);
305
+ const info = getElementInfo(state.selectedElement);
306
+ sendToParent("START_INLINE_EDIT", {
307
+ tagName: info.tagName,
308
+ filePath: info.filePath,
309
+ line: info.line,
310
+ text: state.originalText
311
+ });
312
+ }
313
+ function selectElement() {
314
+ if (!state.hoveredElement) return;
315
+ state.selectedElement = state.hoveredElement;
316
+ const info = getElementInfo(state.hoveredElement);
317
+ const text = getElementTextContent(state.hoveredElement);
318
+ state.originalText = text;
319
+ sendToParent("CLICK_ELEMENT", {
320
+ tagName: info.tagName,
321
+ filePath: info.filePath,
322
+ line: info.line,
323
+ depth: info.depth,
324
+ text
325
+ });
326
+ }
327
+ function startEditing() {
328
+ if (!state.hoveredElement) return;
329
+ state.selectedElement = state.hoveredElement;
330
+ const info = getElementInfo(state.hoveredElement);
331
+ const text = getElementTextContent(state.hoveredElement);
332
+ state.originalText = text;
333
+ sendToParent("CLICK_ELEMENT", {
334
+ tagName: info.tagName,
335
+ filePath: info.filePath,
336
+ line: info.line,
337
+ depth: info.depth,
338
+ text
339
+ });
340
+ enterEditMode();
341
+ }
342
+ const handleMouseMove = (e) => {
343
+ if (!state.isActive) return;
344
+ const target = e.target;
345
+ if (target.id === "lego-element-selector-overlay" || target.id === "lego-element-selector-tooltip") {
346
+ return;
347
+ }
348
+ if (target === state.hoveredElement) return;
349
+ highlightElement(state, target);
350
+ };
351
+ const handleScroll = () => {
352
+ if (state.isActive && state.hoveredElement) {
353
+ updateOverlayPosition(state);
354
+ }
355
+ };
356
+ const handleResize = () => {
357
+ if (state.isActive && state.hoveredElement) {
358
+ updateOverlayPosition(state);
359
+ }
360
+ };
361
+ const handleKeyDown = (e) => {
362
+ if (!state.isActive) return;
363
+ if (state.isEditing) {
364
+ if (e.key === "Escape") {
365
+ e.preventDefault();
366
+ cancelInlineEdit();
367
+ return;
368
+ }
369
+ return;
370
+ }
371
+ switch (e.key) {
372
+ case "Escape":
373
+ sendToParent("SELECTOR_EXIT", true);
374
+ break;
375
+ case "ArrowUp":
376
+ e.preventDefault();
377
+ navigateDOM(state, "parent");
378
+ break;
379
+ case "ArrowDown":
380
+ e.preventDefault();
381
+ navigateDOM(state, "child");
382
+ break;
383
+ case "ArrowLeft":
384
+ e.preventDefault();
385
+ navigateDOM(state, "prev");
386
+ break;
387
+ case "ArrowRight":
388
+ e.preventDefault();
389
+ navigateDOM(state, "next");
390
+ break;
391
+ case "Enter":
392
+ e.preventDefault();
393
+ selectElement();
394
+ break;
395
+ case "e":
396
+ case "E":
397
+ if (state.hoveredElement) {
398
+ e.preventDefault();
399
+ startEditing();
400
+ }
401
+ break;
402
+ }
403
+ };
404
+ const handleClick = (e) => {
405
+ if (!state.isActive) return;
406
+ if (state.isEditing) {
407
+ const target = e.target;
408
+ if (state.selectedElement?.contains(target) || target === state.selectedElement) {
409
+ return;
410
+ }
411
+ e.preventDefault();
412
+ e.stopPropagation();
413
+ e.stopImmediatePropagation();
414
+ saveInlineEdit();
415
+ return;
416
+ }
417
+ e.preventDefault();
418
+ e.stopPropagation();
419
+ e.stopImmediatePropagation();
420
+ selectElement();
421
+ };
422
+ const handleDoubleClick = (e) => {
423
+ if (!state.isActive || state.isEditing) return;
424
+ e.preventDefault();
425
+ e.stopPropagation();
426
+ e.stopImmediatePropagation();
427
+ startEditing();
428
+ };
429
+ const activateSelector = () => {
430
+ state.isActive = true;
431
+ document.addEventListener("mousemove", handleMouseMove, true);
432
+ document.addEventListener("click", handleClick, true);
433
+ document.addEventListener("dblclick", handleDoubleClick, true);
434
+ document.addEventListener("keydown", handleKeyDown, true);
435
+ window.addEventListener("scroll", handleScroll, true);
436
+ window.addEventListener("resize", handleResize);
437
+ document.body.style.cursor = "crosshair";
438
+ state.overlay = createOverlay();
439
+ state.tooltip = createTooltip();
440
+ };
441
+ const deactivateSelector = () => {
442
+ state.isActive = false;
443
+ state.isEditing = false;
444
+ document.removeEventListener("mousemove", handleMouseMove, true);
445
+ document.removeEventListener("click", handleClick, true);
446
+ document.removeEventListener("dblclick", handleDoubleClick, true);
447
+ document.removeEventListener("keydown", handleKeyDown, true);
448
+ window.removeEventListener("scroll", handleScroll, true);
449
+ window.removeEventListener("resize", handleResize);
450
+ if (state.overlay) {
451
+ state.overlay.remove();
452
+ state.overlay = null;
453
+ }
454
+ if (state.tooltip) {
455
+ state.tooltip.remove();
456
+ state.tooltip = null;
457
+ }
458
+ if (state.inlineEditor) {
459
+ state.inlineEditor.remove();
460
+ state.inlineEditor = null;
461
+ }
462
+ state.hoveredElement = null;
463
+ state.selectedElement = null;
464
+ state.originalText = "";
465
+ document.body.style.cursor = "";
466
+ state.depth = 0;
467
+ };
468
+ window.addEventListener("message", (e) => {
469
+ const msg = e.data;
470
+ if (msg.type === "TOGGLE_ELEMENT_SELECTOR") {
471
+ if (msg.payload) {
472
+ activateSelector();
473
+ } else {
474
+ deactivateSelector();
475
+ }
476
+ }
477
+ });
478
+ }
479
+ function initIframeBridge() {
480
+ try {
481
+ if (window.parent !== window) {
482
+ setupIframeBridge();
483
+ setupElementSelector();
484
+ }
485
+ } catch {
486
+ }
487
+ }
488
+
489
+ export {
490
+ sendToParent,
491
+ setupIframeBridge,
492
+ setupElementSelector,
493
+ initIframeBridge
494
+ };
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  sendToParent,
4
4
  setupElementSelector,
5
5
  setupIframeBridge
6
- } from "./chunk-FJKEG5SO.js";
6
+ } from "./chunk-SYZFFTAJ.js";
7
7
  export {
8
8
  initIframeBridge,
9
9
  sendToParent,
package/dist/react.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import {
3
3
  initIframeBridge
4
- } from "./chunk-FJKEG5SO.js";
4
+ } from "./chunk-SYZFFTAJ.js";
5
5
 
6
6
  // src/react.tsx
7
7
  import { useEffect } from "react";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lego-build/plugins",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",