@base44/vite-plugin 0.2.24 → 0.2.26

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 (38) hide show
  1. package/dist/injections/layer-dropdown/consts.d.ts +19 -0
  2. package/dist/injections/layer-dropdown/consts.d.ts.map +1 -0
  3. package/dist/injections/layer-dropdown/consts.js +40 -0
  4. package/dist/injections/layer-dropdown/consts.js.map +1 -0
  5. package/dist/injections/layer-dropdown/controller.d.ts +4 -0
  6. package/dist/injections/layer-dropdown/controller.d.ts.map +1 -0
  7. package/dist/injections/layer-dropdown/controller.js +88 -0
  8. package/dist/injections/layer-dropdown/controller.js.map +1 -0
  9. package/dist/injections/layer-dropdown/dropdown-ui.d.ts +13 -0
  10. package/dist/injections/layer-dropdown/dropdown-ui.d.ts.map +1 -0
  11. package/dist/injections/layer-dropdown/dropdown-ui.js +176 -0
  12. package/dist/injections/layer-dropdown/dropdown-ui.js.map +1 -0
  13. package/dist/injections/layer-dropdown/types.d.ts +26 -0
  14. package/dist/injections/layer-dropdown/types.d.ts.map +1 -0
  15. package/dist/injections/layer-dropdown/types.js +3 -0
  16. package/dist/injections/layer-dropdown/types.js.map +1 -0
  17. package/dist/injections/layer-dropdown/utils.d.ts +25 -0
  18. package/dist/injections/layer-dropdown/utils.d.ts.map +1 -0
  19. package/dist/injections/layer-dropdown/utils.js +143 -0
  20. package/dist/injections/layer-dropdown/utils.js.map +1 -0
  21. package/dist/injections/utils.d.ts +9 -0
  22. package/dist/injections/utils.d.ts.map +1 -1
  23. package/dist/injections/utils.js +33 -0
  24. package/dist/injections/utils.js.map +1 -1
  25. package/dist/injections/visual-edit-agent.d.ts.map +1 -1
  26. package/dist/injections/visual-edit-agent.js +115 -69
  27. package/dist/injections/visual-edit-agent.js.map +1 -1
  28. package/dist/statics/index.mjs +1 -1
  29. package/dist/statics/index.mjs.map +1 -1
  30. package/package.json +1 -1
  31. package/src/injections/layer-dropdown/LAYERS.md +258 -0
  32. package/src/injections/layer-dropdown/consts.ts +49 -0
  33. package/src/injections/layer-dropdown/controller.ts +109 -0
  34. package/src/injections/layer-dropdown/dropdown-ui.ts +230 -0
  35. package/src/injections/layer-dropdown/types.ts +30 -0
  36. package/src/injections/layer-dropdown/utils.ts +175 -0
  37. package/src/injections/utils.ts +44 -1
  38. package/src/injections/visual-edit-agent.ts +141 -80
@@ -0,0 +1,230 @@
1
+ /** Dropdown UI component for layer navigation */
2
+
3
+ import {
4
+ DROPDOWN_CONTAINER_STYLES,
5
+ DROPDOWN_ITEM_BASE_STYLES,
6
+ DROPDOWN_ITEM_ACTIVE_COLOR,
7
+ DROPDOWN_ITEM_ACTIVE_BG,
8
+ DROPDOWN_ITEM_ACTIVE_FONT_WEIGHT,
9
+ DROPDOWN_ITEM_HOVER_BG,
10
+ DEPTH_INDENT_PX,
11
+ BASE_PADDING_PX,
12
+ CHEVRON_COLLAPSED,
13
+ CHEVRON_EXPANDED,
14
+ LAYER_DROPDOWN_ATTR,
15
+ } from "./consts.js";
16
+ import { applyStyles, getLayerDisplayName } from "./utils.js";
17
+ import type { LayerInfo, DropdownCallbacks } from "./types.js";
18
+
19
+ let activeDropdown: HTMLDivElement | null = null;
20
+ let activeLabel: HTMLDivElement | null = null;
21
+ let outsideMousedownHandler: ((e: MouseEvent) => void) | null = null;
22
+ let activeOnHoverEnd: (() => void) | null = null;
23
+ let activeKeydownHandler: ((e: KeyboardEvent) => void) | null = null;
24
+
25
+ function createDropdownItem(
26
+ layer: LayerInfo,
27
+ isActive: boolean,
28
+ { onSelect, onHover, onHoverEnd }: DropdownCallbacks
29
+ ): HTMLDivElement {
30
+ const item = document.createElement("div");
31
+ item.textContent = getLayerDisplayName(layer);
32
+ applyStyles(item, DROPDOWN_ITEM_BASE_STYLES);
33
+
34
+ const depth = layer.depth ?? 0;
35
+ if (depth > 0) {
36
+ item.style.paddingLeft = `${BASE_PADDING_PX + depth * DEPTH_INDENT_PX}px`;
37
+ }
38
+
39
+ if (isActive) {
40
+ item.style.color = DROPDOWN_ITEM_ACTIVE_COLOR;
41
+ item.style.backgroundColor = DROPDOWN_ITEM_ACTIVE_BG;
42
+ item.style.fontWeight = DROPDOWN_ITEM_ACTIVE_FONT_WEIGHT;
43
+ }
44
+
45
+ item.addEventListener("mouseenter", () => {
46
+ if (!isActive) item.style.backgroundColor = DROPDOWN_ITEM_HOVER_BG;
47
+ if (onHover) onHover(layer);
48
+ });
49
+
50
+ item.addEventListener("mouseleave", () => {
51
+ if (!isActive) item.style.backgroundColor = "transparent";
52
+ if (onHoverEnd) onHoverEnd();
53
+ });
54
+
55
+ item.addEventListener("click", (e: MouseEvent) => {
56
+ e.stopPropagation();
57
+ e.preventDefault();
58
+ onSelect(layer);
59
+ });
60
+
61
+ return item;
62
+ }
63
+
64
+ /** Create the dropdown DOM element with layer items */
65
+ export function createDropdownElement(
66
+ layers: LayerInfo[],
67
+ currentElement: Element | null,
68
+ callbacks: DropdownCallbacks
69
+ ): HTMLDivElement {
70
+ const container = document.createElement("div");
71
+ container.setAttribute(LAYER_DROPDOWN_ATTR, "true");
72
+ applyStyles(container, DROPDOWN_CONTAINER_STYLES);
73
+
74
+ layers.forEach((layer) => {
75
+ const isActive = layer.element === currentElement;
76
+ container.appendChild(createDropdownItem(layer, isActive, callbacks));
77
+ });
78
+
79
+ return container;
80
+ }
81
+
82
+ /** Add chevron indicator and pointer-events to the label */
83
+ export function enhanceLabelWithChevron(label: HTMLDivElement): void {
84
+ const t = label.textContent ?? "";
85
+ if (t.endsWith(CHEVRON_COLLAPSED) || t.endsWith(CHEVRON_EXPANDED)) return;
86
+
87
+ label.textContent = t + CHEVRON_COLLAPSED;
88
+ label.style.cursor = "pointer";
89
+ label.style.userSelect = "none";
90
+ label.style.whiteSpace = "nowrap";
91
+ label.style.pointerEvents = "auto";
92
+ label.setAttribute(LAYER_DROPDOWN_ATTR, "true");
93
+ }
94
+
95
+ function setupKeyboardNavigation(
96
+ dropdown: HTMLDivElement,
97
+ layers: LayerInfo[],
98
+ currentElement: Element | null,
99
+ { onSelect, onHover, onHoverEnd }: DropdownCallbacks
100
+ ): void {
101
+ const items = Array.from(dropdown.children) as HTMLDivElement[];
102
+ let focusedIndex = layers.findIndex((l) => l.element === currentElement);
103
+
104
+ const setFocusedItem = (index: number) => {
105
+ if (focusedIndex >= 0 && focusedIndex < items.length) {
106
+ const prev = items[focusedIndex]!;
107
+ if (prev.style.color !== DROPDOWN_ITEM_ACTIVE_COLOR) {
108
+ prev.style.backgroundColor = "transparent";
109
+ }
110
+ }
111
+ focusedIndex = index;
112
+ if (focusedIndex >= 0 && focusedIndex < items.length) {
113
+ const cur = items[focusedIndex]!;
114
+ if (cur.style.color !== DROPDOWN_ITEM_ACTIVE_COLOR) {
115
+ cur.style.backgroundColor = DROPDOWN_ITEM_HOVER_BG;
116
+ }
117
+ cur.scrollIntoView({ block: "nearest" });
118
+ if (onHover && focusedIndex >= 0 && focusedIndex < layers.length) {
119
+ onHover(layers[focusedIndex]!);
120
+ }
121
+ }
122
+ };
123
+
124
+ activeKeydownHandler = (e: KeyboardEvent) => {
125
+ if (e.key === "ArrowDown") {
126
+ e.preventDefault();
127
+ e.stopPropagation();
128
+ setFocusedItem(focusedIndex < items.length - 1 ? focusedIndex + 1 : 0);
129
+ } else if (e.key === "ArrowUp") {
130
+ e.preventDefault();
131
+ e.stopPropagation();
132
+ setFocusedItem(focusedIndex > 0 ? focusedIndex - 1 : items.length - 1);
133
+ } else if (e.key === "Enter" && focusedIndex >= 0 && focusedIndex < layers.length) {
134
+ e.preventDefault();
135
+ e.stopPropagation();
136
+ if (onHoverEnd) onHoverEnd();
137
+ onSelect(layers[focusedIndex]!);
138
+ closeDropdown();
139
+ }
140
+ };
141
+ document.addEventListener("keydown", activeKeydownHandler, true);
142
+ }
143
+
144
+ function setupOutsideClickHandler(
145
+ dropdown: HTMLDivElement,
146
+ label: HTMLDivElement
147
+ ): void {
148
+ let skipFirst = true;
149
+ outsideMousedownHandler = (e: MouseEvent) => {
150
+ if (skipFirst) { skipFirst = false; return; }
151
+ const target = e.target as Node;
152
+ if (!dropdown.contains(target) && target !== label) {
153
+ closeDropdown();
154
+ }
155
+ };
156
+ document.addEventListener("mousedown", outsideMousedownHandler, true);
157
+ }
158
+
159
+ /** Show the dropdown below the label element */
160
+ export function showDropdown(
161
+ label: HTMLDivElement,
162
+ layers: LayerInfo[],
163
+ currentElement: Element | null,
164
+ callbacks: DropdownCallbacks
165
+ ): void {
166
+ closeDropdown();
167
+
168
+ const dropdown = createDropdownElement(
169
+ layers,
170
+ currentElement,
171
+ {
172
+ ...callbacks,
173
+ onSelect: (layer) => {
174
+ if (callbacks.onHoverEnd) callbacks.onHoverEnd();
175
+ callbacks.onSelect(layer);
176
+ closeDropdown();
177
+ },
178
+ }
179
+ );
180
+
181
+ const overlay = label.parentElement;
182
+ if (!overlay) return;
183
+
184
+ dropdown.style.top = `${label.offsetTop + label.offsetHeight + 2}px`;
185
+ dropdown.style.left = `${label.offsetLeft}px`;
186
+
187
+ overlay.appendChild(dropdown);
188
+ activeDropdown = dropdown;
189
+ activeLabel = label;
190
+ if (label.textContent?.endsWith(CHEVRON_COLLAPSED.trim())) {
191
+ label.textContent = label.textContent.slice(0, -CHEVRON_COLLAPSED.length) + CHEVRON_EXPANDED;
192
+ }
193
+ activeOnHoverEnd = callbacks.onHoverEnd ?? null;
194
+
195
+ setupKeyboardNavigation(dropdown, layers, currentElement, callbacks);
196
+ setupOutsideClickHandler(dropdown, label);
197
+ }
198
+
199
+ /** Close the active dropdown and clean up listeners */
200
+ export function closeDropdown(): void {
201
+ if (activeLabel?.textContent?.includes(CHEVRON_EXPANDED)) {
202
+ activeLabel.textContent = activeLabel.textContent.replace(CHEVRON_EXPANDED, CHEVRON_COLLAPSED);
203
+ }
204
+ activeLabel = null;
205
+
206
+ if (activeOnHoverEnd) {
207
+ activeOnHoverEnd();
208
+ activeOnHoverEnd = null;
209
+ }
210
+
211
+ if (activeDropdown && activeDropdown.parentNode) {
212
+ activeDropdown.remove();
213
+ }
214
+ activeDropdown = null;
215
+
216
+ if (outsideMousedownHandler) {
217
+ document.removeEventListener("mousedown", outsideMousedownHandler, true);
218
+ outsideMousedownHandler = null;
219
+ }
220
+
221
+ if (activeKeydownHandler) {
222
+ document.removeEventListener("keydown", activeKeydownHandler, true);
223
+ activeKeydownHandler = null;
224
+ }
225
+ }
226
+
227
+ /** Check if a dropdown is currently visible */
228
+ export function isDropdownOpen(): boolean {
229
+ return activeDropdown !== null;
230
+ }
@@ -0,0 +1,30 @@
1
+ /** Shared types for the layer-dropdown module */
2
+
3
+ export interface LayerInfo {
4
+ element: Element;
5
+ tagName: string;
6
+ selectorId: string | null;
7
+ depth?: number;
8
+ }
9
+
10
+ export type OnLayerSelect = (layer: LayerInfo) => void;
11
+ export type OnLayerHover = (layer: LayerInfo) => void;
12
+ export type OnLayerHoverEnd = () => void;
13
+
14
+ export interface DropdownCallbacks {
15
+ onSelect: OnLayerSelect;
16
+ onHover?: OnLayerHover;
17
+ onHoverEnd?: OnLayerHoverEnd;
18
+ }
19
+
20
+ export interface LayerControllerConfig {
21
+ createPreviewOverlay: (element: Element) => HTMLDivElement;
22
+ getSelectedElementId: () => string | null;
23
+ selectElement: (element: Element) => HTMLDivElement | undefined;
24
+ onDeselect: () => void;
25
+ }
26
+
27
+ export interface LayerController {
28
+ attachToOverlay: (overlay: HTMLDivElement | undefined, element: Element) => void;
29
+ cleanup: () => void;
30
+ }
@@ -0,0 +1,175 @@
1
+ /** DOM utilities for the layer-dropdown module */
2
+
3
+ import { isInstrumentedElement, getElementSelectorId } from "../utils.js";
4
+ import { MAX_PARENT_DEPTH, MAX_CHILD_DEPTH } from "./consts.js";
5
+
6
+ import type { LayerInfo } from "./types.js";
7
+
8
+ /** Apply a style map to an element */
9
+ export function applyStyles(element: HTMLElement, styles: Record<string, string>): void {
10
+ for (const key of Object.keys(styles)) {
11
+ element.style.setProperty(
12
+ key.replace(/[A-Z]/g, (m) => `-${m.toLowerCase()}`),
13
+ styles[key]!
14
+ );
15
+ }
16
+ }
17
+
18
+ /** Display name for a layer — just the real tag name */
19
+ export function getLayerDisplayName(layer: LayerInfo): string {
20
+ return layer.tagName;
21
+ }
22
+
23
+ function toLayerInfo(element: Element, depth?: number): LayerInfo {
24
+ const info: LayerInfo = {
25
+ element,
26
+ tagName: element.tagName.toLowerCase(),
27
+ selectorId: getElementSelectorId(element),
28
+ };
29
+ if (depth !== undefined) info.depth = depth;
30
+ return info;
31
+ }
32
+
33
+ /**
34
+ * Collect instrumented descendants up to `maxDepth` instrumented nesting levels.
35
+ * Non-instrumented wrappers are walked through without counting toward depth.
36
+ * Results are in DOM order.
37
+ * When `startDepth` is provided, assigns `depth` to each item during collection.
38
+ */
39
+ export function getInstrumentedDescendants(
40
+ parent: Element,
41
+ maxDepth: number,
42
+ startDepth?: number
43
+ ): LayerInfo[] {
44
+ const result: LayerInfo[] = [];
45
+
46
+ function walk(el: Element, instrDepth: number): void {
47
+ if (instrDepth > maxDepth) return;
48
+ for (let i = 0; i < el.children.length; i++) {
49
+ const child = el.children[i]!;
50
+ if (isInstrumentedElement(child)) {
51
+ const info: LayerInfo = {
52
+ element: child,
53
+ tagName: child.tagName.toLowerCase(),
54
+ selectorId: getElementSelectorId(child),
55
+ };
56
+ if (startDepth !== undefined) {
57
+ info.depth = startDepth + instrDepth - 1;
58
+ }
59
+ result.push(info);
60
+ walk(child, instrDepth + 1);
61
+ } else {
62
+ walk(child, instrDepth);
63
+ }
64
+ }
65
+ }
66
+
67
+ walk(parent, 1);
68
+ return result;
69
+ }
70
+
71
+ /** Collect instrumented ancestors from selected element up to MAX_PARENT_DEPTH (outermost first). */
72
+ function collectInstrumentedParents(selectedElement: Element): LayerInfo[] {
73
+ const parents: LayerInfo[] = [];
74
+ let current = selectedElement.parentElement;
75
+ while (
76
+ current &&
77
+ current !== document.documentElement &&
78
+ current !== document.body &&
79
+ parents.length < MAX_PARENT_DEPTH
80
+ ) {
81
+ if (isInstrumentedElement(current)) {
82
+ parents.push(toLayerInfo(current));
83
+ }
84
+ current = current.parentElement;
85
+ }
86
+ parents.reverse();
87
+ return parents;
88
+ }
89
+
90
+ /** Add parents to chain with depth 0, 1, …; returns depth of selected (parents.length). */
91
+ function addParentsToChain(chain: LayerInfo[], parents: LayerInfo[]): number {
92
+ parents.forEach((p, i) => {
93
+ chain.push({ ...p, depth: i });
94
+ });
95
+ return parents.length;
96
+ }
97
+
98
+ /** Add selected element and its descendants at the given depth. */
99
+ function addSelfAndDescendantsToChain(
100
+ chain: LayerInfo[],
101
+ selectedElement: Element,
102
+ selfDepth: number
103
+ ): void {
104
+ chain.push(toLayerInfo(selectedElement, selfDepth));
105
+ const descendants = getInstrumentedDescendants(
106
+ selectedElement,
107
+ MAX_CHILD_DEPTH,
108
+ selfDepth + 1
109
+ );
110
+ chain.push(...descendants);
111
+ }
112
+
113
+ /** Get the innermost instrumented parent's DOM element, or null if none. */
114
+ function getImmediateInstrParent(parents: LayerInfo[]): Element | null {
115
+ return parents.at(-1)?.element ?? null;
116
+ }
117
+
118
+ /** Collect instrumented siblings of the selected element from its parent (DOM order). */
119
+ function collectSiblings(parent: Element, selectedElement: Element): LayerInfo[] {
120
+ const siblings = getInstrumentedDescendants(parent, 1);
121
+ if (!siblings.some((s) => s.element === selectedElement)) {
122
+ siblings.push(toLayerInfo(selectedElement));
123
+ }
124
+ return siblings;
125
+ }
126
+
127
+ /** Add siblings at selfDepth, expanding children only for the selected element. */
128
+ function appendSiblingsWithSelected(
129
+ chain: LayerInfo[],
130
+ siblings: LayerInfo[],
131
+ selectedElement: Element,
132
+ selfDepth: number
133
+ ): void {
134
+ const selectedSelectorId = getElementSelectorId(selectedElement);
135
+ const seen = new Set<string>();
136
+ for (const sibling of siblings) {
137
+ if (sibling.element === selectedElement) {
138
+ addSelfAndDescendantsToChain(chain, selectedElement, selfDepth);
139
+ if (selectedSelectorId) seen.add(selectedSelectorId);
140
+ } else {
141
+ const id = sibling.selectorId;
142
+ if (id != null) {
143
+ if (id === selectedSelectorId || seen.has(id)) continue;
144
+ seen.add(id);
145
+ }
146
+ chain.push({ ...sibling, depth: selfDepth });
147
+ }
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Build the layer chain for the dropdown:
153
+ *
154
+ * Parents – up to MAX_PARENT_DEPTH instrumented ancestors, outer → inner.
155
+ * Siblings – instrumented children of the immediate parent, at the same depth.
156
+ * Current – the selected element (highlighted), with children expanded.
157
+ * Children – instrumented descendants within MAX_CHILD_DEPTH levels, DOM order.
158
+ *
159
+ * Each item carries a `depth` for visual indentation.
160
+ */
161
+ export function buildLayerChain(selectedElement: Element): LayerInfo[] {
162
+ const parents = collectInstrumentedParents(selectedElement);
163
+ const chain: LayerInfo[] = [];
164
+ const selfDepth = addParentsToChain(chain, parents);
165
+
166
+ const instrParent = getImmediateInstrParent(parents);
167
+ if (instrParent) {
168
+ const siblings = collectSiblings(instrParent, selectedElement);
169
+ appendSiblingsWithSelected(chain, siblings, selectedElement, selfDepth);
170
+ } else {
171
+ addSelfAndDescendantsToChain(chain, selectedElement, selfDepth);
172
+ }
173
+
174
+ return chain;
175
+ }
@@ -1,3 +1,23 @@
1
+ /** Check if an element has instrumentation attributes */
2
+ export function isInstrumentedElement(element: Element): boolean {
3
+ const htmlEl = element as HTMLElement;
4
+ return !!(
5
+ htmlEl.dataset?.sourceLocation || htmlEl.dataset?.visualSelectorId
6
+ );
7
+ }
8
+
9
+ /** Get the selector ID from an element's data attributes (prefers source-location) */
10
+ export function getElementSelectorId(element: Element): string | null {
11
+ const htmlEl = element as HTMLElement;
12
+ return (
13
+ htmlEl.dataset?.sourceLocation ||
14
+ htmlEl.dataset?.visualSelectorId ||
15
+ null
16
+ );
17
+ }
18
+
19
+ export const ALLOWED_ATTRIBUTES: string[] = ["src"];
20
+
1
21
  /** Find elements by ID - first try data-source-location, fallback to data-visual-selector-id */
2
22
  export function findElementsById(id: string | null): Element[] {
3
23
  if (!id) return [];
@@ -19,5 +39,28 @@ export function findElementsById(id: string | null): Element[] {
19
39
  export function updateElementClasses(elements: Element[], classes: string): void {
20
40
  elements.forEach((element) => {
21
41
  element.setAttribute("class", classes);
22
- });
42
+ });
43
+ }
44
+
45
+ /** Set a single attribute on all provided elements. */
46
+ export function updateElementAttribute(elements: Element[], attribute: string, value: string): void {
47
+ if (!ALLOWED_ATTRIBUTES.includes(attribute)) {
48
+ return;
49
+ }
50
+
51
+ elements.forEach((element) => {
52
+ element.setAttribute(attribute, value);
53
+ });
54
+ }
55
+
56
+ /** Collect attribute values from an element for a given allowlist. */
57
+ export function collectAllowedAttributes(element: Element, allowedAttributes: string[]): Record<string, string> {
58
+ const attributes: Record<string, string> = {};
59
+ for (const attr of allowedAttributes) {
60
+ const val = element.getAttribute(attr);
61
+ if (val !== null) {
62
+ attributes[attr] = val;
63
+ }
64
+ }
65
+ return attributes;
23
66
  }