Skip to content

Commit

Permalink
add code embed component with gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
outofambit committed Dec 11, 2023
1 parent e7e16df commit 1dd5281
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"preact": "^10.6.5",
"prism-code-editor": "^2.2.4",
"remark": "^15.0.1",
"remark-mdx": "^3.0.0",
"simple-git": "^3.20.0",
Expand Down
87 changes: 81 additions & 6 deletions src/components/reference/CodeEmbed/index.jsx
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;
59 changes: 59 additions & 0 deletions src/pages/en/embed-gallery.astro
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>
1 change: 1 addition & 0 deletions src/pages/en/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ Handy links for navigating this prototype:

- [Reference](./reference/)
- [An Example](./examples/arm/)
- [Code Embed Component Gallery](./embed-gallery/)

También, [en español](../es/)
1 change: 1 addition & 0 deletions src/pages/es/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ Handy links for navigating this prototype:

- [Referencía](./reference/)
- [Un Ejemplo](./examples/arm/)
- [Galería del Componente Code Embed](./embed-gallery/)

Also, [in english](../en/)
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,11 @@
resolved "https://registry.yarnpkg.com/@types/parse5/-/parse5-6.0.3.tgz#705bb349e789efa06f43f128cef51240753424cb"
integrity sha512-SuT16Q1K51EAVPz1K29DJ/sXjhSQ0zjvsypYJ6tlwVsRV9jwW5Adq2ch8Dq8kDBCkYnELS7N7VNCSB5nC56t/g==

"@types/prismjs@^1.26.2":
version "1.26.3"
resolved "https://registry.yarnpkg.com/@types/prismjs/-/prismjs-1.26.3.tgz#47fe8e784c2dee24fe636cab82e090d3da9b7dec"
integrity sha512-A0D0aTXvjlqJ5ZILMz3rNfDBOx9hHxLZYv2by47Sm/pqW35zzjusrZTryatjN/Rf8Us2gZrJD+KeHbUSTux1Cw==

"@types/prop-types@*":
version "15.7.10"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.10.tgz#892afc9332c4d62a5ea7e897fe48ed2085bbb08a"
Expand Down Expand Up @@ -5172,6 +5177,13 @@ pretty-format@^3.8.0:
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-3.8.0.tgz#bfbed56d5e9a776645f4b1ff7aa1a3ac4fa3c385"
integrity sha512-WuxUnVtlWL1OfZFQFuqvnvs6MiAGk9UNsBostyBOB0Is9wb5uRESevA6rnl/rkksXaGX3GzZhPup5d6Vp1nFew==

prism-code-editor@^2.2.4:
version "2.2.4"
resolved "https://registry.yarnpkg.com/prism-code-editor/-/prism-code-editor-2.2.4.tgz#2f7853eca8a4ed715d669e553b479f4db8847600"
integrity sha512-2qvoO6LxZ9dtjDJUlqD8Bs7cjlcCMKV5huqEog0m7wSVIBQ4+/En9WJK2HSq2NSUZV2pbXVTgd/NuFbWY9FemQ==
dependencies:
"@types/prismjs" "^1.26.2"

prismjs@^1.29.0:
version "1.29.0"
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.29.0.tgz#f113555a8fa9b57c35e637bba27509dcf802dd12"
Expand Down

0 comments on commit 1dd5281

Please sign in to comment.