@base44-preview/vite-plugin 0.2.26-pr.43.e9195be → 0.2.27-pr.41.77be9cb
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 +3 -0
- package/dist/capabilities/inline-edit/controller.d.ts.map +1 -0
- package/dist/capabilities/inline-edit/controller.js +194 -0
- package/dist/capabilities/inline-edit/controller.js.map +1 -0
- package/dist/capabilities/inline-edit/dom-utils.d.ts +6 -0
- package/dist/capabilities/inline-edit/dom-utils.d.ts.map +1 -0
- package/dist/capabilities/inline-edit/dom-utils.js +49 -0
- package/dist/capabilities/inline-edit/dom-utils.js.map +1 -0
- package/dist/capabilities/inline-edit/index.d.ts +3 -0
- package/dist/capabilities/inline-edit/index.d.ts.map +1 -0
- package/dist/capabilities/inline-edit/index.js +2 -0
- package/dist/capabilities/inline-edit/index.js.map +1 -0
- package/dist/capabilities/inline-edit/types.d.ts +25 -0
- package/dist/capabilities/inline-edit/types.d.ts.map +1 -0
- package/dist/capabilities/inline-edit/types.js.map +1 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/core.d.ts +25 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/core.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/core.js +95 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/core.js.map +1 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/index.d.ts +4 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/index.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/index.js +4 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/index.js.map +1 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/styles.d.ts +9 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/styles.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/styles.js +26 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/styles.js.map +1 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/validation.d.ts +17 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/validation.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/validation.js +76 -0
- package/dist/injections/visual-edit-agent/capabilities/inline-editing/validation.js.map +1 -0
- package/dist/injections/visual-edit-agent/constants.d.ts +10 -0
- package/dist/injections/visual-edit-agent/constants.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/constants.js +10 -0
- package/dist/injections/visual-edit-agent/constants.js.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/click-handlers.d.ts +10 -0
- package/dist/injections/visual-edit-agent/handlers/click-handlers.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/click-handlers.js +117 -0
- package/dist/injections/visual-edit-agent/handlers/click-handlers.js.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/hover-handlers.d.ts +14 -0
- package/dist/injections/visual-edit-agent/handlers/hover-handlers.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/hover-handlers.js +64 -0
- package/dist/injections/visual-edit-agent/handlers/hover-handlers.js.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/inline-edit-handlers.d.ts +14 -0
- package/dist/injections/visual-edit-agent/handlers/inline-edit-handlers.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/inline-edit-handlers.js +114 -0
- package/dist/injections/visual-edit-agent/handlers/inline-edit-handlers.js.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/message-handlers.d.ts +26 -0
- package/dist/injections/visual-edit-agent/handlers/message-handlers.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/message-handlers.js +148 -0
- package/dist/injections/visual-edit-agent/handlers/message-handlers.js.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.d.ts +7 -0
- package/dist/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.js +54 -0
- package/dist/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.js.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.d.ts +11 -0
- package/dist/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.js +32 -0
- package/dist/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.js.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/messages/types.d.ts +105 -0
- package/dist/injections/visual-edit-agent/handlers/messages/types.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/handlers/messages/types.js +28 -0
- package/dist/injections/visual-edit-agent/handlers/messages/types.js.map +1 -0
- package/dist/injections/visual-edit-agent/index.d.ts +6 -0
- package/dist/injections/visual-edit-agent/index.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/index.js +109 -0
- package/dist/injections/visual-edit-agent/index.js.map +1 -0
- package/dist/injections/visual-edit-agent/state/agent-state.d.ts +17 -0
- package/dist/injections/visual-edit-agent/state/agent-state.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/state/agent-state.js +18 -0
- package/dist/injections/visual-edit-agent/state/agent-state.js.map +1 -0
- package/dist/injections/visual-edit-agent/ui/overlay.d.ts +26 -0
- package/dist/injections/visual-edit-agent/ui/overlay.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/ui/overlay.js +104 -0
- package/dist/injections/visual-edit-agent/ui/overlay.js.map +1 -0
- package/dist/injections/visual-edit-agent/utils/dom-utils.d.ts +14 -0
- package/dist/injections/visual-edit-agent/utils/dom-utils.d.ts.map +1 -0
- package/dist/injections/visual-edit-agent/utils/dom-utils.js +34 -0
- package/dist/injections/visual-edit-agent/utils/dom-utils.js.map +1 -0
- package/dist/injections/visual-edit-agent.d.ts +1 -1
- package/dist/injections/visual-edit-agent.d.ts.map +1 -1
- package/dist/injections/visual-edit-agent.js +1 -506
- package/dist/injections/visual-edit-agent.js.map +1 -1
- package/dist/jsx-processor.d.ts +1 -4
- package/dist/jsx-processor.d.ts.map +1 -1
- package/dist/jsx-processor.js +6 -33
- package/dist/jsx-processor.js.map +1 -1
- package/dist/jsx-utils.d.ts +0 -9
- package/dist/jsx-utils.d.ts.map +1 -1
- package/dist/jsx-utils.js +0 -86
- package/dist/jsx-utils.js.map +1 -1
- package/dist/processors/shared-utils.d.ts +0 -64
- package/dist/processors/shared-utils.d.ts.map +1 -1
- package/dist/processors/shared-utils.js +0 -468
- package/dist/processors/shared-utils.js.map +1 -1
- package/dist/processors/static-array-processor.d.ts +3 -2
- package/dist/processors/static-array-processor.d.ts.map +1 -1
- package/dist/processors/static-array-processor.js +3 -2
- package/dist/processors/static-array-processor.js.map +1 -1
- package/dist/statics/index.mjs +5 -1
- package/dist/statics/index.mjs.map +1 -1
- package/dist/visual-edit-plugin.d.ts +1 -0
- package/dist/visual-edit-plugin.d.ts.map +1 -1
- package/dist/visual-edit-plugin.js +178 -29
- package/dist/visual-edit-plugin.js.map +1 -1
- package/package.json +1 -1
- package/src/capabilities/inline-edit/controller.ts +245 -0
- package/src/capabilities/inline-edit/dom-utils.ts +48 -0
- package/src/capabilities/inline-edit/index.ts +2 -0
- package/src/capabilities/inline-edit/types.ts +30 -0
- package/src/injections/visual-edit-agent/README.md +222 -0
- package/src/injections/visual-edit-agent/capabilities/inline-editing/core.ts +120 -0
- package/src/injections/visual-edit-agent/capabilities/inline-editing/index.ts +10 -0
- package/src/injections/visual-edit-agent/capabilities/inline-editing/styles.ts +26 -0
- package/src/injections/visual-edit-agent/capabilities/inline-editing/validation.ts +87 -0
- package/src/injections/visual-edit-agent/constants.ts +9 -0
- package/src/injections/visual-edit-agent/handlers/click-handlers.ts +146 -0
- package/src/injections/visual-edit-agent/handlers/hover-handlers.ts +78 -0
- package/src/injections/visual-edit-agent/handlers/inline-edit-handlers.ts +146 -0
- package/src/injections/visual-edit-agent/handlers/message-handlers.ts +201 -0
- package/src/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.ts +65 -0
- package/src/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.ts +40 -0
- package/src/injections/visual-edit-agent/handlers/messages/types.ts +134 -0
- package/src/injections/visual-edit-agent/index.ts +121 -0
- package/src/injections/visual-edit-agent/state/agent-state.ts +31 -0
- package/src/injections/visual-edit-agent/ui/overlay.ts +126 -0
- package/src/injections/visual-edit-agent/utils/dom-utils.ts +39 -0
- package/src/injections/visual-edit-agent.ts +1 -625
- package/src/jsx-processor.ts +14 -41
- package/src/jsx-utils.ts +0 -116
- package/src/processors/shared-utils.ts +0 -596
- package/src/processors/static-array-processor.ts +3 -6
- package/src/visual-edit-plugin.ts +215 -34
- package/dist/consts.d.ts +0 -12
- package/dist/consts.d.ts.map +0 -1
- package/dist/consts.js +0 -12
- package/dist/consts.js.map +0 -1
- package/dist/processors/collection-id-processor.d.ts +0 -20
- package/dist/processors/collection-id-processor.d.ts.map +0 -1
- package/dist/processors/collection-id-processor.js +0 -182
- package/dist/processors/collection-id-processor.js.map +0 -1
- package/dist/processors/collection-item-field-processor.d.ts +0 -33
- package/dist/processors/collection-item-field-processor.d.ts.map +0 -1
- package/dist/processors/collection-item-field-processor.js +0 -275
- package/dist/processors/collection-item-field-processor.js.map +0 -1
- package/dist/processors/collection-item-id-processor.d.ts +0 -12
- package/dist/processors/collection-item-id-processor.d.ts.map +0 -1
- package/dist/processors/collection-item-id-processor.js +0 -50
- package/dist/processors/collection-item-id-processor.js.map +0 -1
- package/dist/processors/collection-reference-field-processor.d.ts +0 -31
- package/dist/processors/collection-reference-field-processor.d.ts.map +0 -1
- package/dist/processors/collection-reference-field-processor.js +0 -174
- package/dist/processors/collection-reference-field-processor.js.map +0 -1
- package/dist/processors/collection-tracing-utils.d.ts +0 -31
- package/dist/processors/collection-tracing-utils.d.ts.map +0 -1
- package/dist/processors/collection-tracing-utils.js +0 -326
- package/dist/processors/collection-tracing-utils.js.map +0 -1
- package/dist/types.d.ts +0 -5
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/src/consts.ts +0 -12
- package/src/processors/collection-id-processor.ts +0 -261
- package/src/processors/collection-item-field-processor.ts +0 -410
- package/src/processors/collection-item-id-processor.ts +0 -69
- package/src/processors/collection-reference-field-processor.ts +0 -225
- package/src/processors/collection-tracing-utils.ts +0 -436
- package/src/types.ts +0 -4
- /package/dist/{types.js → capabilities/inline-edit/types.js} +0 -0
|
@@ -2,27 +2,175 @@ 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 { StaticArrayProcessor } from "./processors/static-array-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
|
+
}
|
|
7
102
|
export function visualEditPlugin() {
|
|
8
103
|
return {
|
|
9
104
|
name: "visual-edit-transform",
|
|
10
105
|
apply: (config) => config.mode === "development",
|
|
11
106
|
enforce: "pre",
|
|
12
107
|
order: "pre",
|
|
108
|
+
// Inject Tailwind CDN for visual editing capabilities
|
|
13
109
|
transformIndexHtml(html) {
|
|
110
|
+
// Inject the Tailwind CSS CDN script right before the closing </head> tag
|
|
14
111
|
const tailwindScript = ` <!-- Tailwind CSS CDN for visual editing -->\n <script src="https://cdn.tailwindcss.com"></script>\n `;
|
|
15
112
|
return html.replace("</head>", tailwindScript + "</head>");
|
|
16
113
|
},
|
|
17
114
|
transform(code, id) {
|
|
115
|
+
// Skip node_modules and visual-edit-agent itself
|
|
18
116
|
if (id.includes("node_modules") || id.includes("visual-edit-agent")) {
|
|
19
117
|
return null;
|
|
20
118
|
}
|
|
119
|
+
// Process JS/JSX/TS/TSX files
|
|
21
120
|
if (!id.match(/\.(jsx?|tsx?)$/)) {
|
|
22
121
|
return null;
|
|
23
122
|
}
|
|
24
|
-
|
|
123
|
+
// Extract filename from path, preserving pages/ or components/ structure
|
|
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
|
+
}
|
|
25
172
|
try {
|
|
173
|
+
// Parse the code into an AST
|
|
26
174
|
const ast = parse(code, {
|
|
27
175
|
sourceType: "module",
|
|
28
176
|
plugins: [
|
|
@@ -43,16 +191,41 @@ export function visualEditPlugin() {
|
|
|
43
191
|
"throwExpressions",
|
|
44
192
|
],
|
|
45
193
|
});
|
|
194
|
+
// Traverse the AST and add source location and dynamic content attributes to JSX elements
|
|
46
195
|
JSXUtils.init(t);
|
|
47
|
-
const
|
|
196
|
+
const staticArrayProcessor = new StaticArrayProcessor(t);
|
|
197
|
+
let elementsProcessed = 0;
|
|
48
198
|
traverse.default(ast, {
|
|
49
199
|
JSXElement(path) {
|
|
50
200
|
const jsxElement = path.node;
|
|
201
|
+
const openingElement = jsxElement.openingElement;
|
|
202
|
+
// Skip fragments
|
|
51
203
|
if (t.isJSXFragment(jsxElement))
|
|
52
204
|
return;
|
|
53
|
-
|
|
205
|
+
// Skip if already has source location attribute
|
|
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++;
|
|
54
226
|
},
|
|
55
227
|
});
|
|
228
|
+
// Generate the code back from the AST
|
|
56
229
|
const result = generate.default(ast, {
|
|
57
230
|
compact: false,
|
|
58
231
|
concise: false,
|
|
@@ -66,35 +239,11 @@ export function visualEditPlugin() {
|
|
|
66
239
|
catch (error) {
|
|
67
240
|
console.error("Failed to add source location to JSX:", error);
|
|
68
241
|
return {
|
|
69
|
-
code: code,
|
|
242
|
+
code: code, // Return original code on failure
|
|
70
243
|
map: null,
|
|
71
244
|
};
|
|
72
245
|
}
|
|
73
246
|
},
|
|
74
247
|
};
|
|
75
248
|
}
|
|
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
|
-
}
|
|
100
249
|
//# 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,IAAI,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,OAAO,EAAE,
|
|
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,IAAI,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,OAAO,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,mEAAmE;AACnE,MAAM,UAAU,+BAA+B,CAAC,UAAe;IAC7D,IAAI,iBAAiB,GAAG,KAAK,CAAC;IAE9B,iEAAiE;IACjE,SAAS,0BAA0B,CAAC,IAAS;QAC3C,wDAAwD;QACxD,IAAI,CAAC,CAAC,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC;YACrC,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;YAEnC,4BAA4B;YAC5B,IAAI,CAAC,CAAC,oBAAoB,CAAC,UAAU,CAAC,EAAE,CAAC;gBACvC,OAAO,KAAK,CAAC;YACf,CAAC;YA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,CAC9C,CAAC;wBAEF,+DAA+D;wBAC/D,cAAc,CAAC,UAAU,CAAC,OAAO,CAC/B,kBAAkB,EAClB,kBAAkB,CACnB,CAAC;wBAEF,oBAAoB,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC;wBACzD,iBAAiB,EAAE,CAAC;oBACtB,CAAC;iBACF,CAAC,CAAC;gBAEH,sCAAsC;gBACtC,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE;oBACnC,OAAO,EAAE,KAAK;oBACd,OAAO,EAAE,KAAK;oBACd,WAAW,EAAE,IAAI;iBAClB,CAAC,CAAC;gBAEH,OAAO;oBACL,IAAI,EAAE,MAAM,CAAC,IAAI;oBACjB,GAAG,EAAE,IAAI;iBACV,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,uCAAuC,EAAE,KAAK,CAAC,CAAC;gBAC9D,OAAO;oBACL,IAAI,EAAE,IAAI,EAAE,kCAAkC;oBAC9C,GAAG,EAAE,IAAI;iBACV,CAAC;YACJ,CAAC;QACH,CAAC;KACQ,CAAC;AACd,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import type { InlineEditHost, InlineEditController } from "./types.js";
|
|
2
|
+
import {
|
|
3
|
+
injectFocusOutlineCSS,
|
|
4
|
+
removeFocusOutlineCSS,
|
|
5
|
+
selectText,
|
|
6
|
+
shouldEnterInlineEditingMode,
|
|
7
|
+
} from "./dom-utils.js";
|
|
8
|
+
|
|
9
|
+
const DEBOUNCE_MS = 500;
|
|
10
|
+
|
|
11
|
+
export function createInlineEditController(
|
|
12
|
+
host: InlineEditHost
|
|
13
|
+
): InlineEditController {
|
|
14
|
+
let currentEditingElement: HTMLElement | null = null;
|
|
15
|
+
let debouncedSendTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
16
|
+
let enabled = false;
|
|
17
|
+
const listenerAbortControllers = new WeakMap<HTMLElement, AbortController>();
|
|
18
|
+
|
|
19
|
+
// --- Private helpers ---
|
|
20
|
+
|
|
21
|
+
const repositionOverlays = () => {
|
|
22
|
+
const selectedId = host.getSelectedElementId();
|
|
23
|
+
if (!selectedId) return;
|
|
24
|
+
const elements = host.findElementsById(selectedId);
|
|
25
|
+
const overlays = host.getSelectedOverlays();
|
|
26
|
+
overlays.forEach((overlay, i) => {
|
|
27
|
+
if (i < elements.length && elements[i]) {
|
|
28
|
+
host.positionOverlay(overlay, elements[i]);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const reportEdit = (element: HTMLElement) => {
|
|
34
|
+
const originalContent = element.dataset.originalTextContent;
|
|
35
|
+
const newContent = element.textContent;
|
|
36
|
+
|
|
37
|
+
const svgElement = element as unknown as SVGElement;
|
|
38
|
+
const rect = element.getBoundingClientRect();
|
|
39
|
+
|
|
40
|
+
window.parent.postMessage(
|
|
41
|
+
{
|
|
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,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
originalContent,
|
|
67
|
+
newContent,
|
|
68
|
+
},
|
|
69
|
+
"*"
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
element.dataset.originalTextContent = newContent || "";
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const debouncedReport = (element: HTMLElement) => {
|
|
76
|
+
if (debouncedSendTimeout) clearTimeout(debouncedSendTimeout);
|
|
77
|
+
debouncedSendTimeout = setTimeout(() => reportEdit(element), DEBOUNCE_MS);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const onTextInput = (element: HTMLElement) => {
|
|
81
|
+
repositionOverlays();
|
|
82
|
+
debouncedReport(element);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const handleInputEvent = function (this: HTMLElement) {
|
|
86
|
+
onTextInput(this);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const makeEditable = (element: HTMLElement) => {
|
|
90
|
+
injectFocusOutlineCSS();
|
|
91
|
+
|
|
92
|
+
element.dataset.originalTextContent = element.textContent || "";
|
|
93
|
+
element.dataset.originalCursor = element.style.cursor;
|
|
94
|
+
element.contentEditable = "true";
|
|
95
|
+
|
|
96
|
+
const abortController = new AbortController();
|
|
97
|
+
listenerAbortControllers.set(element, abortController);
|
|
98
|
+
element.addEventListener("input", handleInputEvent, {
|
|
99
|
+
signal: abortController.signal,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
element.style.cursor = "text";
|
|
103
|
+
selectText(element);
|
|
104
|
+
setTimeout(() => {
|
|
105
|
+
if (element.isConnected) {
|
|
106
|
+
element.focus();
|
|
107
|
+
}
|
|
108
|
+
}, 0);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const makeNonEditable = (element: HTMLElement) => {
|
|
112
|
+
const abortController = listenerAbortControllers.get(element);
|
|
113
|
+
if (abortController) {
|
|
114
|
+
abortController.abort();
|
|
115
|
+
listenerAbortControllers.delete(element);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!element.isConnected) return;
|
|
119
|
+
|
|
120
|
+
removeFocusOutlineCSS();
|
|
121
|
+
element.contentEditable = "false";
|
|
122
|
+
delete element.dataset.originalTextContent;
|
|
123
|
+
|
|
124
|
+
if (element.dataset.originalCursor !== undefined) {
|
|
125
|
+
element.style.cursor = element.dataset.originalCursor;
|
|
126
|
+
delete element.dataset.originalCursor;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// --- Public API ---
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
get enabled() {
|
|
134
|
+
return enabled;
|
|
135
|
+
},
|
|
136
|
+
set enabled(value: boolean) {
|
|
137
|
+
enabled = value;
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
isEditing() {
|
|
141
|
+
return currentEditingElement !== null;
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
getCurrentElement() {
|
|
145
|
+
return currentEditingElement;
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
canEdit(element: Element) {
|
|
149
|
+
return shouldEnterInlineEditingMode(element);
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
startEditing(element: HTMLElement) {
|
|
153
|
+
currentEditingElement = element;
|
|
154
|
+
|
|
155
|
+
host.getSelectedOverlays().forEach((o) => {
|
|
156
|
+
o.style.display = "none";
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
makeEditable(element);
|
|
160
|
+
|
|
161
|
+
window.parent.postMessage(
|
|
162
|
+
{
|
|
163
|
+
type: "content-editing-started",
|
|
164
|
+
visualSelectorId: host.getSelectedElementId(),
|
|
165
|
+
},
|
|
166
|
+
"*"
|
|
167
|
+
);
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
stopEditing() {
|
|
171
|
+
if (!currentEditingElement) return;
|
|
172
|
+
|
|
173
|
+
if (debouncedSendTimeout) {
|
|
174
|
+
clearTimeout(debouncedSendTimeout);
|
|
175
|
+
debouncedSendTimeout = null;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const element = currentEditingElement;
|
|
179
|
+
makeNonEditable(element);
|
|
180
|
+
|
|
181
|
+
host.getSelectedOverlays().forEach((o) => {
|
|
182
|
+
o.style.display = "";
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
repositionOverlays();
|
|
186
|
+
|
|
187
|
+
window.parent.postMessage(
|
|
188
|
+
{
|
|
189
|
+
type: "content-editing-ended",
|
|
190
|
+
visualSelectorId: host.getSelectedElementId(),
|
|
191
|
+
},
|
|
192
|
+
"*"
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
currentEditingElement = null;
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
markElementsSelected(elements: Element[]) {
|
|
199
|
+
elements.forEach((el) => {
|
|
200
|
+
if (el instanceof HTMLElement) {
|
|
201
|
+
el.dataset.selected = "true";
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
clearSelectedMarks(elementId: string | null) {
|
|
207
|
+
if (!elementId) return;
|
|
208
|
+
host.findElementsById(elementId).forEach((el) => {
|
|
209
|
+
if (el instanceof HTMLElement) {
|
|
210
|
+
delete el.dataset.selected;
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
handleToggleMessage(data: { dataSourceLocation: string; inlineEditingMode: boolean }) {
|
|
216
|
+
if (!enabled) return;
|
|
217
|
+
|
|
218
|
+
const elements = host.findElementsById(data.dataSourceLocation);
|
|
219
|
+
if (elements.length === 0 || !(elements[0] instanceof HTMLElement)) return;
|
|
220
|
+
|
|
221
|
+
const element = elements[0];
|
|
222
|
+
|
|
223
|
+
if (data.inlineEditingMode) {
|
|
224
|
+
if (!shouldEnterInlineEditingMode(element)) return;
|
|
225
|
+
|
|
226
|
+
// Select the element first if not already selected
|
|
227
|
+
if (host.getSelectedElementId() !== data.dataSourceLocation) {
|
|
228
|
+
this.stopEditing();
|
|
229
|
+
host.clearSelection();
|
|
230
|
+
this.markElementsSelected(elements);
|
|
231
|
+
host.createSelectionOverlays(elements, data.dataSourceLocation);
|
|
232
|
+
}
|
|
233
|
+
this.startEditing(element);
|
|
234
|
+
} else {
|
|
235
|
+
if (currentEditingElement === element) {
|
|
236
|
+
this.stopEditing();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
cleanup() {
|
|
242
|
+
this.stopEditing();
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const FOCUS_STYLE_ID = "visual-edit-focus-styles";
|
|
2
|
+
|
|
3
|
+
const EDITABLE_TAGS = [
|
|
4
|
+
"div", "p", "h1", "h2", "h3", "h4", "h5", "h6",
|
|
5
|
+
"span", "li", "td", "a", "button", "label",
|
|
6
|
+
];
|
|
7
|
+
|
|
8
|
+
export const injectFocusOutlineCSS = () => {
|
|
9
|
+
if (document.getElementById(FOCUS_STYLE_ID)) return;
|
|
10
|
+
|
|
11
|
+
const style = document.createElement("style");
|
|
12
|
+
style.id = FOCUS_STYLE_ID;
|
|
13
|
+
style.textContent = `
|
|
14
|
+
[data-selected="true"][contenteditable="true"]:focus {
|
|
15
|
+
outline: none !important;
|
|
16
|
+
}
|
|
17
|
+
`;
|
|
18
|
+
document.head.appendChild(style);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const removeFocusOutlineCSS = () => {
|
|
22
|
+
document.getElementById(FOCUS_STYLE_ID)?.remove();
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const selectText = (element: HTMLElement) => {
|
|
26
|
+
const range = document.createRange();
|
|
27
|
+
range.selectNodeContents(element);
|
|
28
|
+
const selection = window.getSelection();
|
|
29
|
+
selection?.removeAllRanges();
|
|
30
|
+
selection?.addRange(range);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const isEditableTextElement = (element: Element): boolean => {
|
|
34
|
+
if (!(element instanceof HTMLElement)) return false;
|
|
35
|
+
if (!EDITABLE_TAGS.includes(element.tagName.toLowerCase())) return false;
|
|
36
|
+
if (!element.textContent?.trim()) return false;
|
|
37
|
+
if (element.querySelector("img, video, canvas, svg")) return false;
|
|
38
|
+
if (element.children?.length > 0) return false;
|
|
39
|
+
if (element.dataset.dynamicContent === "true") return false;
|
|
40
|
+
return true;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const shouldEnterInlineEditingMode = (element: Element): boolean => {
|
|
44
|
+
if (!(element instanceof HTMLElement) || element.dataset.selected !== "true") {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return isEditableTextElement(element);
|
|
48
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface InlineEditHost {
|
|
2
|
+
findElementsById(id: string | null): Element[];
|
|
3
|
+
getSelectedElementId(): string | null;
|
|
4
|
+
getSelectedOverlays(): HTMLDivElement[];
|
|
5
|
+
positionOverlay(
|
|
6
|
+
overlay: HTMLDivElement,
|
|
7
|
+
element: Element,
|
|
8
|
+
isSelected?: boolean
|
|
9
|
+
): void;
|
|
10
|
+
clearSelection(): void;
|
|
11
|
+
createSelectionOverlays(elements: Element[], elementId: string): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ToggleInlineEditData {
|
|
15
|
+
dataSourceLocation: string;
|
|
16
|
+
inlineEditingMode: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface InlineEditController {
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
isEditing(): boolean;
|
|
22
|
+
getCurrentElement(): HTMLElement | null;
|
|
23
|
+
canEdit(element: Element): boolean;
|
|
24
|
+
startEditing(element: HTMLElement): void;
|
|
25
|
+
stopEditing(): void;
|
|
26
|
+
markElementsSelected(elements: Element[]): void;
|
|
27
|
+
clearSelectedMarks(elementId: string | null): void;
|
|
28
|
+
handleToggleMessage(data: ToggleInlineEditData): void;
|
|
29
|
+
cleanup(): void;
|
|
30
|
+
}
|