@matterfact/embed 0.17.0 → 0.18.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.
package/dist/index.cjs CHANGED
@@ -15,10 +15,70 @@ var __export = (target, all) => {
15
15
  // src/deeplink.ts
16
16
  var deeplink_exports = {};
17
17
  __export(deeplink_exports, {
18
+ compileDeeplink: () => compileDeeplink,
18
19
  compileDeeplinkFormat: () => compileDeeplinkFormat,
19
20
  installDeeplinkWatch: () => installDeeplinkWatch
20
21
  });
21
22
  function compileDeeplinkFormat(format) {
23
+ return compileDeeplink(format)?.match ?? null;
24
+ }
25
+ function compileDeeplink(format) {
26
+ if (format.includes(PATH_TOKEN)) return compileDynamic(format);
27
+ const match = compileStatic(format);
28
+ return match ? { match, mintFormat: () => format, dynamic: false } : null;
29
+ }
30
+ function compileDynamic(format) {
31
+ const pi = format.indexOf(PATH_TOKEN);
32
+ if (format.indexOf(PATH_TOKEN, pi + 1) >= 0) return null;
33
+ let prefix = format.slice(0, pi);
34
+ if (prefix.endsWith("/")) prefix = prefix.slice(0, -1);
35
+ const suffix = format.slice(pi + PATH_TOKEN.length);
36
+ let po;
37
+ try {
38
+ po = new URL(prefix);
39
+ } catch {
40
+ return null;
41
+ }
42
+ if (po.protocol !== "https:" && po.protocol !== "http:") return null;
43
+ if (po.origin !== prefix) return null;
44
+ if (!suffix.startsWith("?")) return null;
45
+ const c1 = suffix.indexOf(PLACEHOLDER);
46
+ if (c1 < 0 || suffix.indexOf(PLACEHOLDER, c1 + 1) >= 0) return null;
47
+ let probe;
48
+ try {
49
+ probe = new URL(prefix + "/__mfp__" + suffix.replace(PLACEHOLDER, SENTINEL));
50
+ } catch {
51
+ return null;
52
+ }
53
+ const keys = [...probe.searchParams.keys()];
54
+ if (new Set(keys).size !== keys.length) return null;
55
+ let queryKey = null;
56
+ for (const [k, v] of probe.searchParams) {
57
+ if (v === SENTINEL) queryKey = k;
58
+ }
59
+ if (!queryKey) return null;
60
+ const match = (href) => {
61
+ let u;
62
+ try {
63
+ u = new URL(href);
64
+ } catch {
65
+ return null;
66
+ }
67
+ if (u.origin !== prefix) return null;
68
+ for (const [k, v] of probe.searchParams) {
69
+ if (v === SENTINEL) continue;
70
+ if (u.searchParams.get(k) !== v) return null;
71
+ }
72
+ const token = u.searchParams.get(queryKey);
73
+ return token && TOKEN_RE.test(token) ? token : null;
74
+ };
75
+ return {
76
+ match,
77
+ mintFormat: (pathname) => prefix + pathname + suffix,
78
+ dynamic: true
79
+ };
80
+ }
81
+ function compileStatic(format) {
22
82
  const first = format.indexOf(PLACEHOLDER);
23
83
  if (first < 0 || format.indexOf(PLACEHOLDER, first + 1) >= 0) return null;
24
84
  let f;
@@ -69,18 +129,33 @@ function compileDeeplinkFormat(format) {
69
129
  return token && TOKEN_RE.test(token) ? token : null;
70
130
  };
71
131
  }
72
- function installDeeplinkWatch(format, onChange2) {
73
- const match = compileDeeplinkFormat(format);
74
- if (!match) return () => {
132
+ function installDeeplinkWatch(format, onChange2, onFormat) {
133
+ const c = compileDeeplink(format);
134
+ if (!c) return () => {
75
135
  };
76
136
  let last = null;
137
+ let lastTokenPath = null;
138
+ let lastMintPath = null;
77
139
  let stopped = false;
140
+ const emitFormat = () => {
141
+ if (!onFormat) return;
142
+ const p = location.pathname;
143
+ if (lastMintPath === null || c.dynamic && p !== lastMintPath) {
144
+ lastMintPath = p;
145
+ onFormat(c.mintFormat(p));
146
+ }
147
+ };
78
148
  const fire = () => {
79
149
  if (stopped) return;
80
150
  try {
81
- const token = match(location.href);
151
+ emitFormat();
152
+ const token = c.match(location.href);
153
+ if (token === null && last !== null && trimSlash(location.pathname) === lastTokenPath) {
154
+ return;
155
+ }
82
156
  if (token !== last) {
83
157
  last = token;
158
+ if (token !== null) lastTokenPath = trimSlash(location.pathname);
84
159
  onChange2(token);
85
160
  }
86
161
  } catch (err) {
@@ -110,10 +185,11 @@ function installDeeplinkWatch(format, onChange2) {
110
185
  if (history.replaceState === ourReplace) history.replaceState = origReplace;
111
186
  };
112
187
  }
113
- var PLACEHOLDER, SENTINEL, TOKEN_RE, trimSlash;
188
+ var PLACEHOLDER, PATH_TOKEN, SENTINEL, TOKEN_RE, trimSlash;
114
189
  var init_deeplink = __esm({
115
190
  "src/deeplink.ts"() {
116
191
  PLACEHOLDER = "{share_token}";
192
+ PATH_TOKEN = "{path}";
117
193
  SENTINEL = "mf-share-token-sentinel";
118
194
  TOKEN_RE = /^[A-Za-z0-9._~-]{4,256}$/;
119
195
  trimSlash = (path) => path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
@@ -1914,7 +1990,7 @@ function dockBox(g, vw, _vh) {
1914
1990
 
1915
1991
  // src/loader.ts
1916
1992
  var DEFAULT_ORIGIN = "https://app.matterfact.com";
1917
- var EMBED_VERSION = "0.17.0";
1993
+ var EMBED_VERSION = "0.18.0";
1918
1994
  function devRequested() {
1919
1995
  try {
1920
1996
  if (new URLSearchParams(location.search).get("mfdev") === "1") return true;
@@ -2103,12 +2179,13 @@ var EmbedHost = class {
2103
2179
  }
2104
2180
  const fmt = this.config.shareDeeplinkFormat;
2105
2181
  if (fmt) {
2106
- this.send({ type: "host.deeplinkFormat", format: fmt });
2107
2182
  void Promise.resolve().then(() => (init_deeplink(), deeplink_exports)).then((m) => {
2108
2183
  if (this.destroyed) return;
2109
- this.deeplinkStop = m.installDeeplinkWatch(fmt, (token) => {
2110
- this.send({ type: "host.openShare", token });
2111
- });
2184
+ this.deeplinkStop = m.installDeeplinkWatch(
2185
+ fmt,
2186
+ (token) => this.send({ type: "host.openShare", token }),
2187
+ (format) => this.send({ type: "host.deeplinkFormat", format })
2188
+ );
2112
2189
  }).catch(() => {
2113
2190
  });
2114
2191
  }