diff --git a/.gitignore b/.gitignore index 39aee32..7b377b1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,13 @@ .aider* +# Project-related files + # Run logs backend/run_logs/* -.env \ No newline at end of file +# Weird Docker setup related files +backend/backend/* + +# Env vars +frontend/.env.local +.env diff --git a/README.md b/README.md index 97bc183..81cc82a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # screenshot-to-code -This simple app converts a screenshot to HTML/Tailwind CSS. It uses GPT-4 Vision to generate the code and DALL-E 3 to generate similar-looking images. +This simple app converts a screenshot to HTML/Tailwind CSS. It uses GPT-4 Vision to generate the code and DALL-E 3 to generate similar-looking images. You can now also enter a URL to clone a live website! https://github.com/abi/screenshot-to-code/assets/23818/6cebadae-2fe3-4986-ac6a-8fb9db030045 diff --git a/backend/main.py b/backend/main.py index 4eb09fa..72b4dd7 100644 --- a/backend/main.py +++ b/backend/main.py @@ -59,7 +59,7 @@ def write_logs(prompt_messages, completion): @app.websocket("/generate-code") -async def stream_code_test(websocket: WebSocket): +async def stream_code(websocket: WebSocket): await websocket.accept() params = await websocket.receive_json() diff --git a/frontend/package.json b/frontend/package.json index 7a34ab2..912d0b7 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -22,6 +22,7 @@ "classnames": "^2.3.2", "clsx": "^2.0.0", "codemirror": "^6.0.1", + "copy-to-clipboard": "^3.3.3", "react": "^18.2.0", "react-dom": "^18.2.0", "react-dropzone": "^14.2.3", diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 44883ff..7628d0e 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useRef, useState, useCallback } from "react"; import ImageUpload from "./components/ImageUpload"; import CodePreview from "./components/CodePreview"; import Preview from "./components/Preview"; @@ -7,28 +7,30 @@ import Spinner from "./components/Spinner"; import classNames from "classnames"; import { FaCode, + FaCopy, FaDesktop, FaDownload, FaMobile, FaUndo, } from "react-icons/fa"; +import copy from "copy-to-clipboard"; +import toast from "react-hot-toast"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "./components/ui/tabs"; import CodeMirror from "./components/CodeMirror"; import SettingsDialog from "./components/SettingsDialog"; -import { Settings, EditorTheme } from "./types"; +import { AppState, Settings, EditorTheme } from "./types"; import { IS_RUNNING_ON_CLOUD } from "./config"; import { PicoBadge } from "./components/PicoBadge"; import { OnboardingNote } from "./components/OnboardingNote"; import { usePersistedState } from "./hooks/usePersistedState"; import { UrlInputSection } from "./components/UrlInputSection"; import TermsOfServiceDialog from "./components/TermsOfServiceDialog"; +import { USER_CLOSE_WEB_SOCKET_CODE } from "./constants"; function App() { - const [appState, setAppState] = useState<"INITIAL" | "CODING" | "CODE_READY">( - "INITIAL" - ); + const [appState, setAppState] = useState(AppState.INITIAL); const [generatedCode, setGeneratedCode] = useState(""); const [referenceImages, setReferenceImages] = useState([]); const [executionConsole, setExecutionConsole] = useState([]); @@ -43,6 +45,7 @@ function App() { }, "setting" ); + const wsRef = useRef(null); const downloadCode = () => { // Create a blob from the generated code @@ -62,26 +65,31 @@ function App() { }; const reset = () => { - setAppState("INITIAL"); + setAppState(AppState.INITIAL); setGeneratedCode(""); setReferenceImages([]); setExecutionConsole([]); setHistory([]); }; + const stop = () => { + wsRef.current?.close?.(USER_CLOSE_WEB_SOCKET_CODE); + }; + function doGenerateCode(params: CodeGenerationParams) { setExecutionConsole([]); - setAppState("CODING"); + setAppState(AppState.CODING); // Merge settings with params const updatedParams = { ...params, ...settings }; generateCode( + wsRef, updatedParams, (token) => setGeneratedCode((prev) => prev + token), (code) => setGeneratedCode(code), (line) => setExecutionConsole((prev) => [...prev, line]), - () => setAppState("CODE_READY") + () => setAppState(AppState.CODE_READY) ); } @@ -111,6 +119,11 @@ function App() { setUpdateInstruction(""); } + const doCopyCode = useCallback(() => { + copy(generatedCode); + toast.success("Copied to clipboard"); + }, [generatedCode]); + return (
{IS_RUNNING_ON_CLOUD && } @@ -122,7 +135,7 @@ function App() {

Screenshot to Code

- {appState === "INITIAL" && ( + {appState === AppState.INITIAL && (

Drag & drop a screenshot to get started.

@@ -130,20 +143,26 @@ function App() { {IS_RUNNING_ON_CLOUD && !settings.openAiApiKey && } - {(appState === "CODING" || appState === "CODE_READY") && ( + {(appState === AppState.CODING || + appState === AppState.CODE_READY) && ( <> {/* Show code preview only when coding */} - {appState === "CODING" && ( + {appState === AppState.CODING && (
{executionConsole.slice(-1)[0]}
+
+ +
)} - {appState === "CODE_READY" && ( + {appState === AppState.CODE_READY && (