@base44/vite-plugin 1.0.1 → 1.0.2

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.
@@ -33,10 +33,12 @@ export const DROPDOWN_ITEM_HOVER_BG = "#f1f5f9";
33
33
 
34
34
  export const DEPTH_INDENT_PX = 10;
35
35
 
36
- /** Chevron shown when dropdown is collapsed (click to expand) */
37
- export const CHEVRON_COLLAPSED = " \u25BE";
38
- /** Chevron shown when dropdown is expanded (click to collapse) */
39
- export const CHEVRON_EXPANDED = " \u25B4";
36
+ /** SVG chevron shown when dropdown is collapsed (click to expand) */
37
+ export const CHEVRON_COLLAPSED = `<svg width="12" height="12" viewBox="0 0 24 24" style="vertical-align:middle;margin-left:4px"><path d="M6 9l6 6 6-6" stroke="currentColor" stroke-width="2" fill="none"/></svg>`;
38
+ /** SVG chevron shown when dropdown is expanded (click to collapse) */
39
+ export const CHEVRON_EXPANDED = `<svg width="12" height="12" viewBox="0 0 24 24" style="vertical-align:middle;margin-left:4px"><path d="M18 15l-6-6-6 6" stroke="currentColor" stroke-width="2" fill="none"/></svg>`;
40
+
41
+ export const CHEVRON_ATTR = "data-chevron";
40
42
 
41
43
  export const BASE_PADDING_PX = 12;
42
44
 
@@ -11,6 +11,7 @@ import {
11
11
  BASE_PADDING_PX,
12
12
  CHEVRON_COLLAPSED,
13
13
  CHEVRON_EXPANDED,
14
+ CHEVRON_ATTR,
14
15
  LAYER_DROPDOWN_ATTR,
15
16
  } from "./consts.js";
16
17
  import { applyStyles, getLayerDisplayName } from "./utils.js";
@@ -83,10 +84,16 @@ export function createDropdownElement(
83
84
 
84
85
  /** Add chevron indicator and pointer-events to the label */
85
86
  export function enhanceLabelWithChevron(label: HTMLDivElement): void {
86
- const t = label.textContent ?? "";
87
- if (t.endsWith(CHEVRON_COLLAPSED) || t.endsWith(CHEVRON_EXPANDED)) return;
87
+ if (label.querySelector(`[${CHEVRON_ATTR}]`)) return;
88
88
 
89
- label.textContent = t + CHEVRON_COLLAPSED;
89
+ const chevron = document.createElement("span");
90
+ chevron.setAttribute(CHEVRON_ATTR, "true");
91
+ chevron.style.display = "inline-flex";
92
+ chevron.innerHTML = CHEVRON_COLLAPSED;
93
+ label.appendChild(chevron);
94
+
95
+ label.style.display = "inline-flex";
96
+ label.style.alignItems = "center";
90
97
  label.style.cursor = "pointer";
91
98
  label.style.userSelect = "none";
92
99
  label.style.whiteSpace = "nowrap";
@@ -190,8 +197,9 @@ export function showDropdown(
190
197
  overlay.appendChild(dropdown);
191
198
  activeDropdown = dropdown;
192
199
  activeLabel = label;
193
- if (label.textContent?.endsWith(CHEVRON_COLLAPSED.trim())) {
194
- label.textContent = label.textContent.slice(0, -CHEVRON_COLLAPSED.length) + CHEVRON_EXPANDED;
200
+ const chevronEl = label.querySelector(`[${CHEVRON_ATTR}]`);
201
+ if (chevronEl) {
202
+ chevronEl.innerHTML = CHEVRON_EXPANDED;
195
203
  }
196
204
  activeOnHoverEnd = callbacks.onHoverEnd ?? null;
197
205
 
@@ -201,8 +209,9 @@ export function showDropdown(
201
209
 
202
210
  /** Close the active dropdown and clean up listeners */
203
211
  export function closeDropdown(): void {
204
- if (activeLabel?.textContent?.includes(CHEVRON_EXPANDED)) {
205
- activeLabel.textContent = activeLabel.textContent.replace(CHEVRON_EXPANDED, CHEVRON_COLLAPSED);
212
+ const chevronEl = activeLabel?.querySelector(`[${CHEVRON_ATTR}]`);
213
+ if (chevronEl) {
214
+ chevronEl.innerHTML = CHEVRON_COLLAPSED;
206
215
  }
207
216
  activeLabel = null;
208
217
 
@@ -1,3 +1,40 @@
1
+ const LABEL_HEIGHT = 27;
2
+
3
+ /**
4
+ * Positions an element-tag label relative to its highlighted overlay.
5
+ *
6
+ * Strategy (in priority order):
7
+ * 1. If the element is near the viewport top AND tall enough (>= 2x label height),
8
+ * place the label **inside** the element at the top-left.
9
+ * 2. If the element is near the viewport top but too short, place the label
10
+ * **below** the element.
11
+ * 3. Otherwise, place the label **above** the element (default).
12
+ *
13
+ * For full-width elements (spanning nearly the entire viewport), the left offset
14
+ * is nudged inward (6px) to prevent clipping against the viewport edge.
15
+ *
16
+ * @param label - The label div to position (style.top and style.left are set).
17
+ * @param rect - The bounding client rect of the highlighted element.
18
+ */
19
+ export function positionLabel(label: HTMLDivElement, rect: DOMRect): void {
20
+ const nearTop = rect.top < LABEL_HEIGHT;
21
+ const tallEnough = rect.height >= LABEL_HEIGHT * 2;
22
+ const isFullWidth = rect.width >= window.innerWidth - 4;
23
+ const edgeLeft = isFullWidth ? "8px" : "-2px";
24
+ const insideLeft = isFullWidth ? "8px" : "4px";
25
+
26
+ if (nearTop && tallEnough) {
27
+ label.style.top = "2px";
28
+ label.style.left = insideLeft;
29
+ } else if (nearTop) {
30
+ label.style.top = `${rect.height + 2}px`;
31
+ label.style.left = edgeLeft;
32
+ } else {
33
+ label.style.top = `-${LABEL_HEIGHT}px`;
34
+ label.style.left = edgeLeft;
35
+ }
36
+ }
37
+
1
38
  /** Check if an element has instrumentation attributes */
2
39
  export function isInstrumentedElement(element: Element): boolean {
3
40
  const htmlEl = element as HTMLElement;
@@ -1,8 +1,10 @@
1
- import { findElementsById, updateElementClasses, updateElementAttribute, collectAllowedAttributes, ALLOWED_ATTRIBUTES, getElementSelectorId, stopAnimations, resumeAnimations, findInstrumentedElement, resolveHoverTarget } from "./utils.js";
1
+ import { findElementsById, updateElementClasses, updateElementAttribute, collectAllowedAttributes, ALLOWED_ATTRIBUTES, getElementSelectorId, stopAnimations, resumeAnimations, findInstrumentedElement, resolveHoverTarget, positionLabel } from "./utils.js";
2
2
  import { createLayerController } from "./layer-dropdown/controller.js";
3
3
  import { LAYER_DROPDOWN_ATTR } from "./layer-dropdown/consts.js";
4
4
  import { createInlineEditController } from "../capabilities/inline-edit/index.js";
5
5
 
6
+ const REPOSITION_DELAY_MS = 50;
7
+
6
8
  export function setupVisualEditAgent() {
7
9
  // State variables (replacing React useState/useRef)
8
10
  let isVisualEditMode = false;
@@ -12,8 +14,7 @@ export function setupVisualEditAgent() {
12
14
  let selectedOverlays: HTMLDivElement[] = [];
13
15
  let currentHighlightedElements: Element[] = [];
14
16
  let selectedElementId: string | null = null;
15
-
16
- const REPOSITION_DELAY_MS = 50;
17
+ let selectedElement: Element | null = null;
17
18
 
18
19
  // Create overlay element
19
20
  const createOverlay = (isSelected = false): HTMLDivElement => {
@@ -58,18 +59,19 @@ export function setupVisualEditAgent() {
58
59
  label = document.createElement("div");
59
60
  label.textContent = element.tagName.toLowerCase();
60
61
  label.style.position = "absolute";
61
- label.style.top = "-27px";
62
62
  label.style.left = "-2px";
63
63
  label.style.padding = "2px 8px";
64
64
  label.style.fontSize = "11px";
65
65
  label.style.fontWeight = isSelected ? "500" : "400";
66
66
  label.style.color = isSelected ? "#ffffff" : "#526cff";
67
- label.style.backgroundColor = isSelected ? "#526cff" : "#DBEAFE";
67
+ label.style.backgroundColor = isSelected ? "#2563EB" : "#DBEAFE";
68
68
  label.style.borderRadius = "3px";
69
69
  label.style.minWidth = "24px";
70
70
  label.style.textAlign = "center";
71
71
  overlay.appendChild(label);
72
72
  }
73
+
74
+ positionLabel(label, rect);
73
75
  };
74
76
 
75
77
  // --- Inline edit controller ---
@@ -82,6 +84,7 @@ export function setupVisualEditAgent() {
82
84
  inlineEdit.clearSelectedMarks(selectedElementId);
83
85
  clearSelectedOverlays();
84
86
  selectedElementId = null;
87
+ selectedElement = null;
85
88
  },
86
89
  createSelectionOverlays: (elements, elementId) => {
87
90
  elements.forEach((el) => {
@@ -98,6 +101,7 @@ export function setupVisualEditAgent() {
98
101
  inlineEdit.clearSelectedMarks(selectedElementId);
99
102
  clearSelectedOverlays();
100
103
  selectedElementId = null;
104
+ selectedElement = null;
101
105
  };
102
106
 
103
107
  // Clear hover overlays
@@ -180,6 +184,7 @@ export function setupVisualEditAgent() {
180
184
  });
181
185
 
182
186
  selectedElementId = visualSelectorId || null;
187
+ selectedElement = element;
183
188
  clearHoverOverlays();
184
189
  notifyElementSelected(element);
185
190
 
@@ -436,10 +441,9 @@ export function setupVisualEditAgent() {
436
441
  // Handle scroll events to update popover position
437
442
  const handleScroll = () => {
438
443
  if (selectedElementId) {
439
- const elements = findElementsById(selectedElementId);
440
- if (elements.length > 0) {
441
- const element = elements[0];
442
- const rect = element!.getBoundingClientRect();
444
+ const element = selectedElement;
445
+ if (element && element.isConnected) {
446
+ const rect = element.getBoundingClientRect();
443
447
 
444
448
  const viewportHeight = window.innerHeight;
445
449
  const viewportWidth = window.innerWidth;
@@ -543,41 +547,37 @@ export function setupVisualEditAgent() {
543
547
  break;
544
548
 
545
549
  case "request-element-position":
546
- if (selectedElementId) {
547
- const elements = findElementsById(selectedElementId);
548
- if (elements.length > 0) {
549
- const element = elements[0];
550
- const rect = element!.getBoundingClientRect();
551
-
552
- const viewportHeight = window.innerHeight;
553
- const viewportWidth = window.innerWidth;
554
- const isInViewport =
555
- rect.top < viewportHeight &&
556
- rect.bottom > 0 &&
557
- rect.left < viewportWidth &&
558
- rect.right > 0;
559
-
560
- const elementPosition = {
561
- top: rect.top,
562
- left: rect.left,
563
- right: rect.right,
564
- bottom: rect.bottom,
565
- width: rect.width,
566
- height: rect.height,
567
- centerX: rect.left + rect.width / 2,
568
- centerY: rect.top + rect.height / 2,
569
- };
570
-
571
- window.parent.postMessage(
572
- {
573
- type: "element-position-update",
574
- position: elementPosition,
575
- isInViewport: isInViewport,
576
- visualSelectorId: selectedElementId,
577
- },
578
- "*"
579
- );
580
- }
550
+ if (selectedElementId && selectedElement && selectedElement.isConnected) {
551
+ const rect = selectedElement.getBoundingClientRect();
552
+
553
+ const viewportHeight = window.innerHeight;
554
+ const viewportWidth = window.innerWidth;
555
+ const isInViewport =
556
+ rect.top < viewportHeight &&
557
+ rect.bottom > 0 &&
558
+ rect.left < viewportWidth &&
559
+ rect.right > 0;
560
+
561
+ const elementPosition = {
562
+ top: rect.top,
563
+ left: rect.left,
564
+ right: rect.right,
565
+ bottom: rect.bottom,
566
+ width: rect.width,
567
+ height: rect.height,
568
+ centerX: rect.left + rect.width / 2,
569
+ centerY: rect.top + rect.height / 2,
570
+ };
571
+
572
+ window.parent.postMessage(
573
+ {
574
+ type: "element-position-update",
575
+ position: elementPosition,
576
+ isInViewport: isInViewport,
577
+ visualSelectorId: selectedElementId,
578
+ },
579
+ "*"
580
+ );
581
581
  }
582
582
  break;
583
583