From 9a545600229bde6ef281f1fd79257e30ba103720 Mon Sep 17 00:00:00 2001 From: evelyn masso Date: Wed, 22 May 2024 15:23:31 -0700 Subject: [PATCH] add script to pull in latest p5 version and use across website pages --- docs/scripts.md | 6 ++- package.json | 3 +- src/components/CodeEmbed/frame.tsx | 11 ++---- src/components/CodeEmbed/frameForServer.tsx | 41 +++++++++++---------- src/components/CodeEmbed/index.jsx | 11 ++---- src/content/text-detail/en/download.mdx | 14 +++++-- src/globals/globals.ts | 13 ++++++- src/globals/p5-version.ts | 1 + src/scripts/p5-version.ts | 31 ++++++++++++++++ 9 files changed, 87 insertions(+), 44 deletions(-) create mode 100644 src/globals/p5-version.ts create mode 100644 src/scripts/p5-version.ts diff --git a/docs/scripts.md b/docs/scripts.md index a7112986b9..9bff504599 100644 --- a/docs/scripts.md +++ b/docs/scripts.md @@ -18,7 +18,11 @@ To update the content on the p5.js website following a new release of p5.js, you ## Contributors List Build Script -`npm run build:contribtuors` parses the list of contributors from the all contributors [config file](https://github.com/processing/p5.js/blob/main/.all-contributorsrc) in the p5.js repo and adds entries in the [people collection](src/content/people/en) for anyone who is missing. These are used to render the list of contributors on the [People page](https://p5js.org/people/). This script will **not** remove or update existing entries, that must be done manually. +`npm run build:contributors` parses the list of contributors from the all contributors [config file](https://github.com/processing/p5.js/blob/main/.all-contributorsrc) in the p5.js repo and adds entries in the [people collection](src/content/people/en) for anyone who is missing. These are used to render the list of contributors on the [People page](https://p5js.org/people/). This script will **not** remove or update existing entries, that must be done manually. + +## Update p5 version Build Script + +`npm run build:p5-version` reads the latest version of the p5.js library (for example, "2.19.3") and updates the website config to use that version of the library for embeds and download links across the site. ## Search Indices Build Script diff --git a/package.json b/package.json index ac26b92e0f..699a808666 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "build:contributor-docs": "tsx ./src/scripts/builders/contribute.ts", "build:contributors": "tsx ./src/scripts/builders/people.ts", "build:reference": "tsx ./src/scripts/builders/reference.ts", - "build:search": "tsx ./src/scripts/builders/search.ts" + "build:search": "tsx ./src/scripts/builders/search.ts", + "build:p5-version": "tsx ./src/scripts/p5-version.ts" }, "dependencies": { "@astrojs/check": "^0.5.5", diff --git a/src/components/CodeEmbed/frame.tsx b/src/components/CodeEmbed/frame.tsx index 5f40c97609..f345471b20 100644 --- a/src/components/CodeEmbed/frame.tsx +++ b/src/components/CodeEmbed/frame.tsx @@ -1,10 +1,5 @@ import { useRef, useLayoutEffect, useEffect } 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`; +import { cdnLibraryUrl } from "@/src/globals/globals"; interface CodeBundle { css?: string; @@ -53,7 +48,7 @@ ${code.css || ""} window.addEventListener("message", event => { // Include check to prevent p5.min.js from being loaded twice const scriptExists = !!document.getElementById("p5ScriptTagInIframe"); - if (!scriptExists && event.data?.sender === '${p5LibraryUrl}') { + if (!scriptExists && event.data?.sender === '${cdnLibraryUrl}') { const p5ScriptElement = document.createElement('script'); p5ScriptElement.id = "p5ScriptTagInIframe"; p5ScriptElement.type = 'text/javascript'; @@ -136,7 +131,7 @@ export const CodeFrame = (props: CodeFrameProps) => { ); iframeRef.current.contentWindow?.postMessage( { - sender: p5LibraryUrl, + sender: cdnLibraryUrl, message: p5ScriptText, }, "*", diff --git a/src/components/CodeEmbed/frameForServer.tsx b/src/components/CodeEmbed/frameForServer.tsx index 105740d8f1..3819805c1d 100644 --- a/src/components/CodeEmbed/frameForServer.tsx +++ b/src/components/CodeEmbed/frameForServer.tsx @@ -1,10 +1,5 @@ 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`; +import { cdnLibraryUrl } from "@/src/globals/globals"; interface CodeBundle { css?: string; @@ -33,7 +28,7 @@ ${code.css || ""} ${code.htmlBody || ""} - + `.replace(/\u00A0/g, " "); export interface CodeFrameProps { @@ -63,23 +58,29 @@ export const CodeFrameForServer = (props: CodeFrameProps) => { 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) + 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 ( -
+