pmx-canvas 0.4.1 → 0.4.2

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.
@@ -60,6 +60,7 @@ import { buildAgentContextPreamble } from './agent-context.js';
60
60
  import { buildCanvasAxSurfaceSnapshot } from './ax-context.js';
61
61
  import { resolveNodeAxCapabilities } from './ax-interaction.js';
62
62
  import { normalizeCanvasTheme, type CanvasTheme } from './canvas-db.js';
63
+ import { canvasThemeScheme, isCanvasTheme } from '../shared/themes.js';
63
64
  import { validateLocalImageFile } from './image-source.js';
64
65
  import {
65
66
  cancelCodeGraphRecompute,
@@ -984,6 +985,14 @@ function canvasSpaHtml(): string {
984
985
  }, 4000);
985
986
  })();
986
987
  </script>
988
+ ${
989
+ // Amp orb services run with AMP_ORB=1. The portal's nested-iframe embed
990
+ // blocks src-URL child iframes, and the runtime probe is unreliable there
991
+ // (a tiny probe iframe can load even though node-sized ones will not), so
992
+ // the client needs to KNOW it is in an orb and force srcdoc mode directly
993
+ // (see src/client/state/iframe-mode.ts).
994
+ process.env.AMP_ORB ? '<script>window.__PMX_AMP_ORB = true;</script>' : ''
995
+ }
987
996
  <script type="module" src="/canvas/index.js?v=${CANVAS_ASSET_VERSION}"></script>
988
997
  </body>
989
998
  </html>`;
@@ -1040,6 +1049,22 @@ function resolveCanvasBundleDir(): string {
1040
1049
  return fallback;
1041
1050
  }
1042
1051
 
1052
+ // Memoized surface-theme.css content for inlining into HTML surface documents
1053
+ // (srcdoc-rendered surfaces in nested-iframe hosts may fail to load the <link>
1054
+ // subresource, leaving them unstyled — dark-on-dark reads as blank). Read from
1055
+ // the same bundle root that serves /canvas/surface-theme.css; null keeps the
1056
+ // <link> fallback when the asset is missing (e.g. an unbuilt dev tree).
1057
+ let _surfaceThemeCss: string | null | undefined;
1058
+ function surfaceThemeCssInline(): string | null {
1059
+ if (_surfaceThemeCss !== undefined) return _surfaceThemeCss;
1060
+ try {
1061
+ _surfaceThemeCss = readFileSync(resolve(resolveCanvasBundleDir(), 'surface-theme.css'), 'utf-8');
1062
+ } catch {
1063
+ _surfaceThemeCss = null;
1064
+ }
1065
+ return _surfaceThemeCss;
1066
+ }
1067
+
1043
1068
  /**
1044
1069
  * Containment check for bundle-asset serving. Separator-agnostic: on Windows,
1045
1070
  * `resolve()` returns backslash paths, so comparing against a `${dir}/` template
@@ -1262,6 +1287,9 @@ function handleNodeSurface(pathname: string, url: URL): Response {
1262
1287
  ...(url.searchParams.get('frameToken')
1263
1288
  ? { contentHeightToken: url.searchParams.get('frameToken') as string }
1264
1289
  : {}),
1290
+ // Inline the theme tokens so srcdoc-rendered surfaces (Amp orb portals)
1291
+ // are styled without depending on the <link> subresource.
1292
+ ...(surfaceThemeCssInline() ? { inlineThemeCss: surfaceThemeCssInline() as string } : {}),
1265
1293
  });
1266
1294
  return surfaceHtmlResponse(doc, HTML_SURFACE_SANDBOX);
1267
1295
  }
@@ -1358,8 +1386,14 @@ async function handleJsonRenderView(url: URL): Promise<Response> {
1358
1386
  );
1359
1387
 
1360
1388
  const themeValue = url.searchParams.get('theme');
1389
+ // The viewer bundle themes dark/light/high-contrast directly; other named
1390
+ // canvas themes (midnight, sepia, …) collapse to their dark/light scheme.
1361
1391
  const theme =
1362
- themeValue === 'dark' || themeValue === 'light' || themeValue === 'high-contrast' ? themeValue : undefined;
1392
+ themeValue === 'dark' || themeValue === 'light' || themeValue === 'high-contrast'
1393
+ ? themeValue
1394
+ : isCanvasTheme(themeValue)
1395
+ ? canvasThemeScheme(themeValue)
1396
+ : undefined;
1363
1397
  const title = (node.data.title as string) || node.id;
1364
1398
  // Devtools panel is double-gated: the operator must opt in via the env flag
1365
1399
  // AND the request must carry ?devtools=1. Off by default in all normal runs.
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Canonical canvas theme registry — single source of truth for every layer
3
+ * that validates, stores, or renders a theme name (client shell, HTML
4
+ * surfaces, json-render viewer, CLI/env plumbing, persistence).
5
+ *
6
+ * Adding a theme: add the name here, author its variable blocks in
7
+ * src/client/theme/global.css AND src/client/theme/surface-theme.css (guarded
8
+ * by tests/unit/surface-theme-tokens.test.ts), and give it meta below. The
9
+ * theme picker, server validation, and surface documents pick it up from this
10
+ * list.
11
+ */
12
+
13
+ export const CANVAS_THEMES = [
14
+ 'dark',
15
+ 'light',
16
+ 'high-contrast',
17
+ 'midnight',
18
+ 'sepia',
19
+ 'arctic',
20
+ 'ember',
21
+ 'forest',
22
+ ] as const;
23
+
24
+ export type CanvasThemeName = (typeof CANVAS_THEMES)[number];
25
+
26
+ export function isCanvasTheme(value: unknown): value is CanvasThemeName {
27
+ return typeof value === 'string' && (CANVAS_THEMES as readonly string[]).includes(value);
28
+ }
29
+
30
+ export function normalizeCanvasThemeName(value: unknown, fallback: CanvasThemeName = 'dark'): CanvasThemeName {
31
+ return isCanvasTheme(value) ? value : fallback;
32
+ }
33
+
34
+ export interface CanvasThemeMeta {
35
+ /** Human label shown in the theme picker. */
36
+ label: string;
37
+ /** Whether embedded viewers that only know dark/light should treat it as light. */
38
+ scheme: 'dark' | 'light';
39
+ /** Picker swatch colors — MUST match --c-bg / --c-accent in global.css. */
40
+ swatchBg: string;
41
+ swatchAccent: string;
42
+ }
43
+
44
+ export const CANVAS_THEME_META: Record<CanvasThemeName, CanvasThemeMeta> = {
45
+ dark: { label: 'Dark', scheme: 'dark', swatchBg: '#081524', swatchAccent: '#4BBCFF' },
46
+ light: { label: 'Light', scheme: 'light', swatchBg: '#F4EFE6', swatchAccent: '#1A7ABF' },
47
+ 'high-contrast': { label: 'High contrast', scheme: 'dark', swatchBg: '#000000', swatchAccent: '#00ffff' },
48
+ midnight: { label: 'Midnight', scheme: 'dark', swatchBg: '#0A0D1C', swatchAccent: '#8B96FF' },
49
+ sepia: { label: 'Sepia', scheme: 'light', swatchBg: '#F2E7D5', swatchAccent: '#B4632C' },
50
+ arctic: { label: 'Arctic', scheme: 'dark', swatchBg: '#20242E', swatchAccent: '#7FC8DE' },
51
+ ember: { label: 'Ember', scheme: 'dark', swatchBg: '#0E0E11', swatchAccent: '#FF5C2E' },
52
+ forest: { label: 'Forest', scheme: 'dark', swatchBg: '#0C1712', swatchAccent: '#5CCF8F' },
53
+ };
54
+
55
+ /** Collapse a theme to the dark/light scheme embedded dark|light-only viewers understand. */
56
+ export function canvasThemeScheme(value: unknown): 'dark' | 'light' {
57
+ return CANVAS_THEME_META[normalizeCanvasThemeName(value)].scheme;
58
+ }