-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loads p5.min.js
for CodeFrame
iframes using manually created script
#224
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ada9cf
Address #213 by update how iframes created by CodeFrame make use of p…
howard-e 17b5a2f
Merge branch 'main' into issue-213
howard-e 3f29425
Handle fetch error
howard-e 35f7a9b
Merge branch 'issue-213' of github.com:bocoup/p5.js-website into issu…
howard-e 4b4be8d
Additional frame for temporarily supporting async loads like with Ske…
howard-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { useRef, useLayoutEffect } from "preact/hooks"; | ||
import { p5VersionForEmbeds } from "@/src/globals/globals"; | ||
|
||
/* | ||
* Url to fetch the p5.js library from | ||
*/ | ||
const p5LibraryUrl = `https://cdnjs.cloudflare.com/ajax/libs/p5.js/${p5VersionForEmbeds}/p5.min.js`; | ||
|
||
interface CodeBundle { | ||
css?: string; | ||
htmlBody?: string; | ||
js?: string; | ||
base?: string; | ||
} | ||
|
||
/* | ||
* Wraps the given code in a html document for display. | ||
* Single object argument, all properties optional: | ||
*/ | ||
const wrapInMarkup = (code: CodeBundle) => | ||
`<!DOCTYPE html> | ||
<meta charset="utf8" /> | ||
<base href="${code.base || "/assets/"}" /> | ||
<style type='text/css'> | ||
html, body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
canvas { | ||
display: block; | ||
} | ||
${code.css || ""} | ||
</style> | ||
<body>${code.htmlBody || ""}</body> | ||
<script id="code" type="text/javascript">${code.js || ""}</script> | ||
<script src="${p5LibraryUrl}"></script> | ||
`.replace(/\u00A0/g, " "); | ||
|
||
export interface CodeFrameProps { | ||
jsCode: string; | ||
cssCode?: string; | ||
htmlBodyCode?: string; | ||
height?: number | string; | ||
width?: number | string; | ||
base?: string; | ||
} | ||
|
||
/* | ||
* Component that uses an iframe to run code with the p5 library included. | ||
* | ||
*/ | ||
export const CodeFrameForServer = (props: CodeFrameProps) => { | ||
const iframeRef = useRef<HTMLIFrameElement>(null); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
// For performance, set the iframe to display:none when | ||
// not visible on the page. This will stop the browser | ||
// from running `draw` every frame, which helps performance | ||
// on pages with multiple sketches, and causes sketch | ||
// animations only to start when they become visible. | ||
useLayoutEffect(() => { | ||
if (!containerRef.current) return; | ||
const { IntersectionObserver } = window; | ||
if (!IntersectionObserver) return; | ||
|
||
const observer = new IntersectionObserver((entries) => { | ||
entries.forEach(entry => { | ||
if (!iframeRef.current) return; | ||
if (entry.isIntersecting) { | ||
iframeRef.current.style.removeProperty('display'); | ||
} else { | ||
iframeRef.current.style.display = 'none'; | ||
} | ||
}); | ||
}, { rootMargin: '20px' }); | ||
observer.observe(containerRef.current) | ||
|
||
return () => observer.disconnect(); | ||
}, []); | ||
|
||
return ( | ||
<div ref={containerRef} style={{ width: props.width, height: props.height }}> | ||
<iframe | ||
ref={iframeRef} | ||
srcDoc={wrapInMarkup({ | ||
js: props.jsCode, | ||
css: props.cssCode, | ||
htmlBody: props.htmlBodyCode, | ||
base: props.base, | ||
})} | ||
sandbox="allow-scripts allow-popups allow-modals allow-forms" | ||
aria-label="Code Preview" | ||
title="Code Preview" | ||
height={props.height} | ||
width={props.width} | ||
/> | ||
</div> | ||
); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition!