@deskwork/studio 0.14.1 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,710 @@
1
+ /**
2
+ * Per-post review page — `/dev/editorial-review/:slug`.
3
+ *
4
+ * Renders one workflow's current draft inside a margin-note review
5
+ * shell. The body markdown is rendered server-side (so the operator
6
+ * sees content immediately, no flash of unrendered text), with the
7
+ * frontmatter description injected as a dek paragraph after the
8
+ * body's repeated H1.
9
+ *
10
+ * Ported from `pages/dev/editorial-review/[slug].astro`. Differences:
11
+ * - The full <BlogLayout> Astro component went away; we render the
12
+ * review shell as the page body. Site-specific blog chrome was
13
+ * never useful in the review surface (the review is about the
14
+ * prose, not the page chrome).
15
+ * - Site defaults to `config.defaultSite` rather than the hardcoded
16
+ * `'editorialcontrol'` upstream used.
17
+ * - The outline-split helper lives under the plugin tree's `public/src/`
18
+ * (it's a browser module) but it's pure TS, so it can run server-side
19
+ * for the initial render too. After the marketplace-install fix
20
+ * (issue #4), `public/` was relocated from packages/studio/ into
21
+ * plugins/deskwork-studio/, hence the long relative import below.
22
+ */
23
+
24
+ import { handleGetWorkflow } from '@deskwork/core/review/handlers';
25
+ import type {
26
+ DraftVersion,
27
+ DraftWorkflowItem,
28
+ } from '@deskwork/core/review/types';
29
+ import {
30
+ parseDraftFrontmatter,
31
+ renderMarkdownToHtml,
32
+ } from '@deskwork/core/review/render';
33
+ import { readCalendar } from '@deskwork/core/calendar';
34
+ import { findEntry, findEntryById } from '@deskwork/core/calendar-mutations';
35
+ import type { CalendarEntry } from '@deskwork/core/types';
36
+ import type { ContentIndex } from '@deskwork/core/content-index';
37
+ import { splitOutline } from '@deskwork/core/outline-split';
38
+ import type { StudioContext } from '../routes/api.ts';
39
+ import { html, unsafe, type RawHtml } from './html.ts';
40
+ import { layout } from './layout.ts';
41
+ import { renderEditorialFolio } from './chrome.ts';
42
+ import { escapeHtml, gloss } from './html.ts';
43
+ import { renderScrapbookDrawer } from './review-scrapbook-drawer.ts';
44
+ import { existsSync } from 'node:fs';
45
+ import { resolveCalendarPath } from '@deskwork/core/paths';
46
+
47
+ /**
48
+ * Per-request content-index getter. The route layer wires this to the
49
+ * Hono context's memoized cache so a single review render only builds
50
+ * the index once per site even though both the inline-text loader and
51
+ * the scrapbook drawer ask for it. When omitted, callers fall back to
52
+ * slug-template path resolution.
53
+ */
54
+ export type ReviewIndexGetter = (site: string) => ContentIndex;
55
+
56
+ interface ReviewQuery {
57
+ /** ?site=<slug> override; null falls back to config.defaultSite. */
58
+ site: string | null;
59
+ /** ?v=<n>; null shows the workflow's currentVersion. */
60
+ version: string | null;
61
+ /** ?kind=outline | longform; null defaults to longform. */
62
+ kind?: string | null;
63
+ }
64
+
65
+ /**
66
+ * How the route resolved the request. Phase 19d added the canonical
67
+ * id-based URL; the legacy slug URL still resolves to a render via the
68
+ * 302-redirect path (the redirect target lands here as `kind: 'id'`).
69
+ *
70
+ * `kind: 'slug'` is reserved for the legacy fallback when the calendar
71
+ * entry has no id stamped on it yet — pre-doctor state, not a "fallback"
72
+ * the project rules forbid but the migration path the plan calls out.
73
+ *
74
+ * Phase 21c added `kind: 'workflow'` so the dashboard can deep-link
75
+ * straight to a specific workflow id without first knowing the entry
76
+ * id — shortform cells use this to land on the unified review surface
77
+ * after `start-shortform` returns the new workflow.
78
+ */
79
+ export type ReviewLookup =
80
+ | { kind: 'id'; entryId: string; slug: string }
81
+ | { kind: 'slug'; slug: string }
82
+ | { kind: 'workflow'; workflowId: string };
83
+
84
+ function isSuccessBody(
85
+ body: unknown,
86
+ ): body is { workflow: DraftWorkflowItem; versions: DraftVersion[] } {
87
+ if (typeof body !== 'object' || body === null) return false;
88
+ return 'workflow' in body && 'versions' in body;
89
+ }
90
+
91
+ function errorFromBody(body: unknown): string {
92
+ if (typeof body === 'object' && body !== null) {
93
+ const value = Reflect.get(body, 'error');
94
+ if (typeof value === 'string') return value;
95
+ }
96
+ return 'unknown error';
97
+ }
98
+
99
+ function pickContentKind(
100
+ rawKind: string | null | undefined,
101
+ ): 'longform' | 'outline' | 'shortform' {
102
+ if (rawKind === 'outline') return 'outline';
103
+ if (rawKind === 'shortform') return 'shortform';
104
+ return 'longform';
105
+ }
106
+
107
+ function pickSite(ctx: StudioContext, raw: string | null): string {
108
+ if (raw && raw in ctx.config.sites) return raw;
109
+ return ctx.config.defaultSite;
110
+ }
111
+
112
+ function stringField(v: unknown): string | undefined {
113
+ return typeof v === 'string' ? v : undefined;
114
+ }
115
+
116
+ interface PreparedRender {
117
+ fm: Record<string, unknown>;
118
+ bodyHtml: string;
119
+ outlineHtml: string;
120
+ }
121
+
122
+ async function prepareRender(
123
+ markdown: string,
124
+ contentKind: 'longform' | 'outline' | 'shortform',
125
+ ): Promise<PreparedRender> {
126
+ const parsed = parseDraftFrontmatter(markdown);
127
+ const fm = parsed.frontmatter;
128
+
129
+ // Outline + shortform render the body as-is (no outline-split). Only
130
+ // longform pulls the optional briefing-sheet drawer out of the body.
131
+ const split = contentKind !== 'longform'
132
+ ? { body: parsed.body, outline: '', present: false, startLine: -1, endLine: -1 }
133
+ : splitOutline(parsed.body);
134
+
135
+ const bodyHtml = await renderMarkdownToHtml(split.body);
136
+
137
+ // Inject the description as a dek after the body's first <h1>.
138
+ const description = stringField(fm.description);
139
+ const dekHtml = description
140
+ ? `<p class="er-dispatch-dek">${escapeHtml(description)}</p>`
141
+ : '';
142
+ const h1Close = bodyHtml.indexOf('</h1>');
143
+ const renderedHtml =
144
+ dekHtml && h1Close >= 0
145
+ ? bodyHtml.slice(0, h1Close + 5) + dekHtml + bodyHtml.slice(h1Close + 5)
146
+ : dekHtml + bodyHtml;
147
+
148
+ const outlineHtml = split.outline
149
+ ? await renderMarkdownToHtml(split.outline)
150
+ : '';
151
+
152
+ return { fm, bodyHtml: renderedHtml, outlineHtml };
153
+ }
154
+
155
+ function stateLabel(state?: string): string {
156
+ return (state ?? '').replace('-', ' ');
157
+ }
158
+
159
+ function renderVersionsStrip(
160
+ versions: readonly DraftVersion[],
161
+ site: string,
162
+ contentKind: 'longform' | 'outline' | 'shortform',
163
+ current: DraftVersion,
164
+ ): RawHtml {
165
+ if (versions.length <= 1) return unsafe('');
166
+ const kindBit =
167
+ contentKind === 'outline'
168
+ ? '&kind=outline'
169
+ : contentKind === 'shortform'
170
+ ? '&kind=shortform'
171
+ : '';
172
+ const links = versions
173
+ .map((v) => {
174
+ const isActive = v.version === current.version;
175
+ const href = `?site=${site}${kindBit}&v=${v.version}`;
176
+ return html`<a href="${href}" class="${isActive ? 'active' : ''}">v${v.version}</a>`;
177
+ })
178
+ .join('');
179
+ return unsafe(html`<span class="er-strip-versions">${unsafe(links)}</span>`);
180
+ }
181
+
182
+ /**
183
+ * Build the slash command that the operator pastes into Claude Code to
184
+ * advance the workflow from its current pending state. Mirrors the
185
+ * client-side button-handler logic so server-rendered "copy again"
186
+ * affordances stay consistent.
187
+ */
188
+ function pendingSkillCmd(workflow: DraftWorkflowItem): string {
189
+ const { site, slug, contentKind, state } = workflow;
190
+ if (state === 'iterating') {
191
+ return contentKind === 'outline'
192
+ ? `/deskwork:iterate --kind outline --site ${site} ${slug}`
193
+ : `/deskwork:iterate --site ${site} ${slug}`;
194
+ }
195
+ if (state === 'approved') {
196
+ // Outline-approve semantics still TBD (see editorial-review-client.ts);
197
+ // for now both kinds emit the same /deskwork:approve.
198
+ return `/deskwork:approve --site ${site} ${slug}`;
199
+ }
200
+ return '';
201
+ }
202
+
203
+ /**
204
+ * Wrap an action button in a `.er-shortcut-chip-wrap` span carrying a
205
+ * small chord chip beneath the button. The chord style mirrors the
206
+ * shortcuts modal's verbatim two-tap rendering (e.g. `<kbd>a</kbd>
207
+ * <kbd>a</kbd>` for approve) — the destructive-shortcut UX, post-#108,
208
+ * is bare-letter double-tap (no Cmd/Ctrl modifier; verified in the
209
+ * keybinding handler at editorial-review-client.ts).
210
+ *
211
+ * The chip is hidden on narrow viewports via the cross-surface CSS
212
+ * media query — the wrap stays in the markup at every breakpoint so
213
+ * the column flex it triggers (`.er-strip-right > *:has(.er-shortcut-chip)`)
214
+ * is consistent with the chip's visibility state.
215
+ *
216
+ * Issue 5 — keyboard-shortcut chips on action buttons.
217
+ */
218
+ function shortcutChipWrap(buttonHtml: string, letter: 'a' | 'i' | 'r'): string {
219
+ return html`<span class="er-shortcut-chip-wrap">${unsafe(buttonHtml)}<small class="er-shortcut-chip"><kbd>${letter}</kbd><kbd>${letter}</kbd></small></span>`;
220
+ }
221
+
222
+ function renderControlsRight(workflow: DraftWorkflowItem): RawHtml {
223
+ const isActive = workflow.state === 'open' || workflow.state === 'in-review';
224
+ const isApproved = workflow.state === 'approved';
225
+ const isIterating = workflow.state === 'iterating';
226
+ const isTerminal = workflow.state === 'applied' || workflow.state === 'cancelled';
227
+ const buttons: string[] = [];
228
+ // Issue 7 — emit the edit-mode disclosure label next to the Edit
229
+ // button. The client (editorial-review-client.ts) flips both the
230
+ // `data-mode` attribute AND inner text on each toggle. Initial state
231
+ // matches the surface's initial mode (preview).
232
+ buttons.push(html`<button class="er-btn er-btn-small" data-action="toggle-edit" type="button">Edit</button><span class="er-edit-mode-label" data-mode="preview">preview</span>`);
233
+ if (isActive) {
234
+ // Issue 5 — wrap each destructive action button with its chord chip.
235
+ buttons.push(
236
+ shortcutChipWrap(
237
+ html`<button class="er-btn er-btn-small er-btn-approve" data-action="approve" type="button">Approve</button>`,
238
+ 'a',
239
+ ),
240
+ );
241
+ buttons.push(
242
+ shortcutChipWrap(
243
+ html`<button class="er-btn er-btn-small" data-action="iterate" type="button">Iterate</button>`,
244
+ 'i',
245
+ ),
246
+ );
247
+ buttons.push(
248
+ shortcutChipWrap(
249
+ html`<button class="er-btn er-btn-small er-btn-reject" data-action="reject" type="button">Reject</button>`,
250
+ 'r',
251
+ ),
252
+ );
253
+ }
254
+ if (isApproved) {
255
+ const applyCmd = pendingSkillCmd(workflow);
256
+ buttons.push(html`<span class="er-pending-state">awaiting apply…</span>`);
257
+ buttons.push(html`<button class="er-btn er-btn-small" data-action="copy-cmd" data-cmd="${applyCmd}" title="Copy ${applyCmd} to clipboard" type="button">copy <code>/deskwork:approve</code></button>`);
258
+ buttons.push(
259
+ shortcutChipWrap(
260
+ html`<button class="er-btn er-btn-small er-btn-reject" data-action="reject" type="button">Reject</button>`,
261
+ 'r',
262
+ ),
263
+ );
264
+ }
265
+ if (isIterating) {
266
+ const iterateCmd = pendingSkillCmd(workflow);
267
+ buttons.push(html`<span class="er-pending-state">agent iterating…</span>`);
268
+ buttons.push(html`<button class="er-btn er-btn-small" data-action="copy-cmd" data-cmd="${iterateCmd}" title="Copy ${iterateCmd} to clipboard" type="button">copy <code>/deskwork:iterate</code></button>`);
269
+ }
270
+ if (isTerminal) {
271
+ buttons.push(html`<span class="er-pending-state er-pending-state--filed">filed (${workflow.state})</span>`);
272
+ }
273
+ buttons.push(html`<button class="er-btn er-btn-small" data-action="shortcuts" type="button" aria-label="Show keyboard shortcuts" title="Keyboard shortcuts">?</button>`);
274
+ return unsafe(`<span class="er-strip-right">${buttons.join('')}</span>`);
275
+ }
276
+
277
+ function renderError(
278
+ slug: string,
279
+ site: string,
280
+ contentKind: 'longform' | 'outline' | 'shortform',
281
+ message: string,
282
+ ): string {
283
+ const startCmd =
284
+ contentKind === 'outline'
285
+ ? `/deskwork:outline --site ${site} ${slug}`
286
+ : contentKind === 'shortform'
287
+ ? `/deskwork:shortform-start --site ${site} ${slug} <platform>`
288
+ : `/deskwork:review-start --site ${site} ${slug}`;
289
+ const body = html`
290
+ <div data-review-ui="longform">
291
+ ${renderEditorialFolio('longform', `longform · ${slug}`)}
292
+ <div class="er-error">
293
+ <h1>No galley to review.</h1>
294
+ <p><strong>Slug:</strong> <code>${slug}</code></p>
295
+ <p>${message}</p>
296
+ <p>Start one with:</p>
297
+ <p><code>${startCmd}</code></p>
298
+ <p style="margin-top: 2rem;"><a href="/dev/editorial-studio">← back to the studio</a></p>
299
+ </div>
300
+ </div>`;
301
+ return layout({
302
+ title: `Review — ${slug} — error`,
303
+ cssHrefs: [
304
+ '/static/css/editorial-review.css',
305
+ '/static/css/editorial-nav.css',
306
+ ],
307
+ bodyHtml: body,
308
+ scriptModules: [],
309
+ });
310
+ }
311
+
312
+ function renderShortcutsOverlay(): RawHtml {
313
+ return unsafe(html`
314
+ <div class="er-shortcuts" data-shortcuts-overlay hidden role="dialog" aria-modal="true" aria-label="Keyboard shortcuts">
315
+ <div class="er-shortcuts-backdrop" data-shortcuts-backdrop></div>
316
+ <div class="er-shortcuts-panel">
317
+ <h2>Keyboard</h2>
318
+ <dl>
319
+ <dt><kbd>e</kbd> / dbl-click</dt><dd>toggle edit mode</dd>
320
+ <dt>select text</dt><dd>leave a margin note</dd>
321
+ <dt><kbd>⌘</kbd><kbd>↵</kbd> / <kbd>ctrl</kbd><kbd>↵</kbd></dt><dd>save margin note (in composer)</dd>
322
+ <dt><kbd>a</kbd> <kbd>a</kbd></dt><dd>approve <em>— press twice within 500ms; first press arms, second fires</em></dd>
323
+ <dt><kbd>i</kbd> <kbd>i</kbd></dt><dd>iterate <em>— press twice within 500ms</em></dd>
324
+ <dt><kbd>r</kbd> <kbd>r</kbd></dt><dd>reject <em>— press twice within 500ms</em></dd>
325
+ <dt><kbd>j</kbd> / <kbd>k</kbd></dt><dd>next / previous margin note</dd>
326
+ <dt><kbd>shift</kbd><kbd>F</kbd></dt><dd>focus mode <em>(edit mode only)</em></dd>
327
+ <dt><kbd>shift</kbd><kbd>M</kbd></dt><dd>show / hide margin notes column <em>— or click the chevron in the head when visible, or the pull tab on the right edge when stowed</em></dd>
328
+ <dt><kbd>?</kbd></dt><dd>this panel</dd>
329
+ <dt><kbd>esc</kbd></dt><dd>close / cancel composer</dd>
330
+ </dl>
331
+ <p class="er-shortcuts-footer">Press <kbd>?</kbd> anytime.</p>
332
+ </div>
333
+ </div>`);
334
+ }
335
+
336
+ /* Issue #159 — marginalia stow affordance.
337
+ *
338
+ * The toggle for "show / hide the margin-notes column" lives ON the
339
+ * marginalia component, not in a generic toolbar. Two paired
340
+ * affordances drive the same state:
341
+ *
342
+ * - `.er-marginalia-stow` — chevron button INSIDE the marginalia
343
+ * head (next to "Margin notes" label). Clicking it stows the
344
+ * column. Visible only when marginalia is visible (the head is
345
+ * inside `.er-marginalia`, which is `display: none` when stowed).
346
+ *
347
+ * - `.er-marginalia-tab` — pull tab on the right edge of the
348
+ * viewport, mirroring `.er-outline-tab` on the left edge. Visible
349
+ * ONLY when marginalia is stowed (CSS rule `body[data-marginalia=
350
+ * "hidden"] .er-marginalia-tab { display: block }`). Clicking it
351
+ * unstows.
352
+ *
353
+ * Both affordances + Shift+M dispatch through the same client-side
354
+ * toggleMarginalia handler. Mirrors the outline-drawer's pull-tab
355
+ * pattern so the project's affordance vocabulary stays consistent.
356
+ */
357
+ function renderMarginaliaTab(): RawHtml {
358
+ return unsafe(html`
359
+ <button class="er-marginalia-tab" data-action="toggle-marginalia" type="button" aria-pressed="true" aria-label="Show margin notes (Shift+M)" title="Show margin notes (Shift+M)">
360
+ <span class="er-marginalia-tab-glyph" aria-hidden="true">‹</span>
361
+ <span class="er-marginalia-tab-label">Notes</span>
362
+ </button>`);
363
+ }
364
+
365
+ function renderMarginalia(): RawHtml {
366
+ return unsafe(html`
367
+ <aside class="er-marginalia" data-comments-sidebar aria-label="Margin notes">
368
+ <p class="er-marginalia-head">
369
+ <button class="er-marginalia-stow" data-action="toggle-marginalia" type="button" aria-pressed="false" aria-label="Hide margin notes (Shift+M)" title="Hide margin notes (Shift+M)">
370
+ <span aria-hidden="true">›</span>
371
+ </button>
372
+ <span class="er-marginalia-head-label">Margin notes</span>
373
+ </p>
374
+ <p class="er-marginalia-empty" data-sidebar-empty>Select text in the draft to leave a <em>margin note</em>.</p>
375
+ <section class="er-marginalia-composer" data-comment-composer hidden aria-label="New margin note">
376
+ <p class="er-marginalia-composer-head">New mark</p>
377
+ <div class="er-marginalia-composer-quote" data-composer-quote></div>
378
+ <label class="er-marginalia-composer-label" for="comment-category">Mark as</label>
379
+ <select id="comment-category" class="er-marginalia-composer-select" data-comment-category>
380
+ <option value="other" selected>other</option>
381
+ <option value="voice-drift">voice-drift</option>
382
+ <option value="missing-receipt">missing-receipt</option>
383
+ <option value="tutorial-framing">tutorial-framing</option>
384
+ <option value="saas-vocabulary">saas-vocabulary</option>
385
+ <option value="fake-authority">fake-authority</option>
386
+ <option value="structural">structural</option>
387
+ </select>
388
+ <label class="er-marginalia-composer-label" for="comment-text">Note</label>
389
+ <textarea id="comment-text" class="er-marginalia-composer-textarea" data-comment-text rows="4"
390
+ placeholder="What needs attention here?"></textarea>
391
+ <div class="er-marginalia-composer-actions">
392
+ <button type="button" class="er-btn er-btn-small" data-action="cancel-comment">Cancel</button>
393
+ <button type="button" class="er-btn er-btn-small er-btn-primary" data-action="submit-comment">Leave mark</button>
394
+ </div>
395
+ </section>
396
+ <ol class="er-marginalia-list" data-sidebar-list></ol>
397
+ </aside>`);
398
+ }
399
+
400
+ /**
401
+ * Issue #154 Dispatch C — the edit-mode chrome was previously a single
402
+ * `.er-edit-mode` block rendered inside `.er-draft-frame` (below
403
+ * `#draft-body`). With the page-grid in place, the natural layout is:
404
+ *
405
+ * - the toolbar (Source/Split/Preview tabs + Outline/Focus/Save/
406
+ * Cancel actions) sticks above `.er-page`, replacing the strip's
407
+ * right-side action buttons;
408
+ * - the source/preview panes take over the article column where
409
+ * `#draft-body` was.
410
+ *
411
+ * `renderEditToolbar` emits the bar that lives ABOVE `.er-page`; the
412
+ * client toggles its `[hidden]` attribute on enter/exit. Keeps
413
+ * `data-edit-toolbar` on the wrapper so `editorial-review-client.ts`'s
414
+ * existing `q('[data-edit-toolbar]')` lookup keeps working.
415
+ */
416
+ function renderEditToolbar(outlineHasContent: boolean): RawHtml {
417
+ const outlineBtnAttrs = outlineHasContent ? '' : ' hidden';
418
+ return unsafe(html`
419
+ <div class="er-edit-toolbar" data-edit-toolbar hidden>
420
+ <div class="er-edit-modes" role="tablist" aria-label="Editor mode">
421
+ <button class="er-edit-mode-btn" data-edit-view="source" type="button" aria-pressed="true">Source</button>
422
+ <button class="er-edit-mode-btn" data-edit-view="split" type="button" aria-pressed="false">Split</button>
423
+ <button class="er-edit-mode-btn" data-edit-view="preview" type="button" aria-pressed="false">Preview</button>
424
+ </div>
425
+ <div class="er-edit-actions">
426
+ <button class="er-btn er-btn-small" data-action="outline-drawer" type="button" title="Show the outline for reference (O)" aria-pressed="false"${unsafe(outlineBtnAttrs)}>Outline ↗</button>
427
+ <button class="er-btn er-btn-small" data-action="focus-mode" type="button" title="Distraction-free mode (Shift+F)" aria-pressed="false">Focus ⛶</button>
428
+ <button class="er-btn er-btn-primary" data-action="save-version" type="button">Save as new version</button>
429
+ <button class="er-btn" data-action="cancel-edit" type="button">Cancel</button>
430
+ <span class="er-edit-hint" data-edit-hint></span>
431
+ </div>
432
+ </div>`);
433
+ }
434
+
435
+ /**
436
+ * Issue #154 Dispatch C — the source/preview panes (and supporting
437
+ * focus-mode affordances + backing textarea) live inside the article
438
+ * column, replacing `#draft-body`. The wrapper keeps the
439
+ * `er-edit-mode` class so existing CSS (panes-host paper-2 background,
440
+ * focus-mode full-viewport canvas) cascades unchanged. Adds
441
+ * `data-edit-panes-host` so the client can flip `[hidden]` on the
442
+ * panes wrapper independently of the toolbar.
443
+ */
444
+ function renderEditPanes(): RawHtml {
445
+ return unsafe(html`
446
+ <div class="er-edit-mode" data-edit-panes-host hidden>
447
+ <div class="er-edit-panes" data-edit-panes data-view="source">
448
+ <div class="er-edit-source" data-edit-source aria-label="Markdown source"></div>
449
+ <div class="er-edit-preview" data-edit-preview aria-label="Rendered preview"></div>
450
+ </div>
451
+ <textarea id="draft-edit" data-draft-edit hidden></textarea>
452
+ <div class="er-focus-exit" data-focus-exit aria-hidden="true">
453
+ <button type="button" data-action="exit-focus" title="Exit focus (Esc)">← exit focus</button>
454
+ </div>
455
+ <div class="er-focus-save" data-focus-save aria-hidden="true">
456
+ <button type="button" class="er-btn er-btn-small er-btn-primary" data-action="save-version">Save</button>
457
+ <span class="er-focus-save-hint" data-focus-save-hint></span>
458
+ </div>
459
+ </div>`);
460
+ }
461
+
462
+ function renderOutlineDrawer(outlineHtml: string): RawHtml {
463
+ const hidden = outlineHtml ? '' : ' hidden';
464
+ return unsafe(html`
465
+ <button class="er-outline-tab" data-outline-tab type="button" aria-label="Show outline"${unsafe(hidden)}>
466
+ <span class="er-outline-tab-label">Outline</span>
467
+ </button>
468
+ <aside class="er-outline-drawer" data-outline-drawer aria-label="Outline reference" hidden>
469
+ <header class="er-outline-drawer-head">
470
+ <span class="er-outline-drawer-kicker">Briefing sheet</span>
471
+ <button type="button" class="er-outline-drawer-close" data-outline-close aria-label="Close outline (O or Esc)">×</button>
472
+ </header>
473
+ <div class="er-outline-drawer-body" data-outline-drawer-body>${unsafe(outlineHtml)}</div>
474
+ <footer class="er-outline-drawer-foot">
475
+ <span>Read-only · edit via <code>/editorial-iterate --kind outline</code></span>
476
+ </footer>
477
+ </aside>`);
478
+ }
479
+
480
+ /**
481
+ * Resolve the calendar entry that backs this review surface. Callers
482
+ * have either an `entryId` (id-canonical route) or a slug (legacy
483
+ * route) to work with. Returns `null` when no calendar entry matches —
484
+ * ad-hoc workflows + pre-doctor entries fall through to the slug-
485
+ * template legacy path elsewhere.
486
+ *
487
+ * Failures (calendar absent, parse error) are swallowed to null so a
488
+ * transient calendar issue never blocks the review render.
489
+ */
490
+ function lookupReviewEntry(
491
+ ctx: StudioContext,
492
+ site: string,
493
+ lookup: ReviewLookup,
494
+ fallbackSlug: string,
495
+ ): CalendarEntry | null {
496
+ try {
497
+ const calendarPath = resolveCalendarPath(ctx.projectRoot, ctx.config, site);
498
+ if (!existsSync(calendarPath)) return null;
499
+ const cal = readCalendar(calendarPath);
500
+ if (lookup.kind === 'id') {
501
+ const byId = findEntryById(cal, lookup.entryId);
502
+ if (byId !== undefined) return byId;
503
+ }
504
+ const slug = lookup.kind === 'workflow' ? fallbackSlug : lookup.slug;
505
+ const bySlug = findEntry(cal, slug);
506
+ return bySlug ?? null;
507
+ } catch {
508
+ return null;
509
+ }
510
+ }
511
+
512
+ export async function renderReviewPage(
513
+ ctx: StudioContext,
514
+ lookup: ReviewLookup,
515
+ query: ReviewQuery,
516
+ getIndex?: ReviewIndexGetter,
517
+ ): Promise<string> {
518
+ const queryKind = pickContentKind(query.kind ?? null);
519
+
520
+ // Workflow-id lookup short-circuits site + entryId resolution: the
521
+ // workflow record carries everything we need to render. Phase 21c
522
+ // added this path so the dashboard's shortform matrix (and any other
523
+ // surface that knows a workflow id) can deep-link to the unified
524
+ // review surface without first knowing the calendar entry id.
525
+ const fetched = lookup.kind === 'workflow'
526
+ ? handleGetWorkflow(ctx.projectRoot, ctx.config, {
527
+ id: lookup.workflowId,
528
+ entryId: null,
529
+ site: null,
530
+ slug: null,
531
+ contentKind: null,
532
+ platform: null,
533
+ channel: null,
534
+ })
535
+ : handleGetWorkflow(ctx.projectRoot, ctx.config, {
536
+ id: null,
537
+ entryId: lookup.kind === 'id' ? lookup.entryId : null,
538
+ site: pickSite(ctx, query.site),
539
+ slug: lookup.slug,
540
+ contentKind: queryKind,
541
+ platform: null,
542
+ channel: null,
543
+ });
544
+
545
+ // Slug used in error messages and the title fallback. For
546
+ // workflow-id lookups we don't know the slug until the fetch
547
+ // succeeds, so use the id as a placeholder for any pre-fetch error.
548
+ const lookupSlug =
549
+ lookup.kind === 'workflow' ? lookup.workflowId : lookup.slug;
550
+ // Site for the chrome / outbound links. Workflow-id lookups carry
551
+ // their own site through the fetched workflow record.
552
+ let resolvedSite = pickSite(ctx, query.site);
553
+
554
+ if (fetched.status !== 200 || !isSuccessBody(fetched.body)) {
555
+ return renderError(
556
+ lookupSlug,
557
+ resolvedSite,
558
+ queryKind,
559
+ errorFromBody(fetched.body),
560
+ );
561
+ }
562
+
563
+ const { workflow, versions } = fetched.body;
564
+ // Workflow-id paths drive contentKind from the workflow itself —
565
+ // it's the source of truth, not the URL kind hint.
566
+ const contentKind: 'longform' | 'outline' | 'shortform' =
567
+ lookup.kind === 'workflow' ? workflow.contentKind : queryKind;
568
+ if (lookup.kind === 'workflow') resolvedSite = workflow.site;
569
+ const slug = workflow.slug;
570
+
571
+ const requested = query.version ? parseInt(query.version, 10) : workflow.currentVersion;
572
+ const currentVersion =
573
+ versions.find((v) => v.version === requested) ?? versions[versions.length - 1];
574
+
575
+ if (!currentVersion) {
576
+ return renderError(
577
+ slug,
578
+ resolvedSite,
579
+ contentKind,
580
+ 'no current version on this workflow',
581
+ );
582
+ }
583
+
584
+ const { fm, bodyHtml, outlineHtml } = await prepareRender(
585
+ currentVersion.markdown,
586
+ contentKind,
587
+ );
588
+
589
+ const draftState = { workflow, currentVersion, versions };
590
+
591
+ const titleField = stringField(fm.title) ?? `Draft: ${slug}`;
592
+
593
+ // Phase 19c+: look up the calendar entry so the scrapbook drawer +
594
+ // inline-text loader can resolve the on-disk scrapbook directory via
595
+ // the content index when a frontmatter-id binding exists. Falls back
596
+ // to slug-template addressing when no entry / no id is present.
597
+ // Shortform skips the scrapbook drawer entirely — different surface
598
+ // shape, no margin-note workflow.
599
+ const reviewEntry = lookupReviewEntry(ctx, resolvedSite, lookup, slug);
600
+ const reviewIndex = getIndex ? getIndex(resolvedSite) : undefined;
601
+ const isShortform = contentKind === 'shortform';
602
+
603
+ // Phase 21c — shortform header. Renders above the editor on the
604
+ // unified review surface so the operator sees the platform (and
605
+ // channel, if any) at a glance. Reuses existing `--er-*` design
606
+ // tokens; no new CSS introduced.
607
+ const shortformMeta: RawHtml = isShortform
608
+ ? unsafe(html`
609
+ <div class="er-shortform-meta">
610
+ <span class="er-platform">${workflow.platform ?? 'other'}</span>
611
+ ${workflow.channel
612
+ ? unsafe(html`<span class="er-channel">${workflow.channel}</span>`)
613
+ : ''}
614
+ </div>`)
615
+ : unsafe('');
616
+
617
+ const reviewUiAttr = isShortform ? 'shortform' : 'longform';
618
+ const folioSpine = isShortform
619
+ ? `shortform · ${workflow.platform ?? '?'}${workflow.channel ? ` · ${workflow.channel}` : ''} · ${slug}`
620
+ : `longform · ${slug}`;
621
+ // Issue 4 — shortform reviews highlight the "Shortform" nav item;
622
+ // longform reviews don't match any nav-item (no longform desk
623
+ // exists). Pre-Issue-4, longform mistakenly highlighted shortform
624
+ // because the chrome treated all review surfaces as 'reviews'.
625
+ const folioActive: 'shortform' | 'longform' = isShortform
626
+ ? 'shortform'
627
+ : 'longform';
628
+
629
+ // Issue #154 Dispatch A — `.er-page` wraps the draft frame +
630
+ // marginalia inside a CSS Grid composition so marginalia sits next
631
+ // to the prose it annotates rather than pinned to the viewport.
632
+ // Shortform reviews skip the marginalia column (no margin-note
633
+ // workflow on shortform), so the page collapses to the draft frame
634
+ // alone for that surface — keeping the same `.er-page` shell
635
+ // preserves the desk metaphor across longform/shortform.
636
+ // Issue #154 Dispatch C — edit-mode panes-host lives inside the
637
+ // article column (in place of #draft-body when editing); the
638
+ // toolbar that drives it lives ABOVE `.er-page` (rendered below,
639
+ // outside the grid). Shortform never enters edit mode on this
640
+ // surface, so the panes-host is rendered but stays hidden — keeps
641
+ // the JS hooks present for forward compatibility without flipping
642
+ // any visible chrome.
643
+ const pageGrid = isShortform
644
+ ? html`
645
+ <div class="er-page-grid">
646
+ <div class="er-draft-frame">
647
+ <div id="draft-body" data-draft-body
648
+ title="Double-click to edit · select text to leave a margin note">${unsafe(bodyHtml)}</div>
649
+ ${renderEditPanes()}
650
+ </div>
651
+ </div>`
652
+ : html`
653
+ <div class="er-page-grid">
654
+ <div class="er-draft-frame">
655
+ <div id="draft-body" data-draft-body
656
+ title="Double-click to edit · select text to leave a margin note">${unsafe(bodyHtml)}</div>
657
+ ${renderEditPanes()}
658
+ </div>
659
+ <div class="er-page-gutter" aria-hidden="true"></div>
660
+ ${renderMarginalia()}
661
+ </div>`;
662
+
663
+ const body = html`
664
+ <div data-review-ui="${reviewUiAttr}" class="er-review-shell">
665
+ ${renderEditorialFolio(folioActive, folioSpine)}
666
+ ${shortformMeta}
667
+ <div class="er-strip">
668
+ <div class="er-strip-inner">
669
+ <a class="er-strip-back" href="/dev/editorial-studio" title="Back to the editorial studio">← studio</a>
670
+ <span class="er-strip-galley">${gloss('galley')} <em>№ ${currentVersion.version}</em></span>
671
+ <span class="er-strip-slug">${workflow.site} / ${workflow.slug}</span>
672
+ ${renderVersionsStrip(versions, resolvedSite, contentKind, currentVersion)}
673
+ <span class="er-strip-center">
674
+ <span class="er-stamp er-stamp-big er-stamp-${workflow.state}" data-state-label>
675
+ ${stateLabel(workflow.state)}
676
+ </span>
677
+ <span class="er-strip-hint">select text to <span class="er-gloss" data-term="marginalia" tabindex="0" role="button" aria-describedby="glossary-marginalia">mark</span> · double-click to edit · <kbd>?</kbd> for shortcuts</span>
678
+ </span>
679
+ ${renderControlsRight(workflow)}
680
+ </div>
681
+ </div>
682
+ ${renderEditToolbar(outlineHtml.length > 0)}
683
+ <article class="er-page">
684
+ ${unsafe(pageGrid)}
685
+ </article>
686
+ ${isShortform ? unsafe('') : renderMarginaliaTab()}
687
+ <button class="er-pencil-btn" data-add-comment-btn hidden type="button">Mark</button>
688
+ ${isShortform ? unsafe('') : renderOutlineDrawer(outlineHtml)}
689
+ ${isShortform
690
+ ? unsafe('')
691
+ : renderScrapbookDrawer(ctx, resolvedSite, reviewEntry, workflow.slug, reviewIndex)}
692
+ <div class="er-toast" data-toast hidden></div>
693
+ ${renderShortcutsOverlay()}
694
+ <div class="er-poll-indicator" data-poll>auto-refresh · 8s</div>
695
+ </div>`;
696
+
697
+ return layout({
698
+ title: `${titleField} — Review`,
699
+ cssHrefs: [
700
+ '/static/css/editorial-review.css',
701
+ '/static/css/editorial-nav.css',
702
+ '/static/css/blog-figure.css',
703
+ '/static/css/review-viewport.css',
704
+ '/static/css/scrap-row.css',
705
+ ],
706
+ bodyHtml: body,
707
+ embeddedJson: [{ id: 'draft-state', data: draftState }],
708
+ scriptModules: ['editorial-review-client'],
709
+ });
710
+ }