@matterfact/embed 0.12.0 → 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.
Files changed (42) hide show
  1. package/README.md +41 -0
  2. package/dist/{chunk-M5TS546Z.js → chunk-C7DXO37G.js} +2 -2
  3. package/dist/{chunk-ZXMJWCQV.js → chunk-JLJG3MDX.js} +57 -35
  4. package/dist/chunk-JLJG3MDX.js.map +1 -0
  5. package/dist/chunk-QZCUQZJK.js +3 -0
  6. package/dist/chunk-QZCUQZJK.js.map +7 -0
  7. package/dist/{chunk-C4XGE6BO.js → chunk-S6OT47DL.js} +56 -34
  8. package/dist/chunk-S6OT47DL.js.map +1 -0
  9. package/dist/{chunk-WAUSVV7Y.js → chunk-UA4Y2O64.js} +2 -2
  10. package/dist/{context-4LP3EDXL.js → context-DAXACLSP.js} +3 -3
  11. package/dist/{context-4LP3EDXL.js.map → context-DAXACLSP.js.map} +1 -1
  12. package/dist/{context-ZSPDZWIB.js → context-IJK345PN.js} +2 -2
  13. package/dist/embed.js +1 -1
  14. package/dist/embed.js.map +3 -3
  15. package/dist/index.cjs +82 -32
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +73 -2
  18. package/dist/index.d.ts +73 -2
  19. package/dist/index.js +29 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/react.cjs +281 -55
  22. package/dist/react.cjs.map +1 -1
  23. package/dist/react.d.cts +168 -12
  24. package/dist/react.d.ts +168 -12
  25. package/dist/react.js +221 -19
  26. package/dist/react.js.map +1 -1
  27. package/dist/{snapshot-OR5SVEUD.js → snapshot-EVBNVVCT.js} +2 -2
  28. package/dist/{snapshot-X6FPP3HF.js → snapshot-P5SVKSPB.js} +3 -3
  29. package/dist/{snapshot-X6FPP3HF.js.map → snapshot-P5SVKSPB.js.map} +1 -1
  30. package/examples/embed-demo/.env.example +9 -0
  31. package/examples/embed-demo/README.md +66 -12
  32. package/examples/embed-demo/src/App.tsx +56 -12
  33. package/examples/embed-demo/src/config.ts +8 -0
  34. package/package.json +1 -1
  35. package/dist/chunk-C4XGE6BO.js.map +0 -1
  36. package/dist/chunk-SR4ZNUAN.js +0 -3
  37. package/dist/chunk-SR4ZNUAN.js.map +0 -7
  38. package/dist/chunk-ZXMJWCQV.js.map +0 -1
  39. /package/dist/{chunk-M5TS546Z.js.map → chunk-C7DXO37G.js.map} +0 -0
  40. /package/dist/{chunk-WAUSVV7Y.js.map → chunk-UA4Y2O64.js.map} +0 -0
  41. /package/dist/{context-ZSPDZWIB.js.map → context-IJK345PN.js.map} +0 -0
  42. /package/dist/{snapshot-OR5SVEUD.js.map → snapshot-EVBNVVCT.js.map} +0 -0
package/dist/react.cjs CHANGED
@@ -480,7 +480,7 @@ function mfTools(win) {
480
480
  if (typeof document2 === "function") {
481
481
  out.push({
482
482
  name: "mf.resolveDocument",
483
- description: 'Resolve a matterfact document (dossier, report, briefing) that is NOT on the current page to a concrete id. Call it with the document type and a key (e.g. a ticker) when the site map shows the document lives on another route. Returns {id, label, href}. Cite the result inline as <MFRef kind="document" id="<id>" label="<label>" /> \u2014 it renders as a chip that opens the document in the side panel; href is its "open on page" deeplink.',
483
+ description: `Resolve a matterfact document (dossier, report, briefing) that is NOT on the current page to a concrete id. Call it with the document type and the subject's key, when the site map shows the document lives on another route. For the key, use the current route parameter VERBATIM when the page context states one (e.g. ticker = BP~+LN+Equity \u2192 key "BP~+LN+Equity"): it is the host's own identifier and often differs from the ticker or name a person would write. Do not normalise, decode or re-spell it, and do not substitute what the user typed. Returns {id, label, href}. Cite the result inline as <MFRef kind="document" id="<id>" label="<label>" /> \u2014 it renders as a chip that opens the document in the side panel; href is its "open on page" deeplink.`,
484
484
  inputSchema: {
485
485
  type: "object",
486
486
  properties: {
@@ -1193,6 +1193,7 @@ async function resolveDeclaredContext() {
1193
1193
  async function publishContext() {
1194
1194
  if (!pageContextOn()) return;
1195
1195
  const declared = await resolveDeclaredContext();
1196
+ if (!pageContextOn()) return;
1196
1197
  send?.({ type: "host.context", context: buildPageContext(declared) });
1197
1198
  }
1198
1199
  function provideContext() {
@@ -1270,6 +1271,7 @@ function navigateHost(href) {
1270
1271
  location.assign(target.href);
1271
1272
  }
1272
1273
  function pushActivity(e) {
1274
+ if (!allowsAuto(effectivePageContextMode())) return;
1273
1275
  activity.push({ ...e, seq: ++activitySeq, ts: Date.now() });
1274
1276
  if (activity.length > MAX_ACTIVITY) activity.shift();
1275
1277
  send?.({ type: "host.activity", events: [activity[activity.length - 1]] });
@@ -1334,6 +1336,7 @@ function watchNavigation() {
1334
1336
  publishSitemap();
1335
1337
  publishTools();
1336
1338
  };
1339
+ if (navFire) return;
1337
1340
  navFire = fire;
1338
1341
  for (const name of ["pushState", "replaceState"]) {
1339
1342
  const orig = history[name];
@@ -1404,6 +1407,7 @@ async function sendSnapshot(emit) {
1404
1407
  return;
1405
1408
  }
1406
1409
  const { snapshot: snapshot2 } = await loadSnapshotModule();
1410
+ if (!allowsAuto(effectivePageContextMode())) return;
1407
1411
  const { yaml, truncated, visibleRefs } = snapshot2();
1408
1412
  emit({
1409
1413
  type: "host.snapshot",
@@ -1415,6 +1419,7 @@ async function sendSnapshot(emit) {
1415
1419
  async function sendRegion(ref, emit) {
1416
1420
  if (!allowsAuto(effectivePageContextMode())) return;
1417
1421
  const { snapshotRegion: snapshotRegion2, resolveRef: resolveRef2 } = await loadSnapshotModule();
1422
+ if (!allowsAuto(effectivePageContextMode())) return;
1418
1423
  const el = resolveRef2(ref);
1419
1424
  if (!el) {
1420
1425
  emit({
@@ -1454,13 +1459,39 @@ async function callTool(call, emit) {
1454
1459
  error: r.error
1455
1460
  });
1456
1461
  }
1457
- function stop() {
1462
+ function installAutoObservers() {
1463
+ document.addEventListener("click", onClick, { capture: true, passive: true });
1464
+ document.addEventListener("submit", onSubmit, {
1465
+ capture: true,
1466
+ passive: true
1467
+ });
1468
+ document.addEventListener("change", onChange, {
1469
+ capture: true,
1470
+ passive: true
1471
+ });
1472
+ document.addEventListener("selectionchange", scheduleFocus, { passive: true });
1473
+ document.addEventListener("focusin", scheduleFocus, {
1474
+ capture: true,
1475
+ passive: true
1476
+ });
1477
+ window.addEventListener("scroll", scheduleFocus, {
1478
+ capture: true,
1479
+ passive: true
1480
+ });
1481
+ }
1482
+ function removeAutoObservers() {
1458
1483
  document.removeEventListener("click", onClick, { capture: true });
1459
1484
  document.removeEventListener("submit", onSubmit, { capture: true });
1460
1485
  document.removeEventListener("change", onChange, { capture: true });
1461
1486
  document.removeEventListener("selectionchange", scheduleFocus);
1462
1487
  document.removeEventListener("focusin", scheduleFocus, { capture: true });
1463
1488
  window.removeEventListener("scroll", scheduleFocus, { capture: true });
1489
+ if (focusTimer) {
1490
+ clearTimeout(focusTimer);
1491
+ focusTimer = null;
1492
+ }
1493
+ }
1494
+ function unwatchNavigation() {
1464
1495
  if (navFire) {
1465
1496
  window.removeEventListener("popstate", navFire);
1466
1497
  navFire = null;
@@ -1473,10 +1504,10 @@ function stop() {
1473
1504
  history.replaceState = origReplaceState;
1474
1505
  origReplaceState = null;
1475
1506
  }
1476
- if (focusTimer) {
1477
- clearTimeout(focusTimer);
1478
- focusTimer = null;
1479
- }
1507
+ }
1508
+ function stop() {
1509
+ removeAutoObservers();
1510
+ unwatchNavigation();
1480
1511
  send = null;
1481
1512
  contextProvider = void 0;
1482
1513
  activity.length = 0;
@@ -1486,6 +1517,22 @@ function stop() {
1486
1517
  }
1487
1518
  function setPageContextMax(max) {
1488
1519
  adminPageContextMax = parsePageContextMode(max);
1520
+ applyObservation();
1521
+ }
1522
+ function applyObservation() {
1523
+ const mode = effectivePageContextMode();
1524
+ if (allowsAuto(mode)) {
1525
+ installAutoObservers();
1526
+ } else {
1527
+ removeAutoObservers();
1528
+ activity.length = 0;
1529
+ lastFocus = {};
1530
+ }
1531
+ if (allowsDeclared(mode)) {
1532
+ watchNavigation();
1533
+ } else {
1534
+ unwatchNavigation();
1535
+ }
1489
1536
  }
1490
1537
  function start(emit, origin, pageContext = true, provider) {
1491
1538
  stop();
@@ -1498,33 +1545,8 @@ function start(emit, origin, pageContext = true, provider) {
1498
1545
  void publishContext();
1499
1546
  publishArtifactGrants();
1500
1547
  publishSitemap();
1501
- watchNavigation();
1502
- if (allowsAuto(effectivePageContextMode())) {
1503
- document.addEventListener("click", onClick, {
1504
- capture: true,
1505
- passive: true
1506
- });
1507
- document.addEventListener("submit", onSubmit, {
1508
- capture: true,
1509
- passive: true
1510
- });
1511
- document.addEventListener("change", onChange, {
1512
- capture: true,
1513
- passive: true
1514
- });
1515
- document.addEventListener("selectionchange", scheduleFocus, {
1516
- passive: true
1517
- });
1518
- document.addEventListener("focusin", scheduleFocus, {
1519
- capture: true,
1520
- passive: true
1521
- });
1522
- window.addEventListener("scroll", scheduleFocus, {
1523
- capture: true,
1524
- passive: true
1525
- });
1526
- }
1527
1548
  }
1549
+ applyObservation();
1528
1550
  const theme = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
1529
1551
  emit({ type: "host.theme", mode: theme });
1530
1552
  {
@@ -1588,10 +1610,12 @@ var init_context = __esm({
1588
1610
  var react_exports = {};
1589
1611
  __export(react_exports, {
1590
1612
  MatterfactAgent: () => MatterfactAgent,
1591
- MatterfactArtifact: () => MatterfactArtifact
1613
+ MatterfactArtifact: () => MatterfactArtifact,
1614
+ MatterfactAuthProvider: () => MatterfactAuthProvider,
1615
+ MatterfactDoc: () => MatterfactDoc
1592
1616
  });
1593
1617
  module.exports = __toCommonJS(react_exports);
1594
- var import_react = require("react");
1618
+ var import_react2 = require("react");
1595
1619
 
1596
1620
  // src/protocol.ts
1597
1621
  var PROTOCOL_VERSION = 3;
@@ -2202,6 +2226,13 @@ var EmbedHost = class {
2202
2226
  case "widget.chat":
2203
2227
  this.emit({ type: "chat", phase: msg.phase, chatId: msg.chatId });
2204
2228
  break;
2229
+ case "widget.artifactParams":
2230
+ this.emit({
2231
+ type: "artifactParams",
2232
+ params: msg.params,
2233
+ source: msg.source
2234
+ });
2235
+ break;
2205
2236
  }
2206
2237
  }
2207
2238
  /**
@@ -2347,6 +2378,23 @@ var EmbedHost = class {
2347
2378
  this.queue = [];
2348
2379
  for (const m of pending) this.send(m);
2349
2380
  }
2381
+ /**
2382
+ * Push artifact param values into the widget — a PARTIAL patch; only the named
2383
+ * params change. Buffered like every other host→widget message until the widget
2384
+ * says it's listening.
2385
+ *
2386
+ * Explicit and deliberate, so it applies even to a param whose page-context binding
2387
+ * the viewer has overridden (spec §7.3).
2388
+ */
2389
+ setArtifactParams(params) {
2390
+ if (params == null || typeof params !== "object" || Array.isArray(params)) {
2391
+ console.error(
2392
+ "[matterfact] setArtifactParams expects an object of param values"
2393
+ );
2394
+ return;
2395
+ }
2396
+ this.send({ type: "host.artifactParams", params });
2397
+ }
2350
2398
  /** Tear down: stop listening and remove the host element. For the React wrapper's
2351
2399
  * unmount — the vanilla `<script>` loader lives for the page's lifetime and never
2352
2400
  * calls this. */
@@ -2364,12 +2412,70 @@ var EmbedHost = class {
2364
2412
  function mount(config) {
2365
2413
  const host = new EmbedHost(config);
2366
2414
  host.mount();
2415
+ window.matterfact = {
2416
+ ...window.matterfact ?? {},
2417
+ setArtifactParams: (params) => host.setArtifactParams(params)
2418
+ };
2367
2419
  return host;
2368
2420
  }
2369
2421
 
2370
- // src/react.tsx
2422
+ // src/react-context.tsx
2423
+ var import_react = require("react");
2371
2424
  var import_jsx_runtime = require("react/jsx-runtime");
2372
2425
  var DEFAULT_ORIGIN = "https://app.matterfact.com";
2426
+ var MatterfactAuthContext = (0, import_react.createContext)(null);
2427
+ function useMatterfactConfig() {
2428
+ return (0, import_react.useContext)(MatterfactAuthContext);
2429
+ }
2430
+ function MatterfactAuthProvider({
2431
+ publishableKey,
2432
+ widgetOrigin: widgetOrigin2,
2433
+ getAuthToken,
2434
+ theme,
2435
+ onEvent,
2436
+ children
2437
+ }) {
2438
+ const resolvedWidgetOrigin = widgetOrigin2 || DEFAULT_ORIGIN;
2439
+ const value = (0, import_react.useMemo)(
2440
+ () => ({
2441
+ publishableKey,
2442
+ widgetOrigin: resolvedWidgetOrigin,
2443
+ getAuthToken,
2444
+ theme: theme || "auto",
2445
+ onEvent
2446
+ }),
2447
+ [publishableKey, resolvedWidgetOrigin, getAuthToken, theme, onEvent]
2448
+ );
2449
+ (0, import_react.useEffect)(() => {
2450
+ if (typeof window === "undefined" || !getAuthToken) return;
2451
+ let inflight = null;
2452
+ const onMessage = (e) => {
2453
+ if (e.origin !== resolvedWidgetOrigin) return;
2454
+ if (!e.data || e.data.type !== "mf.hostAuth.request") return;
2455
+ const source = e.source;
2456
+ if (!source) return;
2457
+ try {
2458
+ inflight = inflight ?? Promise.resolve(getAuthToken()).catch(() => null);
2459
+ } catch {
2460
+ return;
2461
+ }
2462
+ const p = inflight;
2463
+ void p.then((token) => {
2464
+ if (p === inflight) inflight = null;
2465
+ if (typeof token === "string" && token) {
2466
+ source.postMessage({ type: "mf.hostAuth.grant", token }, resolvedWidgetOrigin);
2467
+ }
2468
+ });
2469
+ };
2470
+ window.addEventListener("message", onMessage);
2471
+ return () => window.removeEventListener("message", onMessage);
2472
+ }, [getAuthToken, resolvedWidgetOrigin]);
2473
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MatterfactAuthContext.Provider, { value, children });
2474
+ }
2475
+
2476
+ // src/react.tsx
2477
+ var import_jsx_runtime2 = require("react/jsx-runtime");
2478
+ var DEFAULT_ORIGIN2 = "https://app.matterfact.com";
2373
2479
  function writeActionsGlobal(actions) {
2374
2480
  if (typeof window === "undefined") return;
2375
2481
  const w = window;
@@ -2413,11 +2519,11 @@ function toolDescriptorKey(artifacts, resolve, tools) {
2413
2519
  });
2414
2520
  }
2415
2521
  function MatterfactAgent({
2416
- publishableKey,
2417
- widgetOrigin: widgetOrigin2,
2522
+ publishableKey: publishableKeyProp,
2523
+ widgetOrigin: widgetOriginProp,
2418
2524
  surface = "",
2419
- theme = "auto",
2420
- getAuthToken,
2525
+ theme: themeProp,
2526
+ getAuthToken: getAuthTokenProp,
2421
2527
  getPageContext,
2422
2528
  inline = false,
2423
2529
  className: className2,
@@ -2432,30 +2538,41 @@ function MatterfactAgent({
2432
2538
  onToolEvent,
2433
2539
  onEvent
2434
2540
  }) {
2435
- const authRef = (0, import_react.useRef)(getAuthToken);
2541
+ const ctx = useMatterfactConfig();
2542
+ const publishableKey = publishableKeyProp ?? ctx?.publishableKey;
2543
+ if (!publishableKey) {
2544
+ throw new Error(
2545
+ "MatterfactAgent requires publishableKey (as a prop or via MatterfactAuthProvider)"
2546
+ );
2547
+ }
2548
+ const widgetOrigin2 = widgetOriginProp ?? ctx?.widgetOrigin ?? DEFAULT_ORIGIN2;
2549
+ const getAuthToken = getAuthTokenProp ?? ctx?.getAuthToken;
2550
+ const theme = themeProp ?? ctx?.theme ?? "auto";
2551
+ const authRef = (0, import_react2.useRef)(getAuthToken);
2436
2552
  authRef.current = getAuthToken;
2437
- const pageContextRef = (0, import_react.useRef)(getPageContext);
2553
+ const pageContextRef = (0, import_react2.useRef)(getPageContext);
2438
2554
  pageContextRef.current = getPageContext;
2439
- const actionsRef = (0, import_react.useRef)(actions);
2555
+ const actionsRef = (0, import_react2.useRef)(actions);
2440
2556
  actionsRef.current = actions;
2441
2557
  const actionsKey = JSON.stringify(actions ?? null);
2442
- const slot = (0, import_react.useRef)(null);
2558
+ const slot = (0, import_react2.useRef)(null);
2443
2559
  const sitemapKey = JSON.stringify(sitemap ?? null);
2444
- (0, import_react.useEffect)(() => {
2560
+ (0, import_react2.useEffect)(() => {
2445
2561
  writeSitemapGlobal(sitemap);
2446
2562
  }, [sitemapKey]);
2447
- (0, import_react.useEffect)(() => {
2563
+ (0, import_react2.useEffect)(() => {
2448
2564
  writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent);
2449
2565
  });
2450
2566
  const toolsKey = toolDescriptorKey(artifacts, resolve, tools);
2451
- (0, import_react.useEffect)(() => {
2567
+ (0, import_react2.useEffect)(() => {
2452
2568
  if (typeof window === "undefined") return;
2453
2569
  if (inline && !slot.current) return;
2454
2570
  writeActionsGlobal(actionsRef.current);
2455
2571
  writeToolGlobals(artifacts, resolve, tools, onToolEvent, onEvent);
2456
2572
  const config = {
2457
2573
  publishableKey,
2458
- origin: widgetOrigin2 || DEFAULT_ORIGIN,
2574
+ // Already fully resolved above (prop ?? context ?? DEFAULT_ORIGIN).
2575
+ origin: widgetOrigin2,
2459
2576
  theme,
2460
2577
  surface,
2461
2578
  // Always present; a null return (no getAuthToken supplied) makes the core fall
@@ -2497,7 +2614,7 @@ function MatterfactAgent({
2497
2614
  // eslint-disable-next-line react-hooks/exhaustive-deps
2498
2615
  ]);
2499
2616
  if (!inline) return null;
2500
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
2617
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2501
2618
  "div",
2502
2619
  {
2503
2620
  ref: slot,
@@ -2508,30 +2625,139 @@ function MatterfactAgent({
2508
2625
  }
2509
2626
  var DEFAULT_ARTIFACT_ORIGIN = "https://app.matterfact.com";
2510
2627
  function MatterfactArtifact({
2628
+ name: nameProp,
2511
2629
  slug,
2512
2630
  owner,
2513
2631
  token,
2514
- widgetOrigin: widgetOrigin2,
2515
- theme = "auto",
2632
+ widgetOrigin: widgetOriginProp,
2633
+ theme: themeProp,
2516
2634
  className: className2,
2517
2635
  style
2518
2636
  }) {
2519
- const origin = widgetOrigin2 || DEFAULT_ARTIFACT_ORIGIN;
2520
- const src = `${origin}/embed/artifacts/${encodeURIComponent(slug)}?owner=${encodeURIComponent(owner)}&t=${encodeURIComponent(token)}&theme=${theme}`;
2521
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
2637
+ const ctx = useMatterfactConfig();
2638
+ const artifactName = nameProp ?? slug;
2639
+ if (!artifactName) {
2640
+ throw new Error(
2641
+ "MatterfactArtifact requires name (or its deprecated alias, slug)"
2642
+ );
2643
+ }
2644
+ const origin = widgetOriginProp ?? ctx?.widgetOrigin ?? DEFAULT_ARTIFACT_ORIGIN;
2645
+ const theme = themeProp ?? ctx?.theme ?? "auto";
2646
+ let src;
2647
+ if (token) {
2648
+ src = `${origin}/embed/artifacts/${encodeURIComponent(artifactName)}?owner=${encodeURIComponent(owner)}&t=${encodeURIComponent(token)}&theme=${theme}`;
2649
+ } else if (ctx) {
2650
+ const hostOrigin = typeof window === "undefined" ? "" : window.location.origin;
2651
+ src = `${origin}/embed/artifacts/${encodeURIComponent(artifactName)}?owner=${encodeURIComponent(owner)}&k=${encodeURIComponent(ctx.publishableKey)}&o=${encodeURIComponent(hostOrigin)}&theme=${theme}`;
2652
+ } else {
2653
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2654
+ "div",
2655
+ {
2656
+ className: className2,
2657
+ style: {
2658
+ width: "100%",
2659
+ height: 600,
2660
+ display: "flex",
2661
+ alignItems: "center",
2662
+ justifyContent: "center",
2663
+ textAlign: "center",
2664
+ ...style
2665
+ },
2666
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { children: "This embedded artifact link is incomplete." })
2667
+ }
2668
+ );
2669
+ }
2670
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2522
2671
  "iframe",
2523
2672
  {
2524
2673
  src,
2525
- title: `matterfact artifact ${slug}`,
2674
+ title: `matterfact artifact ${artifactName}`,
2526
2675
  className: className2,
2527
2676
  style: { width: "100%", height: 600, border: 0, ...style },
2528
2677
  sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox allow-downloads"
2529
2678
  }
2530
2679
  );
2531
2680
  }
2681
+ var DEFAULT_DOC_ORIGIN = "https://app.matterfact.com";
2682
+ function diffDocParams(params, sent) {
2683
+ const changed = {};
2684
+ for (const [key, value] of Object.entries(params)) {
2685
+ if (value === void 0) continue;
2686
+ const snapshot2 = JSON.stringify(value);
2687
+ if (sent.get(key) === snapshot2) continue;
2688
+ changed[key] = value;
2689
+ sent.set(key, snapshot2);
2690
+ }
2691
+ return changed;
2692
+ }
2693
+ function MatterfactDoc({
2694
+ docKey,
2695
+ ticker = "",
2696
+ version = "latest",
2697
+ theme: themeProp,
2698
+ params,
2699
+ className: className2,
2700
+ style
2701
+ }) {
2702
+ const ctx = useMatterfactConfig();
2703
+ const iframeRef = (0, import_react2.useRef)(null);
2704
+ const sentParamsRef = (0, import_react2.useRef)(/* @__PURE__ */ new Map());
2705
+ const origin = ctx?.widgetOrigin ?? DEFAULT_DOC_ORIGIN;
2706
+ const paramsKey = JSON.stringify(params ?? null);
2707
+ const pushParams = (force) => {
2708
+ if (!params) return;
2709
+ const win = iframeRef.current?.contentWindow;
2710
+ if (!win) return;
2711
+ if (force) sentParamsRef.current.clear();
2712
+ const changed = diffDocParams(params, sentParamsRef.current);
2713
+ if (Object.keys(changed).length === 0) return;
2714
+ win.postMessage({ type: "host.docParams", params: changed }, origin);
2715
+ };
2716
+ (0, import_react2.useEffect)(() => {
2717
+ pushParams(false);
2718
+ }, [paramsKey]);
2719
+ if (!docKey) {
2720
+ throw new Error("MatterfactDoc requires docKey");
2721
+ }
2722
+ if (!ctx || !ctx.publishableKey) {
2723
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2724
+ "div",
2725
+ {
2726
+ className: className2,
2727
+ style: {
2728
+ width: "100%",
2729
+ height: 600,
2730
+ display: "flex",
2731
+ alignItems: "center",
2732
+ justifyContent: "center",
2733
+ textAlign: "center",
2734
+ ...style
2735
+ },
2736
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { children: "This embedded document link is incomplete." })
2737
+ }
2738
+ );
2739
+ }
2740
+ const theme = themeProp ?? ctx.theme ?? "auto";
2741
+ const hostOrigin = typeof window === "undefined" ? "" : window.location.origin;
2742
+ const src = `${origin}/embed/document?k=${encodeURIComponent(ctx.publishableKey)}&o=${encodeURIComponent(hostOrigin)}&key=${encodeURIComponent(docKey)}&ticker=${encodeURIComponent(ticker)}&version=${encodeURIComponent(version)}&theme=${theme}`;
2743
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
2744
+ "iframe",
2745
+ {
2746
+ ref: iframeRef,
2747
+ src,
2748
+ title: `matterfact document ${docKey}`,
2749
+ className: className2,
2750
+ style: { width: "100%", height: 600, border: 0, ...style },
2751
+ sandbox: "allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox allow-downloads",
2752
+ onLoad: () => pushParams(true)
2753
+ }
2754
+ );
2755
+ }
2532
2756
  // Annotate the CommonJS export names for ESM import in node:
2533
2757
  0 && (module.exports = {
2534
2758
  MatterfactAgent,
2535
- MatterfactArtifact
2759
+ MatterfactArtifact,
2760
+ MatterfactAuthProvider,
2761
+ MatterfactDoc
2536
2762
  });
2537
2763
  //# sourceMappingURL=react.cjs.map