-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add code embed component with gallery
- Loading branch information
1 parent
e7e16df
commit 1dd5281
Showing
6 changed files
with
155 additions
and
6 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
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 |
---|---|---|
@@ -1,11 +1,86 @@ | ||
const CodeEmbed = ({ code }) => { | ||
import { useEffect, useRef, useState } from "preact/hooks"; | ||
import { createEditor } from "prism-code-editor"; | ||
import { copyButton } from "prism-code-editor/copy-button"; | ||
import { matchBrackets } from "prism-code-editor/match-brackets"; | ||
import { indentGuides } from "prism-code-editor/guides"; | ||
import "prism-code-editor/grammars/javascript"; | ||
|
||
// jsCode: string | ||
const wrapJsInMarkup = (jsCode) => `<!DOCTYPE html> | ||
<meta charset="utf8" /> | ||
<body></body> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.8.0/p5.js"></script> | ||
<script id="code" type="module">${jsCode}</script> | ||
`; | ||
|
||
// props: { code: string } | ||
const CodeFrame = (props) => ( | ||
<iframe | ||
srcDoc={wrapJsInMarkup(props.code)} | ||
sandbox="allow-scripts allow-popups allow-modals allow-forms" | ||
aria-label="Code Preview" | ||
title="Code Preview" | ||
/> | ||
); | ||
|
||
// interface CodeEmbedProps { | ||
// initialValue?: string; | ||
// editable: boolean; | ||
// previewable: boolean; | ||
// } | ||
export const CodeEmbed = (props) => { | ||
// < HTMLDivElement > | ||
const divRef = useRef(null); | ||
// < PrismCodeEditor > | ||
const editorRef = useRef(null); | ||
const [codeString, setCodeString] = useState(props.initialValue ?? ""); | ||
|
||
useEffect(() => { | ||
const editor = (editorRef.current = createEditor( | ||
divRef.current, | ||
{ | ||
language: "javascript", | ||
value: props.initialValue ?? "", | ||
theme: "prism", | ||
insertSpaces: true, | ||
tabSize: 2, | ||
readOnly: !props.editable, | ||
wordWrap: true, | ||
}, | ||
copyButton(), | ||
matchBrackets(), | ||
indentGuides() | ||
)); | ||
|
||
divRef.current.querySelector("textarea").ariaLabel = "Code Editor"; | ||
|
||
return editor.remove; | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<pre> | ||
<code>{code}</code> | ||
</pre> | ||
</div> | ||
<> | ||
<div ref={divRef} /> | ||
{props.previewable ? ( | ||
<> | ||
<button | ||
onClick={() => { | ||
console.log("updating code"); | ||
setCodeString(editorRef.current.value); | ||
}} | ||
> | ||
Run Code | ||
</button> | ||
<CodeFrame code={codeString} /> | ||
</> | ||
) : null} | ||
</> | ||
); | ||
}; | ||
|
||
// css imports for bundling | ||
import "prism-code-editor/layout.css"; | ||
import "prism-code-editor/scrollbar.css"; | ||
import "prism-code-editor/copy-button.css"; | ||
import "prism-code-editor/themes/vs-code-light.css"; | ||
|
||
export default CodeEmbed; |
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,59 @@ | ||
--- | ||
import CodeEmbed from "@components/reference/CodeEmbed/"; | ||
const codeString = ` | ||
let sketch = function (p) { | ||
let x = 100; | ||
let y = 100; | ||
p.setup = function () { | ||
p.createCanvas(700, 410); | ||
}; | ||
p.draw = function () { | ||
p.background(0); | ||
p.fill(255); | ||
p.rect(x, y, 50, 50); | ||
}; | ||
}; | ||
let myp5 = new p5(sketch); | ||
`; | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>p5.js Code Embed Preact Component</title> | ||
</head> | ||
<body> | ||
<h1>p5.js Code Embed Preact Component</h1> | ||
<h2>Readonly</h2> | ||
<CodeEmbed | ||
client:only="preact" | ||
editable={false} | ||
previewable={false} | ||
initialValue={codeString} | ||
/> | ||
<h2>Readonly And Previewable</h2> | ||
<CodeEmbed | ||
client:only="preact" | ||
editable={false} | ||
previewable={true} | ||
initialValue={codeString} | ||
/> | ||
<h2>Editable</h2> | ||
<CodeEmbed | ||
client:only="preact" | ||
editable={true} | ||
previewable={false} | ||
initialValue={codeString} | ||
/> | ||
<h2>Editable And Previewable</h2> | ||
<CodeEmbed | ||
client:only="preact" | ||
editable={true} | ||
previewable={true} | ||
initialValue={codeString} | ||
/> | ||
</body> | ||
</html> |
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