Skip to content

feat(editor): prepare preview documents before mounting - #9

Merged
aryasaatvik merged 2 commits into
mainfrom
fix/editor-preview-document-preparation
Oct 5, 2026
Merged

aryasaatvik merged 2 commits into
mainfrom
fix/editor-preview-document-preparation

Conversation

@aryasaatvik

@aryasaatvik aryasaatvik commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Hosts can prepare preview HTML before the attached iframe parser sees it, then mount document resources before measurement and paint. Canvas and Preview share this optional seam, including forced light/dark schemes.

import { EditorProvider, EditorShell, type PreparedPreviewDocument } from "@samva/editor/shell";

const preparePreviewDocument = (html: string): PreparedPreviewDocument => ({
  html: prepareHtml(html),
  mount: (document) => mountResources(document), // returns cleanup for this document
});

<EditorProvider host={host} preparePreviewDocument={preparePreviewDocument}>
  <EditorShell />
</EditorProvider>;
  • Preparation is pure and synchronous; canvas passes host HTML and Preview applies its forced scheme first. Only returned HTML is written. Mount runs after document.close() and before measuring or mounting canvas overlays.
  • Each mounted document owns its cleanup, including replacement, unmount, and StrictMode replay. Changing callback identity replaces frames without resetting the editor session.
  • Omitted preparation retains existing behavior. Exported HTML remains the host's output; preview policy stays host-owned.

Chromium coverage includes parser-level font request and CSP-event probes, with an unprepared positive control, and exact resource ownership through StrictMode, revisions, callback replacement, selection overlays, Preview schemes, omission, and unmount. The packed shell is consumed through its public export at runtime and with NodeNext types.

@aryasaatvik
aryasaatvik marked this pull request as ready for review October 5, 2026 05:00
@greptile-apps

greptile-apps Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Adds optional preview document preparation hook to editor.

The PR appears safe to merge; no actionable new issue or outstanding previous finding was identified.

What we checked:

  • Fixture revision cannot inject HTML: The dev host makes revision strings from its own counter; it does not copy user content into them.
  • Replaced frames release resources: The frame's effect keeps the cleanup returned by mount and calls it when that frame is removed.

Summary

This PR lets hosts prepare iframe HTML before parsing it and mount document resources before the editor measures the frame.

  • Canvas and Preview share the optional preparation callback; Preview applies its forced scheme first.
  • Frame replacement, unmount, and StrictMode replay run document-specific cleanup.
  • Unit and browser tests cover parsing order, schemes, and resource ownership.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Host HTML] --> B{Preview scheme?}
  B --> C[Prepare HTML]
  C --> D[Write and close iframe document]
  D --> E[Mount resources]
  E --> F[Measure frame and mount overlays]
  F --> G[Clean up on replacement or unmount]
Loading

Reviews (2) · Last reviewed commit: "fix(editor): prepare forced schemes befo..."

@aryasaatvik
aryasaatvik force-pushed the fix/editor-preview-document-preparation branch from a438dde to 5acc21e Compare October 5, 2026 06:38
@pkg-pr-new

pkg-pr-new Bot commented Oct 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

bun add https://pkg.pr.new/@samva/editor@9
bun add https://pkg.pr.new/@samva/markup@9
bun add https://pkg.pr.new/@samva/vite@9

commit: 5acc21e

@aryasaatvik
aryasaatvik merged commit 3bd6a5e into main Oct 5, 2026
4 checks passed
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.

1 participant