Skip to content

Configure project and asset APIs for Scratch#1347

Open
zetter-rpf wants to merge 7 commits intomainfrom
scratch-request-spike
Open

Configure project and asset APIs for Scratch#1347
zetter-rpf wants to merge 7 commits intomainfrom
scratch-request-spike

Conversation

@zetter-rpf
Copy link
Contributor

@zetter-rpf zetter-rpf commented Feb 26, 2026

Closes: https://github.com/RaspberryPiFoundation/digital-editor-issues/issues/1136

Uses APIs introduced in: RaspberryPiFoundation/editor-api#695

This uses our new APIs for loading and updating projects as well as loading and creating and assets. See commits for more.

I've also kept it so the web component test page loads assets from the web component project and doesn't depend on the API.

@zetter-rpf zetter-rpf temporarily deployed to previews/1347/merge February 26, 2026 17:11 — with GitHub Actions Inactive
Before it call the third argument 'headers' when they are not headers, but a config object that may have a headers key.
This is to allow us to drop a cookie that can be used to authorize requests made by Scratch.

Using withCredentials allows cookies to be set in the request response.
I made set up my projects locally to be served from *.rpfdev.com and this was a small thing that caused me some confusing by sending me back to the wrong host.
As part of this I've passed the project id and api url as params into the iframe.

I've used a seperate param to the existing api endpoint so it can be configured independently. This is useful for testing scratch locally in the editor UI without a dependency on the api project.
This moves the assets around to where they are expected.
This is a quick way to add it so we can that the save urls are configured correctly in different environment, we've written a separate issue to implement a save button
@zetter-rpf zetter-rpf force-pushed the scratch-request-spike branch from e68584f to b0d2d0a Compare February 27, 2026 10:15
@zetter-rpf zetter-rpf temporarily deployed to previews/1347/merge February 27, 2026 10:15 — with GitHub Actions Inactive
@zetter-rpf zetter-rpf marked this pull request as ready for review February 27, 2026 10:40
Copilot AI review requested due to automatic review settings February 27, 2026 10:40
@zetter-rpf zetter-rpf marked this pull request as draft February 27, 2026 10:40
@zetter-rpf zetter-rpf changed the title Scratch request spike Configure project and asset APIs for Scratch Feb 27, 2026
@zetter-rpf zetter-rpf marked this pull request as ready for review February 27, 2026 10:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements Scratch project integration by adding support for configurable API endpoints and query parameter-based project loading. The changes enable the Scratch editor to load projects and assets from a configurable API endpoint passed via URL parameters, moving away from hardcoded project IDs.

Changes:

  • Added scratch_api_endpoint web component attribute and Redux state management for Scratch-specific API configuration
  • Modified Scratch iframe loading to use URL query parameters (project_id and api_url) instead of hardcoded values
  • Refactored API call handler to use Axios config objects instead of separate header parameters, adding withCredentials support for project reads
  • Added Scratch-specific save button in ProjectBar with postMessage-based save triggering

Reviewed changes

Copilot reviewed 15 out of 17 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
webpack.config.js Changed CORS header logic from exact match to prefix match for scratch.html to support query parameters
src/web-component.js Added scratch_api_endpoint to the list of supported web component attributes
src/web-component.html Updated sample project link from "blank-scratch" to "cool-scratch" and added scratch_api_endpoint attribute initialization
src/utils/apiCallHandler.test.js Updated readProject tests to include withCredentials: true in expected API calls
src/utils/apiCallHandler.js Refactored to use Axios config objects, simplified headers function to return headers directly, added withCredentials support for readProject
src/scratch.jsx Changed from hardcoded project ID to URL search params for project_id and api_url
src/redux/EditorSlice.js Added setScratchApiEndpoint reducer and exported action for managing Scratch API endpoint state
src/projects/cool-scratch.json Updated project identifier and name from "blank-scratch" to "cool-scratch"
src/containers/WebComponentLoader.test.js Added test coverage for scratch_api_endpoint prop handling with default and custom values
src/containers/WebComponentLoader.jsx Added scratchApiEndpoint prop with default value and useEffect to dispatch it to Redux state
src/components/ProjectBar/ProjectBar.jsx Added Scratch-specific save button that uses postMessage to trigger save in iframe
src/components/Editor/Project/ScratchContainer.test.js Added new test file for ScratchContainer component to verify iframe src construction
src/components/Editor/Project/ScratchContainer.jsx Modified to build iframe src URL with query parameters from Redux state (project_id and api_url)
public/index.html Changed web component test page link from absolute to relative URL
public/api/scratch/projects/cool-scratch.json Added sample Scratch project JSON with basic sprite and variable configuration
public/api/scratch/assets/internalapi/asset/8a9dadf4eea61892ec6908b1c99e4961.svg/get Added SVG asset (teapot sprite) for sample Scratch project
cypress/e2e/spec-scratch.cy.js Updated Cypress test to load "cool-scratch" instead of "blank-scratch"
Comments suppressed due to low confidence (1)

src/projects/cool-scratch.json:2

  • The identifier includes the ".json" extension ("cool-scratch.json"), but other project files in the same directory (e.g., cool-python.json) use identifiers without the file extension (e.g., "cool-python"). This inconsistency could cause issues if the identifier is used to construct file paths or URLs. Consider removing the ".json" extension to maintain consistency with other project files.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

These are required for scratch to work and should always be set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants