Add embedded shape experiments (#1)

* push latest changes

* tweaks

* send the html back
pull/46/head
Lu Wilson 3 years ago committed by GitHub
parent d4f274ff6c
commit af6aba089a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,13 +1,18 @@
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.`;
a single html file that uses tailwind to create the website.
They may also provide you with the html of a previous design that they want you to iterate from.
Carry out any changes they request from you.
In the wireframe, the previous design's html will appear as a white rectangle.
Use creative license to make the application more fleshed out.
if you need to insert an image, use a colored fill rectangle as a placeholder. Respond only with the html file.`;
export async function POST(request: Request) {
const { image } = await request.json();
const { image, html } = await request.json();
const body: GPT4VCompletionRequest = {
model: "gpt-4-vision-preview",
max_tokens: 4096,
temperature: 0,
messages: [
{
role: "system",
@ -18,9 +23,19 @@ export async function POST(request: Request) {
content: [
{
type: "image_url",
image_url: image,
image_url: {
url: image,
detail: "high",
},
},
{
type: "text",
text: "Turn this into a single html file using tailwind.",
},
{
type: "text",
text: html,
},
"Turn this into a single html file using tailwind.",
],
},
],
@ -50,7 +65,22 @@ export async function POST(request: Request) {
type MessageContent =
| string
| (string | { type: "image_url"; image_url: string })[];
| (
| string
| {
type: "image_url";
image_url:
| string
| {
url: string;
detail: "low" | "high" | "auto";
};
}
| {
type: "text";
text: string;
}
)[];
export type GPT4VCompletionRequest = {
model: "gpt-4-vision-preview";

@ -1,42 +1,108 @@
/* eslint-disable react-hooks/rules-of-hooks */
"use client";
import dynamic from "next/dynamic";
import "@tldraw/tldraw/tldraw.css";
import { useEditor } from "@tldraw/tldraw";
import {
BaseBoxShapeUtil,
Geometry2d,
HTMLContainer,
Rectangle2d,
ShapeUtil,
TLBaseShape,
TLEmbedShape,
TLImageShape,
TLShapeUtilFlag,
toDomPrecision,
useEditor,
useExportAs,
useIsEditing,
} 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";
import { format } from "path";
type PreviewShapeType = TLBaseShape<
"preview",
{
html: string;
w: number;
h: number;
}
>;
class PreviewShape extends BaseBoxShapeUtil<PreviewShapeType> {
static override type = "preview" as const;
getDefaultProps(): PreviewShapeType["props"] {
return {
html: "",
w: (960 * 2) / 3,
h: (540 * 2) / 3,
};
}
override canEdit = () => true;
override isAspectRatioLocked = (_shape: PreviewShapeType) => false;
override canResize = (_shape: PreviewShapeType) => true;
override canBind = (_shape: PreviewShapeType) => false;
override component(shape: PreviewShapeType) {
const isEditing = useIsEditing(shape.id);
return (
<HTMLContainer className="tl-embed-container" id={shape.id}>
<iframe
className="tl-embed"
srcDoc={shape.props.html}
width={toDomPrecision(shape.props.w)}
height={toDomPrecision(shape.props.h)}
draggable={false}
style={{
border: 0,
pointerEvents: isEditing ? "auto" : "none",
}}
/>
</HTMLContainer>
);
}
indicator(shape: PreviewShapeType) {
return <rect width={shape.props.w} height={shape.props.h} />;
}
}
const Tldraw = dynamic(async () => (await import("@tldraw/tldraw")).Tldraw, {
ssr: false,
});
export default function Home() {
const [html, setHtml] = useState<null | string>(null);
// const [html, setHtml] = useState<null | string>(null);
useEffect(() => {
const listener = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setHtml(null);
}
};
window.addEventListener("keydown", listener);
// useEffect(() => {
// const listener = (e: KeyboardEvent) => {
// if (e.key === "Escape") {
// setHtml(null);
// }
// };
// window.addEventListener("keydown", listener);
return () => {
window.removeEventListener("keydown", listener);
};
});
// return () => {
// window.removeEventListener("keydown", listener);
// };
// });
return (
<>
<div className={`w-screen h-screen`}>
<Tldraw persistenceKey="tldraw">
<ExportButton setHtml={setHtml} />
<Tldraw persistenceKey="tldraw" shapeUtils={[PreviewShape]}>
<ExportButton /*setHtml={setHtml}*/ />
</Tldraw>
</div>
{html &&
{/* {html &&
ReactDOM.createPortal(
<div
className="fixed top-0 left-0 right-0 bottom-0 flex justify-center items-center"
@ -46,14 +112,15 @@ export default function Home() {
<PreviewModal html={html} setHtml={setHtml} />
</div>,
document.body
)}
)} */}
</>
);
}
function ExportButton({ setHtml }: { setHtml: (html: string) => void }) {
function ExportButton(/*{ setHtml }: { setHtml: (html: string) => void }*/) {
const editor = useEditor();
const [loading, setLoading] = useState(false);
const exportAs = useExportAs();
// A tailwind styled button that is pinned to the bottom right of the screen
return (
<button
@ -61,12 +128,40 @@ function ExportButton({ setHtml }: { setHtml: (html: string) => void }) {
setLoading(true);
try {
e.preventDefault();
const svg = await editor.getSvg(
Array.from(editor.currentPageShapeIds)
const previewPosition = editor.selectedShapes.reduce(
(acc, shape) => {
const bounds = editor.getShapePageBounds(shape);
const right = bounds?.maxX ?? 0;
const top = bounds?.minY ?? 0;
return {
x: Math.max(acc.x, right),
y: Math.min(acc.y, top),
};
},
{ x: 0, y: Infinity }
);
const previousPreviews = editor.selectedShapes.filter((shape) => {
return shape.type === "preview";
}) as PreviewShapeType[];
if (previousPreviews.length > 1) {
throw new Error(
"You can only give the developer one previous design to work with."
);
}
const previousHtml =
previousPreviews.length === 1
? previousPreviews[0].props.html
: "No previous design has been provided this time.";
const svg = await editor.getSvg(editor.selectedShapeIds);
if (!svg) {
return;
}
const png = await getSvgAsImage(svg, {
type: "png",
quality: 1,
@ -78,7 +173,10 @@ function ExportButton({ setHtml }: { setHtml: (html: string) => void }) {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ image: dataUrl }),
body: JSON.stringify({
image: dataUrl,
html: previousHtml,
}),
});
const json = await resp.json();
@ -92,7 +190,15 @@ function ExportButton({ setHtml }: { setHtml: (html: string) => void }) {
const start = message.indexOf("<!DOCTYPE html>");
const end = message.indexOf("</html>");
const html = message.slice(start, end + "</html>".length);
setHtml(html);
editor.createShape<PreviewShapeType>({
type: "preview",
x: previewPosition.x,
y: previewPosition.y,
props: { html },
});
// setHtml(html);
} finally {
setLoading(false);
}

10
package-lock.json generated

@ -1793,13 +1793,13 @@
"version": "15.7.9",
"resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.9.tgz",
"integrity": "sha512-n1yyPsugYNSmHgxDFjicaI2+gCNjsBck8UX9kuofAKlc0h1bL+20oSF72KeNaW2DUlesbEVCFgyV2dPGTiY42g==",
"dev": true
"devOptional": true
},
"node_modules/@types/react": {
"version": "18.2.34",
"resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.34.tgz",
"integrity": "sha512-U6eW/alrRk37FU/MS2RYMjx0Va2JGIVXELTODaTIYgvWGCV4Y4TfTUzG8DdmpDNIT0Xpj/R7GfyHOJJrDttcvg==",
"dev": true,
"devOptional": true,
"dependencies": {
"@types/prop-types": "*",
"@types/scheduler": "*",
@ -1810,7 +1810,7 @@
"version": "18.2.14",
"resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-18.2.14.tgz",
"integrity": "sha512-V835xgdSVmyQmI1KLV2BEIUgqEuinxp9O4G6g3FqO/SqLac049E53aysv0oEFD2kHfejeKU+ZqL2bcFWj9gLAQ==",
"dev": true,
"devOptional": true,
"dependencies": {
"@types/react": "*"
}
@ -1819,7 +1819,7 @@
"version": "0.16.5",
"resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.5.tgz",
"integrity": "sha512-s/FPdYRmZR8SjLWGMCuax7r3qCWQw9QKHzXVukAuuIJkXkDRwp+Pu5LMIVFi0Fxbav35WURicYr8u1QsoybnQw==",
"dev": true
"devOptional": true
},
"node_modules/@typescript-eslint/parser": {
"version": "6.9.1",
@ -2543,7 +2543,7 @@
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.2.tgz",
"integrity": "sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==",
"dev": true
"devOptional": true
},
"node_modules/damerau-levenshtein": {
"version": "1.0.8",

Loading…
Cancel
Save