docs: OpenSpec change add-page-visual-editor
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
a0691188ca
commit
55d9535ef2
5 files changed
+338
No files matched your search
@@ -0,0 +1,2 @@
|
||||
schema: spec-driven
|
||||
created: 2026-12-03
|
||||
@@ -0,0 +1,107 @@
|
||||
# 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 canvas height follows the frame's content.
|
||||
- **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.
|
||||
@@ -0,0 +1,50 @@
|
||||
# Proposal: add-page-visual-editor
|
||||
|
||||
## Why
|
||||
|
||||
INS and CV marketing staff build a page for every event and are not technical. Today they fill in a form of blocks and check a separate preview of the last saved version. The owner asked for a Wix-like editor: staff click the page itself to change it and see the result as they work. This is change 3 of the page builder (`docs/superpowers/specs/2026-12-03-page-builder-design.md`). Changes 1 (sections) and 2 (drafts, templates, Duplicate) are live.
|
||||
|
||||
## What Changes
|
||||
|
||||
- **On-page editor** at 1024 px and wider. The page renders in a canvas inside the editor, at the desktop width scaled to fit, or at phone width (390 px). It uses the same `LandingPage` rendering as visitors see. A side panel shows the selected section's settings, or the page settings when no section is selected.
|
||||
- **Edit in place.**
|
||||
- Headings, short texts, button labels, card titles and texts, and FAQ questions and answers are edited as plain text on the canvas, with the server's length limits.
|
||||
- Rich text (Text, and Image with text) is edited in place with a small toolbar.
|
||||
- Clicking an image spot opens the file picker.
|
||||
- **Section actions.** Select, a toolbar (drag handle, up, down, duplicate, delete), and "+ Add section" between sections and at the end.
|
||||
- **Undo and redo** for the page, up to 50 steps, with Ctrl+Z, Ctrl+Shift+Z and buttons.
|
||||
- **Change 2 carried over.** The editor's top bar keeps Save, Preview, View page, Duplicate, Delete, the unpublished-changes banner with Publish changes and Discard changes, and the status. Templates and Duplicate open in the new editor.
|
||||
- **Refusals on the canvas.** A refused field's section is selected and outlined, and the panel shows the message at the field.
|
||||
- **Unsaved changes.** In-app links now ask before leaving with unsaved changes, in both editors. Today only reload and close ask.
|
||||
- **Below 1024 px** the current form editor stays, with Preview.
|
||||
|
||||
### Departures from the approved design doc
|
||||
|
||||
These are builder calls, recorded in `design.md`:
|
||||
|
||||
1. **Canvas wiring.** The editor renders the page into a blank same-origin frame itself, instead of loading a canvas address that exchanges the draft by `postMessage`. There is one copy of the draft and no message protocol. A spike task proves it first, with a canvas route as the named fallback.
|
||||
2. **Desktop view** renders at 1280 px and is scaled down to fit beside the panel, so the page's own breakpoints match a desktop visitor's. "Full width" would show the tablet layout on a laptop.
|
||||
3. **Custom HTML** on the canvas shows the last saved version, with a "Save to update" mark once its HTML is edited. It reloads after Save. Unsaved pasted HTML cannot run sealed with today's frame routes and script policy.
|
||||
|
||||
## Capabilities
|
||||
|
||||
### New Capabilities
|
||||
|
||||
None.
|
||||
|
||||
### Modified Capabilities
|
||||
|
||||
- `landing-pages`:
|
||||
- The Page Editor, Saving And Deleting In The Editor, The Custom HTML Block In The Editor, and New Sections In The Editor change.
|
||||
- The on-page editor requirements are added: canvas, editing in place, section actions, undo, and refusals on the canvas.
|
||||
|
||||
## Impact
|
||||
|
||||
- **Portal only:**
|
||||
- New: `app/components/pages/visual/*` (canvas, panel, top bar, history).
|
||||
- `PageEditor` state moves into a shared hook used by both editors.
|
||||
- `CmsBlocks` gains edit-mode hooks, which the public page does not use.
|
||||
- `HtmlFrame` listens on the window of the document it is in.
|
||||
- A leave guard for in-app links.
|
||||
- **No server, API, database or SQL changes.** New labels only, with a label SQL file.
|
||||
- **No new dependencies.** Tiptap is already installed; dragging uses native drag-and-drop.
|
||||
@@ -0,0 +1,127 @@
|
||||
## MODIFIED Requirements
|
||||
|
||||
### Requirement: The Page Editor
|
||||
|
||||
At 1024 px and wider the editor SHALL be the on-page editor. Below 1024 px it SHALL be the form editor. Both SHALL offer the title, a slug filled from the title until edited, a meta description with a 160-character counter, the status, adding, moving and removing blocks, and images uploaded only on save. A FORM block SHALL pick from the company's enabled events that have not ended.
|
||||
|
||||
#### Scenario: Slug follows the title until edited
|
||||
|
||||
- **GIVEN** a new page
|
||||
- **WHEN** staff type the title "Hotel Technology"
|
||||
- **THEN** the slug becomes `hotel-technology`
|
||||
|
||||
#### Scenario: Narrow window keeps the form
|
||||
|
||||
- **WHEN** staff open a page in a window 900 px wide
|
||||
- **THEN** the form editor shows, with Preview
|
||||
|
||||
### Requirement: Saving And Deleting In The Editor
|
||||
|
||||
Server field errors SHALL show at their fields, keeping all entered values. After a save the editor SHALL offer a link to view the page. Deleting a page SHALL ask first. With unsaved edits, reloading, closing and following an in-app link SHALL ask first.
|
||||
|
||||
#### Scenario: Server error at its field
|
||||
|
||||
- **WHEN** staff save a HERO button link `javascript:alert(1)`
|
||||
- **THEN** the error shows at that button link and every entered value stays
|
||||
|
||||
#### Scenario: Leaving by an in-app link
|
||||
|
||||
- **GIVEN** staff changed a heading and did not save
|
||||
- **WHEN** they click Pages in the menu
|
||||
- **THEN** the editor asks before leaving, and Cancel keeps them on the page with the change
|
||||
|
||||
### Requirement: The Custom HTML Block In The Editor
|
||||
|
||||
The editor SHALL offer adding a Custom HTML block, edited in a monospace text box with a 200000-character counter and a note that forms inside it do not send. On the canvas the block SHALL show its last saved version as a sealed frame, marked "Save to update" while its HTML has unsaved edits. The editor SHALL offer a "hide page header" switch.
|
||||
|
||||
#### Scenario: Adding a Custom HTML block
|
||||
|
||||
- **WHEN** staff add a Custom HTML block and paste HTML into it
|
||||
- **THEN** the block shows the pasted HTML, the character count and the note about forms
|
||||
|
||||
#### Scenario: Unsaved HTML on the canvas
|
||||
|
||||
- **GIVEN** a saved page with a Custom HTML block
|
||||
- **WHEN** staff change its HTML in the panel
|
||||
- **THEN** the canvas marks the block "Save to update"
|
||||
- **AND** after Save the canvas shows the new HTML
|
||||
|
||||
#### Scenario: Hiding the header
|
||||
|
||||
- **WHEN** staff turn on "hide page header" and save
|
||||
- **THEN** reading the page returns "hide header" on
|
||||
|
||||
### Requirement: New Sections In The Editor
|
||||
|
||||
Both editors SHALL offer adding FEATURES, STEPS, TRUST and CTA blocks, editing their fields, adding, removing and reordering cards, and choosing any block's look. Refusals SHALL show at the field they name, including card fields.
|
||||
|
||||
#### Scenario: A refused card title shows at its field
|
||||
|
||||
- **GIVEN** staff editing a page with a FEATURES block
|
||||
- **WHEN** the save is refused on field `blocks[0].cards[1].title`
|
||||
- **THEN** the message shows at the second card's title field
|
||||
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: The Page Canvas
|
||||
|
||||
The on-page editor SHALL show the page on a canvas, rendered as visitors see it, with a Desktop view (1280 px wide, scaled to fit) and a Phone view (390 px wide). Changes SHALL show on the canvas as they are made, before saving. Links and buttons on the canvas SHALL NOT navigate, and the sign-up form SHALL show its preview note.
|
||||
|
||||
#### Scenario: Phone view uses the phone layout
|
||||
|
||||
- **GIVEN** a page with a FEATURES block of three cards
|
||||
- **WHEN** staff switch to Phone
|
||||
- **THEN** the cards stack in one column on the canvas
|
||||
|
||||
#### Scenario: A change shows before saving
|
||||
|
||||
- **WHEN** staff change a FEATURES card title in the side panel
|
||||
- **THEN** the canvas shows the new title at once
|
||||
|
||||
### Requirement: Edit Text On The Page
|
||||
|
||||
Clicking a heading, short text, button label, card title or text, or FAQ question or answer on the canvas SHALL edit it in place as plain text, with the server's length limit shown when close. Rich text SHALL be edited in place with bold, italic, link, lists and alignment, and links SHALL follow the page link rule. Clicking an image spot SHALL open the file picker.
|
||||
|
||||
#### Scenario: Edit a heading in place
|
||||
|
||||
- **WHEN** staff click the hero heading on the canvas and type "Hotel week"
|
||||
- **THEN** the canvas and the side panel show "Hotel week"
|
||||
|
||||
#### Scenario: An unsafe link is refused in rich text
|
||||
|
||||
- **WHEN** staff add the link `javascript:alert(1)` in a Text block on the canvas
|
||||
- **THEN** the link is not added and the toolbar says why
|
||||
|
||||
### Requirement: Section Selection And Actions
|
||||
|
||||
Clicking a section on the canvas SHALL select it, outline it and show its settings in the side panel. With none selected, the panel SHALL show the page settings. A selected section SHALL offer drag, up, down, duplicate and delete. "Add section" between sections and at the end SHALL offer every block type. Up and down SHALL work from the keyboard.
|
||||
|
||||
#### Scenario: Duplicate a section
|
||||
|
||||
- **WHEN** staff select the second section and press Duplicate
|
||||
- **THEN** a copy appears as the third section and is selected
|
||||
|
||||
#### Scenario: Add a section between two
|
||||
|
||||
- **WHEN** staff press Add section between the first and second sections and choose Steps
|
||||
- **THEN** a Steps section appears second and is selected
|
||||
|
||||
### Requirement: Undo And Redo
|
||||
|
||||
The on-page editor SHALL undo and redo page changes, up to 50 steps, by buttons and by Ctrl+Z and Ctrl+Shift+Z. Typing in one field SHALL be one step. Saving SHALL NOT clear the history.
|
||||
|
||||
#### Scenario: Undo a delete
|
||||
|
||||
- **GIVEN** staff deleted the FAQ section
|
||||
- **WHEN** they press Ctrl+Z
|
||||
- **THEN** the FAQ section is back in its place with its content
|
||||
|
||||
### Requirement: Refusals On The Canvas
|
||||
|
||||
When a save or publish is refused on a block field, the editor SHALL select that block, outline it, scroll it into view and show the message at the field in the side panel. A refusal on a page field SHALL show in the page settings.
|
||||
|
||||
#### Scenario: A form without an event
|
||||
|
||||
- **GIVEN** a page from the Event sign-up template
|
||||
- **WHEN** staff save without choosing the form's event
|
||||
- **THEN** the form section is selected and outlined and the panel shows the message at the Event field
|
||||
@@ -0,0 +1,52 @@
|
||||
# Tasks: add-page-visual-editor
|
||||
|
||||
Design: `design.md`. Every task is TDD. Work is serial in the main session, with a self-review per task.
|
||||
|
||||
- Verify (portal): `cd solvway-admin-portal && npx vitest run; echo VITEST=$? && npx tsc --noEmit && npm run lint`
|
||||
|
||||
## 1. Foundations
|
||||
|
||||
- [ ] 1.1 **Spike, kept only if it passes:** render `LandingPage` into a blank frame by portal, with copied styles and theme. It passes when all of these work, in `next dev` and in one `next build` + `next start`:
|
||||
- Tailwind breakpoints follow the frame width (390 vs 1280).
|
||||
- Fonts and dark mode look right.
|
||||
- A Tiptap editor inside the frame types, selects and applies bold.
|
||||
- A `contentEditable` field keeps its caret while typing.
|
||||
- Native drag events fire.
|
||||
- A Custom HTML frame inside it reports its height.
|
||||
|
||||
On failure, stop and switch to the canvas-route fallback (design Decision 1), noting why.
|
||||
- [ ] 1.2 `HtmlFrame` listens on its own document's window (test: a frame inside another document still resizes).
|
||||
- [ ] 1.3 Move `PageEditor` state and actions into `usePageDraft`, with no behaviour change. All existing `PageEditor*.test.tsx` stay green and unchanged.
|
||||
- [ ] 1.4 Leave guard for in-app links in the form editor (test: an in-app link click with unsaved edits asks; Cancel stays, Leave navigates).
|
||||
|
||||
## 2. Canvas
|
||||
|
||||
- [ ] 2.1 `PageCanvas`: the frame, the Desktop view (1280 px scaled) and the Phone view (390 px), live updates from the draft, and links and buttons inert.
|
||||
- [ ] 2.2 Edit-mode hooks in `CmsBlocks`: `EditContext`, `EditableText` for every plain text field with its server limit, image spots, `Section` outline and `data-index`. Public rendering stays unchanged: the existing `CmsBlocks*.test.tsx` stay green.
|
||||
- [ ] 2.3 Rich text in place: `RichTextEditor` bare mode with a floating toolbar (test: an unsafe link is refused with a message).
|
||||
- [ ] 2.4 Custom HTML on the canvas: the saved-draft frame, the "Save to update" mark, and a reload after save.
|
||||
|
||||
## 3. Editor
|
||||
|
||||
- [ ] 3.1 `VisualEditor` layout:
|
||||
- The top bar: title, status, Desktop and Phone, undo and redo, Duplicate, Preview, View page, Delete, Save.
|
||||
- The change 2 banner with Publish changes and Discard changes.
|
||||
- The side panel: `BlockFields` for the selected block, or the page settings.
|
||||
- The editor is chosen by width, 1024 px and up.
|
||||
- [ ] 3.2 Selection and section actions: select, toolbar (up, down, duplicate, delete), "Add section" between sections and at the end, native drag reorder (reorder function unit-tested), and the keyboard path.
|
||||
- [ ] 3.3 Undo and redo: history of 50, one step per field edit and per action, Ctrl+Z and Ctrl+Shift+Z, kept across saves.
|
||||
- [ ] 3.4 Refusals on the canvas: select, outline, scroll into view, message at the panel field; page-field refusals in the page settings.
|
||||
- [ ] 3.5 Templates (`?template=`) and Duplicate (`?from=`) open in the visual editor.
|
||||
- [ ] 3.6 Labels in `defaultMessages.ts` and `zh_CN.seed.json`, `npm run i18n:sql`, `sql/01-visual-editor-labels.sql`, `sql/99-rollback.sql`.
|
||||
|
||||
## 4. Release
|
||||
|
||||
- [ ] 4.1 `docs/deployment.md` §12: the on-page editor for staff, the narrow-window form, and Custom HTML showing after Save.
|
||||
- [ ] 4.2 Live check with Playwright (snapshot, find and screenshots only; no `eval`), on disposable pages, at 1280 px and at 900 px for the form, with Desktop and Phone views:
|
||||
- Create from a template, edit headings and a card in place, change a look in the panel, and add, duplicate, move and delete sections.
|
||||
- Undo and redo.
|
||||
- Save without an event: the form is outlined with the message at Event.
|
||||
- Choose the event and save. Publish changes.
|
||||
- Rich text with a safe link and a refused unsafe one.
|
||||
- A Custom HTML block: the "Save to update" mark, then the new HTML after save.
|
||||
- Leaving by a menu link with unsaved changes.
|
||||
Reference in new issue
Block a user