| 1 | --- |
| 2 | description: Main-pipeline editor stage for starting live preview and applying submitted annotations. |
| 3 | --- |
| 4 | |
| 5 | # Live Preview Stage |
| 6 | |
| 7 | > **Purpose**: (1) start/reopen the browser SVG editor when no preview service is currently running, and (2) apply user-submitted annotations after Step 7 export completes. |
| 8 | > |
| 9 | > **Not in scope**: Executor's mandatory auto-startup — that lives in [`generate-pptx`](../generate-pptx.md) Step 6. Do not re-launch a preview that is already running. |
| 10 | |
| 11 | ## When to Run |
| 12 | |
| 13 | - **Start (Step 1)** — preview service is not currently running and the user wants to look at the deck or click an element. Typical cases: post-export re-entry in a fresh chat, or the user clicked **Exit preview** earlier and now wants it back. |
| 14 | - **Apply annotations (Step 2)** — Step 7 has produced at least one PPTX, and the user signals that submitted annotations should now be applied. Triggers include: |
| 15 | - quoting the browser prompt (`Changes saved to svg_output...` / `修改已保存到 svg_output...`) |
| 16 | - saying `apply my annotations` / `apply my edits` / `应用注解` / `开始应用` / 等价表达 |
| 17 | |
| 18 | ## When NOT to Run |
| 19 | |
| 20 | - The preview service is already running → just give the user the URL; do not restart. |
| 21 | - The user gave a precise chat edit ("change page 3 title to X") → edit the SVG directly. |
| 22 | - The user wants a full regeneration → use the main workflow. |
| 23 | - Step 7 has never run for this project → annotations cannot be applied yet; finish the main pipeline first. |
| 24 | |
| 25 | --- |
| 26 | |
| 27 | ## Step 1: Start / reopen the editor |
| 28 | |
| 29 | **Precondition**: no preview service running on this project. |
| 30 | |
| 31 | ```bash |
| 32 | python3 ${SKILL_DIR}/scripts/svg_editor/server.py <project_path> --daemon |
| 33 | ``` |
| 34 | |
| 35 | (Plain mode — no `--live`. The `--live` flag is reserved for Step 6's auto-startup.) |
| 36 | |
| 37 | The launcher starts the server in the background on its selected port, waits for `GET /api/health`, records the actual pid + port in `<project_path>/live_preview/lock.json`, opens the browser when possible, and edits `<project_path>/svg_output/` in place. After it prints the running URL, tell the user in their language, in one short message: |
| 38 | |
| 39 | - editor is at the URL reported by the launcher, e.g. `http://127.0.0.1:5050` |
| 40 | - **Direct edit** (deterministic tweaks — wording, color, coordinates, SVG attributes): select an element → change the controls in the right panel → preview updates immediately, but nothing is written to `svg_output/` until **Apply changes**. `Ctrl+Z` or the **Undo** button drops staged edits step by step; applied changes are logged to `<project>/live_preview/edits.jsonl`. Re-export stays chat-driven and separate: say "re-export" / "重新导出" to refresh the PPTX. |
| 41 | - **Annotate** (changes that need AI judgement / re-layout): select an element → write the instruction, optionally starting from a quick type such as move / resize / replace image / copy / relayout → click **Add annotation** to stage it → click **Apply changes** to write annotation markers → return to the chat and say `apply my annotations` (or quote the browser prompt) |
| 42 | - to skip the editor, just describe the change in chat |
| 43 | |
| 44 | Launch immediately — the user already asked for preview. Report the actual URL from the output or project lock; never infer it from `5050`. Remote access → see the appendix. |
| 45 | |
| 46 | --- |
| 47 | |
| 48 | ## Step 2: Apply submitted annotations |
| 49 | |
| 50 | 🚧 **GATE**: `<project_path>/exports/` contains at least one `*.pptx` (Step 7 has completed). If not, do not apply annotations — tell the user to finish the main pipeline first. |
| 51 | |
| 52 | Triggered by the user signals listed in "When to Run". |
| 53 | |
| 54 | 1. Discover annotations: |
| 55 | ```bash |
| 56 | python3 ${SKILL_DIR}/scripts/check_annotations.py <project_path> |
| 57 | ``` |
| 58 | The output already lists each pending change as `file → element_id → annotation text → content preview`. Use it directly as the to-do list; no need to re-parse SVG attributes yourself. |
| 59 | 2. If the output says no annotations: tell the user, stop. |
| 60 | 3. For each listed annotation: |
| 61 | - Edit the targeted element in `<project_path>/svg_output/<file>` per the annotation text. |
| 62 | - Remove `data-edit-target` and `data-edit-annotation` from that element. |
| 63 | - Append one `annotation_applied` JSONL record to `<project_path>/live_preview/annotations.jsonl` with `ts`, `file`, `element_id`, and the original annotation text. |
| 64 | 4. Re-enter [`generate-pptx`](../generate-pptx.md) Step 7.2, wait for its success criterion, then run Step 7.3. Do not rerun Step 7.1 unless speaker notes changed. |
| 65 | 5. Tell the user (in their language): annotations applied, new PPTX exported, preview is still running. If the browser still shows the old slide, refresh or reselect the page. |
| 66 | 6. Loop: more annotations submitted → repeat from step 1. User signals done or "stop preview" → end. |
| 67 | |
| 68 | --- |
| 69 | |
| 70 | ## Notes (editor invariants — referenced from Generate Step 6) |
| 71 | |
| 72 | - **UI**: trilingual (中文 / English / 日本語); auto-detects from `navigator.language`, persists in `localStorage`, switched via the language dropdown on the right panel. The right panel is an **Edit / Annotate** surface: direct SVG edits and AI-needed annotations are visually separated, with a pending-status strip showing staged direct edits and pages with unsaved annotations. Slide navigation: first/prev/next/last buttons at the top of the center panel, plus `←` / `→` / `Home` / `End` (suppressed while typing in the annotation textarea). |
| 73 | - **Buttons**: `Add annotation` stages annotation text in memory; `Apply changes` writes staged direct edits plus annotation markers to disk and keeps the service running; `Exit preview` is the only UI action that stops Flask. |
| 74 | - **Direct edit (no AI)**: selection mode determines the right-panel surface. Single element = full object inspector (geometry, safe text content, computed text styles for the selected text node or descendant text inside a selected textbox/group, raw SVG attributes except protected fields like `id`, UI `class`, event handlers, and hrefs). SVG `<g>` group = group-level edit surface; select via `Alt/Option` + click or **Select parent group** from a child element. Multi-select = limited batch editor over top-level selected objects only: shared x/y plus `fill` / `stroke` / `opacity`; text style fields (`font-size` / `font-family` / `font-weight` / `text-anchor`) appear only when every selected object is `text`/`tspan`. Preview updates immediately; disk writes wait for **Apply changes**. |
| 75 | - **Drag to move**: press and drag an already-selected element on the canvas to reposition it (selection stays a separate click, so the background is never dragged by accident); the whole selection moves together under multi-select. The pointer delta is mapped through each element's own CTM, so moves track the cursor regardless of viewport scale or group transforms. Each release stages one direct edit per moved element (the same `x`/`y`-or-`transform` write the geometry inputs produce), previewed live and written only on **Apply changes**; dragging on empty canvas is still rubber-band selection. A failed stage rolls the canvas back to the pre-drag position. |
| 76 | - **Arrow-key nudge**: with one or more elements selected, `↑ ↓ ← →` moves the selection 1px and `Shift + arrow` moves 10px (suppressed while typing in the annotation box). Arrow keys navigate slides only when nothing is selected. Same staging/coalescing path as drag, so a burst of nudges collapses to one undo step. |
| 77 | - **Overlap picker**: right-click anywhere on the canvas to list every selectable element under the pointer (top→bottom), so stacked shapes can be reached without blind cycling. Left-click is unchanged (selects the topmost). Hovering a row highlights that element; clicking it selects it; `Esc` or an outside click closes the list. With exactly one element under the pointer, right-click selects it directly. |
| 78 | - **Undo**: `Ctrl+Z` or the **Undo** button drops the last staged direct edit on the current slide (per-slide LIFO, this session). Consecutive edits to the *same element and same field set* (e.g. nudging one color or coordinate several times) coalesce into a single undo step, keeping the original pre-edit value; switching element or field starts a new step. Applied old→new history is appended to `<project>/live_preview/edits.jsonl`; annotation save/update/remove history is appended to `<project>/live_preview/annotations.jsonl`; un-applied staged edits are in-memory only. |
| 79 | - **Unsaved-work guard**: staged direct edits and annotation changes (added or removed) live in server memory until **Apply changes**; closing the tab triggers the browser's native "leave site?" prompt while any are unapplied, since an idle timeout or process kill would drop them. |
| 80 | - **Re-export is chat-driven**: applying changes updates `svg_output/` only. Refreshing the PPTX (finalize + svg_to_pptx) stays a chat step — the editor never runs the export pipeline or presents browser-side export as part of applying edits. |
| 81 | - **Stop conditions**: the service stops when the user clicks **Exit preview** in the browser, asks in chat to stop it, the idle timeout fires, or the process is killed externally. |
| 82 | - **Port**: without `--port`, use the first free port from `5050`; `--port N` binds `N` strictly and fails if unavailable. Read the actual URL from launch output or `<project_path>/live_preview/lock.json`. |
| 83 | - **Idle timeout**: plain mode `900s`, `--live` mode `7200s`; override with `--timeout <seconds>` (`0` disables). |
| 84 | - **Single instance per project**: `<project_path>/live_preview/lock.json` records the running pid + actual port and is the discovery source for project-local consumers. A second launch reuses the live instance unless an explicit, different `--port N` was requested; that mismatch fails and requires `--shutdown` before restart. Stale locks (dead pid) are overwritten on the next launch. Legacy root locks at `<project_path>/.live_preview.lock` are still detected when they point to a live process. |
| 85 | - **Transient ids**: each element gets a temporary `_edit_N` id while the editor is running. On save, only annotated elements keep their id; unannotated `_edit_N` ids are stripped before write-back. |
| 86 | - **Browser preview**: the server inlines `<use data-icon>` placeholders and serves `images/*` so SVG renders correctly; the on-disk SVG is unchanged by this preview. |
| 87 | |
| 88 | --- |
| 89 | |
| 90 | ## Appendix: Remote access |
| 91 | |
| 92 | If the project lives on a remote Linux server, run with `--no-browser`: |
| 93 | |
| 94 | ```bash |
| 95 | python3 ${SKILL_DIR}/scripts/svg_editor/server.py <project_path> --daemon --no-browser |
| 96 | # or for Step 6's auto-startup on a remote host: |
| 97 | python3 ${SKILL_DIR}/scripts/svg_editor/server.py <project_path> --live --daemon --no-browser |
| 98 | ``` |
| 99 | |
| 100 | Let `<P>` be the port in launch output or `<project_path>/live_preview/lock.json`: |
| 101 | |
| 102 | - **VS Code / Cursor Remote-SSH**: in the **PORTS** panel, forward `<P>`. |
| 103 | - **Termius**: add a Local rule with Binding and Destination both `127.0.0.1:<P>`, then start it. |
| 104 | - **Plain SSH**: `ssh -L <P>:127.0.0.1:<P> <user>@<host>`. |
| 105 | |
| 106 | Then open `http://localhost:<P>` locally. |
| 107 |