From c95ae2742c65099b685ffb16b1582098dc4678aa Mon Sep 17 00:00:00 2001 From: Sawyer Hood Date: Mon, 6 Nov 2023 21:46:40 -0800 Subject: [PATCH] Initial Commit --- app/api/toHtml/route.ts | 78 ++ app/globals.css | 24 +- app/layout.tsx | 24 +- app/page.tsx | 203 ++-- components/PreviewModal.tsx | 101 ++ lib/blobToBase64.ts | 7 + lib/getBrowserCanvasMaxSize.ts | 28 + lib/getSvgAsImage.ts | 132 +++ lib/png.ts | 186 ++++ package-lock.json | 1822 ++++++++++++++++++++++++++++++-- package.json | 14 +- 11 files changed, 2381 insertions(+), 238 deletions(-) create mode 100644 app/api/toHtml/route.ts create mode 100644 components/PreviewModal.tsx create mode 100644 lib/blobToBase64.ts create mode 100644 lib/getBrowserCanvasMaxSize.ts create mode 100644 lib/getSvgAsImage.ts create mode 100644 lib/png.ts diff --git a/app/api/toHtml/route.ts b/app/api/toHtml/route.ts new file mode 100644 index 0000000..5c1abfc --- /dev/null +++ b/app/api/toHtml/route.ts @@ -0,0 +1,78 @@ +const systemPrompt = `You are an expert tailwind developer. A user will provide you with a + low-fidelity wireframe of an application and you will return + a single html file that uses tailwind to create the website. Use creative license to make the application more fleshed out. +if you need to insert an image, use placehold.co to create a placeholder image. Respond only with the html file.`; + +export async function POST(request: Request) { + const { image } = await request.json(); + const body: GPT4VCompletionRequest = { + model: "gpt-4-vision-preview", + max_tokens: 4096, + messages: [ + { + role: "system", + content: systemPrompt, + }, + { + role: "user", + content: [ + { + type: "image_url", + image_url: image, + }, + "Turn this into a single html file using tailwind.", + ], + }, + ], + }; + + let json = null; + try { + const resp = await fetch("https://api.openai.com/v1/chat/completions", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, + }, + body: JSON.stringify(body), + }); + json = await resp.json(); + } catch (e) { + console.log(e); + } + + return new Response(JSON.stringify(json), { + headers: { + "content-type": "application/json; charset=UTF-8", + }, + }); +} + +type MessageContent = + | string + | (string | { type: "image_url"; image_url: string })[]; + +export type GPT4VCompletionRequest = { + model: "gpt-4-vision-preview"; + messages: { + role: "system" | "user" | "assistant" | "function"; + content: MessageContent; + name?: string | undefined; + }[]; + functions?: any[] | undefined; + function_call?: any | undefined; + stream?: boolean | undefined; + temperature?: number | undefined; + top_p?: number | undefined; + max_tokens?: number | undefined; + n?: number | undefined; + best_of?: number | undefined; + frequency_penalty?: number | undefined; + presence_penalty?: number | undefined; + logit_bias?: + | { + [x: string]: number; + } + | undefined; + stop?: (string[] | string) | undefined; +}; diff --git a/app/globals.css b/app/globals.css index fd81e88..0400eb4 100644 --- a/app/globals.css +++ b/app/globals.css @@ -2,26 +2,10 @@ @tailwind components; @tailwind utilities; -:root { - --foreground-rgb: 0, 0, 0; - --background-start-rgb: 214, 219, 220; - --background-end-rgb: 255, 255, 255; +.tlui-help-menu { + display: none !important; } -@media (prefers-color-scheme: dark) { - :root { - --foreground-rgb: 255, 255, 255; - --background-start-rgb: 0, 0, 0; - --background-end-rgb: 0, 0, 0; - } -} - -body { - color: rgb(var(--foreground-rgb)); - background: linear-gradient( - to bottom, - transparent, - rgb(var(--background-end-rgb)) - ) - rgb(var(--background-start-rgb)); +.tlui-debug-panel { + display: none !important; } diff --git a/app/layout.tsx b/app/layout.tsx index 40e027f..fa6cad5 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,22 +1,28 @@ -import type { Metadata } from 'next' -import { Inter } from 'next/font/google' -import './globals.css' +import type { Metadata } from "next"; +import { Inter } from "next/font/google"; +import "./globals.css"; -const inter = Inter({ subsets: ['latin'] }) +const inter = Inter({ subsets: ["latin"] }); export const metadata: Metadata = { - title: 'Create Next App', - description: 'Generated by create next app', -} + title: "Create Next App", + description: "Generated by create next app", +}; export default function RootLayout({ children, }: { - children: React.ReactNode + children: React.ReactNode; }) { return ( + + + {children} - ) + ); } diff --git a/app/page.tsx b/app/page.tsx index 7a8286b..2136283 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,113 +1,106 @@ -import Image from 'next/image' +"use client"; -export default function Home() { - return ( -
-
-

- Get started by editing  - app/page.tsx -

-
- - By{' '} - Vercel Logo - -
-
+import dynamic from "next/dynamic"; +import "@tldraw/tldraw/tldraw.css"; +import { useEditor } from "@tldraw/tldraw"; +import { getSvgAsImage } from "@/lib/getSvgAsImage"; +import { blobToBase64 } from "@/lib/blobToBase64"; +import React, { useEffect, useState } from "react"; +import ReactDOM from "react-dom"; +import { PreviewModal } from "@/components/PreviewModal"; -
- Next.js Logo -
+const Tldraw = dynamic(async () => (await import("@tldraw/tldraw")).Tldraw, { + ssr: false, +}); -
- -

- Docs{' '} - - -> - -

-

- Find in-depth information about Next.js features and API. -

-
+export default function Home() { + const [html, setHtml] = useState(null); - -

- Learn{' '} - - -> - -

-

- Learn about Next.js in an interactive course with quizzes! -

-
+ useEffect(() => { + const listener = (e: KeyboardEvent) => { + if (e.key === "Escape") { + setHtml(null); + } + }; + window.addEventListener("keydown", listener); - -

- Templates{' '} - - -> - -

-

- Explore the Next.js 13 playground. -

-
+ return () => { + window.removeEventListener("keydown", listener); + }; + }); - -

- Deploy{' '} - - -> - -

-

- Instantly deploy your Next.js site to a shareable URL with Vercel. -

-
+ return ( + <> +
+ + +
-
- ) + {html && + ReactDOM.createPortal( +
setHtml(null)} + > + +
, + document.body + )} + + ); +} + +function ExportButton({ setHtml }: { setHtml: (html: string) => void }) { + const editor = useEditor(); + const [loading, setLoading] = useState(false); + // A tailwind styled button that is pinned to the bottom right of the screen + return ( + + ); } diff --git a/components/PreviewModal.tsx b/components/PreviewModal.tsx new file mode 100644 index 0000000..ce4bb70 --- /dev/null +++ b/components/PreviewModal.tsx @@ -0,0 +1,101 @@ +"use client"; + +import { useEffect, useState } from "react"; +import Prism from "prismjs"; +import "prismjs/components/prism-cshtml"; + +import "prismjs/themes/prism-tomorrow.css"; + +export function PreviewModal({ + html, + setHtml, +}: { + html: string | null; + setHtml: (html: string | null) => void; +}) { + const [activeTab, setActiveTab] = useState<"preview" | "code">("preview"); + + useEffect(() => { + const highlight = async () => { + await Prism.highlightAll(); // <--- prepare Prism + }; + highlight(); // <--- call the async function + }, [html, activeTab]); // <--- run when post updates + + if (!html) { + return null; + } + + return ( +
+
+
+ { + setActiveTab("preview"); + }} + > + Preview + + { + setActiveTab("code"); + }} + > + Code + +
+ + +
+ + {activeTab === "preview" ? ( +