Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Support select and edit for all stacks

Abi Rajad984b01fc943
diff --git a/backend/prompts/system_prompt.py b/backend/prompts/system_prompt.py
index 0fa1d9b9..cb7dedb1 100644
--- a/backend/prompts/system_prompt.py
+++ b/backend/prompts/system_prompt.py
@@ -83,4 +83,9 @@ You are a coding agent that's an expert at building front-ends.
- You can use Google Fonts or other publicly accessible fonts.
- Except for Ionic, Font Awesome for icons: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"></link>
+# Targeted element edits
+
+- The user can select an element in the rendered preview to scope an update. When the request includes the selected element's outerHTML, treat it as a locator: it is captured from the live DOM, so it can differ from the source code (JSX uses className, Vue templates use directives and interpolations, and Ionic/Bootstrap scripts may inject classes or attributes at runtime).
+- Find the code in the current file that produces the selected element (match by tag, classes, ids, and text content) and apply the requested change only to that element and its rendering logic, leaving the rest of the file unchanged.
+
"""
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index c2a60c65..6b878c0a 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -25,7 +25,10 @@ import {
import { useAppStore } from "./store/app-store";
import { useProjectStore } from "./store/project-store";
import { useDesignSystems } from "./hooks/useDesignSystems";
-import { removeHighlight } from "./components/select-and-edit/utils";
+import {
+ buildSelectedElementInstruction,
+ removeHighlight,
+} from "./components/select-and-edit/utils";
import Sidebar from "./components/sidebar/Sidebar";
import IconStrip from "./components/sidebar/IconStrip";
import HistoryDisplay from "./components/history/HistoryDisplay";
@@ -157,10 +160,6 @@ function App() {
openDesignSystemsManager,
setSelectedDesignSystemId,
]);
- const showSelectAndEditFeature =
- settings.generatedCodeConfig === Stack.HTML_TAILWIND ||
- settings.generatedCodeConfig === Stack.HTML_CSS;
-
// Indicate coding state using the browser tab's favicon and title
useBrowserTabIndicator(appState === AppState.CODING);
@@ -637,10 +636,10 @@ function App() {
if (selectedElement) {
const elementHtml = removeHighlight(selectedElement).outerHTML;
selectedElementHtml = elementHtml;
- modifiedUpdateInstruction =
- updateInstruction +
- " referring to this element specifically: " +
- elementHtml;
+ modifiedUpdateInstruction = buildSelectedElementInstruction(
+ updateInstruction,
+ elementHtml
+ );
setSelectedElement(null);
}
@@ -844,7 +843,6 @@ function App() {
{(appState === AppState.CODING ||
appState === AppState.CODE_READY) && (
<Sidebar
- showSelectAndEditFeature={showSelectAndEditFeature}
doUpdate={doUpdate}
regenerate={regenerate}
cancelCodeGeneration={cancelCodeGeneration}
diff --git a/frontend/src/components/preview/PreviewComponent.tsx b/frontend/src/components/preview/PreviewComponent.tsx
index 8be4560c..a5210a05 100644
--- a/frontend/src/components/preview/PreviewComponent.tsx
+++ b/frontend/src/components/preview/PreviewComponent.tsx
@@ -29,12 +29,27 @@ function PreviewComponent({
// Select and edit functionality
const [clickEvent, setClickEvent] = useState<MouseEvent | null>(null);
const activeMode = viewMode ?? "fit";
+
+ // In select-and-edit mode, intercept clicks in the capture phase so the
+ // generated app's own handlers (React/Vue listeners, Bootstrap/Ionic
+ // behaviors, link navigation, form submits) never fire while selecting.
const handleIframeClick = useCallback((event: MouseEvent) => {
+ if (!inSelectAndEditModeRef.current) return;
+ event.preventDefault();
+ event.stopPropagation();
setClickEvent(event);
}, []);
+ // Suppress the rest of the pointer sequence (and form submits) while
+ // selecting, since app handlers can be bound to those events too.
+ const handleIframeInteraction = useCallback((event: Event) => {
+ if (!inSelectAndEditModeRef.current) return;
+ event.preventDefault();
+ event.stopPropagation();
+ }, []);
+
const handleIframeLinkClick = useCallback((event: MouseEvent) => {
- const target = (event.target as HTMLElement).closest("a");
+ const target = (event.target as HTMLElement).closest?.("a");
if (!target) return;
const href = target.getAttribute("href");
if (href && href.startsWith("#")) {
@@ -59,8 +74,6 @@ function PreviewComponent({
return;
}
- clickEvent.preventDefault();
-
const targetElement = clickEvent.target as HTMLElement;
if (!targetElement) return;
@@ -131,24 +144,42 @@ function PreviewComponent({
const iframe = iframeRef.current;
if (!iframe) return;
+ const suppressedEvents = ["pointerdown", "mousedown", "mouseup", "submit"];
+
const handleLoad = () => {
- const body = iframe.contentWindow?.document.body;
- if (!body) return;
- body.addEventListener("click", handleIframeClick);
- body.addEventListener("click", handleIframeLinkClick);
+ const win = iframe.contentWindow;
+ if (!win) return;
+ // Intercept on the window in the capture phase: the window is the
+ // first node in the propagation path, so this runs before any handler
+ // the generated app registered, including capture-phase delegated
+ // handlers on document (e.g. Bootstrap's data API).
+ win.addEventListener("click", handleIframeClick, true);
+ for (const type of suppressedEvents) {
+ win.addEventListener(type, handleIframeInteraction, true);
+ }
+ win.document.addEventListener("click", handleIframeLinkClick);
};
iframe.addEventListener("load", handleLoad);
+ // The current document may already be loaded (e.g. the component
+ // re-rendered after the iframe's load event); attach to it directly.
+ // addEventListener dedupes identical handlers, so this is safe.
+ if (iframe.contentWindow?.document.readyState === "complete") {
+ handleLoad();
+ }
return () => {
iframe.removeEventListener("load", handleLoad);
- const body = iframe.contentWindow?.document.body;
- if (body) {
- body.removeEventListener("click", handleIframeClick);
- body.removeEventListener("click", handleIframeLinkClick);
+ const win = iframe.contentWindow;
+ if (win) {
+ win.removeEventListener("click", handleIframeClick, true);
+ for (const type of suppressedEvents) {
+ win.removeEventListener(type, handleIframeInteraction, true);
+ }
+ win.document.removeEventListener("click", handleIframeLinkClick);
}
};
- }, [handleIframeClick, handleIframeLinkClick]);
+ }, [handleIframeClick, handleIframeLinkClick, handleIframeInteraction]);
useEffect(() => {
const iframe = iframeRef.current;
diff --git a/frontend/src/components/select-and-edit/utils.test.ts b/frontend/src/components/select-and-edit/utils.test.ts
new file mode 100644
index 00000000..30a99afa
--- /dev/null
+++ b/frontend/src/components/select-and-edit/utils.test.ts
@@ -0,0 +1,36 @@
+import { buildSelectedElementInstruction } from "./utils";
+
+describe("buildSelectedElementInstruction", () => {
+ it("includes the instruction and the element html", () => {
+ const result = buildSelectedElementInstruction(
+ "Make the button red",
+ '<button class="btn">Buy</button>'
+ );
+ expect(result).toContain("Make the button red");
+ expect(result).toContain('<button class="btn">Buy</button>');
+ expect(result).toContain("selected in the preview");
+ });
+
+ it("mentions that the snippet is rendered DOM, not source", () => {
+ const result = buildSelectedElementInstruction(
+ "Center it",
+ "<div>x</div>"
+ );
+ expect(result).toContain("outerHTML captured from the rendered page");
+ });
+
+ it("truncates very large element html", () => {
+ const hugeHtml = `<div>${"a".repeat(20000)}</div>`;
+ const result = buildSelectedElementInstruction("Shrink it", hugeHtml);
+ expect(result).toContain("truncated");
+ expect(result.length).toBeLessThan(hugeHtml.length);
+ });
+
+ it("does not truncate small element html", () => {
+ const result = buildSelectedElementInstruction(
+ "Bold it",
+ "<span>hello</span>"
+ );
+ expect(result).not.toContain("truncated");
+ });
+});
diff --git a/frontend/src/components/select-and-edit/utils.ts b/frontend/src/components/select-and-edit/utils.ts
index 47e62bc2..a0ddb68f 100644
--- a/frontend/src/components/select-and-edit/utils.ts
+++ b/frontend/src/components/select-and-edit/utils.ts
@@ -1,11 +1,47 @@
+// Cap the element HTML included in the prompt. The model already receives the
+// full current code, so the snippet only needs to identify the element.
+const MAX_ELEMENT_HTML_LENGTH = 12000;
+
export function removeHighlight(element: HTMLElement) {
- element.style.outline = "";
- element.style.backgroundColor = "";
+ element.style.outline = element.dataset.s2cPrevOutline ?? "";
+ element.style.backgroundColor = element.dataset.s2cPrevBackgroundColor ?? "";
+ delete element.dataset.s2cPrevOutline;
+ delete element.dataset.s2cPrevBackgroundColor;
+ // Avoid leaving an empty style attribute behind so the captured outerHTML
+ // matches the source markup as closely as possible.
+ if (!element.getAttribute("style")) {
+ element.removeAttribute("style");
+ }
return element;
}
export function addHighlight(element: HTMLElement) {
+ if (element.dataset.s2cPrevOutline === undefined) {
+ element.dataset.s2cPrevOutline = element.style.outline;
+ element.dataset.s2cPrevBackgroundColor = element.style.backgroundColor;
+ }
element.style.outline = "2px dashed #1846db";
element.style.backgroundColor = "#bfcbf5";
return element;
}
+
+export function buildSelectedElementInstruction(
+ instruction: string,
+ elementHtml: string
+): string {
+ const truncated =
+ elementHtml.length > MAX_ELEMENT_HTML_LENGTH
+ ? elementHtml.slice(0, MAX_ELEMENT_HTML_LENGTH) +
+ "\n<!-- truncated; locate the element in the current code -->"
+ : elementHtml;
+
+ return `${instruction}
+
+Apply the change to this specific element that the user selected in the preview:
+
+\`\`\`html
+${truncated}
+\`\`\`
+
+This snippet is the element's outerHTML captured from the rendered page, so it can differ from the source code (for example JSX uses className, Vue templates use directives and interpolations, and frameworks like Ionic or Bootstrap may inject classes or attributes at runtime). Find the code that produces this element and apply the change there, leaving unrelated code untouched.`;
+}
diff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsx
index 85391be7..efb3afaf 100644
--- a/frontend/src/components/sidebar/Sidebar.tsx
+++ b/frontend/src/components/sidebar/Sidebar.tsx
@@ -19,7 +19,6 @@ import DesignSystemSelector, {
} from "../settings/DesignSystemSelector";
interface SidebarProps {
- showSelectAndEditFeature: boolean;
doUpdate: (instruction: string) => void;
regenerate: () => void;
cancelCodeGeneration: () => void;
@@ -69,7 +68,6 @@ function isSlowGeminiModel(model?: string): boolean {
}
function Sidebar({
- showSelectAndEditFeature,
doUpdate,
regenerate,
cancelCodeGeneration,
@@ -558,19 +556,17 @@ function Sidebar({
updateImages={updateImages}
setUpdateImages={setUpdateImages}
/>
- {showSelectAndEditFeature && (
- <button
- onClick={toggleInSelectAndEditMode}
- className={`rounded-lg p-2 transition-colors ${
- inSelectAndEditMode
- ? "bg-violet-100 text-violet-600 dark:bg-violet-900/30 dark:text-violet-400"
- : "text-gray-400 hover:bg-gray-100 hover:text-gray-600 dark:text-zinc-500 dark:hover:bg-zinc-800 dark:hover:text-zinc-300"
- }`}
- title={inSelectAndEditMode ? "Exit selection mode" : "Select an element in the preview to target your edit"}
- >
- <LuMousePointerClick className="w-[18px] h-[18px]" />
- </button>
- )}
+ <button
+ onClick={toggleInSelectAndEditMode}
+ className={`rounded-lg p-2 transition-colors ${
+ inSelectAndEditMode
+ ? "bg-violet-100 text-violet-600 dark:bg-violet-900/30 dark:text-violet-400"
+ : "text-gray-400 hover:bg-gray-100 hover:text-gray-600 dark:text-zinc-500 dark:hover:bg-zinc-800 dark:hover:text-zinc-300"
+ }`}
+ title={inSelectAndEditMode ? "Exit selection mode" : "Select an element in the preview to target your edit"}
+ >
+ <LuMousePointerClick className="w-[18px] h-[18px]" />
+ </button>
<DesignSystemSelector {...designSystem} compact />
</div>
<button