Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
108 lines
7.6 KiB
Markdown
108 lines
7.6 KiB
Markdown
# Design: add-page-visual-editor
|
|
|
|
## Context
|
|
|
|
- **Design doc:** `docs/superpowers/specs/2026-12-03-page-builder-design.md`, change 3. Changes 1 and 2 are live: section types and looks, draft and live content, templates, and Duplicate.
|
|
- **Today's editor** is `app/components/pages/PageEditor.tsx`, about 415 lines. It holds all page state and actions:
|
|
- load, the draft, `show`, images uploaded on save;
|
|
- save, publish changes, discard and delete;
|
|
- mapping refusals to fields.
|
|
- **Block fields** are edited by `BlockFields.tsx`.
|
|
- **Rendering.** The public page and the preview render with `LandingPage` and `Block` in `CmsBlocks.tsx`. Custom HTML renders through `HtmlFrame`, a sealed frame loaded from a frame address that skips the nonce policy.
|
|
- **Page policy.** The baseline CSP sets no `style-src`. Scripts need the per-request nonce.
|
|
|
|
## Goals / Non-Goals
|
|
|
|
**Goals**
|
|
- Edit the page on the page, with Desktop and Phone views.
|
|
- Text editing in place, section actions and undo.
|
|
- Change 2's draft controls and entries kept.
|
|
- The form editor kept below 1024 px.
|
|
|
|
**Non-Goals**
|
|
- Free placement or per-element styling.
|
|
- Version history.
|
|
- Editing on phones.
|
|
- Live rendering of unsaved Custom HTML.
|
|
- Any server or API change.
|
|
|
|
## Decisions
|
|
|
|
### 1. The canvas is a blank same-origin frame that the editor renders into
|
|
|
|
The editor mounts `<iframe>` with no `src` (about:blank, same origin), then renders the page into its body with `createPortal`.
|
|
|
|
**Styles and theme.**
|
|
- The `<link rel=stylesheet>` and `<style>` elements in the editor's `<head>` are cloned into the frame. A MutationObserver keeps them in step when `next dev` swaps them.
|
|
- `<html>`'s `class`, `data-theme` and `style` are copied, which carries the Inter font variable, the dark class and the variables.
|
|
|
|
**Why the frame.** It has its own viewport, so `sm:`, `md:` and `lg:` respond to the canvas width, as on a visitor's device. React context (messages, auth) and events work through the portal, and there is a single copy of the draft.
|
|
|
|
**Rejected: a canvas address that exchanges the draft by `postMessage`,** as the design doc sketched. It needs a sync protocol both ways, origin checks and a second copy of the draft, which makes undo and in-place editing harder.
|
|
|
|
**Fallback.** Task 1 is a spike with pass criteria. If it fails (Tiptap inside the frame, focus, drag events, or production CSS), use a canvas route `/pages/canvas` that reads the draft and posts edits back. Only the canvas wiring changes.
|
|
|
|
### 2. Desktop is 1280 px, scaled
|
|
|
|
- **Desktop** renders the frame 1280 px wide and scales it with a CSS transform to the width left beside the panel. The frame is one screen tall and scrolls inside, like a device (the spike found that growing it to its content loops, since a landing page is at least one screen tall: `min-h-svh`).
|
|
- **Phone** is 390 px at 1:1.
|
|
- Outlines, toolbars and "Add section" buttons are drawn inside the frame document, so they scale with the page.
|
|
|
|
### 3. Edit-mode hooks in `LandingPage`
|
|
|
|
- An optional `edit` prop on `LandingPage` provides an `EditContext`. Without it, rendering is unchanged, and the public page and preview never pass it.
|
|
- **Plain text fields.** In edit mode, `Block` renders text fields through `<EditableText field=… max=…>`: a `contentEditable="plaintext-only"` element. It reports input on each keystroke and is never re-rendered from props while it has focus, so the caret stays.
|
|
- **Rich text** (RICH_TEXT and IMAGE_TEXT html) renders through the existing `RichTextEditor`. A `bare` option adds a floating toolbar and drops the boxed look. It uses the same `isSafeLink` rule.
|
|
- **Images.** An image spot is a button that opens a file input in the editor document. The chosen file previews from a data URL, as today.
|
|
- **`Section`** gains `data-index`, an outline when hovered or selected, and the toolbar. Gaps get "+ Add section".
|
|
- **Clicks.** Clicks on links, buttons and the form are prevented in edit mode. The FORM block already renders its preview note.
|
|
|
|
### 4. `HtmlFrame` listens on its own window
|
|
|
|
- It adds its message listener to `ref.current.ownerDocument.defaultView`. Inside the canvas, the sealed frame's parent is the canvas window, so the height and anchor messages arrive there.
|
|
- The canvas passes `frameBase` = the preview frame address (saved draft). Changing a block's HTML sets "Save to update" on that block until the next save. After Save, the frame reloads with a cache-busting `?v=<save count>`.
|
|
|
|
### 5. Shared state: `usePageDraft`
|
|
|
|
`PageEditor`'s state and actions move, unchanged, into `usePageDraft(pageId, fromId, initialBlocks)`. Both editors use it. This is a refactor guarded by the existing editor tests. The visual editor adds:
|
|
|
|
- **`selected` index** (or none).
|
|
- **History.** Arrays of `{ draft, rows }` snapshots, up to 50.
|
|
- A text field pushes one snapshot when it gains focus and changes.
|
|
- A structural action pushes one before it applies.
|
|
- Undo and redo replace the state and blur the focused editable first, so a focused field is re-rendered from the restored value.
|
|
- Ctrl+Z inside a rich-text field is Tiptap's own undo while that field has focus. Page undo applies after blur.
|
|
- **Refusal mapping.** A refused field `blocks[i].…` sets `selected = i` and scrolls the section into view in the frame. The panel then shows `BlockFields` errors as today.
|
|
|
|
### 6. Layout
|
|
|
|
```
|
|
┌ ← Pages Title [status] [Desktop|Phone] ↶ ↷ [Duplicate][Preview][View page][Delete] [Save] ┐
|
|
├ unpublished-changes banner (Publish changes / Discard changes), as in change 2 ──────────────────┤
|
|
├──────────────────── canvas ───────────────────────────────────┬──── panel (360 px) ────────────┤
|
|
```
|
|
|
|
- **Panel.** It shows `BlockFields` for the selected block, which already covers every field, the look and the link name. With nothing selected it shows the page settings card and the status card from today's form.
|
|
- **Which editor.** `matchMedia("(min-width: 1024px)")` chooses the editor. Resizing across the line switches editors with the state kept, since both use the same hook.
|
|
|
|
### 7. Leave guard for in-app links
|
|
|
|
A capture-phase click listener on `document`, active while there are unsaved changes, catches same-origin `<a href>` clicks that would leave the page. It shows the existing `ConfirmDialog`; Leave then calls `router.push`. This replaces the gap noted in `PageEditor` (`ponytail:` comment). Browser back is not guarded; this is named in the comment.
|
|
|
|
### 8. Drag and drop
|
|
|
|
Drag and drop uses native HTML5 events on the section handle inside the frame, with a drop line between sections. Keyboard users use up and down. jsdom cannot run native drag-and-drop, so the reorder function is unit-tested and the drag itself is checked live.
|
|
|
|
## Risks / Trade-offs
|
|
|
|
- **Tiptap or focus inside a portal frame.** ProseMirror reads `ownerDocument` in most places, but this is unverified for selection handling in a frame. The spike covers it, and the fallback is in Decision 1.
|
|
- **Style cloning in production.** A production build serves hashed CSS links, which clone fine. The spike checks a `next build` once, not only `next dev`.
|
|
- **Two editors share one hook.** Each keeps its own tests. The form editor stays the narrow-window path, and the fallback if the canvas misbehaves.
|
|
- **Saved-only Custom HTML on the canvas.** Accepted: staff paste HTML rarely. The marker says what they see.
|
|
|
|
## Migration Plan
|
|
|
|
- Portal-only deploy, then run `sql/01-visual-editor-labels.sql`.
|
|
- Rollback: the previous portal image, then `sql/99-rollback.sql` (labels).
|
|
- No data change.
|