Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Make select and edit UX better
Flightdeck Agente7cc7c4e940a
diff --git a/frontend/src/components/preview/PreviewComponent.tsx b/frontend/src/components/preview/PreviewComponent.tsxindex 158ca2d1..c2231fe6 100644--- a/frontend/src/components/preview/PreviewComponent.tsx+++ b/frontend/src/components/preview/PreviewComponent.tsx@@ -74,6 +74,16 @@ function PreviewComponent({if (!inSelectAndEditModeRef.current) return;const target = event.target as HTMLElement;if (!target || !target.getBoundingClientRect) return;++ // The locked target owns this space. Avoid stacking the lighter hover+ // treatment over it, which can obscure the stronger selection ring/label.+ const selected = useAppStore.getState().selectedElement;+ if (selected && (target === selected || selected.contains(target))) {+ hoveredElementRef.current = null;+ hideHoverOverlay(target.ownerDocument);+ return;+ }+hoveredElementRef.current = target;showHoverOverlay(target);}, []);@@ -129,6 +139,10 @@ function PreviewComponent({const targetElement = clickEvent.target as HTMLElement;if (!targetElement) return;+ // Replace the transient hover treatment with the visually stronger locked+ // target immediately; hovering other elements remains available afterward.+ hoveredElementRef.current = null;+ hideHoverOverlay(targetElement.ownerDocument);setSelectedElement(targetElement);}, [clickEvent, setSelectedElement]);diff --git a/frontend/src/components/select-and-edit/overlays.test.ts b/frontend/src/components/select-and-edit/overlays.test.tsnew file mode 100644index 00000000..6469b62b--- /dev/null+++ b/frontend/src/components/select-and-edit/overlays.test.ts@@ -0,0 +1,114 @@+import { showHoverOverlay, showSelectionOverlay } from "./overlays";++interface FakeNode {+ id: string;+ style: Record<string, string>;+ textContent: string;+ children: FakeNode[];+ firstChild: FakeNode | null;+ appendChild: (child: FakeNode) => FakeNode;+ remove: () => void;+}++function createFakeDocument() {+ const nodes = new Map<string, FakeNode>();++ const createNode = (): FakeNode => {+ const node: FakeNode = {+ id: "",+ style: {},+ textContent: "",+ children: [],+ firstChild: null,+ appendChild(child) {+ this.children.push(child);+ this.firstChild ??= child;+ if (child.id) nodes.set(child.id, child);+ return child;+ },+ remove() {+ if (this.id) nodes.delete(this.id);+ },+ };+ return node;+ };++ const documentElement = createNode();+ documentElement.appendChild = (child) => {+ documentElement.children.push(child);+ documentElement.firstChild ??= child;+ if (child.id) nodes.set(child.id, child);+ return child;+ };++ const doc = {+ documentElement,+ createElement: () => createNode(),+ getElementById: (id: string) => nodes.get(id) ?? null,+ };++ return { doc: doc as unknown as Document, nodes };+}++function fakeElement(+ doc: Document,+ tagName: string,+ rect: { top: number; left: number; width: number; height: number }+): HTMLElement {+ return {+ id: "",+ ownerDocument: doc,+ tagName: tagName.toUpperCase(),+ getBoundingClientRect: () => rect,+ } as unknown as HTMLElement;+}++describe("select-and-edit overlays", () => {+ it("moves the hover box without any transition or animation", () => {+ const { doc, nodes } = createFakeDocument();+ const first = fakeElement(doc, "div", {+ top: 10,+ left: 20,+ width: 100,+ height: 40,+ });+ const second = fakeElement(doc, "button", {+ top: 220,+ left: 440,+ width: 80,+ height: 32,+ });++ showHoverOverlay(first);+ showHoverOverlay(second);++ const overlay = nodes.get("__s2c-hover-overlay");+ expect(overlay?.style.transition).toBe("none");+ expect(overlay?.style.animation).toBe("none");+ expect(overlay?.style.top).toBe("220px");+ expect(overlay?.style.left).toBe("440px");+ expect(overlay?.style.width).toBe("80px");+ expect(overlay?.style.height).toBe("32px");+ });++ it("keeps the locked target visually distinct from hover", () => {+ const { doc, nodes } = createFakeDocument();+ const target = fakeElement(doc, "button", {+ top: 20,+ left: 40,+ width: 100,+ height: 36,+ });++ showHoverOverlay(target);+ showSelectionOverlay(target);++ const hover = nodes.get("__s2c-hover-overlay");+ const selection = nodes.get("__s2c-selection-overlay");+ expect(selection?.style.border).not.toBe(hover?.style.border);+ expect(selection?.style.background).not.toBe(hover?.style.background);+ expect(selection?.style.top).toBe("17px");+ expect(selection?.style.left).toBe("37px");+ expect(selection?.firstChild?.textContent).toBe("✓ <button>");+ });+});diff --git a/frontend/src/components/select-and-edit/overlays.ts b/frontend/src/components/select-and-edit/overlays.tsindex c63b4683..c9a357fe 100644--- a/frontend/src/components/select-and-edit/overlays.ts+++ b/frontend/src/components/select-and-edit/overlays.ts@@ -60,8 +60,11 @@ function ensureOverlay(doc: Document, kind: OverlayKind): HTMLElement {height: "0",pointerEvents: "none",borderRadius: "4px",- transition:- "top 90ms ease-out, left 90ms ease-out, width 90ms ease-out, height 90ms ease-out",+ // Geometry must snap to the element under the pointer. Explicitly+ // disabling both properties also protects the overlay from broad styles+ // in generated pages such as `* { transition: all 200ms }`.+ transition: "none",+ animation: "none",display: "none",...OVERLAY_STYLES[kind],});diff --git a/frontend/src/store/app-store.ts b/frontend/src/store/app-store.tsindex a5c1018b..5ec7351c 100644--- a/frontend/src/store/app-store.ts+++ b/frontend/src/store/app-store.ts@@ -38,8 +38,16 @@ export const useAppStore = create<AppStore>((set) => ({inSelectAndEditMode: false,toggleInSelectAndEditMode: () =>- set((state) => ({ inSelectAndEditMode: !state.inSelectAndEditMode })),- disableInSelectAndEditMode: () => set({ inSelectAndEditMode: false }),+ set((state) =>+ state.inSelectAndEditMode+ ? { inSelectAndEditMode: false, selectedElement: null }+ : { inSelectAndEditMode: true }+ ),+ // Exiting selection mode and releasing its locked target are one action.+ // Keeping this atomic prevents a stale iframe element from surviving a+ // version change until PreviewComponent's effects get a chance to run.+ disableInSelectAndEditMode: () =>+ set({ inSelectAndEditMode: false, selectedElement: null }),selectedElement: null,setSelectedElement: (element: HTMLElement | null) =>diff --git a/frontend/src/store/project-store.test.ts b/frontend/src/store/project-store.test.tsindex 4b78fb85..d266db3c 100644--- a/frontend/src/store/project-store.test.ts+++ b/frontend/src/store/project-store.test.ts@@ -1,4 +1,5 @@import { Commit, VariantStatus } from "../components/commits/types";+import { useAppStore } from "./app-store";import { useProjectStore } from "./project-store";function createGeneratingCommit(): Commit {@@ -14,6 +15,40 @@ function createGeneratingCommit(): Commit {};}+describe("version navigation", () => {+ const selectedElement = { tagName: "BUTTON" } as HTMLElement;++ beforeEach(() => {+ useProjectStore.setState({ head: "latest" });+ useAppStore.setState({+ inSelectAndEditMode: true,+ selectedElement,+ });+ });++ afterEach(() => {+ useAppStore.setState({+ inSelectAndEditMode: false,+ selectedElement: null,+ });+ });++ it("exits select-and-edit and clears its target when the head changes", () => {+ useProjectStore.getState().setHead("previous");++ expect(useProjectStore.getState().head).toBe("previous");+ expect(useAppStore.getState().inSelectAndEditMode).toBe(false);+ expect(useAppStore.getState().selectedElement).toBeNull();+ });++ it("does not exit select-and-edit when the requested head is already active", () => {+ useProjectStore.getState().setHead("latest");++ expect(useAppStore.getState().inSelectAndEditMode).toBe(true);+ expect(useAppStore.getState().selectedElement).toBe(selectedElement);+ });+});+describe("variant completion timestamps", () => {beforeEach(() => {useProjectStore.setState({diff --git a/frontend/src/store/project-store.ts b/frontend/src/store/project-store.tsindex 3a4aa9e0..03286dc5 100644--- a/frontend/src/store/project-store.ts+++ b/frontend/src/store/project-store.ts@@ -7,6 +7,7 @@ import {VariantStatus,} from "../components/commits/types";import { PromptAsset } from "../types";+import { useAppStore } from "./app-store";// Store for app-wide stateinterface ProjectStore {@@ -82,7 +83,7 @@ interface ProjectStore {resetExecutionConsoles: () => void;}-export const useProjectStore = create<ProjectStore>((set) => ({+export const useProjectStore = create<ProjectStore>((set, get) => ({// Inputs and their settersinputMode: "image",setInputMode: (mode) => set({ inputMode: mode }),@@ -440,7 +441,15 @@ export const useProjectStore = create<ProjectStore>((set) => ({};}),- setHead: (hash: CommitHash) => set({ head: hash }),+ setHead: (hash: CommitHash) => {+ // A target is a live element from the current preview document. It cannot+ // safely carry across versions, so every real version change exits select+ // mode before swapping the project head. All navigation entry points use+ // setHead (Previous/Next, Versions, and Back to latest).+ if (get().head === hash) return;+ useAppStore.getState().disableInSelectAndEditMode();+ set({ head: hash });+ },resetHead: () => set({ head: null }),executionConsoles: {},