Skip to content

[Preview NG] Introduce hooks to manage preview communication#3495

Draft
jeremywiebe wants to merge 3 commits intomainfrom
jer/preview-ng-5-hooks
Draft

[Preview NG] Introduce hooks to manage preview communication#3495
jeremywiebe wants to merge 3 commits intomainfrom
jer/preview-ng-5-hooks

Conversation

@jeremywiebe
Copy link
Copy Markdown
Collaborator

@jeremywiebe jeremywiebe commented Apr 15, 2026

Summary:

This PR is part of a series building a typed, hook-based preview system for the Perseus editor. The new system replaces the untyped window.iframeDataStore + raw postMessage(string) communication with structured, validated message passing via usePreviewController and usePreviewPresenter hooks. The new system is being built alongside the old one — no existing behavior changes until the final PR in the series flips the switch.

Previous PRs in this series:


Adds the two core hooks that implement the new preview communication protocol.

usePreviewController(iframeRef) is used on the editor/parent side. It sends sanitized preview data to an iframe via typed postMessage, listens for height updates back, and filters incoming messages by event.source to ensure they come from the correct iframe. Returns {sendData, height}.

usePreviewPresenter() is used inside the preview iframe. It listens for content data from the parent, sends back height reports and data requests via the typed message protocol. Throws if used outside an iframe. Returns {data, reportHeight}.

Both hooks use the message types and validators from #3474 and the data sanitizer from #3492. Purely additive — no existing behaviour is changed.

Issue: LEMS-3741

Test plan:

pnpm test packages/perseus-editor/src/preview/use-preview-controller.test.ts packages/perseus-editor/src/preview/use-preview-presenter.test.ts — 48 tests covering initialization, message sending/receiving, sanitization, event.source filtering, iframe validation, height reporting, error handling, and cleanup.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 15, 2026

🗄️ Schema Change: No Changes ✅

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 15, 2026

Size Change: 0 B

Total Size: 499 kB

ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 20.5 kB
packages/keypad-context/dist/es/index.js 1 kB
packages/kmath/dist/es/index.js 6.36 kB
packages/math-input/dist/es/index.js 98.5 kB
packages/math-input/dist/es/strings.js 1.61 kB
packages/perseus-core/dist/es/index.item-splitting.js 11.9 kB
packages/perseus-core/dist/es/index.js 25.1 kB
packages/perseus-editor/dist/es/index.js 102 kB
packages/perseus-linter/dist/es/index.js 9.3 kB
packages/perseus-score/dist/es/index.js 9.7 kB
packages/perseus-utils/dist/es/index.js 403 B
packages/perseus/dist/es/index.js 196 kB
packages/perseus/dist/es/strings.js 8.27 kB
packages/pure-markdown/dist/es/index.js 1.39 kB
packages/simple-markdown/dist/es/index.js 6.71 kB

compressed-size-action

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 15, 2026

🛠️ Item Splitting: No Changes ✅

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 15, 2026

npm Snapshot: Published

Good news!! We've packaged up the latest commit from this PR (b8f19ac) and published it to npm. You
can install it using the tag PR3495.

Example:

pnpm add @khanacademy/perseus@PR3495

If you are working in Khan Academy's frontend, you can run the below command.

./dev/tools/bump_perseus_version.ts -t PR3495

If you are working in Khan Academy's webapp, you can run the below command.

./dev/tools/bump_perseus_version.js -t PR3495

@jeremywiebe jeremywiebe force-pushed the jer/preview-ng-5-hooks branch from 16904a3 to 818c218 Compare April 15, 2026 19:20
@jeremywiebe jeremywiebe force-pushed the jer/preview-ng-4-sanitize-api-options branch from d534a50 to 0419ad1 Compare April 15, 2026 19:27
@jeremywiebe jeremywiebe force-pushed the jer/preview-ng-5-hooks branch from 818c218 to feb3f11 Compare April 15, 2026 19:27
Base automatically changed from jer/preview-ng-4-sanitize-api-options to main April 16, 2026 22:52
@jeremywiebe jeremywiebe force-pushed the jer/preview-ng-5-hooks branch from feb3f11 to 9b6a03d Compare April 16, 2026 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant