@lego-build/plugins 0.0.2 → 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.
- package/dist/auto.js +3 -10
- package/dist/babel-plugin.d.ts +18 -0
- package/dist/babel-plugin.js +43 -0
- package/dist/{chunk-MZJDRENL.js → chunk-2R3NUXAB.js} +13 -9
- package/dist/chunk-3TRIUWCT.js +539 -0
- package/dist/chunk-42QHGI4F.js +512 -0
- package/dist/chunk-4EX2R46A.js +528 -0
- package/dist/{chunk-RM4HZDF3.js → chunk-6EMRLRSE.js} +12 -5
- package/dist/chunk-6MOTW5WZ.js +505 -0
- package/dist/chunk-J6RRTMEB.js +553 -0
- package/dist/chunk-NYSOJK4V.js +436 -0
- package/dist/chunk-ONSLHF7O.js +553 -0
- package/dist/chunk-QMPWAJBL.js +539 -0
- package/dist/chunk-RG2IIZUF.js +560 -0
- package/dist/chunk-SQ3BWA54.js +307 -0
- package/dist/chunk-URUG7I3S.js +539 -0
- package/dist/chunk-V3KCJYHC.js +468 -0
- package/dist/chunk-XZZFN45X.js +539 -0
- package/dist/index.d.ts +4 -28
- package/dist/index.js +8 -736
- package/dist/react.d.ts +7 -0
- package/dist/react.js +18 -0
- package/dist/vite-plugin.d.ts +16 -0
- package/dist/vite-plugin.js +103 -0
- package/package.json +30 -21
package/dist/auto.js
CHANGED
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from "./chunk-RM4HZDF3.js";
|
|
2
|
+
initIframeBridge
|
|
3
|
+
} from "./chunk-4ILDPPTG.js";
|
|
5
4
|
|
|
6
5
|
// src/auto.ts
|
|
7
|
-
|
|
8
|
-
if (window.parent !== window) {
|
|
9
|
-
setupIframeBridge();
|
|
10
|
-
setupElementSelector();
|
|
11
|
-
}
|
|
12
|
-
} catch {
|
|
13
|
-
}
|
|
6
|
+
initIframeBridge();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { types, PluginObj } from '@babel/core';
|
|
2
|
+
|
|
3
|
+
interface SourceLocatorPluginOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Patterns to exclude from source location injection
|
|
6
|
+
* @default ['node_modules']
|
|
7
|
+
*/
|
|
8
|
+
exclude?: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Babel plugin that adds data-locator-path and data-locator-line attributes
|
|
12
|
+
* to JSX elements for inline editing support.
|
|
13
|
+
*/
|
|
14
|
+
declare function sourceLocatorBabelPlugin({ types }: {
|
|
15
|
+
types: typeof types;
|
|
16
|
+
}, options?: SourceLocatorPluginOptions): PluginObj;
|
|
17
|
+
|
|
18
|
+
export { type SourceLocatorPluginOptions, sourceLocatorBabelPlugin as default, sourceLocatorBabelPlugin };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/babel-plugin.ts
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
function sourceLocatorBabelPlugin({ types }, options = {}) {
|
|
4
|
+
const { exclude = ["node_modules"] } = options;
|
|
5
|
+
return {
|
|
6
|
+
name: "lego-source-locator",
|
|
7
|
+
visitor: {
|
|
8
|
+
JSXOpeningElement(nodePath, state) {
|
|
9
|
+
const filename = state.filename || "";
|
|
10
|
+
for (const pattern of exclude) {
|
|
11
|
+
if (filename.includes(pattern)) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const cwd = state.cwd || process.cwd();
|
|
16
|
+
const relativePath = path.relative(cwd, filename);
|
|
17
|
+
const hasLocatorPath = nodePath.node.attributes.some(
|
|
18
|
+
(attr) => types.isJSXAttribute(attr) && types.isJSXIdentifier(attr.name) && attr.name.name === "data-locator-path"
|
|
19
|
+
);
|
|
20
|
+
if (hasLocatorPath) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const line = nodePath.node.loc?.start.line || 0;
|
|
24
|
+
nodePath.node.attributes.push(
|
|
25
|
+
types.jsxAttribute(
|
|
26
|
+
types.jsxIdentifier("data-locator-path"),
|
|
27
|
+
types.stringLiteral(relativePath)
|
|
28
|
+
)
|
|
29
|
+
);
|
|
30
|
+
nodePath.node.attributes.push(
|
|
31
|
+
types.jsxAttribute(
|
|
32
|
+
types.jsxIdentifier("data-locator-line"),
|
|
33
|
+
types.stringLiteral(String(line))
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
sourceLocatorBabelPlugin as default,
|
|
42
|
+
sourceLocatorBabelPlugin
|
|
43
|
+
};
|
|
@@ -3,18 +3,18 @@ function sendToParent(type, payload) {
|
|
|
3
3
|
window.parent.postMessage({ type, payload }, "*");
|
|
4
4
|
}
|
|
5
5
|
function setupIframeBridge() {
|
|
6
|
-
const pushState = window.history.pushState;
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const pushState = window.history.pushState.bind(window.history);
|
|
7
|
+
const originalPushState = (...args) => {
|
|
8
|
+
pushState(...args);
|
|
9
9
|
sendToParent("IFRAME_URL_CHANGED", window.location.href);
|
|
10
|
-
return ret;
|
|
11
10
|
};
|
|
12
|
-
|
|
13
|
-
window.history.replaceState
|
|
14
|
-
|
|
11
|
+
window.history.pushState = originalPushState;
|
|
12
|
+
const replaceState = window.history.replaceState.bind(window.history);
|
|
13
|
+
const originalReplaceState = (...args) => {
|
|
14
|
+
replaceState(...args);
|
|
15
15
|
sendToParent("IFRAME_URL_CHANGED", window.location.href);
|
|
16
|
-
return ret;
|
|
17
16
|
};
|
|
17
|
+
window.history.replaceState = originalReplaceState;
|
|
18
18
|
window.addEventListener("popstate", () => {
|
|
19
19
|
sendToParent("IFRAME_URL_CHANGED", window.location.href);
|
|
20
20
|
});
|
|
@@ -43,10 +43,14 @@ function setupIframeBridge() {
|
|
|
43
43
|
function createElementSelectorState() {
|
|
44
44
|
return {
|
|
45
45
|
isActive: false,
|
|
46
|
+
isEditing: false,
|
|
46
47
|
hoveredElement: null,
|
|
48
|
+
selectedElement: null,
|
|
47
49
|
overlay: null,
|
|
48
50
|
tooltip: null,
|
|
49
|
-
|
|
51
|
+
inlineEditor: null,
|
|
52
|
+
depth: 0,
|
|
53
|
+
originalText: ""
|
|
50
54
|
};
|
|
51
55
|
}
|
|
52
56
|
function createOverlay() {
|
|
@@ -0,0 +1,539 @@
|
|
|
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, onSave, onCancel) {
|
|
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
|
+
Ctrl+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
|
+
cancelBtn.onclick = (e) => {
|
|
163
|
+
e.preventDefault();
|
|
164
|
+
e.stopPropagation();
|
|
165
|
+
onCancel();
|
|
166
|
+
};
|
|
167
|
+
const saveBtn = document.createElement("button");
|
|
168
|
+
saveBtn.textContent = "Save";
|
|
169
|
+
saveBtn.style.cssText = `
|
|
170
|
+
padding: 6px 12px;
|
|
171
|
+
border: none;
|
|
172
|
+
border-radius: 4px;
|
|
173
|
+
background: #3b82f6;
|
|
174
|
+
color: white;
|
|
175
|
+
font-size: 12px;
|
|
176
|
+
font-weight: 500;
|
|
177
|
+
cursor: pointer;
|
|
178
|
+
transition: all 0.15s;
|
|
179
|
+
`;
|
|
180
|
+
saveBtn.onmouseenter = () => saveBtn.style.background = "#2563eb";
|
|
181
|
+
saveBtn.onmouseleave = () => saveBtn.style.background = "#3b82f6";
|
|
182
|
+
saveBtn.onclick = (e) => {
|
|
183
|
+
e.preventDefault();
|
|
184
|
+
e.stopPropagation();
|
|
185
|
+
onSave();
|
|
186
|
+
};
|
|
187
|
+
editor.appendChild(header);
|
|
188
|
+
editor.appendChild(input);
|
|
189
|
+
buttons.appendChild(cancelBtn);
|
|
190
|
+
buttons.appendChild(saveBtn);
|
|
191
|
+
editor.appendChild(buttons);
|
|
192
|
+
document.body.appendChild(editor);
|
|
193
|
+
setTimeout(() => {
|
|
194
|
+
input.focus();
|
|
195
|
+
input.select();
|
|
196
|
+
}, 0);
|
|
197
|
+
input.addEventListener("keydown", (e) => {
|
|
198
|
+
if (e.key === "Escape") {
|
|
199
|
+
e.preventDefault();
|
|
200
|
+
e.stopPropagation();
|
|
201
|
+
onCancel();
|
|
202
|
+
} else if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
|
|
203
|
+
e.preventDefault();
|
|
204
|
+
e.stopPropagation();
|
|
205
|
+
onSave();
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
input.addEventListener("keyup", (e) => {
|
|
209
|
+
e.stopPropagation();
|
|
210
|
+
});
|
|
211
|
+
input.addEventListener("keypress", (e) => {
|
|
212
|
+
e.stopPropagation();
|
|
213
|
+
});
|
|
214
|
+
return editor;
|
|
215
|
+
}
|
|
216
|
+
function getElementTextContent(element) {
|
|
217
|
+
let text = "";
|
|
218
|
+
for (const node of element.childNodes) {
|
|
219
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
220
|
+
text += node.textContent?.trim() || "";
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (!text && element.children.length === 0) {
|
|
224
|
+
text = element.textContent?.trim() || "";
|
|
225
|
+
}
|
|
226
|
+
return text;
|
|
227
|
+
}
|
|
228
|
+
function getElementDepth(element) {
|
|
229
|
+
let depth = 0;
|
|
230
|
+
let current = element;
|
|
231
|
+
while (current && current !== document.body) {
|
|
232
|
+
depth++;
|
|
233
|
+
current = current.parentElement;
|
|
234
|
+
}
|
|
235
|
+
return depth;
|
|
236
|
+
}
|
|
237
|
+
function updateOverlayPosition(state) {
|
|
238
|
+
if (!state.hoveredElement || !state.overlay || !state.tooltip) return;
|
|
239
|
+
const rect = state.hoveredElement.getBoundingClientRect();
|
|
240
|
+
const viewportWidth = window.innerWidth;
|
|
241
|
+
state.overlay.style.top = `${rect.top}px`;
|
|
242
|
+
state.overlay.style.left = `${rect.left}px`;
|
|
243
|
+
state.overlay.style.width = `${rect.width}px`;
|
|
244
|
+
state.overlay.style.height = `${rect.height}px`;
|
|
245
|
+
let tooltipTop = rect.top - 32;
|
|
246
|
+
let tooltipLeft = rect.left;
|
|
247
|
+
if (tooltipTop < 8) {
|
|
248
|
+
tooltipTop = rect.bottom + 8;
|
|
249
|
+
}
|
|
250
|
+
const estimatedTooltipWidth = state.tooltip.offsetWidth || 150;
|
|
251
|
+
if (tooltipLeft + estimatedTooltipWidth > viewportWidth - 8) {
|
|
252
|
+
tooltipLeft = viewportWidth - estimatedTooltipWidth - 8;
|
|
253
|
+
}
|
|
254
|
+
if (tooltipLeft < 8) {
|
|
255
|
+
tooltipLeft = 8;
|
|
256
|
+
}
|
|
257
|
+
state.tooltip.style.top = `${tooltipTop}px`;
|
|
258
|
+
state.tooltip.style.left = `${tooltipLeft}px`;
|
|
259
|
+
}
|
|
260
|
+
function getElementInfo(element) {
|
|
261
|
+
const filePath = element.getAttribute("data-locator-path") || "";
|
|
262
|
+
const line = element.getAttribute("data-locator-line");
|
|
263
|
+
const depth = getElementDepth(element);
|
|
264
|
+
const id = element.id ? `#${element.id}` : "";
|
|
265
|
+
const classes = element.className && typeof element.className === "string" ? element.className.split(" ").filter((c) => c).map((c) => `.${c}`).join("") : "";
|
|
266
|
+
return {
|
|
267
|
+
tagName: element.tagName.toLowerCase(),
|
|
268
|
+
selector: element.tagName.toLowerCase() + id + classes.slice(0, 50),
|
|
269
|
+
filePath,
|
|
270
|
+
line: line ? parseInt(line) : void 0,
|
|
271
|
+
depth,
|
|
272
|
+
rect: element.getBoundingClientRect()
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function highlightElement(state, element) {
|
|
276
|
+
state.hoveredElement = element;
|
|
277
|
+
if (!state.overlay) {
|
|
278
|
+
state.overlay = createOverlay();
|
|
279
|
+
}
|
|
280
|
+
if (!state.tooltip) {
|
|
281
|
+
state.tooltip = createTooltip();
|
|
282
|
+
}
|
|
283
|
+
state.overlay.style.display = "block";
|
|
284
|
+
state.tooltip.style.display = "block";
|
|
285
|
+
const info = getElementInfo(element);
|
|
286
|
+
state.depth = info.depth;
|
|
287
|
+
state.tooltip.innerHTML = `
|
|
288
|
+
<span style="opacity: 0.8">${info.selector}</span>
|
|
289
|
+
${info.filePath ? `<span style="margin-left: 8px; opacity: 0.6">\u{1F4C4} ${info.filePath.split("/").pop()}</span>` : ""}
|
|
290
|
+
`;
|
|
291
|
+
updateOverlayPosition(state);
|
|
292
|
+
sendToParent("HOVER_ELEMENT", {
|
|
293
|
+
tagName: info.tagName,
|
|
294
|
+
rect: info.rect,
|
|
295
|
+
filePath: info.filePath || void 0,
|
|
296
|
+
line: info.line,
|
|
297
|
+
depth: info.depth
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
function navigateDOM(state, direction) {
|
|
301
|
+
if (!state.hoveredElement) return;
|
|
302
|
+
let target = null;
|
|
303
|
+
switch (direction) {
|
|
304
|
+
case "parent":
|
|
305
|
+
target = state.hoveredElement.parentElement;
|
|
306
|
+
if (target && target === document.body) target = null;
|
|
307
|
+
break;
|
|
308
|
+
case "child":
|
|
309
|
+
target = state.hoveredElement.firstElementChild;
|
|
310
|
+
break;
|
|
311
|
+
case "next":
|
|
312
|
+
target = state.hoveredElement.nextElementSibling;
|
|
313
|
+
break;
|
|
314
|
+
case "prev":
|
|
315
|
+
target = state.hoveredElement.previousElementSibling;
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
if (target && target !== document.documentElement && target !== document.body) {
|
|
319
|
+
highlightElement(state, target);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function setupElementSelector() {
|
|
323
|
+
const state = createElementSelectorState();
|
|
324
|
+
function saveInlineEdit() {
|
|
325
|
+
if (!state.inlineEditor || !state.selectedElement) return;
|
|
326
|
+
const input = state.inlineEditor.querySelector(
|
|
327
|
+
"#lego-inline-editor-input"
|
|
328
|
+
);
|
|
329
|
+
const newText = input?.value || "";
|
|
330
|
+
const info = getElementInfo(state.selectedElement);
|
|
331
|
+
if (newText !== state.originalText && info.filePath) {
|
|
332
|
+
sendToParent("SAVE_INLINE_EDIT", {
|
|
333
|
+
filePath: info.filePath,
|
|
334
|
+
originalContent: state.originalText,
|
|
335
|
+
newContent: newText,
|
|
336
|
+
line: info.line
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
exitEditMode();
|
|
340
|
+
}
|
|
341
|
+
function cancelInlineEdit() {
|
|
342
|
+
sendToParent("CANCEL_INLINE_EDIT");
|
|
343
|
+
exitEditMode();
|
|
344
|
+
}
|
|
345
|
+
function exitEditMode() {
|
|
346
|
+
state.isEditing = false;
|
|
347
|
+
if (state.inlineEditor) {
|
|
348
|
+
state.inlineEditor.remove();
|
|
349
|
+
state.inlineEditor = null;
|
|
350
|
+
}
|
|
351
|
+
if (state.isActive && state.hoveredElement) {
|
|
352
|
+
if (state.overlay) state.overlay.style.display = "block";
|
|
353
|
+
if (state.tooltip) state.tooltip.style.display = "block";
|
|
354
|
+
}
|
|
355
|
+
state.selectedElement = null;
|
|
356
|
+
state.originalText = "";
|
|
357
|
+
}
|
|
358
|
+
function enterEditMode() {
|
|
359
|
+
if (!state.selectedElement) return;
|
|
360
|
+
state.isEditing = true;
|
|
361
|
+
if (state.overlay) state.overlay.style.display = "none";
|
|
362
|
+
if (state.tooltip) state.tooltip.style.display = "none";
|
|
363
|
+
state.inlineEditor = createInlineEditor(state, state.selectedElement, saveInlineEdit, cancelInlineEdit);
|
|
364
|
+
const info = getElementInfo(state.selectedElement);
|
|
365
|
+
sendToParent("START_INLINE_EDIT", {
|
|
366
|
+
tagName: info.tagName,
|
|
367
|
+
filePath: info.filePath,
|
|
368
|
+
line: info.line,
|
|
369
|
+
text: state.originalText
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
function selectElement() {
|
|
373
|
+
if (!state.hoveredElement) return;
|
|
374
|
+
state.selectedElement = state.hoveredElement;
|
|
375
|
+
const info = getElementInfo(state.hoveredElement);
|
|
376
|
+
const text = getElementTextContent(state.hoveredElement);
|
|
377
|
+
state.originalText = text;
|
|
378
|
+
sendToParent("CLICK_ELEMENT", {
|
|
379
|
+
tagName: info.tagName,
|
|
380
|
+
filePath: info.filePath,
|
|
381
|
+
line: info.line,
|
|
382
|
+
depth: info.depth,
|
|
383
|
+
text
|
|
384
|
+
});
|
|
385
|
+
enterEditMode();
|
|
386
|
+
}
|
|
387
|
+
const handleMouseMove = (e) => {
|
|
388
|
+
if (!state.isActive) return;
|
|
389
|
+
const target = e.target;
|
|
390
|
+
if (target.id === "lego-element-selector-overlay" || target.id === "lego-element-selector-tooltip") {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (target === state.hoveredElement) return;
|
|
394
|
+
highlightElement(state, target);
|
|
395
|
+
};
|
|
396
|
+
const handleScroll = () => {
|
|
397
|
+
if (state.isActive && state.hoveredElement) {
|
|
398
|
+
updateOverlayPosition(state);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
const handleResize = () => {
|
|
402
|
+
if (state.isActive && state.hoveredElement) {
|
|
403
|
+
updateOverlayPosition(state);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
const handleKeyDown = (e) => {
|
|
407
|
+
if (!state.isActive) return;
|
|
408
|
+
if (state.isEditing) {
|
|
409
|
+
if (e.key === "Escape") {
|
|
410
|
+
e.preventDefault();
|
|
411
|
+
cancelInlineEdit(state);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
switch (e.key) {
|
|
417
|
+
case "Escape":
|
|
418
|
+
sendToParent("SELECTOR_EXIT", true);
|
|
419
|
+
break;
|
|
420
|
+
case "ArrowUp":
|
|
421
|
+
e.preventDefault();
|
|
422
|
+
navigateDOM(state, "parent");
|
|
423
|
+
break;
|
|
424
|
+
case "ArrowDown":
|
|
425
|
+
e.preventDefault();
|
|
426
|
+
navigateDOM(state, "child");
|
|
427
|
+
break;
|
|
428
|
+
case "ArrowLeft":
|
|
429
|
+
e.preventDefault();
|
|
430
|
+
navigateDOM(state, "prev");
|
|
431
|
+
break;
|
|
432
|
+
case "ArrowRight":
|
|
433
|
+
e.preventDefault();
|
|
434
|
+
navigateDOM(state, "next");
|
|
435
|
+
break;
|
|
436
|
+
case "Enter":
|
|
437
|
+
e.preventDefault();
|
|
438
|
+
selectElement(state);
|
|
439
|
+
break;
|
|
440
|
+
case "e":
|
|
441
|
+
case "E":
|
|
442
|
+
if (state.hoveredElement) {
|
|
443
|
+
e.preventDefault();
|
|
444
|
+
selectElement(state);
|
|
445
|
+
}
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
const handleClick = (e) => {
|
|
450
|
+
if (!state.isActive) return;
|
|
451
|
+
if (state.isEditing) {
|
|
452
|
+
const target = e.target;
|
|
453
|
+
if (state.inlineEditor?.contains(target)) {
|
|
454
|
+
if (target.tagName === "BUTTON") {
|
|
455
|
+
e.preventDefault();
|
|
456
|
+
e.stopPropagation();
|
|
457
|
+
if (target.textContent === "Save") {
|
|
458
|
+
saveInlineEdit(state);
|
|
459
|
+
} else if (target.textContent === "Cancel") {
|
|
460
|
+
cancelInlineEdit(state);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
e.preventDefault();
|
|
466
|
+
e.stopPropagation();
|
|
467
|
+
e.stopImmediatePropagation();
|
|
468
|
+
cancelInlineEdit(state);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
e.preventDefault();
|
|
472
|
+
e.stopPropagation();
|
|
473
|
+
e.stopImmediatePropagation();
|
|
474
|
+
selectElement(state);
|
|
475
|
+
};
|
|
476
|
+
const activateSelector = () => {
|
|
477
|
+
state.isActive = true;
|
|
478
|
+
document.addEventListener("mousemove", handleMouseMove, true);
|
|
479
|
+
document.addEventListener("click", handleClick, true);
|
|
480
|
+
document.addEventListener("keydown", handleKeyDown, true);
|
|
481
|
+
window.addEventListener("scroll", handleScroll, true);
|
|
482
|
+
window.addEventListener("resize", handleResize);
|
|
483
|
+
document.body.style.cursor = "crosshair";
|
|
484
|
+
state.overlay = createOverlay();
|
|
485
|
+
state.tooltip = createTooltip();
|
|
486
|
+
};
|
|
487
|
+
const deactivateSelector = () => {
|
|
488
|
+
state.isActive = false;
|
|
489
|
+
state.isEditing = false;
|
|
490
|
+
document.removeEventListener("mousemove", handleMouseMove, true);
|
|
491
|
+
document.removeEventListener("click", handleClick, true);
|
|
492
|
+
document.removeEventListener("keydown", handleKeyDown, true);
|
|
493
|
+
window.removeEventListener("scroll", handleScroll, true);
|
|
494
|
+
window.removeEventListener("resize", handleResize);
|
|
495
|
+
if (state.overlay) {
|
|
496
|
+
state.overlay.remove();
|
|
497
|
+
state.overlay = null;
|
|
498
|
+
}
|
|
499
|
+
if (state.tooltip) {
|
|
500
|
+
state.tooltip.remove();
|
|
501
|
+
state.tooltip = null;
|
|
502
|
+
}
|
|
503
|
+
if (state.inlineEditor) {
|
|
504
|
+
state.inlineEditor.remove();
|
|
505
|
+
state.inlineEditor = null;
|
|
506
|
+
}
|
|
507
|
+
state.hoveredElement = null;
|
|
508
|
+
state.selectedElement = null;
|
|
509
|
+
state.originalText = "";
|
|
510
|
+
document.body.style.cursor = "";
|
|
511
|
+
state.depth = 0;
|
|
512
|
+
};
|
|
513
|
+
window.addEventListener("message", (e) => {
|
|
514
|
+
const msg = e.data;
|
|
515
|
+
if (msg.type === "TOGGLE_ELEMENT_SELECTOR") {
|
|
516
|
+
if (msg.payload) {
|
|
517
|
+
activateSelector();
|
|
518
|
+
} else {
|
|
519
|
+
deactivateSelector();
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
function initIframeBridge() {
|
|
525
|
+
try {
|
|
526
|
+
if (window.parent !== window) {
|
|
527
|
+
setupIframeBridge();
|
|
528
|
+
setupElementSelector();
|
|
529
|
+
}
|
|
530
|
+
} catch {
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
export {
|
|
535
|
+
sendToParent,
|
|
536
|
+
setupIframeBridge,
|
|
537
|
+
setupElementSelector,
|
|
538
|
+
initIframeBridge
|
|
539
|
+
};
|