move download code to separate file

This commit is contained in:
Abi Raja 2024-07-25 14:36:22 -04:00
parent 993ff88e2b
commit 1a3086cc9a
2 changed files with 17 additions and 17 deletions

View File

@ -13,6 +13,7 @@ import { useAppStore } from "../../store/app-store";
import { useProjectStore } from "../../store/project-store"; import { useProjectStore } from "../../store/project-store";
import { extractHtml } from "./extractHtml"; import { extractHtml } from "./extractHtml";
import PreviewComponent from "./PreviewComponent"; import PreviewComponent from "./PreviewComponent";
import { downloadCode } from "./download";
interface Props { interface Props {
doUpdate: (instruction: string) => void; doUpdate: (instruction: string) => void;
@ -21,23 +22,6 @@ interface Props {
} }
function Preview({ doUpdate, reset, settings }: Props) { function Preview({ doUpdate, reset, settings }: Props) {
const downloadCode = (code: string) => {
// Create a blob from the generated code
const blob = new Blob([code], { type: "text/html" });
const url = URL.createObjectURL(blob);
// Create an anchor element and set properties for download
const a = document.createElement("a");
a.href = url;
a.download = "index.html"; // Set the file name for download
document.body.appendChild(a); // Append to the document
a.click(); // Programmatically click the anchor to trigger download
// Clean up by removing the anchor and revoking the Blob URL
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
const { appState } = useAppStore(); const { appState } = useAppStore();
const { inputMode, generatedCode, setGeneratedCode } = useProjectStore(); const { inputMode, generatedCode, setGeneratedCode } = useProjectStore();

View File

@ -0,0 +1,16 @@
export const downloadCode = (code: string) => {
// Create a blob from the generated code
const blob = new Blob([code], { type: "text/html" });
const url = URL.createObjectURL(blob);
// Create an anchor element and set properties for download
const a = document.createElement("a");
a.href = url;
a.download = "index.html"; // Set the file name for download
document.body.appendChild(a); // Append to the document
a.click(); // Programmatically click the anchor to trigger download
// Clean up by removing the anchor and revoking the Blob URL
document.body.removeChild(a);
URL.revokeObjectURL(url);
};