@conloca/cms-spa 0.2.2 → 0.2.3

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.
Files changed (161) hide show
  1. package/README.md +26 -0
  2. package/dist/spa/main.css +223 -1
  3. package/dist/ts/.build-inputs.json +178 -0
  4. package/dist/ts/App.d.ts +5 -1
  5. package/dist/ts/App.d.ts.map +1 -1
  6. package/dist/ts/App.js +13 -6
  7. package/dist/ts/App.js.map +1 -1
  8. package/dist/ts/CmsSpaApp.d.ts.map +1 -1
  9. package/dist/ts/CmsSpaApp.js +4 -1
  10. package/dist/ts/CmsSpaApp.js.map +1 -1
  11. package/dist/ts/components/cards/DataEntryCard.d.ts +1 -1
  12. package/dist/ts/components/cards/DataEntryCard.d.ts.map +1 -1
  13. package/dist/ts/components/cards/SectionCard.d.ts +1 -1
  14. package/dist/ts/components/cards/SectionCard.d.ts.map +1 -1
  15. package/dist/ts/components/dialogs/DeleteConfirmDialog.d.ts +1 -1
  16. package/dist/ts/components/dialogs/DeleteConfirmDialog.d.ts.map +1 -1
  17. package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts +1 -1
  18. package/dist/ts/components/dialogs/FragmentPropertiesDialog.d.ts.map +1 -1
  19. package/dist/ts/components/dialogs/MoveFolderDialog.d.ts +1 -1
  20. package/dist/ts/components/dialogs/MoveFolderDialog.d.ts.map +1 -1
  21. package/dist/ts/components/editor/CMSMDXEditor.d.ts.map +1 -1
  22. package/dist/ts/components/editor/CMSMDXEditor.js +46 -55
  23. package/dist/ts/components/editor/CMSMDXEditor.js.map +1 -1
  24. package/dist/ts/components/editor/LocaleSelector.d.ts +1 -1
  25. package/dist/ts/components/editor/LocaleSelector.d.ts.map +1 -1
  26. package/dist/ts/components/editor/PageEditor.d.ts +1 -1
  27. package/dist/ts/components/editor/PageEditor.d.ts.map +1 -1
  28. package/dist/ts/components/editor/PageEditor.js +46 -17
  29. package/dist/ts/components/editor/PageEditor.js.map +1 -1
  30. package/dist/ts/components/editor/PageEditorHeaderActions.d.ts +1 -1
  31. package/dist/ts/components/editor/PageEditorHeaderActions.d.ts.map +1 -1
  32. package/dist/ts/components/editor/PageEditorWrapper.js +31 -5
  33. package/dist/ts/components/editor/PageEditorWrapper.js.map +1 -1
  34. package/dist/ts/components/editor/draft-media-config.d.ts +19 -1
  35. package/dist/ts/components/editor/draft-media-config.d.ts.map +1 -1
  36. package/dist/ts/components/editor/draft-media-config.js +112 -21
  37. package/dist/ts/components/editor/draft-media-config.js.map +1 -1
  38. package/dist/ts/components/editor/page-setting-targets.d.ts +1 -1
  39. package/dist/ts/components/editor/page-setting-targets.d.ts.map +1 -1
  40. package/dist/ts/components/editor/preview-availability.d.ts +6 -5
  41. package/dist/ts/components/editor/preview-availability.d.ts.map +1 -1
  42. package/dist/ts/components/editor/preview-availability.js +6 -5
  43. package/dist/ts/components/editor/preview-availability.js.map +1 -1
  44. package/dist/ts/components/editors/DataEditor.d.ts +1 -1
  45. package/dist/ts/components/editors/DataEditor.d.ts.map +1 -1
  46. package/dist/ts/components/media/AssetCard.d.ts +1 -1
  47. package/dist/ts/components/media/AssetCard.d.ts.map +1 -1
  48. package/dist/ts/components/media/AssetCard.js +3 -2
  49. package/dist/ts/components/media/AssetCard.js.map +1 -1
  50. package/dist/ts/components/media/AssetDetailSidebar.d.ts +1 -1
  51. package/dist/ts/components/media/AssetDetailSidebar.d.ts.map +1 -1
  52. package/dist/ts/components/media/AssetDetailSidebar.js +4 -2
  53. package/dist/ts/components/media/AssetDetailSidebar.js.map +1 -1
  54. package/dist/ts/components/media/MediaLibrary.d.ts +1 -1
  55. package/dist/ts/components/media/MediaLibrary.d.ts.map +1 -1
  56. package/dist/ts/components/media/MediaToolbar.d.ts +1 -1
  57. package/dist/ts/components/media/MediaToolbar.d.ts.map +1 -1
  58. package/dist/ts/components/pages/PageList.d.ts.map +1 -1
  59. package/dist/ts/components/pages/PageList.js +10 -4
  60. package/dist/ts/components/pages/PageList.js.map +1 -1
  61. package/dist/ts/components/sandbox/ContentSandbox.d.ts +1 -1
  62. package/dist/ts/components/sandbox/ContentSandbox.d.ts.map +1 -1
  63. package/dist/ts/components/sandbox/ContentSandbox.js +36 -5
  64. package/dist/ts/components/sandbox/ContentSandbox.js.map +1 -1
  65. package/dist/ts/components/ui/IconButton.d.ts +1 -1
  66. package/dist/ts/components/ui/IconButton.d.ts.map +1 -1
  67. package/dist/ts/content-wrapper.d.ts +65 -0
  68. package/dist/ts/content-wrapper.d.ts.map +1 -0
  69. package/dist/ts/content-wrapper.js +190 -0
  70. package/dist/ts/content-wrapper.js.map +1 -0
  71. package/dist/ts/data-schemas.d.ts.map +1 -1
  72. package/dist/ts/frame/channel.d.ts +40 -0
  73. package/dist/ts/frame/channel.d.ts.map +1 -0
  74. package/dist/ts/frame/channel.js +233 -0
  75. package/dist/ts/frame/channel.js.map +1 -0
  76. package/dist/ts/frame/editor-frame.d.ts +23 -0
  77. package/dist/ts/frame/editor-frame.d.ts.map +1 -0
  78. package/dist/ts/frame/editor-frame.js +154 -0
  79. package/dist/ts/frame/editor-frame.js.map +1 -0
  80. package/dist/ts/frame/media-cache.d.ts +26 -0
  81. package/dist/ts/frame/media-cache.d.ts.map +1 -0
  82. package/dist/ts/frame/media-cache.js +123 -0
  83. package/dist/ts/frame/media-cache.js.map +1 -0
  84. package/dist/ts/frame/memory-router.d.ts +10 -0
  85. package/dist/ts/frame/memory-router.d.ts.map +1 -0
  86. package/dist/ts/frame/memory-router.js +39 -0
  87. package/dist/ts/frame/memory-router.js.map +1 -0
  88. package/dist/ts/frame/parent-hello.d.ts +11 -0
  89. package/dist/ts/frame/parent-hello.d.ts.map +1 -0
  90. package/dist/ts/frame/parent-hello.js +53 -0
  91. package/dist/ts/frame/parent-hello.js.map +1 -0
  92. package/dist/ts/frame/protocol.d.ts +201 -0
  93. package/dist/ts/frame/protocol.d.ts.map +1 -0
  94. package/dist/ts/frame/protocol.js +195 -0
  95. package/dist/ts/frame/protocol.js.map +1 -0
  96. package/dist/ts/frame/rpc-content-client.d.ts +10 -0
  97. package/dist/ts/frame/rpc-content-client.d.ts.map +1 -0
  98. package/dist/ts/frame/rpc-content-client.js +51 -0
  99. package/dist/ts/frame/rpc-content-client.js.map +1 -0
  100. package/dist/ts/frame/save-shortcut.d.ts +8 -0
  101. package/dist/ts/frame/save-shortcut.d.ts.map +1 -0
  102. package/dist/ts/frame/save-shortcut.js +26 -0
  103. package/dist/ts/frame/save-shortcut.js.map +1 -0
  104. package/dist/ts/host-style-probe.d.ts +32 -37
  105. package/dist/ts/host-style-probe.d.ts.map +1 -1
  106. package/dist/ts/host-style-probe.js +86 -68
  107. package/dist/ts/host-style-probe.js.map +1 -1
  108. package/dist/ts/index.d.ts +4 -2
  109. package/dist/ts/index.d.ts.map +1 -1
  110. package/dist/ts/index.js +6 -2
  111. package/dist/ts/index.js.map +1 -1
  112. package/dist/ts/main-spa.d.ts.map +1 -1
  113. package/dist/ts/main-spa.js +2 -1
  114. package/dist/ts/main-spa.js.map +1 -1
  115. package/dist/ts/page-schemas.d.ts.map +1 -1
  116. package/dist/ts/prose-surface-css.d.ts +3 -5
  117. package/dist/ts/prose-surface-css.d.ts.map +1 -1
  118. package/dist/ts/prose-surface-css.js +3 -5
  119. package/dist/ts/prose-surface-css.js.map +1 -1
  120. package/dist/ts/puck-config.d.ts.map +1 -1
  121. package/dist/ts/selected-block.d.ts.map +1 -1
  122. package/dist/ts/site-styles.d.ts +4 -21
  123. package/dist/ts/site-styles.d.ts.map +1 -1
  124. package/dist/ts/site-styles.js.map +1 -1
  125. package/dist/ts/ui-config.d.ts +50 -1
  126. package/dist/ts/ui-config.d.ts.map +1 -1
  127. package/dist/ts/ui-config.js +4 -0
  128. package/dist/ts/ui-config.js.map +1 -1
  129. package/package.json +20 -10
  130. package/src/App.tsx +22 -10
  131. package/src/CmsSpaApp.tsx +5 -1
  132. package/src/components/editor/CMSMDXEditor.tsx +49 -69
  133. package/src/components/editor/PageEditor.tsx +45 -17
  134. package/src/components/editor/PageEditorWrapper.tsx +34 -5
  135. package/src/components/editor/draft-media-config.ts +125 -23
  136. package/src/components/editor/preview-availability.ts +6 -5
  137. package/src/components/media/AssetCard.tsx +10 -7
  138. package/src/components/media/AssetDetailSidebar.tsx +6 -6
  139. package/src/components/pages/PageList.tsx +12 -4
  140. package/src/components/sandbox/ContentSandbox.tsx +34 -5
  141. package/src/content-wrapper.ts +284 -0
  142. package/src/frame/channel.ts +258 -0
  143. package/src/frame/editor-frame.ts +180 -0
  144. package/src/frame/media-cache.ts +144 -0
  145. package/src/frame/memory-router.ts +39 -0
  146. package/src/frame/parent-hello.ts +62 -0
  147. package/src/frame/protocol.ts +323 -0
  148. package/src/frame/rpc-content-client.ts +58 -0
  149. package/src/frame/save-shortcut.ts +25 -0
  150. package/src/host-style-probe.ts +104 -90
  151. package/src/index.ts +7 -2
  152. package/src/main-spa.tsx +2 -0
  153. package/src/main.css +2 -1
  154. package/src/prose-surface-css.ts +3 -5
  155. package/src/site-styles.ts +4 -22
  156. package/src/ui-config.ts +52 -1
  157. package/dist/ts/host-discovery.d.ts +0 -40
  158. package/dist/ts/host-discovery.d.ts.map +0 -1
  159. package/dist/ts/host-discovery.js +0 -39
  160. package/dist/ts/host-discovery.js.map +0 -1
  161. package/src/host-discovery.ts +0 -77
@@ -1,4 +1,7 @@
1
1
  import { useEffect, useRef, useState } from 'react';
2
+ import { type DiscoveredWrappers, findContentWrapper } from './content-wrapper.js';
3
+ import { buildSiteUrl } from './lib/utils.js';
4
+ import type { SiteStyles } from './site-styles.js';
2
5
 
3
6
  /**
4
7
  * Host-style probe — captures the host's computed styles for prose
@@ -161,6 +164,68 @@ export function findWrapperByClass(doc: Document, className: string | null): HTM
161
164
  return (found as HTMLElement) ?? fallback();
162
165
  }
163
166
 
167
+ /** What one load of the real page yields. */
168
+ export interface HostProbe {
169
+ /** The measured prose rules, unlayered; empty when nothing was probable. */
170
+ css: string;
171
+ /** The page's own stylesheets as one text: what the dev styles endpoint answers for its route. */
172
+ styles: SiteStyles;
173
+ /** The page's content and code-block wrappers, by the dev wrapper endpoint's rule. */
174
+ wrappers: DiscoveredWrappers;
175
+ }
176
+
177
+ const EMPTY_PROBE: HostProbe = { css: '', styles: [], wrappers: { content: null, codeBlock: null } };
178
+
179
+ /**
180
+ * Read a loaded page. An editor in a parent frame has no dev endpoints, so
181
+ * with `discover` this is where it gets the route CSS and wrappers they give a
182
+ * local editor, and with no `wrapperClass` from the caller the samples go
183
+ * inside the wrapper the page's own markup names. Without `discover` only the
184
+ * measured rules are read. Exported for tests.
185
+ */
186
+ export function readProbedPage(doc: Document, wrapperClass: string | null, discover: boolean): HostProbe {
187
+ if (!discover) return { ...EMPTY_PROBE, css: buildProbeStylesheet(doc, wrapperClass) };
188
+ // Read before measuring: the samples are added under the wrapper.
189
+ const wrappers = findContentWrapper(doc.documentElement.outerHTML);
190
+ return {
191
+ css: buildProbeStylesheet(doc, wrapperClass ?? wrappers.content?.className ?? null),
192
+ styles: pageStyles(doc),
193
+ wrappers,
194
+ };
195
+ }
196
+
197
+ /**
198
+ * `next`, with `prev`'s styles and wrappers wherever their content is the same,
199
+ * so a re-probe of an unchanged page (a theme toggle) changes no identity the
200
+ * editor's injected sheet and plugins depend on. Exported for tests.
201
+ */
202
+ export function keepUnchanged(prev: HostProbe, next: HostProbe): HostProbe {
203
+ const sameStyles =
204
+ prev.styles.length === next.styles.length && prev.styles.every((text, index) => text === next.styles[index]);
205
+ return {
206
+ css: next.css,
207
+ styles: sameStyles ? prev.styles : next.styles,
208
+ wrappers: JSON.stringify(prev.wrappers) === JSON.stringify(next.wrappers) ? prev.wrappers : next.wrappers,
209
+ };
210
+ }
211
+
212
+ /** The page's stylesheets in document order, each under its own media. Another origin's sheet is unreadable and left out. */
213
+ function pageStyles(doc: Document): SiteStyles {
214
+ const sheets: string[] = [];
215
+ for (const sheet of Array.from(doc.styleSheets)) {
216
+ let rules: string;
217
+ try {
218
+ rules = Array.from(sheet.cssRules, (rule) => rule.cssText).join('\n');
219
+ } catch {
220
+ continue;
221
+ }
222
+ const media = sheet.media.mediaText;
223
+ sheets.push(media && media !== 'all' ? `@media ${media} {\n${rules}\n}` : rules);
224
+ }
225
+ const css = sheets.join('\n');
226
+ return css ? [css] : [];
227
+ }
228
+
164
229
  /**
165
230
  * Read computed styles for each PROBE_TAG inside a freshly loaded
166
231
  * iframe's document. Returns one CSS rule string per tag, scoped to
@@ -463,59 +528,24 @@ function probeInlineTag(view: Window, parent: HTMLElement, tag: string): string
463
528
  }
464
529
 
465
530
  /**
466
- * Where the probe reads from. Two delivery routes, ONE mechanism:
467
- *
468
- * - `{ url }` — the site's real rendered page, for when the editor and the
469
- * site share an origin, so `contentDocument` is readable.
470
- * - `{ srcdoc }` — a same-origin reconstruction assembled from what the host
471
- * set with `setHostDiscovery` (its `routeCss` plus the discovered wrapper
472
- * class). For a site on a DIFFERENT origin, where no header on either side
473
- * can make its document readable: the same-origin
474
- * policy is not a permission, it is the boundary. An `srcdoc` frame
475
- * inherits the parent's origin, so the probe runs against it normally, and
476
- * a CSP of `frame-src 'self'` already covers `srcdoc`.
477
- *
478
- * A reconstruction sees the stylesheet but not the markup, so it cannot
479
- * observe utility-class or inline-style typography on real elements — the very
480
- * thing the probe exists for on utility-first hosts.
481
- */
482
- export type ProbeSource = { url: string } | { srcdoc: string };
483
-
484
- /**
485
- * Build the reconstruction document for `{ srcdoc }` probing: the host's route
486
- * CSS inlined, and an empty clone of its content wrapper for the probe's
487
- * sample elements to be injected into. Nothing else — no shell, no scripts.
488
- */
489
- export function buildProbeReconstruction(routeCss: string, wrapperClass: string | null): string {
490
- const wrapper = wrapperClass ? `<div class="${wrapperClass.replace(/"/g, '&quot;')}"></div>` : '<main></main>';
491
- // Neutralise a literal `</style` in the site's CSS. It is legal inside a
492
- // `content:` string, and because a srcdoc frame inherits THIS origin, an
493
- // early close would let the remainder parse as HTML in the app's own origin.
494
- // Escaping the slash keeps the bytes valid CSS while making the sequence
495
- // unable to terminate the element. Paired with the `sandbox` attribute in
496
- // `probeHostStyles`: a document built from site CSS is a different trust
497
- // boundary than this probe's original job of loading the site's own
498
- // already-trusted page, so it gets belt and braces.
499
- const inert = routeCss.replace(/<\/(style)/gi, '<\\/$1');
500
- return `<!doctype html><html><head><meta charset="utf-8"><style>${inert}</style></head><body>${wrapper}</body></html>`;
501
- }
502
-
503
- /**
504
- * Run a one-shot probe against `source`. Returns the generated stylesheet, or
505
- * an empty string when the frame failed to load or there was nothing probable.
531
+ * Run a one-shot probe against the site's real rendered page at `url`, which
532
+ * shares the editor's origin so `contentDocument` is readable. Resolves to
533
+ * what the page yields (see `readProbedPage`), or an empty probe when the
534
+ * frame failed to load.
506
535
  *
507
536
  * `signal` lets the caller cancel a probe (eg the user navigated to
508
537
  * a different page mid-fetch). Cleanup is best-effort.
509
538
  */
510
539
  export function probeHostStyles(
511
- source: ProbeSource,
540
+ url: string,
512
541
  wrapperClass: string | null,
513
542
  theme: 'light' | 'dark' | null,
543
+ discover: boolean,
514
544
  signal?: AbortSignal,
515
- ): Promise<string> {
545
+ ): Promise<HostProbe> {
516
546
  return new Promise((resolve) => {
517
547
  if (typeof document === 'undefined') {
518
- resolve('');
548
+ resolve(EMPTY_PROBE);
519
549
  return;
520
550
  }
521
551
 
@@ -530,24 +560,14 @@ export function probeHostStyles(
530
560
  iframe.setAttribute('aria-hidden', 'true');
531
561
  iframe.setAttribute('tabindex', '-1');
532
562
  iframe.setAttribute('data-conloca-probe-iframe', '');
533
- if ('srcdoc' in source) {
534
- // `allow-same-origin` ONLY: `contentDocument` stays readable, which the
535
- // probe needs, while no script in the reconstruction can execute. Applied
536
- // to the srcdoc route alone — sandboxing the real-page route would stop
537
- // the site's own scripts running and silently change what the
538
- // same-origin probe measures, which is the path that already works.
539
- iframe.setAttribute('sandbox', 'allow-same-origin');
540
- iframe.srcdoc = source.srcdoc;
541
- } else {
542
- iframe.src = source.url;
543
- }
563
+ iframe.src = url;
544
564
 
545
565
  let settled = false;
546
566
  let timeout: number | undefined;
547
567
  const cleanup = () => {
548
568
  if (iframe.parentElement) iframe.parentElement.removeChild(iframe);
549
569
  };
550
- const settle = (css: string) => {
570
+ const settle = (result: HostProbe = EMPTY_PROBE) => {
551
571
  if (settled) return;
552
572
  settled = true;
553
573
  // Cancel the deadline as well. A superseded probe (re-render, theme or
@@ -556,15 +576,15 @@ export function probeHostStyles(
556
576
  // resolved and is no longer in flight.
557
577
  if (timeout !== undefined) window.clearTimeout(timeout);
558
578
  cleanup();
559
- resolve(css);
579
+ resolve(result);
560
580
  };
561
581
 
562
- const abortHandler = () => settle('');
582
+ const abortHandler = () => settle();
563
583
  signal?.addEventListener('abort', abortHandler);
564
584
 
565
585
  timeout = window.setTimeout(() => {
566
- console.warn('[Conloca] Host style probe timed out for', 'srcdoc' in source ? '<reconstruction>' : source.url);
567
- settle('');
586
+ console.warn('[Conloca] Host style probe timed out for', url);
587
+ settle();
568
588
  }, IFRAME_TIMEOUT_MS);
569
589
 
570
590
  iframe.addEventListener('load', () => {
@@ -602,20 +622,19 @@ export function probeHostStyles(
602
622
  requestAnimationFrame(() => {
603
623
  requestAnimationFrame(() => {
604
624
  if (signal?.aborted) {
605
- settle('');
625
+ settle();
606
626
  return;
607
627
  }
608
628
  try {
609
629
  const doc = iframe.contentDocument;
610
630
  if (!doc) {
611
- settle('');
631
+ settle();
612
632
  return;
613
633
  }
614
- const css = buildProbeStylesheet(doc, wrapperClass);
615
- settle(css);
634
+ settle(readProbedPage(doc, wrapperClass, discover));
616
635
  } catch (err) {
617
636
  console.warn('[Conloca] Host style probe failed:', err);
618
- settle('');
637
+ settle();
619
638
  }
620
639
  });
621
640
  });
@@ -623,7 +642,7 @@ export function probeHostStyles(
623
642
 
624
643
  iframe.addEventListener('error', () => {
625
644
  window.clearTimeout(timeout);
626
- settle('');
645
+ settle();
627
646
  });
628
647
 
629
648
  document.body.appendChild(iframe);
@@ -634,44 +653,39 @@ export function probeHostStyles(
634
653
  * React hook wrapper. Re-probes whenever its inputs change (so dark/light
635
654
  * swaps refresh the captured styles).
636
655
  *
637
- * `routeCss` selects the delivery route. When the registered route CSS is
638
- * present — the site is on another origin, so its page cannot be read — the
639
- * probe measures a same-origin reconstruction of it. Otherwise it measures the
640
- * real page at `routeUrl`, which is the same-origin path and strictly better:
641
- * real markup means utility classes and inline styles are observed.
656
+ * Probes the real page at `routeUrl`: real markup means utility classes and
657
+ * inline styles are observed.
642
658
  *
643
- * Returns both the stylesheet and whether the current inputs have settled
659
+ * Returns what the page yielded and whether the current inputs have settled
644
660
  * once. Callers must gate fallback tiers on `settled`, not on `css === ''`:
645
661
  * the empty string is the initial state AND the definitive "nothing probable"
646
662
  * outcome, and treating them the same paints the derived tier then restyles
647
663
  * when the probe lands.
648
664
  *
649
- * During a re-probe (theme toggle, route change) prior `css` stays visible
665
+ * During a re-probe (theme toggle, route change) the prior result stays visible
650
666
  * and `settled` stays true until the new result arrives, so fallback tiers
651
667
  * do not flash back in.
652
668
  */
653
669
  export function useProbedHostStyles(
654
670
  routeUrl: string | undefined,
655
671
  wrapperClass: string | null,
656
- options: { theme?: 'light' | 'dark' | null; routeCss?: string } = {},
657
- ): { css: string; settled: boolean } {
658
- const [css, setCss] = useState<string>('');
672
+ options: { theme?: 'light' | 'dark' | null; discover?: boolean } = {},
673
+ ): HostProbe & { settled: boolean } {
674
+ const [probe, setProbe] = useState<HostProbe>(EMPTY_PROBE);
659
675
  const [settled, setSettled] = useState(false);
660
- // Mirror of `css` for the effect body — reading state inside the effect
676
+ // Mirror of `probe.css` for the effect body — reading state inside the effect
661
677
  // would stale-close over the mount value and mis-handle re-probes.
662
- const cssRef = useRef(css);
678
+ const cssRef = useRef(probe.css);
663
679
  const theme = options.theme ?? null;
664
- const routeCss = options.routeCss ?? '';
680
+ // Only an editor without the dev endpoints needs the page's styles and wrappers.
681
+ const discover = options.discover ?? false;
682
+ // The route is a site path; the page lives under the site's base, which need not be this document's origin root.
683
+ const pageUrl = routeUrl ? buildSiteUrl(routeUrl) : undefined;
665
684
 
666
685
  useEffect(() => {
667
- const source: ProbeSource | null = routeCss
668
- ? { srcdoc: buildProbeReconstruction(routeCss, wrapperClass) }
669
- : routeUrl
670
- ? { url: routeUrl }
671
- : null;
672
- if (!source) {
686
+ if (!pageUrl) {
673
687
  cssRef.current = '';
674
- setCss('');
688
+ setProbe(EMPTY_PROBE);
675
689
  setSettled(true);
676
690
  return;
677
691
  }
@@ -684,17 +698,17 @@ export function useProbedHostStyles(
684
698
  }
685
699
 
686
700
  const controller = new AbortController();
687
- probeHostStyles(source, wrapperClass, theme, controller.signal).then((result) => {
701
+ void probeHostStyles(pageUrl, wrapperClass, theme, discover, controller.signal).then((result) => {
688
702
  if (controller.signal.aborted) return;
689
- cssRef.current = result;
690
- setCss(result);
703
+ cssRef.current = result.css;
704
+ setProbe((prev) => keepUnchanged(prev, result));
691
705
  setSettled(true);
692
706
  });
693
707
 
694
708
  return () => {
695
709
  controller.abort();
696
710
  };
697
- }, [routeUrl, routeCss, wrapperClass, theme]);
711
+ }, [pageUrl, wrapperClass, theme, discover]);
698
712
 
699
- return { css, settled };
713
+ return { ...probe, settled };
700
714
  }
package/src/index.ts CHANGED
@@ -21,6 +21,9 @@ export {
21
21
  notifyEditorDirtyChanged,
22
22
  registerEditorFlushHandler,
23
23
  } from './editor-flush.js';
24
+ // The editor page's side of a parent frame
25
+ export { connectEditorFrame, type EditorFrame } from './frame/editor-frame.js';
26
+ export { awaitParentHello } from './frame/parent-hello.js';
24
27
  // Export types from hooks (AssetEntry, etc.)
25
28
  export type { AssetEntry, AssetUsage, FolderListing, FolderTreeNode } from './hooks/index.js';
26
29
  // Export configuration function
@@ -28,6 +31,7 @@ export {
28
31
  configureUI,
29
32
  type EditorWriteAccess,
30
33
  type EditorWriteRefusal,
34
+ type MemoryRouterConfig,
31
35
  type OpenPreviewResult,
32
36
  type PreviewCapability,
33
37
  setDataBindings,
@@ -38,5 +42,6 @@ export {
38
42
  type TemplateConfig,
39
43
  type UIConfig,
40
44
  } from './ui-config.js';
41
- // Export the read-only enforcement a host installs through `setEditorReadOnly`
42
- export { ContentWriteRefusedError, editorWriteAccess } from './write-access.js';
45
+ // Export the read-only enforcement a host installs through `setEditorReadOnly`, and the guard a parent frame wraps
46
+ // its own content client in
47
+ export { ContentWriteRefusedError, editorWriteAccess, guardContentWrites } from './write-access.js';
package/src/main-spa.tsx CHANGED
@@ -3,6 +3,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
3
3
  import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
4
4
  import { createRoot } from 'react-dom/client';
5
5
  import App from './App.js';
6
+ import { Toaster } from './components/toast.js';
6
7
  import { CanvasThemeProvider } from './hooks/useCanvasTheme.js';
7
8
  import { ThemeProvider } from './hooks/useTheme.js';
8
9
  import { installDiscoveredMdxComponents, type MdxComponents } from './mdx-components.js';
@@ -62,6 +63,7 @@ export function mountSpa(): void {
62
63
  <App basename={config.basename} />
63
64
  </CanvasThemeProvider>
64
65
  </ThemeProvider>
66
+ <Toaster />
65
67
  {config.enableDevtools && <ReactQueryDevtools initialIsOpen={false} />}
66
68
  </QueryClientProvider>,
67
69
  );
package/src/main.css CHANGED
@@ -80,7 +80,8 @@
80
80
  host's CSS doesn't reach. */
81
81
  @layer base, theme, components, utilities, cms-admin, conloca-host-defaults, conloca-host-preview;
82
82
 
83
- @import "@puckeditor/core/puck.css" layer(cms-admin);
83
+ /* Puck's styles without their remote Inter @import: the editor sets its own font stack (--puck-font-family). */
84
+ @import "@puckeditor/core/no-external.css" layer(cms-admin);
84
85
  /* Granular Tailwind v4 imports (see the preflight note in the header):
85
86
  * theme + utilities keep their exact pre-split homes — the
86
87
  * `cms-admin.theme` / `cms-admin.utilities` sublayers the monolithic
@@ -2,11 +2,9 @@ import type { SiteStyles } from './site-styles.js';
2
2
 
3
3
  /**
4
4
  * The fallback styles tier for the MDX typing surface, used when no dev-server
5
- * discovery endpoint answers (a host app on another origin, or a production
6
- * build). Derived from the siteStyles registry: the route CSS a host app set
7
- * with `setHostDiscovery`, or the `siteStyles` option's CSS, reduced to
8
- * prose-safe rules, plus a paper base sheet. Lives in cms-spa so every setup
9
- * runs one derivation.
5
+ * discovery endpoint answers (a production build). Derived from the siteStyles
6
+ * registry (the `siteStyles` option's CSS), reduced to prose-safe rules, plus
7
+ * a paper base sheet. Lives in cms-spa so every setup runs one derivation.
10
8
  *
11
9
  * CMSMDXEditor consumes it as the fallback below endpoint discovery: when the
12
10
  * `/api/styles` fetch delivers (dev server), this tier stays out of the
@@ -1,4 +1,5 @@
1
1
  import { useEffect, useState } from 'react';
2
+ import type { ContentWrapperInfo, DiscoveredWrappers } from './content-wrapper.js';
2
3
  import { IMPORT_RE } from './site-css-layer.js';
3
4
  import { getCmsRoute } from './ui-config.js';
4
5
 
@@ -217,28 +218,9 @@ export function useFetchedSiteStyles(routeUrl: string | undefined, options: { cm
217
218
  return styles;
218
219
  }
219
220
 
220
- /**
221
- * Shape returned by the `/api/content-wrapper?url=…` endpoint — the
222
- * tag name and class list of the host page's content-root element.
223
- * Kept in lock-step with `ContentWrapperInfo` in
224
- * `packages/astro-cms/src/site-styles/content-wrapper-endpoint.ts`.
225
- */
226
- export interface HostContentWrapper {
227
- tagName: string;
228
- className: string;
229
- }
230
-
231
- /**
232
- * Discovered host wrappers for a given route. `content` is the
233
- * prose-content wrapper (eg `.sl-markdown-content`). `codeBlock` is
234
- * the ordered chain of classed wrappers between the content wrapper
235
- * and `<pre>`, outermost first (eg `[.expressive-code, .frame ...]`).
236
- * Either may be null when not present on the page.
237
- */
238
- export interface HostWrappers {
239
- content: HostContentWrapper | null;
240
- codeBlock: HostContentWrapper[] | null;
241
- }
221
+ /** The shape `/api/content-wrapper?url=…` answers: see `content-wrapper.ts`. */
222
+ export type HostContentWrapper = ContentWrapperInfo;
223
+ export type HostWrappers = DiscoveredWrappers;
242
224
 
243
225
  /**
244
226
  * Fetches the host page's wrappers for the given route URL. The
package/src/ui-config.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /* oxlint-disable no-underscore-dangle -- __UI_CONFIG__ is the existing browser-global compatibility contract read by embedded hosts. */
2
2
  import type { ContentAPIClient, EditorDataBindings } from '@conloca/content-api-client';
3
3
  import type { QueryClientConfig } from '@tanstack/react-query';
4
+ import type { MediaLoadInput, PreviewOpenResult } from './frame/protocol.js';
4
5
  import type { MediaBridge } from './media/types.js';
5
6
 
6
7
  export interface TemplateConfig {
@@ -161,7 +162,20 @@ export interface UIConfig {
161
162
  * about that site — not a failure of the call. The two must
162
163
  * stay distinguishable: one is the site's setup, the other is a fault.
163
164
  */
164
- openPreview?: (input: { path: string }) => Promise<OpenPreviewResult>;
165
+ openPreview?: (input: { path: string }) => Promise<OpenPreviewResult | PreviewOpenResult>;
166
+ /**
167
+ * Who opens the preview tab. Default `'editor'`. With `'parent'` the parent
168
+ * frame opens the tab during the click, so the editor opens none and only
169
+ * asks through `openPreview`.
170
+ */
171
+ previewTab?: 'editor' | 'parent';
172
+ /**
173
+ * Opens the published page at `path` from outside the editor frame, so the
174
+ * tab is not born inside a sandboxed frame. Without it the editor keeps
175
+ * `window.open`. Whoever opens the tab explains a blocked popup, so the
176
+ * editor only waits for the request to settle.
177
+ */
178
+ openSiteUrl?: (input: { path: string }) => Promise<void>;
165
179
  /**
166
180
  * Whether the site actually serves draft preview.
167
181
  * `'unknown'` (the default) means not established yet and the editor leaves
@@ -181,6 +195,43 @@ export interface UIConfig {
181
195
  * is refused with {@link EditorWriteRefusal.reason}.
182
196
  */
183
197
  readOnly?: EditorWriteRefusal;
198
+ /**
199
+ * Path the MDX sandbox frame loads from. An editor page that ships its own
200
+ * static copy of the frame sets it; default `SANDBOX_FRAME_PATH`.
201
+ */
202
+ sandboxFramePath?: string;
203
+ /**
204
+ * The site's `editorCSS` text, appended after the MDX canvas styles so it
205
+ * wins. An editor page that loads it at run time sets it; otherwise the
206
+ * build's copy applies.
207
+ */
208
+ editorCss?: string;
209
+ /**
210
+ * Loads the bytes of a media file. When set, media surfaces never hand media
211
+ * URLs to `<img>`; they show object URLs of the loaded bytes.
212
+ */
213
+ loadMedia?: (input: MediaLoadInput) => Promise<Blob>;
214
+ /**
215
+ * Set by a parent frame: the editor routes in memory and reports each route
216
+ * to it. It also marks the editor as running in a parent frame, where the
217
+ * dev-server endpoints do not exist.
218
+ */
219
+ memoryRouter?: MemoryRouterConfig;
220
+ }
221
+
222
+ /** How the editor routes when a parent frame owns the address bar. */
223
+ export interface MemoryRouterConfig {
224
+ /** The editor route to open first, `pathname + search`. */
225
+ initialPath: string;
226
+ /** Called with each route the editor commits, `pathname + search`. */
227
+ onNavigate(path: string, replace: boolean): void;
228
+ /** Listens for routes the parent asks for; returns the function that stops listening. */
229
+ subscribe(navigate: (path: string) => void): () => void;
230
+ }
231
+
232
+ /** Whether the editor runs inside a parent frame (see `UIConfig.memoryRouter`). */
233
+ export function inEditorFrame(config: UIConfig = getUIConfig()): boolean {
234
+ return config.memoryRouter !== undefined;
184
235
  }
185
236
 
186
237
  const defaultConfig: UIConfig = {
@@ -1,40 +0,0 @@
1
- import type { HostWrappers } from './site-styles.js';
2
- /**
3
- * Registered style-discovery results, written by a host app that embeds the
4
- * editor and read by the MDX editor.
5
- *
6
- * The astro-cms dev server discovers the same facts at runtime
7
- * (`/api/styles`, `/api/content-wrapper`, the computed-style probe); those
8
- * mechanisms need a Vite graph and a same-origin page, which an editor served
9
- * from another origin does not have. A host app that learns the same facts
10
- * some other way writes them into this registry — the editor's consumption
11
- * chain then treats them as the same tier, one layer below live discovery.
12
- *
13
- * astro-cms never writes this, so the chain simply skips it there.
14
- */
15
- export interface HostDiscovery {
16
- /** The discovery route's collected CSS. */
17
- routeCss?: string;
18
- /** The discovery route's content-wrapper shape, same contract as the endpoint's. */
19
- wrappers?: HostWrappers;
20
- }
21
- interface DiscoveryState {
22
- value: HostDiscovery;
23
- subscribers: Set<(value: HostDiscovery) => void>;
24
- }
25
- declare global {
26
- interface Window {
27
- __HOST_DISCOVERY_STATE__?: DiscoveryState;
28
- }
29
- }
30
- /**
31
- * Replace the registered discovery wholesale. Wholesale on purpose: the
32
- * registry is process-global, so a switch to another site that carried only
33
- * SOME fields forward would leak the previous site's wrapper or CSS into the new
34
- * one — the same clear-semantics `setSiteStyles([])` has.
35
- */
36
- export declare function setHostDiscovery(value: HostDiscovery): void;
37
- export declare function getHostDiscovery(): HostDiscovery;
38
- export declare function useHostDiscovery(): HostDiscovery;
39
- export {};
40
- //# sourceMappingURL=host-discovery.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"host-discovery.d.ts","sourceRoot":"","sources":["../../src/host-discovery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAErD;;;;;;;;;;;;GAYG;AAEH,MAAM,WAAW,aAAa;IAC5B,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,oFAAoF;IACpF,QAAQ,CAAC,EAAE,YAAY,CAAC;CACzB;AAED,UAAU,cAAc;IACtB,KAAK,EAAE,aAAa,CAAC;IACrB,WAAW,EAAE,GAAG,CAAC,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC,CAAC;CAClD;AAED,OAAO,CAAC,MAAM,CAAC,CAAC;IACd,UAAU,MAAM;QACd,wBAAwB,CAAC,EAAE,cAAc,CAAC;KAC3C;CACF;AAYD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAM3D;AAED,wBAAgB,gBAAgB,IAAI,aAAa,CAEhD;AAED,wBAAgB,gBAAgB,IAAI,aAAa,CAahD"}
@@ -1,39 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
- const getState = () => {
3
- if (typeof window !== 'undefined') {
4
- if (!window.__HOST_DISCOVERY_STATE__) {
5
- window.__HOST_DISCOVERY_STATE__ = { value: {}, subscribers: new Set() };
6
- }
7
- return window.__HOST_DISCOVERY_STATE__;
8
- }
9
- return { value: {}, subscribers: new Set() };
10
- };
11
- /**
12
- * Replace the registered discovery wholesale. Wholesale on purpose: the
13
- * registry is process-global, so a switch to another site that carried only
14
- * SOME fields forward would leak the previous site's wrapper or CSS into the new
15
- * one — the same clear-semantics `setSiteStyles([])` has.
16
- */
17
- export function setHostDiscovery(value) {
18
- const state = getState();
19
- state.value = value;
20
- state.subscribers.forEach((fn) => {
21
- fn(value);
22
- });
23
- }
24
- export function getHostDiscovery() {
25
- return getState().value;
26
- }
27
- export function useHostDiscovery() {
28
- const [value, setValue] = useState(() => getState().value);
29
- useEffect(() => {
30
- const state = getState();
31
- setValue((current) => (state.value === current ? current : state.value));
32
- state.subscribers.add(setValue);
33
- return () => {
34
- state.subscribers.delete(setValue);
35
- };
36
- }, []);
37
- return value;
38
- }
39
- //# sourceMappingURL=host-discovery.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"host-discovery.js","sourceRoot":"","sources":["../../src/host-discovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAmC5C,MAAM,QAAQ,GAAG,GAAmB,EAAE;IACpC,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,IAAI,CAAC,MAAM,CAAC,wBAAwB,EAAE,CAAC;YACrC,MAAM,CAAC,wBAAwB,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,WAAW,EAAE,IAAI,GAAG,EAAE,EAAE,CAAC;QAC1E,CAAC;QACD,OAAO,MAAM,CAAC,wBAAwB,CAAC;IACzC,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,WAAW,EAAE,IAAI,GAAG,EAAE,EAAE,CAAC;AAC/C,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAoB;IACnD,MAAM,KAAK,GAAG,QAAQ,EAAE,CAAC;IACzB,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,KAAK,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;QAC/B,EAAE,CAAC,KAAK,CAAC,CAAC;IACZ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,gBAAgB;IAC9B,OAAO,QAAQ,EAAE,CAAC,KAAK,CAAC;AAC1B,CAAC;AAED,MAAM,UAAU,gBAAgB;IAC9B,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,CAAC;IAE3D,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,KAAK,GAAG,QAAQ,EAAE,CAAC;QACzB,QAAQ,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;QACzE,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAChC,OAAO,GAAG,EAAE;YACV,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACrC,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -1,77 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
- import type { HostWrappers } from './site-styles.js';
3
-
4
- /**
5
- * Registered style-discovery results, written by a host app that embeds the
6
- * editor and read by the MDX editor.
7
- *
8
- * The astro-cms dev server discovers the same facts at runtime
9
- * (`/api/styles`, `/api/content-wrapper`, the computed-style probe); those
10
- * mechanisms need a Vite graph and a same-origin page, which an editor served
11
- * from another origin does not have. A host app that learns the same facts
12
- * some other way writes them into this registry — the editor's consumption
13
- * chain then treats them as the same tier, one layer below live discovery.
14
- *
15
- * astro-cms never writes this, so the chain simply skips it there.
16
- */
17
-
18
- export interface HostDiscovery {
19
- /** The discovery route's collected CSS. */
20
- routeCss?: string;
21
- /** The discovery route's content-wrapper shape, same contract as the endpoint's. */
22
- wrappers?: HostWrappers;
23
- }
24
-
25
- interface DiscoveryState {
26
- value: HostDiscovery;
27
- subscribers: Set<(value: HostDiscovery) => void>;
28
- }
29
-
30
- declare global {
31
- interface Window {
32
- __HOST_DISCOVERY_STATE__?: DiscoveryState;
33
- }
34
- }
35
-
36
- const getState = (): DiscoveryState => {
37
- if (typeof window !== 'undefined') {
38
- if (!window.__HOST_DISCOVERY_STATE__) {
39
- window.__HOST_DISCOVERY_STATE__ = { value: {}, subscribers: new Set() };
40
- }
41
- return window.__HOST_DISCOVERY_STATE__;
42
- }
43
- return { value: {}, subscribers: new Set() };
44
- };
45
-
46
- /**
47
- * Replace the registered discovery wholesale. Wholesale on purpose: the
48
- * registry is process-global, so a switch to another site that carried only
49
- * SOME fields forward would leak the previous site's wrapper or CSS into the new
50
- * one — the same clear-semantics `setSiteStyles([])` has.
51
- */
52
- export function setHostDiscovery(value: HostDiscovery): void {
53
- const state = getState();
54
- state.value = value;
55
- state.subscribers.forEach((fn) => {
56
- fn(value);
57
- });
58
- }
59
-
60
- export function getHostDiscovery(): HostDiscovery {
61
- return getState().value;
62
- }
63
-
64
- export function useHostDiscovery(): HostDiscovery {
65
- const [value, setValue] = useState(() => getState().value);
66
-
67
- useEffect(() => {
68
- const state = getState();
69
- setValue((current) => (state.value === current ? current : state.value));
70
- state.subscribers.add(setValue);
71
- return () => {
72
- state.subscribers.delete(setValue);
73
- };
74
- }, []);
75
-
76
- return value;
77
- }