@base44/vite-plugin 0.2.27 → 0.2.29
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/capabilities/inline-edit/controller.d.ts.map +1 -1
- package/dist/capabilities/inline-edit/controller.js +9 -3
- package/dist/capabilities/inline-edit/controller.js.map +1 -1
- package/dist/capabilities/inline-edit/dom-utils.d.ts +1 -0
- package/dist/capabilities/inline-edit/dom-utils.d.ts.map +1 -1
- package/dist/capabilities/inline-edit/dom-utils.js +17 -7
- package/dist/capabilities/inline-edit/dom-utils.js.map +1 -1
- package/dist/capabilities/inline-edit/index.d.ts +1 -1
- package/dist/capabilities/inline-edit/index.d.ts.map +1 -1
- package/dist/capabilities/inline-edit/types.d.ts +4 -0
- package/dist/capabilities/inline-edit/types.d.ts.map +1 -1
- package/dist/consts.d.ts +11 -0
- package/dist/consts.d.ts.map +1 -0
- package/dist/consts.js +11 -0
- package/dist/consts.js.map +1 -0
- package/dist/injections/visual-edit-agent.d.ts.map +1 -1
- package/dist/injections/visual-edit-agent.js +26 -4
- package/dist/injections/visual-edit-agent.js.map +1 -1
- package/dist/jsx-processor.d.ts +3 -1
- package/dist/jsx-processor.d.ts.map +1 -1
- package/dist/jsx-processor.js +29 -6
- package/dist/jsx-processor.js.map +1 -1
- package/dist/jsx-utils.d.ts +9 -0
- package/dist/jsx-utils.d.ts.map +1 -1
- package/dist/jsx-utils.js +86 -0
- package/dist/jsx-utils.js.map +1 -1
- package/dist/processors/collection-id-processor.d.ts +20 -0
- package/dist/processors/collection-id-processor.d.ts.map +1 -0
- package/dist/processors/collection-id-processor.js +182 -0
- package/dist/processors/collection-id-processor.js.map +1 -0
- package/dist/processors/collection-item-field-processor.d.ts +39 -0
- package/dist/processors/collection-item-field-processor.d.ts.map +1 -0
- package/dist/processors/collection-item-field-processor.js +281 -0
- package/dist/processors/collection-item-field-processor.js.map +1 -0
- package/dist/processors/collection-item-id-processor.d.ts +12 -0
- package/dist/processors/collection-item-id-processor.d.ts.map +1 -0
- package/dist/processors/collection-item-id-processor.js +50 -0
- package/dist/processors/collection-item-id-processor.js.map +1 -0
- package/dist/processors/static-array-processor.d.ts +0 -3
- package/dist/processors/static-array-processor.d.ts.map +1 -1
- package/dist/processors/static-array-processor.js +2 -4
- package/dist/processors/static-array-processor.js.map +1 -1
- package/dist/processors/utils/collection-tracing-utils.d.ts +36 -0
- package/dist/processors/utils/collection-tracing-utils.d.ts.map +1 -0
- package/dist/processors/utils/collection-tracing-utils.js +390 -0
- package/dist/processors/utils/collection-tracing-utils.js.map +1 -0
- package/dist/processors/utils/shared-utils.d.ts +96 -0
- package/dist/processors/utils/shared-utils.d.ts.map +1 -0
- package/dist/processors/utils/shared-utils.js +600 -0
- package/dist/processors/utils/shared-utils.js.map +1 -0
- package/dist/statics/index.mjs +2 -2
- package/dist/statics/index.mjs.map +1 -1
- package/dist/visual-edit-plugin.d.ts +0 -1
- package/dist/visual-edit-plugin.d.ts.map +1 -1
- package/dist/visual-edit-plugin.js +29 -178
- package/dist/visual-edit-plugin.js.map +1 -1
- package/package.json +1 -1
- package/src/capabilities/inline-edit/controller.ts +35 -29
- package/src/capabilities/inline-edit/dom-utils.ts +14 -4
- package/src/capabilities/inline-edit/index.ts +1 -1
- package/src/capabilities/inline-edit/types.ts +5 -0
- package/src/consts.ts +11 -0
- package/src/injections/visual-edit-agent.ts +33 -4
- package/src/jsx-processor.ts +36 -14
- package/src/jsx-utils.ts +116 -0
- package/src/processors/collection-id-processor.ts +261 -0
- package/src/processors/collection-item-field-processor.ts +433 -0
- package/src/processors/collection-item-id-processor.ts +69 -0
- package/src/processors/static-array-processor.ts +7 -4
- package/src/processors/utils/collection-tracing-utils.ts +507 -0
- package/src/processors/utils/shared-utils.ts +785 -0
- package/src/visual-edit-plugin.ts +34 -215
- package/dist/processors/shared-utils.d.ts +0 -19
- package/dist/processors/shared-utils.d.ts.map +0 -1
- package/dist/processors/shared-utils.js +0 -77
- package/dist/processors/shared-utils.js.map +0 -1
- package/src/processors/shared-utils.ts +0 -116
|
@@ -2,175 +2,27 @@ import { parse } from "@babel/parser";
|
|
|
2
2
|
import { default as traverse } from "@babel/traverse";
|
|
3
3
|
import { default as generate } from "@babel/generator";
|
|
4
4
|
import * as t from "@babel/types";
|
|
5
|
-
import {
|
|
5
|
+
import { JSXProcessor } from "./jsx-processor.js";
|
|
6
6
|
import { JSXUtils } from "./jsx-utils.js";
|
|
7
|
-
// Helper function to check if JSX element contains dynamic content
|
|
8
|
-
export function checkIfElementHasDynamicContent(jsxElement) {
|
|
9
|
-
let hasDynamicContent = false;
|
|
10
|
-
// Helper function to check if any node contains dynamic patterns
|
|
11
|
-
function checkNodeForDynamicContent(node) {
|
|
12
|
-
// JSX expressions like {variable}, {func()}, {obj.prop}
|
|
13
|
-
if (t.isJSXExpressionContainer(node)) {
|
|
14
|
-
const expression = node.expression;
|
|
15
|
-
// Skip empty expressions {}
|
|
16
|
-
if (t.isJSXEmptyExpression(expression)) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
// Any non-literal expression is considered dynamic
|
|
20
|
-
if (!t.isLiteral(expression)) {
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
// Template literals with expressions `Hello ${name}`
|
|
25
|
-
if (t.isTemplateLiteral(node) && node.expressions.length > 0) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
// Member expressions like props.title, state.value
|
|
29
|
-
if (t.isMemberExpression(node)) {
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
// Function calls like getData(), format()
|
|
33
|
-
if (t.isCallExpression(node)) {
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
// Conditional expressions like condition ? "yes" : "no"
|
|
37
|
-
if (t.isConditionalExpression(node)) {
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
// Identifier references (could be props, state, variables)
|
|
41
|
-
if (t.isIdentifier(node)) {
|
|
42
|
-
// Common dynamic identifiers
|
|
43
|
-
const dynamicNames = [
|
|
44
|
-
"props",
|
|
45
|
-
"state",
|
|
46
|
-
"data",
|
|
47
|
-
"item",
|
|
48
|
-
"value",
|
|
49
|
-
"text",
|
|
50
|
-
"content",
|
|
51
|
-
];
|
|
52
|
-
if (dynamicNames.some((name) => node.name.includes(name))) {
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
// Recursively traverse all child nodes
|
|
59
|
-
function traverseNode(node) {
|
|
60
|
-
if (checkNodeForDynamicContent(node)) {
|
|
61
|
-
hasDynamicContent = true;
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
// Recursively check child nodes
|
|
65
|
-
Object.keys(node).forEach((key) => {
|
|
66
|
-
const value = node[key];
|
|
67
|
-
if (Array.isArray(value)) {
|
|
68
|
-
value.forEach((child) => {
|
|
69
|
-
if (child && typeof child === "object" && child.type) {
|
|
70
|
-
traverseNode(child);
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
else if (value && typeof value === "object" && value.type) {
|
|
75
|
-
traverseNode(value);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
// Check the element's own attributes for dynamic content
|
|
80
|
-
const attributes = jsxElement.openingElement?.attributes || [];
|
|
81
|
-
attributes.forEach((attr) => {
|
|
82
|
-
if (hasDynamicContent)
|
|
83
|
-
return; // Early exit if already found dynamic content
|
|
84
|
-
// Spread attributes like {...props} are always dynamic
|
|
85
|
-
if (t.isJSXSpreadAttribute(attr)) {
|
|
86
|
-
hasDynamicContent = true;
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
// Check attribute values for dynamic expressions
|
|
90
|
-
if (t.isJSXAttribute(attr) && attr.value) {
|
|
91
|
-
traverseNode(attr.value);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
// Check all children of the JSX element
|
|
95
|
-
jsxElement.children.forEach((child) => {
|
|
96
|
-
if (hasDynamicContent)
|
|
97
|
-
return; // Early exit if already found dynamic content
|
|
98
|
-
traverseNode(child);
|
|
99
|
-
});
|
|
100
|
-
return hasDynamicContent;
|
|
101
|
-
}
|
|
102
7
|
export function visualEditPlugin() {
|
|
103
8
|
return {
|
|
104
9
|
name: "visual-edit-transform",
|
|
105
10
|
apply: (config) => config.mode === "development",
|
|
106
11
|
enforce: "pre",
|
|
107
12
|
order: "pre",
|
|
108
|
-
// Inject Tailwind CDN for visual editing capabilities
|
|
109
13
|
transformIndexHtml(html) {
|
|
110
|
-
// Inject the Tailwind CSS CDN script right before the closing </head> tag
|
|
111
14
|
const tailwindScript = ` <!-- Tailwind CSS CDN for visual editing -->\n <script src="https://cdn.tailwindcss.com"></script>\n `;
|
|
112
15
|
return html.replace("</head>", tailwindScript + "</head>");
|
|
113
16
|
},
|
|
114
17
|
transform(code, id) {
|
|
115
|
-
// Skip node_modules and visual-edit-agent itself
|
|
116
18
|
if (id.includes("node_modules") || id.includes("visual-edit-agent")) {
|
|
117
19
|
return null;
|
|
118
20
|
}
|
|
119
|
-
// Process JS/JSX/TS/TSX files
|
|
120
21
|
if (!id.match(/\.(jsx?|tsx?)$/)) {
|
|
121
22
|
return null;
|
|
122
23
|
}
|
|
123
|
-
|
|
124
|
-
const pathParts = id.split("/");
|
|
125
|
-
let filename;
|
|
126
|
-
// Check if this is a pages or components file
|
|
127
|
-
if (id.includes("/pages/")) {
|
|
128
|
-
const pagesIndex = pathParts.findIndex((part) => part === "pages");
|
|
129
|
-
if (pagesIndex >= 0 && pagesIndex < pathParts.length - 1) {
|
|
130
|
-
// Get all parts from 'pages' to the file, preserving nested structure
|
|
131
|
-
const relevantParts = pathParts.slice(pagesIndex, pathParts.length);
|
|
132
|
-
const lastPart = relevantParts[relevantParts.length - 1];
|
|
133
|
-
// Remove file extension from the last part
|
|
134
|
-
relevantParts[relevantParts.length - 1] = lastPart.includes(".")
|
|
135
|
-
? lastPart.split(".")[0]
|
|
136
|
-
: lastPart;
|
|
137
|
-
filename = relevantParts.join("/");
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
filename = pathParts[pathParts.length - 1];
|
|
141
|
-
if (filename.includes(".")) {
|
|
142
|
-
filename = filename.split(".")[0];
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
else if (id.includes("/components/")) {
|
|
147
|
-
const componentsIndex = pathParts.findIndex((part) => part === "components");
|
|
148
|
-
if (componentsIndex >= 0 && componentsIndex < pathParts.length - 1) {
|
|
149
|
-
// Get all parts from 'components' to the file, preserving nested structure
|
|
150
|
-
const relevantParts = pathParts.slice(componentsIndex, pathParts.length);
|
|
151
|
-
const lastPart = relevantParts[relevantParts.length - 1];
|
|
152
|
-
// Remove file extension from the last part
|
|
153
|
-
relevantParts[relevantParts.length - 1] = lastPart.includes(".")
|
|
154
|
-
? lastPart.split(".")[0]
|
|
155
|
-
: lastPart;
|
|
156
|
-
filename = relevantParts.join("/");
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
filename = pathParts[pathParts.length - 1];
|
|
160
|
-
if (filename.includes(".")) {
|
|
161
|
-
filename = filename.split(".")[0];
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
// For other files (like layout), just use the filename
|
|
167
|
-
filename = pathParts[pathParts.length - 1];
|
|
168
|
-
if (filename.includes(".")) {
|
|
169
|
-
filename = filename.split(".")[0];
|
|
170
|
-
}
|
|
171
|
-
}
|
|
24
|
+
const filename = extractFilename(id);
|
|
172
25
|
try {
|
|
173
|
-
// Parse the code into an AST
|
|
174
26
|
const ast = parse(code, {
|
|
175
27
|
sourceType: "module",
|
|
176
28
|
plugins: [
|
|
@@ -191,41 +43,16 @@ export function visualEditPlugin() {
|
|
|
191
43
|
"throwExpressions",
|
|
192
44
|
],
|
|
193
45
|
});
|
|
194
|
-
// Traverse the AST and add source location and dynamic content attributes to JSX elements
|
|
195
46
|
JSXUtils.init(t);
|
|
196
|
-
const
|
|
197
|
-
let elementsProcessed = 0;
|
|
47
|
+
const processor = new JSXProcessor(t, filename);
|
|
198
48
|
traverse.default(ast, {
|
|
199
49
|
JSXElement(path) {
|
|
200
50
|
const jsxElement = path.node;
|
|
201
|
-
const openingElement = jsxElement.openingElement;
|
|
202
|
-
// Skip fragments
|
|
203
51
|
if (t.isJSXFragment(jsxElement))
|
|
204
52
|
return;
|
|
205
|
-
|
|
206
|
-
const hasSourceLocation = openingElement.attributes.some((attr) => t.isJSXAttribute(attr) &&
|
|
207
|
-
t.isJSXIdentifier(attr.name) &&
|
|
208
|
-
attr.name.name === "data-source-location");
|
|
209
|
-
if (hasSourceLocation)
|
|
210
|
-
return;
|
|
211
|
-
// Get line and column from AST node location
|
|
212
|
-
const { line, column } = openingElement.loc?.start || {
|
|
213
|
-
line: 1,
|
|
214
|
-
column: 0,
|
|
215
|
-
};
|
|
216
|
-
// Create the source location attribute
|
|
217
|
-
const sourceLocationAttr = t.jsxAttribute(t.jsxIdentifier("data-source-location"), t.stringLiteral(`${filename}:${line}:${column}`));
|
|
218
|
-
// Check if element has dynamic content
|
|
219
|
-
const isDynamic = checkIfElementHasDynamicContent(jsxElement);
|
|
220
|
-
// Create the dynamic content attribute
|
|
221
|
-
const dynamicContentAttr = t.jsxAttribute(t.jsxIdentifier("data-dynamic-content"), t.stringLiteral(isDynamic ? "true" : "false"));
|
|
222
|
-
// Add both attributes to the beginning of the attributes array
|
|
223
|
-
openingElement.attributes.unshift(sourceLocationAttr, dynamicContentAttr);
|
|
224
|
-
staticArrayProcessor.process(path.get("openingElement"));
|
|
225
|
-
elementsProcessed++;
|
|
53
|
+
processor.processJSXElement(path.get("openingElement"));
|
|
226
54
|
},
|
|
227
55
|
});
|
|
228
|
-
// Generate the code back from the AST
|
|
229
56
|
const result = generate.default(ast, {
|
|
230
57
|
compact: false,
|
|
231
58
|
concise: false,
|
|
@@ -239,11 +66,35 @@ export function visualEditPlugin() {
|
|
|
239
66
|
catch (error) {
|
|
240
67
|
console.error("Failed to add source location to JSX:", error);
|
|
241
68
|
return {
|
|
242
|
-
code: code,
|
|
69
|
+
code: code,
|
|
243
70
|
map: null,
|
|
244
71
|
};
|
|
245
72
|
}
|
|
246
73
|
},
|
|
247
74
|
};
|
|
248
75
|
}
|
|
76
|
+
function extractFilename(id) {
|
|
77
|
+
const pathParts = id.split("/");
|
|
78
|
+
const segmentIndex = findSegmentIndex(pathParts, ["pages", "components"]);
|
|
79
|
+
if (segmentIndex >= 0 && segmentIndex < pathParts.length - 1) {
|
|
80
|
+
const relevantParts = pathParts.slice(segmentIndex);
|
|
81
|
+
const last = relevantParts[relevantParts.length - 1];
|
|
82
|
+
relevantParts[relevantParts.length - 1] = stripExtension(last ?? "");
|
|
83
|
+
return relevantParts.join("/");
|
|
84
|
+
}
|
|
85
|
+
const lastPart = pathParts[pathParts.length - 1] ?? "";
|
|
86
|
+
return stripExtension(lastPart);
|
|
87
|
+
}
|
|
88
|
+
function findSegmentIndex(parts, segments) {
|
|
89
|
+
for (const segment of segments) {
|
|
90
|
+
const idx = parts.findIndex((part) => part === segment);
|
|
91
|
+
if (idx >= 0)
|
|
92
|
+
return idx;
|
|
93
|
+
}
|
|
94
|
+
return -1;
|
|
95
|
+
}
|
|
96
|
+
function stripExtension(filename) {
|
|
97
|
+
const dotIndex = filename.indexOf(".");
|
|
98
|
+
return dotIndex >= 0 ? filename.substring(0, dotIndex) : filename;
|
|
99
|
+
}
|
|
249
100
|
//# sourceMappingURL=visual-edit-plugin.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"visual-edit-plugin.js","sourceRoot":"","sources":["../src/visual-edit-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,EAAE,OAAO,I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AC,CAAC,CAAC,QAAQ,CAAC;AACpE,CAAC"}
|
package/package.json
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
removeFocusOutlineCSS,
|
|
5
5
|
selectText,
|
|
6
6
|
shouldEnterInlineEditingMode,
|
|
7
|
+
isStaticArrayTextElement,
|
|
7
8
|
} from "./dom-utils.js";
|
|
8
9
|
|
|
9
10
|
const DEBOUNCE_MS = 500;
|
|
@@ -37,37 +38,42 @@ export function createInlineEditController(
|
|
|
37
38
|
const svgElement = element as unknown as SVGElement;
|
|
38
39
|
const rect = element.getBoundingClientRect();
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
centerY: rect.top + rect.height / 2,
|
|
64
|
-
},
|
|
41
|
+
const message: Record<string, unknown> = {
|
|
42
|
+
type: "inline-edit",
|
|
43
|
+
elementInfo: {
|
|
44
|
+
tagName: element.tagName,
|
|
45
|
+
classes:
|
|
46
|
+
(svgElement.className as unknown as SVGAnimatedString)?.baseVal ||
|
|
47
|
+
element.className ||
|
|
48
|
+
"",
|
|
49
|
+
visualSelectorId: host.getSelectedElementId(),
|
|
50
|
+
content: newContent,
|
|
51
|
+
dataSourceLocation: element.dataset.sourceLocation,
|
|
52
|
+
isDynamicContent: element.dataset.dynamicContent === "true",
|
|
53
|
+
linenumber: element.dataset.linenumber,
|
|
54
|
+
filename: element.dataset.filename,
|
|
55
|
+
position: {
|
|
56
|
+
top: rect.top,
|
|
57
|
+
left: rect.left,
|
|
58
|
+
right: rect.right,
|
|
59
|
+
bottom: rect.bottom,
|
|
60
|
+
width: rect.width,
|
|
61
|
+
height: rect.height,
|
|
62
|
+
centerX: rect.left + rect.width / 2,
|
|
63
|
+
centerY: rect.top + rect.height / 2,
|
|
65
64
|
},
|
|
66
|
-
originalContent,
|
|
67
|
-
newContent,
|
|
68
65
|
},
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
originalContent,
|
|
67
|
+
newContent,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
if (isStaticArrayTextElement(element)) {
|
|
71
|
+
message.arrIndex = element.dataset.arrIndex;
|
|
72
|
+
message.arrVariableName = element.dataset.arrVariableName;
|
|
73
|
+
message.arrField = element.dataset.arrField;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
window.parent.postMessage(message, "*");
|
|
71
77
|
|
|
72
78
|
element.dataset.originalTextContent = newContent || "";
|
|
73
79
|
};
|
|
@@ -5,6 +5,18 @@ const EDITABLE_TAGS = [
|
|
|
5
5
|
"span", "li", "td", "a", "button", "label",
|
|
6
6
|
];
|
|
7
7
|
|
|
8
|
+
export const isStaticArrayTextElement = (element: HTMLElement): boolean => {
|
|
9
|
+
return !!element.dataset.arrField;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const passesStructuralChecks = (element: HTMLElement): boolean => {
|
|
13
|
+
if (!EDITABLE_TAGS.includes(element.tagName.toLowerCase())) return false;
|
|
14
|
+
if (!element.textContent?.trim()) return false;
|
|
15
|
+
if (element.querySelector("img, video, canvas, svg")) return false;
|
|
16
|
+
if (element.children?.length > 0) return false;
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
19
|
+
|
|
8
20
|
export const injectFocusOutlineCSS = () => {
|
|
9
21
|
if (document.getElementById(FOCUS_STYLE_ID)) return;
|
|
10
22
|
|
|
@@ -32,10 +44,8 @@ export const selectText = (element: HTMLElement) => {
|
|
|
32
44
|
|
|
33
45
|
export const isEditableTextElement = (element: Element): boolean => {
|
|
34
46
|
if (!(element instanceof HTMLElement)) return false;
|
|
35
|
-
if (!
|
|
36
|
-
if (
|
|
37
|
-
if (element.querySelector("img, video, canvas, svg")) return false;
|
|
38
|
-
if (element.children?.length > 0) return false;
|
|
47
|
+
if (!passesStructuralChecks(element)) return false;
|
|
48
|
+
if (isStaticArrayTextElement(element)) return true;
|
|
39
49
|
if (element.dataset.dynamicContent === "true") return false;
|
|
40
50
|
return true;
|
|
41
51
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { createInlineEditController } from "./controller.js";
|
|
2
|
-
export type { InlineEditController, InlineEditHost } from "./types.js";
|
|
2
|
+
export type { CollectionInfo, InlineEditController, InlineEditHost } from "./types.js";
|
package/src/consts.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const DATA_COLLECTION_ID = "data-collection-id";
|
|
2
|
+
export const DATA_COLLECTION_ITEM_ID = "data-collection-item-id";
|
|
3
|
+
export const DATA_COLLECTION_ITEM_FIELD = "data-collection-item-field";
|
|
4
|
+
export const DATA_COLLECTION_REFERENCE = "data-collection-reference";
|
|
5
|
+
export const DATA_ARR_INDEX = "data-arr-index";
|
|
6
|
+
export const DATA_ARR_VARIABLE_NAME = "data-arr-variable-name";
|
|
7
|
+
export const DATA_ARR_FIELD = "data-arr-field";
|
|
8
|
+
|
|
9
|
+
export const ALLOWED_CUSTOM_COMPONENTS = ["Image", "Link"];
|
|
10
|
+
export const MAX_JSX_DEPTH = 10;
|
|
11
|
+
export const EXCLUDED_FIELDS = ["children", "length"];
|
|
@@ -127,6 +127,13 @@ export function setupVisualEditAgent() {
|
|
|
127
127
|
const rect = element.getBoundingClientRect();
|
|
128
128
|
const svgElement = element as SVGElement;
|
|
129
129
|
const isTextElement = TEXT_TAGS.includes(element.tagName?.toLowerCase());
|
|
130
|
+
|
|
131
|
+
const arrEl = htmlElement.closest("[data-arr-variable-name]") as HTMLElement | null;
|
|
132
|
+
const staticArrayName = arrEl?.dataset?.arrVariableName || null;
|
|
133
|
+
const rawIdx = arrEl?.dataset?.arrIndex;
|
|
134
|
+
const staticArrayIndex = rawIdx != null ? parseInt(rawIdx, 10) : null;
|
|
135
|
+
const staticArrayField = htmlElement.dataset?.arrField || null;
|
|
136
|
+
|
|
130
137
|
window.parent.postMessage({
|
|
131
138
|
type: "element-selected",
|
|
132
139
|
tagName: element.tagName,
|
|
@@ -152,6 +159,9 @@ export function setupVisualEditAgent() {
|
|
|
152
159
|
},
|
|
153
160
|
attributes: collectAllowedAttributes(element, ALLOWED_ATTRIBUTES),
|
|
154
161
|
isTextElement,
|
|
162
|
+
staticArrayName,
|
|
163
|
+
staticArrayIndex,
|
|
164
|
+
staticArrayField,
|
|
155
165
|
}, "*");
|
|
156
166
|
};
|
|
157
167
|
|
|
@@ -378,14 +388,19 @@ export function setupVisualEditAgent() {
|
|
|
378
388
|
}, REPOSITION_DELAY_MS);
|
|
379
389
|
};
|
|
380
390
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
const elements = findElementsById(visualSelectorId);
|
|
391
|
+
const updateElementContent = (visualSelectorId: string, content: string, arrIndex?: number) => {
|
|
392
|
+
let elements = findElementsById(visualSelectorId);
|
|
384
393
|
|
|
385
394
|
if (elements.length === 0) {
|
|
386
395
|
return;
|
|
387
396
|
}
|
|
388
397
|
|
|
398
|
+
if (arrIndex != null) {
|
|
399
|
+
elements = elements.filter(
|
|
400
|
+
(el) => (el as HTMLElement).dataset.arrIndex === String(arrIndex)
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
|
|
389
404
|
elements.forEach((element) => {
|
|
390
405
|
(element as HTMLElement).innerText = content;
|
|
391
406
|
});
|
|
@@ -537,7 +552,8 @@ export function setupVisualEditAgent() {
|
|
|
537
552
|
if (message.data && message.data.content !== undefined) {
|
|
538
553
|
updateElementContent(
|
|
539
554
|
message.data.visualSelectorId,
|
|
540
|
-
message.data.content
|
|
555
|
+
message.data.content,
|
|
556
|
+
message.data.arrIndex
|
|
541
557
|
);
|
|
542
558
|
} else {
|
|
543
559
|
console.warn(
|
|
@@ -604,6 +620,19 @@ export function setupVisualEditAgent() {
|
|
|
604
620
|
}
|
|
605
621
|
break;
|
|
606
622
|
|
|
623
|
+
case "update-theme-variables":
|
|
624
|
+
if (message.data?.variables) {
|
|
625
|
+
const target = message.data.mode === 'dark'
|
|
626
|
+
? document.querySelector('.dark') as HTMLElement
|
|
627
|
+
: document.documentElement;
|
|
628
|
+
if (target) {
|
|
629
|
+
for (const [name, value] of Object.entries(message.data.variables)) {
|
|
630
|
+
target.style.setProperty(name, value as string);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
break;
|
|
635
|
+
|
|
607
636
|
case "toggle-inline-edit-mode":
|
|
608
637
|
if (message.data) {
|
|
609
638
|
inlineEdit.handleToggleMessage(message.data);
|
package/src/jsx-processor.ts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
import type { NodePath } from "@babel/traverse";
|
|
2
2
|
import type * as t from "@babel/types";
|
|
3
|
+
import { CollectionIdProcessor } from "./processors/collection-id-processor.js";
|
|
4
|
+
import { DataItemIdProcessor } from "./processors/collection-item-id-processor.js";
|
|
5
|
+
import { DataItemFieldProcessor } from "./processors/collection-item-field-processor.js";
|
|
3
6
|
import { StaticArrayProcessor } from "./processors/static-array-processor.js";
|
|
4
7
|
|
|
5
8
|
export class JSXProcessor {
|
|
9
|
+
private collectionIdProcessor: CollectionIdProcessor;
|
|
10
|
+
private dataItemIdProcessor: DataItemIdProcessor;
|
|
11
|
+
private dataItemFieldProcessor: DataItemFieldProcessor;
|
|
6
12
|
private staticArrayProcessor: StaticArrayProcessor;
|
|
7
13
|
|
|
8
14
|
constructor(
|
|
9
15
|
private types: typeof t,
|
|
10
16
|
private filename: string
|
|
11
17
|
) {
|
|
18
|
+
this.collectionIdProcessor = new CollectionIdProcessor(types);
|
|
19
|
+
this.dataItemIdProcessor = new DataItemIdProcessor(types);
|
|
20
|
+
this.dataItemFieldProcessor = new DataItemFieldProcessor(types);
|
|
12
21
|
this.staticArrayProcessor = new StaticArrayProcessor(types);
|
|
13
22
|
}
|
|
14
23
|
|
|
@@ -17,7 +26,10 @@ export class JSXProcessor {
|
|
|
17
26
|
|
|
18
27
|
this.addSourceLocationAttribute(path);
|
|
19
28
|
this.addDynamicContentAttribute(path);
|
|
20
|
-
|
|
29
|
+
|
|
30
|
+
this.collectionIdProcessor.process(path);
|
|
31
|
+
this.dataItemIdProcessor.process(path);
|
|
32
|
+
this.dataItemFieldProcessor.process(path);
|
|
21
33
|
this.staticArrayProcessor.process(path);
|
|
22
34
|
}
|
|
23
35
|
|
|
@@ -27,7 +39,7 @@ export class JSXProcessor {
|
|
|
27
39
|
const { line, column } = path.node.loc?.start || { line: 1, column: 0 };
|
|
28
40
|
const value = `${this.filename}:${line}:${column}`;
|
|
29
41
|
|
|
30
|
-
path.node.attributes.
|
|
42
|
+
path.node.attributes.unshift(
|
|
31
43
|
this.types.jsxAttribute(
|
|
32
44
|
this.types.jsxIdentifier("data-source-location"),
|
|
33
45
|
this.types.stringLiteral(value)
|
|
@@ -45,21 +57,19 @@ export class JSXProcessor {
|
|
|
45
57
|
parentElement.node as t.JSXElement
|
|
46
58
|
);
|
|
47
59
|
|
|
48
|
-
path.node.attributes.
|
|
49
|
-
|
|
50
|
-
this.types.
|
|
51
|
-
this.types.
|
|
52
|
-
|
|
60
|
+
const sourceLocIdx = path.node.attributes.findIndex(
|
|
61
|
+
(attr) =>
|
|
62
|
+
this.types.isJSXAttribute(attr) &&
|
|
63
|
+
this.types.isJSXIdentifier(attr.name) &&
|
|
64
|
+
attr.name.name === "data-source-location"
|
|
53
65
|
);
|
|
54
|
-
}
|
|
55
66
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
path.node.attributes.push(
|
|
67
|
+
path.node.attributes.splice(
|
|
68
|
+
sourceLocIdx + 1,
|
|
69
|
+
0,
|
|
60
70
|
this.types.jsxAttribute(
|
|
61
|
-
this.types.jsxIdentifier("content
|
|
62
|
-
this.types.stringLiteral("true")
|
|
71
|
+
this.types.jsxIdentifier("data-dynamic-content"),
|
|
72
|
+
this.types.stringLiteral(isDynamic ? "true" : "false")
|
|
63
73
|
)
|
|
64
74
|
);
|
|
65
75
|
}
|
|
@@ -137,6 +147,18 @@ export class JSXProcessor {
|
|
|
137
147
|
}
|
|
138
148
|
};
|
|
139
149
|
|
|
150
|
+
const attributes = jsxElement.openingElement?.attributes || [];
|
|
151
|
+
for (const attr of attributes) {
|
|
152
|
+
if (hasDynamicContent) break;
|
|
153
|
+
if (this.types.isJSXSpreadAttribute(attr)) {
|
|
154
|
+
hasDynamicContent = true;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
if (this.types.isJSXAttribute(attr) && attr.value) {
|
|
158
|
+
traverseNode(attr.value as unknown as Record<string, unknown>);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
140
162
|
for (const child of jsxElement.children) {
|
|
141
163
|
if (hasDynamicContent) break;
|
|
142
164
|
traverseNode(child as unknown as Record<string, unknown>);
|