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