@preprio/toolkit 1.0.0-rc.4 → 1.0.0-rc.6

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 (37) hide show
  1. package/README.md +2 -0
  2. package/dist/astro/index.cjs +126 -18
  3. package/dist/astro/index.js +128 -19
  4. package/dist/{chunk-A2J7R4LR.js → chunk-I26P2EQI.js} +1 -1
  5. package/dist/{chunk-QWTSVPEQ.js → chunk-OCTRCNW2.js} +1 -1
  6. package/dist/{chunk-KM2GME62.js → chunk-RWFAFYHU.js} +90 -1
  7. package/dist/{chunk-6PMBN7OR.js → chunk-SAOE54OB.js} +1 -1
  8. package/dist/{chunk-K62RW46G.js → chunk-WQRPW3TM.js} +1 -1
  9. package/dist/index.cjs +128 -19
  10. package/dist/index.d.cts +8 -1
  11. package/dist/index.d.ts +8 -1
  12. package/dist/index.js +44 -23
  13. package/dist/{live-preview-HCBNBC6C.js → live-preview-NUDEDAYQ.js} +2 -2
  14. package/dist/nextjs/components.cjs +1 -1
  15. package/dist/nextjs/components.js +2 -2
  16. package/dist/nextjs/index.cjs +189 -20
  17. package/dist/nextjs/index.d.cts +47 -1
  18. package/dist/nextjs/index.d.ts +47 -1
  19. package/dist/nextjs/index.js +190 -21
  20. package/dist/nextjs/live-action.js +3 -3
  21. package/dist/nextjs/live.cjs +102 -6
  22. package/dist/nextjs/live.d.cts +7 -1
  23. package/dist/nextjs/live.d.ts +7 -1
  24. package/dist/nextjs/live.js +8 -6
  25. package/dist/nuxt/index.cjs +1 -1
  26. package/dist/nuxt/index.js +1 -1
  27. package/dist/nuxt/{live-preview-KO4IKWTX.js → live-preview-V2X6GPSK.js} +88 -1
  28. package/dist/nuxt/live.cjs +130 -17
  29. package/dist/nuxt/live.js +38 -18
  30. package/dist/react/components.cjs +1 -1
  31. package/dist/react/components.js +1 -1
  32. package/dist/sveltekit/index.cjs +1 -1
  33. package/dist/sveltekit/index.js +1 -1
  34. package/dist/sveltekit/{live-preview-KO4IKWTX.js → live-preview-V2X6GPSK.js} +88 -1
  35. package/dist/sveltekit/live.cjs +130 -17
  36. package/dist/sveltekit/live.js +38 -18
  37. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -51,7 +51,7 @@ __export(src_exports, {
51
51
  module.exports = __toCommonJS(src_exports);
52
52
 
53
53
  // src/version.ts
54
- var VERSION = "1.0.0-rc.4";
54
+ var VERSION = "1.0.0-rc.6";
55
55
 
56
56
  // src/core/features.ts
57
57
  function isEnabled(config) {
@@ -4048,6 +4048,94 @@ function indexSnapshot(data) {
4048
4048
 
4049
4049
  // src/core/live-preview/patch.ts
4050
4050
  var import_stega6 = require("@vercel/stega");
4051
+
4052
+ // src/core/live-preview/internal-links.ts
4053
+ var INTERNAL_HREF = /^[pa]uuid#/;
4054
+ var ANCHOR = /<a\b([^>]*)>([\s\S]*?)<\/a>/gi;
4055
+ var HREF = /(\shref\s*=\s*)(["'])(.*?)\2/i;
4056
+ var TAGS = /<[^>]+>/g;
4057
+ function anchorsIn(html) {
4058
+ const out = [];
4059
+ for (const match of html.matchAll(ANCHOR)) {
4060
+ const attributes = match[1];
4061
+ const inner = match[2];
4062
+ out.push({
4063
+ start: match.index ?? 0,
4064
+ end: (match.index ?? 0) + match[0].length,
4065
+ attributes,
4066
+ inner,
4067
+ href: HREF.exec(attributes)?.[3] ?? null,
4068
+ text: inner.replace(TAGS, "").trim()
4069
+ });
4070
+ }
4071
+ return out;
4072
+ }
4073
+ var isInternal = (anchor) => anchor.href !== null && INTERNAL_HREF.test(anchor.href);
4074
+ function fit(pushed, page) {
4075
+ if (!isInternal(pushed) && pushed.href !== page.href) return 0;
4076
+ if (isInternal(pushed) && page.href !== null && INTERNAL_HREF.test(page.href))
4077
+ return 0;
4078
+ return pushed.text === page.text ? 2 : 1;
4079
+ }
4080
+ function align(pushed, page) {
4081
+ const rows = pushed.length + 1;
4082
+ const cols = page.length + 1;
4083
+ const score = Array.from(
4084
+ { length: rows },
4085
+ () => new Array(cols).fill(0)
4086
+ );
4087
+ for (let i3 = pushed.length - 1; i3 >= 0; i3--) {
4088
+ for (let j3 = page.length - 1; j3 >= 0; j3--) {
4089
+ const pair = fit(pushed[i3], page[j3]);
4090
+ score[i3][j3] = Math.max(
4091
+ score[i3 + 1][j3],
4092
+ score[i3][j3 + 1],
4093
+ pair ? pair + score[i3 + 1][j3 + 1] : 0
4094
+ );
4095
+ }
4096
+ }
4097
+ const out = new Array(pushed.length).fill(-1);
4098
+ let i2 = 0;
4099
+ let j2 = 0;
4100
+ while (i2 < pushed.length && j2 < page.length) {
4101
+ const pair = fit(pushed[i2], page[j2]);
4102
+ if (pair && score[i2][j2] === pair + score[i2 + 1][j2 + 1]) {
4103
+ out[i2++] = j2++;
4104
+ } else if (score[i2 + 1][j2] >= score[i2][j2 + 1]) {
4105
+ i2++;
4106
+ } else {
4107
+ j2++;
4108
+ }
4109
+ }
4110
+ return out;
4111
+ }
4112
+ function resolveInternalLinks(current, value) {
4113
+ const pushed = anchorsIn(value);
4114
+ if (!pushed.some(isInternal)) return value;
4115
+ const page = typeof current === "string" ? anchorsIn(current) : [];
4116
+ const pairs = align(pushed, page);
4117
+ let out = "";
4118
+ let cursor = 0;
4119
+ pushed.forEach((anchor, i2) => {
4120
+ if (!isInternal(anchor)) return;
4121
+ out += value.slice(cursor, anchor.start);
4122
+ const match = pairs[i2] >= 0 ? page[pairs[i2]] : null;
4123
+ if (match?.href != null) {
4124
+ const href = match.href;
4125
+ const attributes = anchor.attributes.replace(
4126
+ HREF,
4127
+ (_all, lead, quote) => `${lead}${quote}${href}${quote}`
4128
+ );
4129
+ out += `<a${attributes}>${anchor.inner}</a>`;
4130
+ } else {
4131
+ out += anchor.inner;
4132
+ }
4133
+ cursor = anchor.end;
4134
+ });
4135
+ return out + value.slice(cursor);
4136
+ }
4137
+
4138
+ // src/core/live-preview/patch.ts
4051
4139
  var INDEX_SEGMENT2 = /^\d+$/;
4052
4140
  var FORBIDDEN_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
4053
4141
  function mergeObject(current, incoming) {
@@ -4216,7 +4304,8 @@ function writeAtPath(root, fullPath, value, markChanged = () => {
4216
4304
  } else if (!isPatchable(container[key])) {
4217
4305
  return root;
4218
4306
  } else {
4219
- next = keepEditLink(container[key], value);
4307
+ const linked = typeof value === "string" ? resolveInternalLinks(container[key], value) : value;
4308
+ next = keepEditLink(container[key], linked);
4220
4309
  }
4221
4310
  if (container[key] === next) return root;
4222
4311
  const newRoot = cloneNode(root);
@@ -5018,34 +5107,53 @@ function entryIdOf(element) {
5018
5107
  const first = element.items[0];
5019
5108
  return isObject(first) && typeof first._id === "string" ? first._id : void 0;
5020
5109
  }
5021
- function annotate(value, ctx) {
5022
- if (!ctx.entryId) return value;
5023
- const { cleaned, encoded } = (0, import_stega8.vercelStegaSplit)(value);
5024
- if (!encoded) return value;
5110
+ function encodeLink(payload) {
5111
+ try {
5112
+ return (0, import_stega8.legacyStegaEncode)(payload);
5113
+ } catch {
5114
+ return (0, import_stega8.vercelStegaEncode)(payload);
5115
+ }
5116
+ }
5117
+ function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
5025
5118
  let payload;
5026
5119
  try {
5027
5120
  payload = (0, import_stega8.vercelStegaDecode)(encoded);
5028
5121
  } catch {
5029
- return value;
5122
+ return "";
5030
5123
  }
5031
- if (typeof payload?.href !== "string") return value;
5124
+ if (typeof payload?.href !== "string") return "";
5032
5125
  let url;
5033
5126
  try {
5034
5127
  url = new URL(payload.href);
5035
5128
  } catch {
5036
- return value;
5129
+ return "";
5037
5130
  }
5038
- if (url.searchParams.has(ENTRY_PARAM)) return value;
5039
- url.searchParams.set(ENTRY_PARAM, ctx.entryId);
5131
+ if (url.searchParams.has(ENTRY_PARAM)) return "";
5132
+ url.searchParams.set(ENTRY_PARAM, entryId);
5040
5133
  const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
5041
- if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
5042
- url.searchParams.set("field", ctx.entryField);
5134
+ if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
5135
+ url.searchParams.set("field", entryField);
5043
5136
  }
5044
- return (0, import_stega8.vercelStegaCombine)(
5045
- cleaned,
5046
- { ...payload, href: url.toString() },
5047
- false
5048
- );
5137
+ return encodeLink({ ...payload, href: url.toString() });
5138
+ }
5139
+ var MAX_REWRITTEN = 1e4;
5140
+ var rewritten = /* @__PURE__ */ new Map();
5141
+ function annotate(value, ctx) {
5142
+ if (!ctx.entryId) return value;
5143
+ const { cleaned, encoded } = (0, import_stega8.vercelStegaSplit)(value);
5144
+ if (!encoded) return value;
5145
+ const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
5146
+ const key = `${ctx.entryId}
5147
+ ${entryField}
5148
+ ${ctx.entryOwnerId ?? ""}
5149
+ ${encoded}`;
5150
+ let link = rewritten.get(key);
5151
+ if (link === void 0) {
5152
+ link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
5153
+ if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
5154
+ rewritten.set(key, link);
5155
+ }
5156
+ return link ? cleaned + link : value;
5049
5157
  }
5050
5158
  function walk(value, selections, ctx) {
5051
5159
  if (typeof value === "string") return annotate(value, ctx);
@@ -5405,6 +5513,7 @@ function startHtmlLivePreview(options = {}) {
5405
5513
  strictMode: () => options.strictMode
5406
5514
  });
5407
5515
  bridge.start();
5516
+ const debounceMs = typeof options.syncDebounceMs === "number" && Number.isFinite(options.syncDebounceMs) && options.syncDebounceMs >= 0 ? options.syncDebounceMs : SYNC_DEBOUNCE_MS;
5408
5517
  let timer = null;
5409
5518
  let inFlight = false;
5410
5519
  let pending = null;
@@ -5446,7 +5555,7 @@ function startHtmlLivePreview(options = {}) {
5446
5555
  timer = null;
5447
5556
  if (inFlight) pending = log;
5448
5557
  else void render(log);
5449
- }, SYNC_DEBOUNCE_MS);
5558
+ }, debounceMs);
5450
5559
  };
5451
5560
  const offLog = store.onLogChange((log) => {
5452
5561
  if (log.length) schedule(log);
package/dist/index.d.cts CHANGED
@@ -5,7 +5,7 @@ export { g as PreprEnvironment, h as PreprEventType, i as PreprFeatureConfig, j
5
5
  * Package version; must match package.json. Lives in its own leaf module so
6
6
  * core can import it without cycling through the barrel.
7
7
  */
8
- declare const VERSION = "1.0.0-rc.4";
8
+ declare const VERSION = "1.0.0-rc.6";
9
9
 
10
10
  /**
11
11
  * Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
@@ -447,6 +447,13 @@ interface HtmlLivePreviewOptions {
447
447
  allowedEditorOrigins?: string[];
448
448
  /** See the toolbar's `strictMode`. */
449
449
  strictMode?: boolean;
450
+ /**
451
+ * How long typing must pause, in milliseconds, before the page is
452
+ * requested with the unsaved changes. Default 150. Lower shows changes
453
+ * sooner at the cost of more server renders; raise it when a render is
454
+ * expensive.
455
+ */
456
+ syncDebounceMs?: number;
450
457
  }
451
458
  /** Handle on a running HTML live preview. */
452
459
  interface HtmlLivePreview {
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export { g as PreprEnvironment, h as PreprEventType, i as PreprFeatureConfig, j
5
5
  * Package version; must match package.json. Lives in its own leaf module so
6
6
  * core can import it without cycling through the barrel.
7
7
  */
8
- declare const VERSION = "1.0.0-rc.4";
8
+ declare const VERSION = "1.0.0-rc.6";
9
9
 
10
10
  /**
11
11
  * Resolve a `PreprFeatures` bag to plain booleans, defaulting every omitted key
@@ -447,6 +447,13 @@ interface HtmlLivePreviewOptions {
447
447
  allowedEditorOrigins?: string[];
448
448
  /** See the toolbar's `strictMode`. */
449
449
  strictMode?: boolean;
450
+ /**
451
+ * How long typing must pause, in milliseconds, before the page is
452
+ * requested with the unsaved changes. Default 150. Lower shows changes
453
+ * sooner at the cost of more server renders; raise it when a render is
454
+ * expensive.
455
+ */
456
+ syncDebounceMs?: number;
450
457
  }
451
458
  /** Handle on a running HTML live preview. */
452
459
  interface HtmlLivePreview {
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  setTrackingParam,
9
9
  stegaClean,
10
10
  trackEvent
11
- } from "./chunk-K62RW46G.js";
11
+ } from "./chunk-WQRPW3TM.js";
12
12
  import {
13
13
  PreprError,
14
14
  extractAccessToken,
@@ -19,15 +19,15 @@ import {
19
19
  getPreprUUIDFromHeaders,
20
20
  getToolbarPropsFromHeaders,
21
21
  validatePreprToken
22
- } from "./chunk-6PMBN7OR.js";
22
+ } from "./chunk-SAOE54OB.js";
23
23
  import {
24
24
  VERSION,
25
25
  resolveFeatures
26
- } from "./chunk-QWTSVPEQ.js";
26
+ } from "./chunk-OCTRCNW2.js";
27
27
  import {
28
28
  createLiveBridge,
29
29
  createLiveStore
30
- } from "./chunk-A2J7R4LR.js";
30
+ } from "./chunk-I26P2EQI.js";
31
31
  import "./chunk-Y7JWV7WV.js";
32
32
  import {
33
33
  COOKIE_HUBSPOT,
@@ -48,7 +48,7 @@ import {
48
48
  normalizeQuery,
49
49
  parsePatchLog,
50
50
  readQueryDocument
51
- } from "./chunk-KM2GME62.js";
51
+ } from "./chunk-RWFAFYHU.js";
52
52
  import {
53
53
  createScopedLogger
54
54
  } from "./chunk-V7MCHPE7.js";
@@ -88,8 +88,9 @@ function stegaTarget(value, options = {}) {
88
88
 
89
89
  // src/core/live-preview/edit-links.ts
90
90
  import {
91
- vercelStegaCombine,
91
+ legacyStegaEncode,
92
92
  vercelStegaDecode,
93
+ vercelStegaEncode,
93
94
  vercelStegaSplit
94
95
  } from "@vercel/stega";
95
96
  var ENTRY_PARAM = "entry";
@@ -103,34 +104,53 @@ function entryIdOf(element) {
103
104
  const first = element.items[0];
104
105
  return isObject(first) && typeof first._id === "string" ? first._id : void 0;
105
106
  }
106
- function annotate(value, ctx) {
107
- if (!ctx.entryId) return value;
108
- const { cleaned, encoded } = vercelStegaSplit(value);
109
- if (!encoded) return value;
107
+ function encodeLink(payload) {
108
+ try {
109
+ return legacyStegaEncode(payload);
110
+ } catch {
111
+ return vercelStegaEncode(payload);
112
+ }
113
+ }
114
+ function rewriteLink(encoded, entryId, entryField, entryOwnerId) {
110
115
  let payload;
111
116
  try {
112
117
  payload = vercelStegaDecode(encoded);
113
118
  } catch {
114
- return value;
119
+ return "";
115
120
  }
116
- if (typeof payload?.href !== "string") return value;
121
+ if (typeof payload?.href !== "string") return "";
117
122
  let url;
118
123
  try {
119
124
  url = new URL(payload.href);
120
125
  } catch {
121
- return value;
126
+ return "";
122
127
  }
123
- if (url.searchParams.has(ENTRY_PARAM)) return value;
124
- url.searchParams.set(ENTRY_PARAM, ctx.entryId);
128
+ if (url.searchParams.has(ENTRY_PARAM)) return "";
129
+ url.searchParams.set(ENTRY_PARAM, entryId);
125
130
  const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
126
- if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
127
- url.searchParams.set("field", ctx.entryField);
131
+ if (!url.searchParams.has("field") && entryField && itemId === entryOwnerId) {
132
+ url.searchParams.set("field", entryField);
128
133
  }
129
- return vercelStegaCombine(
130
- cleaned,
131
- { ...payload, href: url.toString() },
132
- false
133
- );
134
+ return encodeLink({ ...payload, href: url.toString() });
135
+ }
136
+ var MAX_REWRITTEN = 1e4;
137
+ var rewritten = /* @__PURE__ */ new Map();
138
+ function annotate(value, ctx) {
139
+ if (!ctx.entryId) return value;
140
+ const { cleaned, encoded } = vercelStegaSplit(value);
141
+ if (!encoded) return value;
142
+ const entryField = ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) ? ctx.entryField : "";
143
+ const key = `${ctx.entryId}
144
+ ${entryField}
145
+ ${ctx.entryOwnerId ?? ""}
146
+ ${encoded}`;
147
+ let link = rewritten.get(key);
148
+ if (link === void 0) {
149
+ link = rewriteLink(encoded, ctx.entryId, entryField, ctx.entryOwnerId);
150
+ if (rewritten.size >= MAX_REWRITTEN) rewritten.clear();
151
+ rewritten.set(key, link);
152
+ }
153
+ return link ? cleaned + link : value;
134
154
  }
135
155
  function walk(value, selections, ctx) {
136
156
  if (typeof value === "string") return annotate(value, ctx);
@@ -262,6 +282,7 @@ function startHtmlLivePreview(options = {}) {
262
282
  strictMode: () => options.strictMode
263
283
  });
264
284
  bridge.start();
285
+ const debounceMs = typeof options.syncDebounceMs === "number" && Number.isFinite(options.syncDebounceMs) && options.syncDebounceMs >= 0 ? options.syncDebounceMs : SYNC_DEBOUNCE_MS;
265
286
  let timer = null;
266
287
  let inFlight = false;
267
288
  let pending = null;
@@ -303,7 +324,7 @@ function startHtmlLivePreview(options = {}) {
303
324
  timer = null;
304
325
  if (inFlight) pending = log;
305
326
  else void render(log);
306
- }, SYNC_DEBOUNCE_MS);
327
+ }, debounceMs);
307
328
  };
308
329
  const offLog = store.onLogChange((log) => {
309
330
  if (log.length) schedule(log);
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  createLiveBridge,
3
3
  createLiveStore
4
- } from "./chunk-A2J7R4LR.js";
4
+ } from "./chunk-I26P2EQI.js";
5
5
  import "./chunk-Y7JWV7WV.js";
6
6
  import "./chunk-UTKH7DOI.js";
7
7
  import {
8
8
  applyPatchLog,
9
9
  normalizeQuery,
10
10
  parsePatchLog
11
- } from "./chunk-KM2GME62.js";
11
+ } from "./chunk-RWFAFYHU.js";
12
12
  import "./chunk-V7MCHPE7.js";
13
13
  export {
14
14
  applyPatchLog,
@@ -3707,7 +3707,7 @@ function createPreprPreview(opts = {}) {
3707
3707
  var import_react = require("react");
3708
3708
 
3709
3709
  // src/version.ts
3710
- var VERSION = "1.0.0-rc.4";
3710
+ var VERSION = "1.0.0-rc.6";
3711
3711
 
3712
3712
  // src/core/pixel.ts
3713
3713
  var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
@@ -2,8 +2,8 @@
2
2
  import {
3
3
  createPreprPreview,
4
4
  loadTrackingPixel
5
- } from "../chunk-K62RW46G.js";
6
- import "../chunk-QWTSVPEQ.js";
5
+ } from "../chunk-WQRPW3TM.js";
6
+ import "../chunk-OCTRCNW2.js";
7
7
  import "../chunk-Y7JWV7WV.js";
8
8
  import "../chunk-UTKH7DOI.js";
9
9
  import "../chunk-V7MCHPE7.js";