@base44-preview/vite-plugin 0.2.26-pr.43.b670203 → 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.
Files changed (168) hide show
  1. package/dist/capabilities/inline-edit/controller.d.ts +3 -0
  2. package/dist/capabilities/inline-edit/controller.d.ts.map +1 -0
  3. package/dist/capabilities/inline-edit/controller.js +194 -0
  4. package/dist/capabilities/inline-edit/controller.js.map +1 -0
  5. package/dist/capabilities/inline-edit/dom-utils.d.ts +6 -0
  6. package/dist/capabilities/inline-edit/dom-utils.d.ts.map +1 -0
  7. package/dist/capabilities/inline-edit/dom-utils.js +49 -0
  8. package/dist/capabilities/inline-edit/dom-utils.js.map +1 -0
  9. package/dist/capabilities/inline-edit/index.d.ts +3 -0
  10. package/dist/capabilities/inline-edit/index.d.ts.map +1 -0
  11. package/dist/capabilities/inline-edit/index.js +2 -0
  12. package/dist/capabilities/inline-edit/index.js.map +1 -0
  13. package/dist/capabilities/inline-edit/types.d.ts +25 -0
  14. package/dist/capabilities/inline-edit/types.d.ts.map +1 -0
  15. package/dist/capabilities/inline-edit/types.js.map +1 -0
  16. package/dist/injections/visual-edit-agent/capabilities/inline-editing/core.d.ts +25 -0
  17. package/dist/injections/visual-edit-agent/capabilities/inline-editing/core.d.ts.map +1 -0
  18. package/dist/injections/visual-edit-agent/capabilities/inline-editing/core.js +95 -0
  19. package/dist/injections/visual-edit-agent/capabilities/inline-editing/core.js.map +1 -0
  20. package/dist/injections/visual-edit-agent/capabilities/inline-editing/index.d.ts +4 -0
  21. package/dist/injections/visual-edit-agent/capabilities/inline-editing/index.d.ts.map +1 -0
  22. package/dist/injections/visual-edit-agent/capabilities/inline-editing/index.js +4 -0
  23. package/dist/injections/visual-edit-agent/capabilities/inline-editing/index.js.map +1 -0
  24. package/dist/injections/visual-edit-agent/capabilities/inline-editing/styles.d.ts +9 -0
  25. package/dist/injections/visual-edit-agent/capabilities/inline-editing/styles.d.ts.map +1 -0
  26. package/dist/injections/visual-edit-agent/capabilities/inline-editing/styles.js +26 -0
  27. package/dist/injections/visual-edit-agent/capabilities/inline-editing/styles.js.map +1 -0
  28. package/dist/injections/visual-edit-agent/capabilities/inline-editing/validation.d.ts +17 -0
  29. package/dist/injections/visual-edit-agent/capabilities/inline-editing/validation.d.ts.map +1 -0
  30. package/dist/injections/visual-edit-agent/capabilities/inline-editing/validation.js +76 -0
  31. package/dist/injections/visual-edit-agent/capabilities/inline-editing/validation.js.map +1 -0
  32. package/dist/injections/visual-edit-agent/constants.d.ts +10 -0
  33. package/dist/injections/visual-edit-agent/constants.d.ts.map +1 -0
  34. package/dist/injections/visual-edit-agent/constants.js +10 -0
  35. package/dist/injections/visual-edit-agent/constants.js.map +1 -0
  36. package/dist/injections/visual-edit-agent/handlers/click-handlers.d.ts +10 -0
  37. package/dist/injections/visual-edit-agent/handlers/click-handlers.d.ts.map +1 -0
  38. package/dist/injections/visual-edit-agent/handlers/click-handlers.js +117 -0
  39. package/dist/injections/visual-edit-agent/handlers/click-handlers.js.map +1 -0
  40. package/dist/injections/visual-edit-agent/handlers/hover-handlers.d.ts +14 -0
  41. package/dist/injections/visual-edit-agent/handlers/hover-handlers.d.ts.map +1 -0
  42. package/dist/injections/visual-edit-agent/handlers/hover-handlers.js +64 -0
  43. package/dist/injections/visual-edit-agent/handlers/hover-handlers.js.map +1 -0
  44. package/dist/injections/visual-edit-agent/handlers/inline-edit-handlers.d.ts +14 -0
  45. package/dist/injections/visual-edit-agent/handlers/inline-edit-handlers.d.ts.map +1 -0
  46. package/dist/injections/visual-edit-agent/handlers/inline-edit-handlers.js +114 -0
  47. package/dist/injections/visual-edit-agent/handlers/inline-edit-handlers.js.map +1 -0
  48. package/dist/injections/visual-edit-agent/handlers/message-handlers.d.ts +26 -0
  49. package/dist/injections/visual-edit-agent/handlers/message-handlers.d.ts.map +1 -0
  50. package/dist/injections/visual-edit-agent/handlers/message-handlers.js +148 -0
  51. package/dist/injections/visual-edit-agent/handlers/message-handlers.js.map +1 -0
  52. package/dist/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.d.ts +7 -0
  53. package/dist/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.d.ts.map +1 -0
  54. package/dist/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.js +54 -0
  55. package/dist/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.js.map +1 -0
  56. package/dist/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.d.ts +11 -0
  57. package/dist/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.d.ts.map +1 -0
  58. package/dist/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.js +32 -0
  59. package/dist/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.js.map +1 -0
  60. package/dist/injections/visual-edit-agent/handlers/messages/types.d.ts +105 -0
  61. package/dist/injections/visual-edit-agent/handlers/messages/types.d.ts.map +1 -0
  62. package/dist/injections/visual-edit-agent/handlers/messages/types.js +28 -0
  63. package/dist/injections/visual-edit-agent/handlers/messages/types.js.map +1 -0
  64. package/dist/injections/visual-edit-agent/index.d.ts +6 -0
  65. package/dist/injections/visual-edit-agent/index.d.ts.map +1 -0
  66. package/dist/injections/visual-edit-agent/index.js +109 -0
  67. package/dist/injections/visual-edit-agent/index.js.map +1 -0
  68. package/dist/injections/visual-edit-agent/state/agent-state.d.ts +17 -0
  69. package/dist/injections/visual-edit-agent/state/agent-state.d.ts.map +1 -0
  70. package/dist/injections/visual-edit-agent/state/agent-state.js +18 -0
  71. package/dist/injections/visual-edit-agent/state/agent-state.js.map +1 -0
  72. package/dist/injections/visual-edit-agent/ui/overlay.d.ts +26 -0
  73. package/dist/injections/visual-edit-agent/ui/overlay.d.ts.map +1 -0
  74. package/dist/injections/visual-edit-agent/ui/overlay.js +104 -0
  75. package/dist/injections/visual-edit-agent/ui/overlay.js.map +1 -0
  76. package/dist/injections/visual-edit-agent/utils/dom-utils.d.ts +14 -0
  77. package/dist/injections/visual-edit-agent/utils/dom-utils.d.ts.map +1 -0
  78. package/dist/injections/visual-edit-agent/utils/dom-utils.js +34 -0
  79. package/dist/injections/visual-edit-agent/utils/dom-utils.js.map +1 -0
  80. package/dist/injections/visual-edit-agent.d.ts +1 -1
  81. package/dist/injections/visual-edit-agent.d.ts.map +1 -1
  82. package/dist/injections/visual-edit-agent.js +1 -506
  83. package/dist/injections/visual-edit-agent.js.map +1 -1
  84. package/dist/jsx-processor.d.ts +1 -4
  85. package/dist/jsx-processor.d.ts.map +1 -1
  86. package/dist/jsx-processor.js +6 -33
  87. package/dist/jsx-processor.js.map +1 -1
  88. package/dist/jsx-utils.d.ts +0 -9
  89. package/dist/jsx-utils.d.ts.map +1 -1
  90. package/dist/jsx-utils.js +0 -86
  91. package/dist/jsx-utils.js.map +1 -1
  92. package/dist/processors/shared-utils.d.ts +0 -64
  93. package/dist/processors/shared-utils.d.ts.map +1 -1
  94. package/dist/processors/shared-utils.js +0 -468
  95. package/dist/processors/shared-utils.js.map +1 -1
  96. package/dist/processors/static-array-processor.d.ts +3 -2
  97. package/dist/processors/static-array-processor.d.ts.map +1 -1
  98. package/dist/processors/static-array-processor.js +3 -2
  99. package/dist/processors/static-array-processor.js.map +1 -1
  100. package/dist/statics/index.mjs +5 -1
  101. package/dist/statics/index.mjs.map +1 -1
  102. package/dist/visual-edit-plugin.d.ts +1 -0
  103. package/dist/visual-edit-plugin.d.ts.map +1 -1
  104. package/dist/visual-edit-plugin.js +178 -29
  105. package/dist/visual-edit-plugin.js.map +1 -1
  106. package/package.json +1 -1
  107. package/src/capabilities/inline-edit/controller.ts +245 -0
  108. package/src/capabilities/inline-edit/dom-utils.ts +48 -0
  109. package/src/capabilities/inline-edit/index.ts +2 -0
  110. package/src/capabilities/inline-edit/types.ts +30 -0
  111. package/src/injections/visual-edit-agent/README.md +222 -0
  112. package/src/injections/visual-edit-agent/capabilities/inline-editing/core.ts +120 -0
  113. package/src/injections/visual-edit-agent/capabilities/inline-editing/index.ts +10 -0
  114. package/src/injections/visual-edit-agent/capabilities/inline-editing/styles.ts +26 -0
  115. package/src/injections/visual-edit-agent/capabilities/inline-editing/validation.ts +87 -0
  116. package/src/injections/visual-edit-agent/constants.ts +9 -0
  117. package/src/injections/visual-edit-agent/handlers/click-handlers.ts +146 -0
  118. package/src/injections/visual-edit-agent/handlers/hover-handlers.ts +78 -0
  119. package/src/injections/visual-edit-agent/handlers/inline-edit-handlers.ts +146 -0
  120. package/src/injections/visual-edit-agent/handlers/message-handlers.ts +201 -0
  121. package/src/injections/visual-edit-agent/handlers/messages/toggle-inline-edit-mode.ts +65 -0
  122. package/src/injections/visual-edit-agent/handlers/messages/toggle-visual-edit-mode.ts +40 -0
  123. package/src/injections/visual-edit-agent/handlers/messages/types.ts +134 -0
  124. package/src/injections/visual-edit-agent/index.ts +121 -0
  125. package/src/injections/visual-edit-agent/state/agent-state.ts +31 -0
  126. package/src/injections/visual-edit-agent/ui/overlay.ts +126 -0
  127. package/src/injections/visual-edit-agent/utils/dom-utils.ts +39 -0
  128. package/src/injections/visual-edit-agent.ts +1 -625
  129. package/src/jsx-processor.ts +14 -41
  130. package/src/jsx-utils.ts +0 -116
  131. package/src/processors/shared-utils.ts +0 -596
  132. package/src/processors/static-array-processor.ts +3 -6
  133. package/src/visual-edit-plugin.ts +215 -34
  134. package/dist/consts.d.ts +0 -12
  135. package/dist/consts.d.ts.map +0 -1
  136. package/dist/consts.js +0 -12
  137. package/dist/consts.js.map +0 -1
  138. package/dist/processors/collection-id-processor.d.ts +0 -20
  139. package/dist/processors/collection-id-processor.d.ts.map +0 -1
  140. package/dist/processors/collection-id-processor.js +0 -182
  141. package/dist/processors/collection-id-processor.js.map +0 -1
  142. package/dist/processors/collection-item-field-processor.d.ts +0 -39
  143. package/dist/processors/collection-item-field-processor.d.ts.map +0 -1
  144. package/dist/processors/collection-item-field-processor.js +0 -281
  145. package/dist/processors/collection-item-field-processor.js.map +0 -1
  146. package/dist/processors/collection-item-id-processor.d.ts +0 -12
  147. package/dist/processors/collection-item-id-processor.d.ts.map +0 -1
  148. package/dist/processors/collection-item-id-processor.js +0 -50
  149. package/dist/processors/collection-item-id-processor.js.map +0 -1
  150. package/dist/processors/collection-reference-field-processor.d.ts +0 -31
  151. package/dist/processors/collection-reference-field-processor.d.ts.map +0 -1
  152. package/dist/processors/collection-reference-field-processor.js +0 -174
  153. package/dist/processors/collection-reference-field-processor.js.map +0 -1
  154. package/dist/processors/collection-tracing-utils.d.ts +0 -31
  155. package/dist/processors/collection-tracing-utils.d.ts.map +0 -1
  156. package/dist/processors/collection-tracing-utils.js +0 -326
  157. package/dist/processors/collection-tracing-utils.js.map +0 -1
  158. package/dist/types.d.ts +0 -5
  159. package/dist/types.d.ts.map +0 -1
  160. package/dist/types.js.map +0 -1
  161. package/src/consts.ts +0 -12
  162. package/src/processors/collection-id-processor.ts +0 -261
  163. package/src/processors/collection-item-field-processor.ts +0 -433
  164. package/src/processors/collection-item-id-processor.ts +0 -69
  165. package/src/processors/collection-reference-field-processor.ts +0 -225
  166. package/src/processors/collection-tracing-utils.ts +0 -436
  167. package/src/types.ts +0 -4
  168. /package/dist/{types.js → capabilities/inline-edit/types.js} +0 -0
@@ -0,0 +1,222 @@
1
+ # Visual Edit Agent - Modular Architecture
2
+
3
+ The visual edit agent has been refactored into a clean, modular structure with proper separation of concerns.
4
+
5
+ ## Directory Structure
6
+
7
+ ```
8
+ src/injections/visual-edit-agent/
9
+ ├── index.ts # Main entry point & setup orchestration
10
+ ├── constants.ts # Shared constants
11
+ ├── state/
12
+ │ └── agent-state.ts # Centralized state management
13
+ ├── ui/
14
+ │ └── overlay.ts # Overlay creation, positioning & selection helpers
15
+ ├── utils/
16
+ │ └── dom-utils.ts # DOM helpers (position, viewport, classes)
17
+ ├── handlers/
18
+ │ ├── hover-handlers.ts # Mouse hover interactions
19
+ │ ├── click-handlers.ts # Element selection & click handling
20
+ │ ├── inline-edit-handlers.ts # Bridges to inline editing capability
21
+ │ ├── message-handlers.ts # Parent window message handling
22
+ │ └── messages/
23
+ │ ├── types.ts # Message type enums & interfaces
24
+ │ ├── toggle-visual-edit-mode.ts # Handles toggle-visual-edit-mode
25
+ │ └── toggle-inline-edit-mode.ts # Handles toggle-inline-edit-mode
26
+ └── capabilities/
27
+ └── inline-editing/ # Inline text editing capability
28
+ ├── index.ts # Public API
29
+ ├── core.ts # ContentEditable logic
30
+ ├── validation.ts # Element eligibility checks
31
+ ├── styles.ts # CSS injection / removal
32
+ └── README.md # Documentation
33
+ ```
34
+
35
+ ## Module Responsibilities
36
+
37
+ ### `index.ts` - Setup & Orchestration
38
+ - Initializes the visual edit agent
39
+ - Sets up all event listeners
40
+ - Creates mutation observers
41
+ - Coordinates between different modules
42
+
43
+ ### `constants.ts` - Configuration
44
+ - Defines all magic numbers and styling constants
45
+ - Single source of truth for configuration values
46
+ - Easy to modify behavior without touching logic
47
+
48
+ ### `state/agent-state.ts` - State Management
49
+ - **`AgentState` interface**: TypeScript interface defining all state
50
+ - **`createAgentState()`**: Factory function to create initial state
51
+ - Centralizes all state in one place for easier debugging
52
+
53
+ ### `ui/overlay.ts` - UI Components & Overlay State Helpers
54
+ - **`createOverlay()`**: Creates hover/selection overlay elements
55
+ - **`positionOverlay()`**: Positions a single overlay relative to an element
56
+ - **`clearOverlays()`**: Removes overlay elements from the DOM
57
+ - **`clearSelection(state)`**: Removes `data-selected`, clears selected overlays, and nulls `selectedElementId` — single source of truth for deselection used by `unselectElement`, `handleElementSelection`, and `toggleVisualEditMode`
58
+ - **`repositionSelectedOverlays(state)`**: Re-positions all selected overlays after layout changes
59
+ - **`repositionHoverOverlays(state)`**: Re-positions all hover overlays after layout changes
60
+
61
+ ### `handlers/hover-handlers.ts` - Hover Interactions
62
+ - **`handleMouseOver()`**: Shows hover overlays on element hover
63
+ - **`handleMouseOut()`**: Clears hover overlays
64
+ - **`clearHoverOverlays()`**: Cleanup utility
65
+ - Manages the hover state and visual feedback
66
+
67
+ ### `handlers/click-handlers.ts` - Selection Logic
68
+ - **`handleElementClick()`**: Main click event handler
69
+ - **`handleElementSelection()`**: Handles first click (selection)
70
+ - Manages the selection flow and second-click inline editing trigger
71
+ - Sends `element-selected` messages to parent
72
+
73
+ ### `handlers/inline-edit-handlers.ts` - Inline Editing
74
+ - **`handleEnterInlineEditingMode()`**: Enables contentEditable
75
+ - **`handleClearInlineEditingMode()`**: Disables contentEditable
76
+ - **`setupInlineEditCallback()`**: Wires up text input handler
77
+ - **Debounced reporting**: 500ms debounce for `inline-edit` messages
78
+ - Bridges between visual edit agent and the inline-editing capability
79
+
80
+ ### `capabilities/inline-editing/` - Inline Editing Capability
81
+ Reusable inline text editing using contentEditable.
82
+ - **`core.ts`**: Main contentEditable logic — `enterInlineEditingMode` injects the focus-suppression CSS; `clearInlineEditingMode` removes it automatically
83
+ - **`validation.ts`**: Element eligibility checks
84
+ - **`styles.ts`**: CSS injection (`injectFocusOutlineCSS`) and removal (`removeFocusOutlineCSS`)
85
+ - **`README.md`**: Complete documentation
86
+ - See [capabilities/inline-editing/README.md](./capabilities/inline-editing/README.md) for details
87
+
88
+ ### `handlers/messages/` - Per-Message Handlers
89
+ Split out of `message-handlers.ts` to keep each handler focused:
90
+ - **`types.ts`**: `IncomingMessageType` / `OutgoingMessageType` enums and all message interfaces
91
+ - **`toggle-visual-edit-mode.ts`**: Enables/disables visual edit mode, clears all overlays & selection via `clearSelection`, updates the inline-edit experiment flag
92
+ - **`toggle-inline-edit-mode.ts`**: Programmatically enters/exits inline editing for a given element
93
+
94
+ ### `handlers/message-handlers.ts` - Parent Communication
95
+ - **`handleMessage()`**: Switch-board for all incoming messages — delegates to per-message handlers above
96
+ - **`handleScroll()`**: Sends `element-position-update` to parent on scroll
97
+ - **`handleResize()`**: Calls `repositionSelectedOverlays` + `repositionHoverOverlays` on resize
98
+ - **`updateElementClassesAndReposition()`**: Updates classes & repositions overlays after CSS reflow
99
+ - **`updateElementContent()`**: Updates element `innerText` & repositions overlays
100
+ - **`unselectElement()`**: Exits inline editing if active, then delegates to `clearSelection`
101
+ - Central hub for all parent-iframe communication
102
+
103
+ ## State Flow
104
+
105
+ ```
106
+ User Action
107
+
108
+ Handler (e.g., handleElementClick)
109
+
110
+ Modifies State (AgentState)
111
+
112
+ Updates UI (overlay.ts)
113
+
114
+ Sends Message to Parent (window.parent.postMessage)
115
+ ```
116
+
117
+ ## Position Update Strategy
118
+
119
+ Following Framewire's reactive pattern, we **update positions at the source** instead of using a broad MutationObserver. This is more performant and predictable.
120
+
121
+ ### Position Updates Happen:
122
+ 1. **Element Selection** - When user clicks an element
123
+ 2. **Inline Editing** - During text input (debounced)
124
+ 3. **Scroll Events** - Via scroll listener
125
+ 4. **Window Resize** - Via resize listener
126
+ 5. **Parent Updates** - When parent sends class/content changes (with 50ms delay for CSS reflow)
127
+ 6. **Hover Events** - When user hovers over elements
128
+
129
+ See [POSITION_UPDATES.md](./POSITION_UPDATES.md) for detailed analysis.
130
+
131
+ ## Key Features
132
+
133
+ ### 1. **Separation of Concerns**
134
+ Each module has a single, well-defined responsibility
135
+
136
+ ### 2. **Shared Helpers Over Repeated Logic**
137
+ Overlay repositioning and selection clearing live in `overlay.ts` as named helpers (`repositionSelectedOverlays`, `repositionHoverOverlays`, `clearSelection`) rather than being duplicated across handler files
138
+
139
+ ### 3. **Centralized State**
140
+ All state is in `AgentState`, passed to handlers explicitly
141
+
142
+ ### 4. **Type Safety**
143
+ TypeScript interfaces define clear contracts between modules
144
+
145
+ ### 5. **Easy to Extend**
146
+ Adding new handlers or message types is straightforward
147
+
148
+ ### 6. **Better Debugging**
149
+ Smaller files and clear responsibilities make debugging easier
150
+
151
+ ## Message Types
152
+
153
+ ### Incoming (from Parent)
154
+ - `toggle-visual-edit-mode` - Enable/disable visual edit mode + experiment flags
155
+ - `update-classes` - Update element classes
156
+ - `update-content` - Update element text content
157
+ - `unselect-element` - Clear current selection
158
+ - `refresh-page` - Reload the page
159
+ - `toggle-inline-edit-mode` - Programmatically enable/disable inline editing
160
+ - `request-element-position` - Request position of selected element
161
+ - `popover-drag-state` - Notify about popover drag state
162
+ - `dropdown-state` - Notify about dropdown open state
163
+
164
+ ### Outgoing (to Parent)
165
+ - `visual-edit-agent-ready` - Agent initialized and ready
166
+ - `element-selected` - Element was selected
167
+ - `element-position-update` - Selected element position changed
168
+ - `content-editing-started` - Inline editing began
169
+ - `inline-edit` - Debounced content change during editing
170
+ - `content-editing-ended` - Inline editing finished
171
+ - `close-dropdowns` - Request to close dropdowns
172
+
173
+ ## Inline Editing Experiment
174
+
175
+ Inline editing is gated behind an experiment flag: `specs.newInlineEditEnabled`
176
+
177
+ **Enable via:**
178
+ ```typescript
179
+ {
180
+ type: "toggle-visual-edit-mode",
181
+ data: {
182
+ enabled: true,
183
+ specs: {
184
+ newInlineEditEnabled: true
185
+ }
186
+ }
187
+ }
188
+ ```
189
+
190
+ **When enabled:**
191
+ - Second click on selected text element enters inline edit mode
192
+ - `toggle-inline-edit-mode` messages work
193
+ - 500ms debounced `inline-edit` messages sent during typing
194
+
195
+ **When disabled:**
196
+ - Second click does nothing
197
+ - `toggle-inline-edit-mode` messages ignored
198
+
199
+ ## Constants
200
+
201
+ All configurable values are in `constants.ts`:
202
+
203
+ ```typescript
204
+ INLINE_EDIT_DEBOUNCE_MS = 500 // Debounce time for inline edits
205
+ OVERLAY_Z_INDEX = "9999" // Z-index for overlays
206
+ SELECTED_BORDER_COLOR = "#2563EB" // Selected element border color
207
+ HOVER_BORDER_COLOR = "#95a5fc" // Hover element border color
208
+ REPOSITION_DELAY_MS = 50 // Delay before repositioning overlays
209
+ // ... and more
210
+ ```
211
+
212
+ ## Usage
213
+
214
+ Import and call the setup function:
215
+
216
+ ```typescript
217
+ import { setupVisualEditAgent } from './visual-edit-agent.js';
218
+
219
+ setupVisualEditAgent();
220
+ ```
221
+
222
+ That's it! The agent will initialize, set up all listeners, and start responding to messages.
@@ -0,0 +1,120 @@
1
+ import { injectFocusOutlineCSS, removeFocusOutlineCSS } from "./styles.js";
2
+ import { selectText, isEditableTextElement } from "./validation.js";
3
+
4
+ /**
5
+ * Callback function type for handling text input changes
6
+ */
7
+ type OnTextInputChange = (element: HTMLElement) => void;
8
+
9
+ /**
10
+ * Global callback that gets triggered on every input event
11
+ * This is set by the visual-edit-agent and includes both updatePosition and reportInlineEdit
12
+ */
13
+ let onTextInputChangeCallback: OnTextInputChange | null = null;
14
+
15
+ /**
16
+ * Set the callback function that will be called on text input changes
17
+ */
18
+ export function setInlineEditCallback(callback: OnTextInputChange | null): void {
19
+ onTextInputChangeCallback = callback;
20
+ }
21
+
22
+ /**
23
+ * WeakMap to track AbortControllers for each element's input listener.
24
+ * Using WeakMap ensures no memory leaks — when an element is garbage collected,
25
+ * its entry is automatically removed.
26
+ */
27
+ const listenerAbortControllers = new WeakMap<HTMLElement, AbortController>();
28
+
29
+ /**
30
+ * Internal handler for the native input event
31
+ */
32
+ function handleInputEvent(this: HTMLElement, e: Event): void {
33
+ if (onTextInputChangeCallback) {
34
+ onTextInputChangeCallback(this);
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Check if an element should enter inline editing mode
40
+ * Called when user clicks on an already-selected element
41
+ */
42
+ export function shouldEnterInlineEditingMode(element: Element): boolean {
43
+ // Must have data-selected="true"
44
+ if (!(element instanceof HTMLElement) || element.dataset.selected !== "true") {
45
+ return false;
46
+ }
47
+
48
+ // Must pass all editability checks
49
+ if (!isEditableTextElement(element)) {
50
+ return false;
51
+ }
52
+
53
+ return true;
54
+ }
55
+
56
+ /**
57
+ * Enable contentEditable mode on an element
58
+ * Follows the exact flow from inlineEdit.md
59
+ */
60
+ export function enterInlineEditingMode(element: HTMLElement): void {
61
+ // Inject CSS to suppress focus outline
62
+ injectFocusOutlineCSS();
63
+
64
+ // Store original state
65
+ element.dataset.originalTextContent = element.textContent || "";
66
+ element.dataset.originalCursor = element.style.cursor;
67
+
68
+ // Enable contentEditable
69
+ element.contentEditable = "true";
70
+
71
+ // Create an AbortController to manage the input listener lifecycle
72
+ const abortController = new AbortController();
73
+ listenerAbortControllers.set(element, abortController);
74
+
75
+ // Add input event listener with AbortSignal for automatic cleanup
76
+ element.addEventListener("input", handleInputEvent, { signal: abortController.signal });
77
+
78
+ // Set cursor to text
79
+ element.style.cursor = "text";
80
+
81
+ // Select all text
82
+ selectText(element);
83
+
84
+ // Focus after render
85
+ setTimeout(() => {
86
+ element.focus();
87
+ }, 0);
88
+ }
89
+
90
+ /**
91
+ * Disable contentEditable mode on an element
92
+ * Reverses everything done by enterInlineEditingMode
93
+ */
94
+ export function clearInlineEditingMode(element: HTMLElement): void {
95
+ // Abort the input event listener using the stored AbortController
96
+ const abortController = listenerAbortControllers.get(element);
97
+ if (abortController) {
98
+ abortController.abort();
99
+ listenerAbortControllers.delete(element);
100
+ }
101
+
102
+ // If element was removed from DOM before cleanup, skip DOM operations
103
+ if (!element.isConnected) {
104
+ return;
105
+ }
106
+
107
+ removeFocusOutlineCSS();
108
+
109
+ // Disable contentEditable
110
+ element.contentEditable = "false";
111
+
112
+ // Remove stored original text content
113
+ delete element.dataset.originalTextContent;
114
+
115
+ // Restore original cursor
116
+ if (element.dataset.originalCursor !== undefined) {
117
+ element.style.cursor = element.dataset.originalCursor;
118
+ delete element.dataset.originalCursor;
119
+ }
120
+ }
@@ -0,0 +1,10 @@
1
+ export {
2
+ enterInlineEditingMode,
3
+ clearInlineEditingMode,
4
+ shouldEnterInlineEditingMode,
5
+ setInlineEditCallback,
6
+ } from "./core.js";
7
+
8
+ export { injectFocusOutlineCSS, removeFocusOutlineCSS } from "./styles.js";
9
+
10
+ export { isEditableTextElement, isStaticArrayTextElement, selectText } from "./validation.js";
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Inject CSS to suppress the browser's default focus outline on contentEditable elements
3
+ */
4
+ export function injectFocusOutlineCSS(): void {
5
+ const existingStyle = document.getElementById("visual-edit-focus-styles");
6
+ if (existingStyle) return;
7
+
8
+ const style = document.createElement("style");
9
+ style.id = "visual-edit-focus-styles";
10
+ style.textContent = `
11
+ [data-selected="true"][contenteditable="true"]:focus {
12
+ outline: none !important;
13
+ }
14
+ `;
15
+ document.head.appendChild(style);
16
+ }
17
+
18
+ /**
19
+ * Remove the injected focus outline CSS
20
+ */
21
+ export function removeFocusOutlineCSS(): void {
22
+ const style = document.getElementById("visual-edit-focus-styles");
23
+ if (style) {
24
+ style.remove();
25
+ }
26
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Select all text content in an element using the Selection API
3
+ */
4
+ export function selectText(element: HTMLElement): void {
5
+ const range = document.createRange();
6
+ range.selectNodeContents(element);
7
+ const selection = window.getSelection();
8
+ selection?.removeAllRanges();
9
+ selection?.addRange(range);
10
+ }
11
+
12
+ /**
13
+ * Check if an element is a static array text element.
14
+ * These elements are "dynamic" (they reference item.field) but their source
15
+ * is a compile-time static array, so they can be safely edited inline.
16
+ */
17
+ export function isStaticArrayTextElement(element: HTMLElement): boolean {
18
+ return !!element.dataset.arrField;
19
+ }
20
+
21
+ const ALLOWED_INLINE_EDIT_TAGS = [
22
+ "div",
23
+ "p",
24
+ "h1",
25
+ "h2",
26
+ "h3",
27
+ "h4",
28
+ "h5",
29
+ "h6",
30
+ "span",
31
+ "li",
32
+ "td",
33
+ "a",
34
+ "button",
35
+ "label",
36
+ ];
37
+
38
+ /**
39
+ * Structural checks shared by both regular and static-array inline editing:
40
+ * allowed tag, non-empty text, no media children, leaf node.
41
+ */
42
+ function passesStructuralChecks(element: HTMLElement): boolean {
43
+ if (!ALLOWED_INLINE_EDIT_TAGS.includes(element.tagName.toLowerCase())) {
44
+ return false;
45
+ }
46
+
47
+ const textContent = element.textContent?.trim() || "";
48
+ if (textContent.length === 0) {
49
+ return false;
50
+ }
51
+
52
+ if (element.querySelector("img, video, canvas, svg") !== null) {
53
+ return false;
54
+ }
55
+
56
+ if (element.children.length > 0) {
57
+ return false;
58
+ }
59
+
60
+ return true;
61
+ }
62
+
63
+ /**
64
+ * Check if an element is an editable text element
65
+ * Based on the eligibility rules from inlineEdit.md (non-dynamic only),
66
+ * with an exception for static array elements.
67
+ */
68
+ export function isEditableTextElement(element: Element): boolean {
69
+ if (!(element instanceof HTMLElement)) {
70
+ return false;
71
+ }
72
+
73
+ if (!passesStructuralChecks(element)) {
74
+ return false;
75
+ }
76
+
77
+ // Static array elements are editable even though they are technically "dynamic"
78
+ if (isStaticArrayTextElement(element)) {
79
+ return true;
80
+ }
81
+
82
+ if (element.dataset.dynamicContent === "true") {
83
+ return false;
84
+ }
85
+
86
+ return true;
87
+ }
@@ -0,0 +1,9 @@
1
+ export const INLINE_EDIT_DEBOUNCE_MS = 500;
2
+ export const OVERLAY_TRANSITION = "all 0.1s ease-in-out";
3
+ export const OVERLAY_Z_INDEX = "9999";
4
+ export const SELECTED_BORDER_COLOR = "#2563EB";
5
+ export const HOVER_BORDER_COLOR = "#95a5fc";
6
+ export const HOVER_BACKGROUND_COLOR = "rgba(99, 102, 241, 0.05)";
7
+ export const LABEL_TOP_OFFSET = "-27px";
8
+ export const LABEL_LEFT_OFFSET = "-2px";
9
+ export const REPOSITION_DELAY_MS = 50;
@@ -0,0 +1,146 @@
1
+ import type { AgentState } from "../state/agent-state.js";
2
+ import { OutgoingMessageType } from "./messages/types.js";
3
+ import { findElementsById } from "../../utils.js";
4
+ import { createOverlay, positionOverlay, clearSelection } from "../ui/overlay.js";
5
+ import { clearHoverOverlays } from "./hover-handlers.js";
6
+ import { shouldEnterInlineEditingMode } from "../capabilities/inline-editing/index.js";
7
+ import { handleClearInlineEditingMode, handleEnterInlineEditingMode } from "./inline-edit-handlers.js";
8
+ import { getElementPosition, getElementClasses } from "../utils/dom-utils.js";
9
+
10
+ /**
11
+ * Handle element selection (first click)
12
+ */
13
+ export function handleElementSelection(
14
+ state: AgentState,
15
+ element: Element,
16
+ visualSelectorId: string | null
17
+ ): void {
18
+ if (state.currentEditingElement) {
19
+ handleClearInlineEditingMode(state);
20
+ }
21
+ clearSelection(state);
22
+
23
+ // Find all elements with the same ID
24
+ const elements = findElementsById(visualSelectorId);
25
+
26
+ if (state.isInlineEditExperimentEnabled) {
27
+ elements.forEach((el) => {
28
+ if (el instanceof HTMLElement) {
29
+ el.dataset.selected = "true";
30
+ }
31
+ });
32
+ }
33
+
34
+ // Create selected overlays for all matching elements
35
+ elements.forEach((el) => {
36
+ const overlay = createOverlay(true);
37
+ document.body.appendChild(overlay);
38
+ state.selectedOverlays.push(overlay);
39
+ positionOverlay(overlay, el, true, state.isVisualEditMode);
40
+ });
41
+
42
+ state.selectedElementId = visualSelectorId;
43
+
44
+ // Clear hover overlays
45
+ clearHoverOverlays(state);
46
+
47
+ // Send message to parent window with element info
48
+ const htmlElement = element as HTMLElement;
49
+
50
+ // Resolve static-array context from vite-plugin attributes (data-arr-*)
51
+ const arrEl = htmlElement.closest("[data-arr-variable-name]") as HTMLElement | null;
52
+ const staticArrayName = arrEl?.dataset?.arrVariableName || null;
53
+ const rawIdx = arrEl?.dataset?.arrIndex;
54
+ const staticArrayIndex = rawIdx != null ? parseInt(rawIdx, 10) : null;
55
+ const staticArrayField = htmlElement.dataset?.arrField || null;
56
+
57
+ const elementData = {
58
+ type: OutgoingMessageType.ELEMENT_SELECTED,
59
+ tagName: element.tagName,
60
+ classes: getElementClasses(element),
61
+ visualSelectorId: visualSelectorId,
62
+ content: htmlElement.innerText,
63
+ dataSourceLocation: htmlElement.dataset.sourceLocation,
64
+ isDynamicContent: htmlElement.dataset.dynamicContent === "true",
65
+ linenumber: htmlElement.dataset.linenumber,
66
+ filename: htmlElement.dataset.filename,
67
+ position: getElementPosition(element.getBoundingClientRect()),
68
+ staticArrayName,
69
+ staticArrayIndex,
70
+ staticArrayField,
71
+ };
72
+ window.parent.postMessage(elementData, "*");
73
+ }
74
+
75
+ /**
76
+ * Handle element click
77
+ */
78
+ export function handleElementClick(state: AgentState, e: MouseEvent): void {
79
+ if (!state.isVisualEditMode) return;
80
+
81
+ const target = e.target as Element;
82
+
83
+ // Bail out early if clicking on a contentEditable element (allow normal editing)
84
+ if (state.isInlineEditExperimentEnabled && target instanceof HTMLElement && target.contentEditable === "true") {
85
+ return;
86
+ }
87
+
88
+ // If currently editing, clicking outside should exit editing mode
89
+ if (state.currentEditingElement) {
90
+ e.preventDefault();
91
+ e.stopPropagation();
92
+ e.stopImmediatePropagation();
93
+ handleClearInlineEditingMode(state);
94
+ return;
95
+ }
96
+
97
+ // Close dropdowns when clicking anywhere in iframe if a dropdown is open
98
+ if (state.isDropdownOpen) {
99
+ e.preventDefault();
100
+ e.stopPropagation();
101
+ e.stopImmediatePropagation();
102
+
103
+ window.parent.postMessage({ type: OutgoingMessageType.CLOSE_DROPDOWNS }, "*");
104
+ return;
105
+ }
106
+
107
+ // Prevent clicking on SVG path elements
108
+ if (target.tagName.toLowerCase() === "path") {
109
+ return;
110
+ }
111
+
112
+ // Prevent default behavior immediately when in visual edit mode
113
+ e.preventDefault();
114
+ e.stopPropagation();
115
+ e.stopImmediatePropagation();
116
+
117
+ // Support both data-source-location and data-visual-selector-id
118
+ const element = target.closest(
119
+ "[data-source-location], [data-visual-selector-id]"
120
+ );
121
+ if (!element) {
122
+ return;
123
+ }
124
+
125
+ const htmlElement = element as HTMLElement;
126
+ const visualSelectorId =
127
+ htmlElement.dataset.sourceLocation ||
128
+ htmlElement.dataset.visualSelectorId;
129
+
130
+ // Check if this element is already selected (second click scenario)
131
+ const isAlreadySelected =
132
+ state.selectedElementId === visualSelectorId &&
133
+ htmlElement.dataset.selected === "true";
134
+
135
+ if (isAlreadySelected) {
136
+ // Second click on already-selected element: check if it should enter inline editing
137
+ // Only if the experiment is enabled
138
+ if (state.isInlineEditExperimentEnabled && shouldEnterInlineEditingMode(htmlElement)) {
139
+ handleEnterInlineEditingMode(state, htmlElement);
140
+ return;
141
+ }
142
+ }
143
+
144
+ // First click: select the element
145
+ handleElementSelection(state, element, visualSelectorId || null);
146
+ }