-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,013 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { useState } from 'react'; | ||
import PostPreview from "../components/PostPreview"; | ||
import Switch from "react-switch"; | ||
import toast from "react-hot-toast"; | ||
|
||
type Props = { | ||
codeSnippet: string; | ||
jsCodeSnippet: string; | ||
setCodeSnippet: (codeSnippet: string) => void; | ||
setJsCodeSnippet: (jsCodeSnippet: string) => void; | ||
}; | ||
|
||
function CodeEditorAndPreview({ | ||
codeSnippet, | ||
jsCodeSnippet, | ||
setCodeSnippet, | ||
setJsCodeSnippet | ||
}: Props) { | ||
const [isPreviewMode, setIsPreviewMode] = useState(false); | ||
|
||
const handleToggle = () => { | ||
setIsPreviewMode(!isPreviewMode); | ||
}; | ||
|
||
const handleTextAreaClick = () => { | ||
if (isPreviewMode) { | ||
toast('Please disable preview mode to edit the code snippet', { | ||
icon: '🚫', | ||
}); | ||
} | ||
}; | ||
|
||
|
||
return ( | ||
<div> | ||
<div> | ||
<label htmlFor="codeSnippet" className="block text-gray-700 text-sm font-semibold"> | ||
HTML | ||
</label> | ||
<textarea | ||
id="codeSnippet" | ||
value={codeSnippet} | ||
onChange={(e) => setCodeSnippet(e.target.value)} | ||
className="mt-1 shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | ||
readOnly={isPreviewMode} | ||
onClick={handleTextAreaClick} | ||
rows={5} | ||
></textarea> | ||
</div> | ||
<div> | ||
<label htmlFor="jsCodeSnippet" className="block text-gray-700 text-sm font-semibold"> | ||
Javascript | ||
</label> | ||
<textarea | ||
id="jsCodeSnippet" | ||
value={jsCodeSnippet} | ||
onChange={(e) => setJsCodeSnippet(e.target.value)} | ||
className="mt-1 shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | ||
readOnly={isPreviewMode} | ||
onClick={handleTextAreaClick} | ||
rows={4} | ||
></textarea> | ||
</div> | ||
<div className="flex items-center mt-2 mb-2"> | ||
<label className="flex items-center"> | ||
<Switch | ||
onChange={handleToggle} | ||
checked={isPreviewMode} | ||
offColor="#888" | ||
onColor="#080" | ||
uncheckedIcon={false} | ||
checkedIcon={false} | ||
disabled={codeSnippet == ""} | ||
height={16} | ||
width={32} | ||
className="mr-2" | ||
/> | ||
<span className="text-gray-700 text-sm font-semibold"> | ||
{isPreviewMode ? 'Edit Code' : 'Preview Component'} | ||
</span> | ||
</label> | ||
</div> | ||
{isPreviewMode ? ( | ||
<PostPreview | ||
jsCodeSnippet={jsCodeSnippet} | ||
codeSnippet={codeSnippet} | ||
/> | ||
) : null} | ||
|
||
</div> | ||
); | ||
|
||
} | ||
|
||
export default CodeEditorAndPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
import DOMPurify from "dompurify" | ||
|
||
function PostPreview({ | ||
codeSnippet, | ||
jsCodeSnippet, | ||
}: { | ||
codeSnippet: string; | ||
jsCodeSnippet: string; | ||
}) { | ||
const ref = useRef<HTMLIFrameElement>(null); | ||
const [height, setHeight] = useState("100px"); | ||
|
||
useEffect(() => { | ||
const handleResize = (event: MessageEvent) => { | ||
if (event.data.type === "setHeight") { | ||
setHeight(event.data.height + "px"); | ||
} | ||
}; | ||
|
||
window.addEventListener("message", handleResize); | ||
return () => { | ||
window.removeEventListener("message", handleResize); | ||
}; | ||
}, []); | ||
|
||
DOMPurify.addHook("uponSanitizeElement", (node, data) => { | ||
if (data.tagName === "img" || data.tagName === "div") { | ||
const src = node.getAttribute("src"); | ||
const style = node.getAttribute("style"); | ||
if (src && src.startsWith("http")) { | ||
node.setAttribute("src", src); | ||
} | ||
if (style && style.includes("url(")) { | ||
node.setAttribute("style", style); | ||
} | ||
} | ||
}); | ||
|
||
const sanitizedSnippet = DOMPurify.sanitize(codeSnippet || "", { | ||
ADD_ATTR: ["style", "background"], | ||
}); | ||
|
||
return ( | ||
<div className="p-4 text-[#000435] bg-white dark:text-white dark:bg-[#fff] z-0 overflow-hidden rounded border-2 border-sky-400"> | ||
<iframe | ||
ref={ref} | ||
className="w-full border-0" | ||
srcDoc={getPreviewTemplate(sanitizedSnippet, jsCodeSnippet)} | ||
title="Preview" | ||
sandbox="allow-scripts allow-modals" | ||
style={{ minHeight: height, maxWidth: "100%" }} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function getPreviewTemplate(sanitizedSnippet: string, jsCodeSnippet: string) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html class='flex w-full h-full'> | ||
<head> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet"> | ||
<script> | ||
document.addEventListener('DOMContentLoaded', function() { | ||
const setHeight = () => { | ||
const height = document.body.scrollHeight; | ||
window.parent.postMessage({ type: 'setHeight', height }, '*'); | ||
}; | ||
setHeight(); | ||
const observer = new MutationObserver(setHeight); | ||
observer.observe(document.body, { childList: true, subtree: true }); | ||
window.addEventListener('beforeunload', () => observer.disconnect()); | ||
}); | ||
</script> | ||
</head> | ||
<body class='w-full h-full flex items-center justify-center min-w-full min-h-full'> | ||
<div class='w-full h-full p-6'>${sanitizedSnippet}</div> | ||
${jsCodeSnippet ? `<script defer>${jsCodeSnippet}</script>` : ""} | ||
</body> | ||
</html> | ||
`; | ||
} | ||
|
||
export default PostPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.