@preprio/toolkit 0.6.0-alpha.9 → 1.0.0-rc.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.
Files changed (116) hide show
  1. package/README.md +111 -1363
  2. package/dist/astro/index.cjs +1555 -182
  3. package/dist/astro/index.d.cts +201 -1
  4. package/dist/astro/index.d.ts +201 -1
  5. package/dist/astro/index.js +1557 -29
  6. package/dist/chunk-A2J7R4LR.js +256 -0
  7. package/dist/{chunk-5QIWVDP2.js → chunk-DBKZWOXA.js} +1 -1
  8. package/dist/{chunk-WQBKQG4R.js → chunk-EWXBE3JD.js} +6 -1
  9. package/dist/chunk-KM2GME62.js +1255 -0
  10. package/dist/{chunk-J66UOCK6.js → chunk-LJA5K4KM.js} +1680 -579
  11. package/dist/chunk-UTKH7DOI.js +58 -0
  12. package/dist/chunk-V7MCHPE7.js +85 -0
  13. package/dist/chunk-Y7JWV7WV.js +374 -0
  14. package/dist/codegen/index.cjs +82 -0
  15. package/dist/codegen/index.d.cts +70 -0
  16. package/dist/codegen/index.d.ts +70 -0
  17. package/dist/codegen/index.js +64 -0
  18. package/dist/index.cjs +3729 -553
  19. package/dist/index.d.cts +403 -19
  20. package/dist/index.d.ts +403 -19
  21. package/dist/index.js +345 -24
  22. package/dist/live-preview-HCBNBC6C.js +19 -0
  23. package/dist/nextjs/components.cjs +1872 -754
  24. package/dist/nextjs/components.d.cts +5 -51
  25. package/dist/nextjs/components.d.ts +5 -51
  26. package/dist/nextjs/components.js +8 -299
  27. package/dist/nextjs/index.cjs +1228 -181
  28. package/dist/nextjs/index.d.cts +247 -92
  29. package/dist/nextjs/index.d.ts +247 -92
  30. package/dist/nextjs/index.js +1629 -111
  31. package/dist/nextjs/live-action.cjs +298 -0
  32. package/dist/nextjs/live-action.d.cts +12 -0
  33. package/dist/nextjs/live-action.d.ts +12 -0
  34. package/dist/nextjs/live-action.js +68 -0
  35. package/dist/nextjs/live.cjs +2010 -0
  36. package/dist/nextjs/live.d.cts +54 -0
  37. package/dist/nextjs/live.d.ts +54 -0
  38. package/dist/nextjs/live.js +287 -0
  39. package/dist/nuxt/chunk-5RBZPRQN.js +350 -0
  40. package/dist/nuxt/chunk-LUKVPOJ2.js +32 -0
  41. package/dist/nuxt/index.cjs +15 -16
  42. package/dist/nuxt/index.d.cts +167 -6
  43. package/dist/nuxt/index.d.ts +167 -6
  44. package/dist/nuxt/index.js +402 -15
  45. package/dist/nuxt/live-preview-KO4IKWTX.js +1234 -0
  46. package/dist/nuxt/live.cjs +1928 -0
  47. package/dist/nuxt/live.d.cts +45 -0
  48. package/dist/nuxt/live.d.ts +45 -0
  49. package/dist/nuxt/live.js +216 -0
  50. package/dist/{types-BYH9XqmN.d.cts → pixel-DqU7NvgW.d.cts} +102 -5
  51. package/dist/{types-BYH9XqmN.d.ts → pixel-DqU7NvgW.d.ts} +102 -5
  52. package/dist/react/components.cjs +1865 -480
  53. package/dist/react/components.d.cts +43 -8
  54. package/dist/react/components.d.ts +43 -8
  55. package/dist/react/components.js +1865 -480
  56. package/dist/sveltekit/chunk-5RBZPRQN.js +350 -0
  57. package/dist/sveltekit/chunk-LUKVPOJ2.js +32 -0
  58. package/dist/sveltekit/index.cjs +15 -16
  59. package/dist/sveltekit/index.d.cts +111 -10
  60. package/dist/sveltekit/index.d.ts +111 -10
  61. package/dist/sveltekit/index.js +398 -15
  62. package/dist/sveltekit/live-preview-KO4IKWTX.js +1234 -0
  63. package/dist/sveltekit/live.cjs +1940 -0
  64. package/dist/sveltekit/live.d.cts +61 -0
  65. package/dist/sveltekit/live.d.ts +61 -0
  66. package/dist/sveltekit/live.js +220 -0
  67. package/package.json +132 -71
  68. package/src/astro/PreprToolbar.astro +7 -1
  69. package/src/astro/PreprTrackingPixel.astro +2 -4
  70. package/src/nuxt/PreprToolbar.vue +12 -1
  71. package/src/nuxt/PreprTrackingPixel.vue +2 -4
  72. package/src/sveltekit/PreprToolbar.svelte +17 -2
  73. package/src/sveltekit/PreprTrackingPixel.svelte +2 -4
  74. package/dist/astro/chunk-UN4JADNU.js +0 -173
  75. package/dist/astro/graphql.cjs +0 -1078
  76. package/dist/astro/graphql.d.cts +0 -300
  77. package/dist/astro/graphql.d.ts +0 -300
  78. package/dist/astro/graphql.js +0 -984
  79. package/dist/astro/index-XT_z-RYT.d.cts +0 -130
  80. package/dist/astro/index-XT_z-RYT.d.ts +0 -130
  81. package/dist/chunk-3NCEVJJ3.js +0 -10
  82. package/dist/chunk-S5RWVMDJ.js +0 -148
  83. package/dist/chunk-XL4Y7FGW.js +0 -499
  84. package/dist/graphql/codegen.cjs +0 -557
  85. package/dist/graphql/codegen.d.cts +0 -23
  86. package/dist/graphql/codegen.d.ts +0 -23
  87. package/dist/graphql/codegen.js +0 -99
  88. package/dist/graphql.cjs +0 -1020
  89. package/dist/graphql.d.cts +0 -335
  90. package/dist/graphql.d.ts +0 -335
  91. package/dist/graphql.js +0 -448
  92. package/dist/nextjs/chunk-CMV6ZP6E.js +0 -549
  93. package/dist/nextjs/graphql.cjs +0 -1836
  94. package/dist/nextjs/graphql.d.cts +0 -498
  95. package/dist/nextjs/graphql.d.ts +0 -498
  96. package/dist/nextjs/graphql.js +0 -1422
  97. package/dist/nextjs/server-BL-sqZvu.d.cts +0 -156
  98. package/dist/nextjs/server-BL-sqZvu.d.ts +0 -156
  99. package/dist/nuxt/chunk-XWOABVMW.js +0 -417
  100. package/dist/nuxt/graphql.cjs +0 -1085
  101. package/dist/nuxt/graphql.d.cts +0 -297
  102. package/dist/nuxt/graphql.d.ts +0 -297
  103. package/dist/nuxt/graphql.js +0 -985
  104. package/dist/nuxt/server-8v6OHbsw.d.cts +0 -158
  105. package/dist/nuxt/server-8v6OHbsw.d.ts +0 -158
  106. package/dist/pixel-DwW7cXJm.d.cts +0 -49
  107. package/dist/pixel-DwW7cXJm.d.ts +0 -49
  108. package/dist/server-Brr1UgtW.d.ts +0 -59
  109. package/dist/server-DJuS3YU0.d.cts +0 -59
  110. package/dist/sveltekit/chunk-3S5RA6G5.js +0 -413
  111. package/dist/sveltekit/graphql.cjs +0 -1091
  112. package/dist/sveltekit/graphql.d.cts +0 -302
  113. package/dist/sveltekit/graphql.d.ts +0 -302
  114. package/dist/sveltekit/graphql.js +0 -991
  115. package/dist/sveltekit/server-CO847mEV.d.cts +0 -98
  116. package/dist/sveltekit/server-CO847mEV.d.ts +0 -98
@@ -0,0 +1,54 @@
1
+ /** Executes a registered query. `query` is opaque — string or DocumentNode. */
2
+ interface LiveFetcher {
3
+ (query: unknown, variables?: Record<string, unknown>): Promise<unknown>;
4
+ }
5
+
6
+ interface LivePreviewConfig {
7
+ /**
8
+ * Pass your preview flag; never `true` on a production deployment.
9
+ * Defaults to false: nothing starts and `useLive` is a passthrough.
10
+ */
11
+ enabled?: boolean;
12
+ /** Runs registered queries against Prepr GraphQL in preview mode. */
13
+ fetcher?: LiveFetcher;
14
+ /** Exact editor origins, replacing the `*.prepr.io` default. */
15
+ allowedEditorOrigins?: string[];
16
+ /** Your preview token uses GraphQL strict mode; see the toolbar option. */
17
+ strictMode?: boolean;
18
+ }
19
+ interface PreprRefreshProps {
20
+ /** Pass your preview flag; never `true` on a production deployment. */
21
+ enabled?: boolean;
22
+ /** Exact editor origins, replacing the `*.prepr.io` default. */
23
+ allowedEditorOrigins?: string[];
24
+ /** Your preview token uses GraphQL strict mode; see the toolbar option. */
25
+ strictMode?: boolean;
26
+ }
27
+ interface UseLiveOptions {
28
+ /** Query (string or DocumentNode) passed as-is to the fetcher. */
29
+ query?: unknown;
30
+ variables?: Record<string, unknown>;
31
+ }
32
+ /**
33
+ * Turn live preview on for `useLive`. Call once on the client with your
34
+ * preview flag as `enabled`. Without a fetcher, saves do not refetch. Not
35
+ * needed when `PreprRefresh` is enabled.
36
+ */
37
+ declare function initLivePreview(config?: LivePreviewConfig): void;
38
+ /**
39
+ * Register a query result for live preview. Field changes re-render
40
+ * instantly; saves refetch `{ query, variables }` when given. A passthrough
41
+ * until live preview is enabled and outside the editor iframe. The data must
42
+ * select `_id` on every level that should update.
43
+ */
44
+ declare function useLive<T>(data: T, opts?: UseLiveOptions): T;
45
+ /**
46
+ * Keeps server components in step with the Prepr editor: unsaved changes
47
+ * re-render the route, where `normalizeQuery` applies them; a save refreshes
48
+ * it. Mount once in the root layout with your preview flag as `enabled`.
49
+ * Use this or `useLive` with a `query`, not both: a save would refetch and
50
+ * refresh.
51
+ */
52
+ declare function PreprRefresh({ enabled, allowedEditorOrigins: origins, strictMode: strict, }: PreprRefreshProps): null;
53
+
54
+ export { type LivePreviewConfig, PreprRefresh, type PreprRefreshProps, type UseLiveOptions, initLivePreview, useLive };
@@ -0,0 +1,54 @@
1
+ /** Executes a registered query. `query` is opaque — string or DocumentNode. */
2
+ interface LiveFetcher {
3
+ (query: unknown, variables?: Record<string, unknown>): Promise<unknown>;
4
+ }
5
+
6
+ interface LivePreviewConfig {
7
+ /**
8
+ * Pass your preview flag; never `true` on a production deployment.
9
+ * Defaults to false: nothing starts and `useLive` is a passthrough.
10
+ */
11
+ enabled?: boolean;
12
+ /** Runs registered queries against Prepr GraphQL in preview mode. */
13
+ fetcher?: LiveFetcher;
14
+ /** Exact editor origins, replacing the `*.prepr.io` default. */
15
+ allowedEditorOrigins?: string[];
16
+ /** Your preview token uses GraphQL strict mode; see the toolbar option. */
17
+ strictMode?: boolean;
18
+ }
19
+ interface PreprRefreshProps {
20
+ /** Pass your preview flag; never `true` on a production deployment. */
21
+ enabled?: boolean;
22
+ /** Exact editor origins, replacing the `*.prepr.io` default. */
23
+ allowedEditorOrigins?: string[];
24
+ /** Your preview token uses GraphQL strict mode; see the toolbar option. */
25
+ strictMode?: boolean;
26
+ }
27
+ interface UseLiveOptions {
28
+ /** Query (string or DocumentNode) passed as-is to the fetcher. */
29
+ query?: unknown;
30
+ variables?: Record<string, unknown>;
31
+ }
32
+ /**
33
+ * Turn live preview on for `useLive`. Call once on the client with your
34
+ * preview flag as `enabled`. Without a fetcher, saves do not refetch. Not
35
+ * needed when `PreprRefresh` is enabled.
36
+ */
37
+ declare function initLivePreview(config?: LivePreviewConfig): void;
38
+ /**
39
+ * Register a query result for live preview. Field changes re-render
40
+ * instantly; saves refetch `{ query, variables }` when given. A passthrough
41
+ * until live preview is enabled and outside the editor iframe. The data must
42
+ * select `_id` on every level that should update.
43
+ */
44
+ declare function useLive<T>(data: T, opts?: UseLiveOptions): T;
45
+ /**
46
+ * Keeps server components in step with the Prepr editor: unsaved changes
47
+ * re-render the route, where `normalizeQuery` applies them; a save refreshes
48
+ * it. Mount once in the root layout with your preview flag as `enabled`.
49
+ * Use this or `useLive` with a `query`, not both: a save would refetch and
50
+ * refresh.
51
+ */
52
+ declare function PreprRefresh({ enabled, allowedEditorOrigins: origins, strictMode: strict, }: PreprRefreshProps): null;
53
+
54
+ export { type LivePreviewConfig, PreprRefresh, type PreprRefreshProps, type UseLiveOptions, initLivePreview, useLive };
@@ -0,0 +1,287 @@
1
+ "use client";
2
+ import {
3
+ ENCODED_SELECTOR
4
+ } from "../chunk-UTKH7DOI.js";
5
+ import {
6
+ createScopedLogger
7
+ } from "../chunk-V7MCHPE7.js";
8
+
9
+ // src/nextjs/live.ts
10
+ import { useRouter } from "next/navigation";
11
+ import { useEffect, useState } from "react";
12
+
13
+ // src/core/live-preview/test-reset.ts
14
+ var resets = /* @__PURE__ */ new Set();
15
+ function onTestReset(reset) {
16
+ resets.add(reset);
17
+ }
18
+
19
+ // src/core/live-preview/starter.ts
20
+ function isFramed() {
21
+ return typeof window !== "undefined" && window.self !== window.top;
22
+ }
23
+ function createLiveStarter(options = {}) {
24
+ let starting = null;
25
+ let store = null;
26
+ let bridge = null;
27
+ let allowedEditorOrigins;
28
+ let strictMode = false;
29
+ const startListeners = /* @__PURE__ */ new Set();
30
+ onTestReset(() => {
31
+ bridge?.stop();
32
+ bridge = null;
33
+ store = null;
34
+ starting = null;
35
+ startListeners.clear();
36
+ allowedEditorOrigins = void 0;
37
+ strictMode = false;
38
+ options.onReset?.();
39
+ });
40
+ return {
41
+ configure(config) {
42
+ if (config.strictMode) strictMode = true;
43
+ if (config.allowedEditorOrigins) {
44
+ allowedEditorOrigins = [...config.allowedEditorOrigins];
45
+ }
46
+ },
47
+ start() {
48
+ starting ?? (starting = import("../live-preview-HCBNBC6C.js").then((runtime) => {
49
+ const started = runtime.createLiveStore(options.storeOptions?.());
50
+ options.onStart?.(started, runtime);
51
+ bridge = runtime.createLiveBridge(started, {
52
+ allowedEditorOrigins: () => allowedEditorOrigins,
53
+ strictMode: () => strictMode
54
+ });
55
+ bridge.start();
56
+ store = started;
57
+ startListeners.forEach((listener) => listener(started));
58
+ startListeners.clear();
59
+ return started;
60
+ }));
61
+ return starting;
62
+ },
63
+ whenLive(listener) {
64
+ if (store) {
65
+ listener(store);
66
+ return () => {
67
+ };
68
+ }
69
+ startListeners.add(listener);
70
+ return () => startListeners.delete(listener);
71
+ }
72
+ };
73
+ }
74
+
75
+ // src/nextjs/live.ts
76
+ import { syncPreprLiveLog } from "./live-action.js";
77
+ var debug = createScopedLogger("live-preview:next");
78
+ var LOG_SYNC_DEBOUNCE_MS = 150;
79
+ var starter = createLiveStarter({
80
+ storeOptions() {
81
+ const skipped = new Set(quarantined());
82
+ return { accept: (patch) => !skipped.has(patchKey(patch)) };
83
+ },
84
+ onStart: (store) => watchClientRenders(store),
85
+ onReset() {
86
+ recovering = false;
87
+ }
88
+ });
89
+ function initLivePreview(config = {}) {
90
+ if (!config.enabled || !isFramed()) return;
91
+ starter.configure(config);
92
+ const { fetcher } = config;
93
+ void starter.start().then((started) => {
94
+ if (fetcher) started.setFetcher(fetcher);
95
+ });
96
+ }
97
+ function useLive(data, opts) {
98
+ const [live, setLive] = useState(data);
99
+ useEffect(() => {
100
+ if (!isFramed()) return;
101
+ let unregister = null;
102
+ const stopWaiting = starter.whenLive((started) => {
103
+ const registration = started.register(data, opts);
104
+ setLive(registration.getSnapshot());
105
+ const unsubscribe = registration.subscribe(
106
+ () => setLive(registration.getSnapshot())
107
+ );
108
+ unregister = () => {
109
+ unsubscribe();
110
+ registration.unregister();
111
+ };
112
+ });
113
+ return () => {
114
+ stopWaiting();
115
+ unregister?.();
116
+ };
117
+ }, [data]);
118
+ return isFramed() ? live : data;
119
+ }
120
+ function PreprRefresh({
121
+ enabled = false,
122
+ allowedEditorOrigins: origins,
123
+ strictMode: strict = false
124
+ }) {
125
+ const router = useRouter();
126
+ const originsKey = origins?.join(" ");
127
+ useEffect(() => {
128
+ if (!enabled || !isFramed()) return;
129
+ starter.configure({
130
+ strictMode: strict,
131
+ allowedEditorOrigins: originsKey?.split(" ")
132
+ });
133
+ const sync = createLogSync();
134
+ let unsubscribe = null;
135
+ let active = true;
136
+ void starter.start().then((started) => {
137
+ if (!active) return;
138
+ const offLog = started.onLogChange((log) => {
139
+ if (log.length) sync.schedule(log);
140
+ else sync.reset();
141
+ });
142
+ const offInvalidate = started.onInvalidate(() => router.refresh());
143
+ unsubscribe = () => {
144
+ offLog();
145
+ offInvalidate();
146
+ };
147
+ });
148
+ return () => {
149
+ active = false;
150
+ unsubscribe?.();
151
+ sync.cancel();
152
+ };
153
+ }, [enabled, originsKey, strict, router]);
154
+ return null;
155
+ }
156
+ var STEGA_CHARS = /[\u200b-\u200d\u2060-\u2064\ufeff]/;
157
+ var RENDER_SETTLE_MS = 1e3;
158
+ var RENDER_POLL_MS = 100;
159
+ function showsPreprContent() {
160
+ return document.querySelector(ENCODED_SELECTOR) !== null || // Visible text only: inline scripts carry the route's data, edit links
161
+ // included, even when the route rendered its error page.
162
+ STEGA_CHARS.test(document.body?.innerText ?? "");
163
+ }
164
+ function watchUntilSettled(onBreak) {
165
+ const interval = setInterval(() => {
166
+ if (showsPreprContent()) return;
167
+ clearInterval(interval);
168
+ onBreak();
169
+ }, RENDER_POLL_MS);
170
+ return () => clearInterval(interval);
171
+ }
172
+ var QUARANTINE_KEY = "__prepr_live_quarantine";
173
+ var MAX_QUARANTINED = 50;
174
+ function quarantined() {
175
+ try {
176
+ const stored = JSON.parse(
177
+ sessionStorage.getItem(QUARANTINE_KEY) ?? "[]"
178
+ );
179
+ return Array.isArray(stored) ? stored.filter((key) => typeof key === "string") : [];
180
+ } catch {
181
+ return [];
182
+ }
183
+ }
184
+ function setQuarantined(keys) {
185
+ try {
186
+ if (keys.length) {
187
+ sessionStorage.setItem(
188
+ QUARANTINE_KEY,
189
+ JSON.stringify(keys.slice(-MAX_QUARANTINED))
190
+ );
191
+ } else {
192
+ sessionStorage.removeItem(QUARANTINE_KEY);
193
+ }
194
+ } catch {
195
+ }
196
+ }
197
+ var patchKey = (patch) => JSON.stringify(patch);
198
+ var CLIENT_RENDER_MS = 300;
199
+ var recovering = false;
200
+ function recoverFrom(log, lastGood) {
201
+ if (recovering) return;
202
+ recovering = true;
203
+ debug.warn("a live change broke the page; reloading without it");
204
+ const good = new Set(lastGood.map(patchKey));
205
+ const broken = log.map(patchKey).filter((key) => !good.has(key));
206
+ setQuarantined([...quarantined(), ...broken]);
207
+ window.location.reload();
208
+ }
209
+ function watchClientRenders(watched) {
210
+ let timer = null;
211
+ let lastGood = [];
212
+ watched.onLogChange((log) => {
213
+ if (timer) clearTimeout(timer);
214
+ timer = null;
215
+ if (!log.length) {
216
+ lastGood = [];
217
+ setQuarantined([]);
218
+ return;
219
+ }
220
+ if (!showsPreprContent()) return;
221
+ timer = setTimeout(() => {
222
+ timer = null;
223
+ if (showsPreprContent()) lastGood = log;
224
+ else recoverFrom(log, lastGood);
225
+ }, CLIENT_RENDER_MS);
226
+ });
227
+ }
228
+ function createLogSync() {
229
+ let timer = null;
230
+ let inFlight = false;
231
+ let pending = null;
232
+ let cancelled = false;
233
+ let lastGood = [];
234
+ const send = async (incoming) => {
235
+ const skipped = new Set(quarantined());
236
+ const log = skipped.size ? incoming.filter((patch) => !skipped.has(patchKey(patch))) : incoming;
237
+ if (!log.length) return;
238
+ inFlight = true;
239
+ const stopWatching = showsPreprContent() ? watchUntilSettled(() => recoverFrom(log, lastGood)) : () => {
240
+ };
241
+ let failed = false;
242
+ try {
243
+ await syncPreprLiveLog(log);
244
+ } catch (error) {
245
+ failed = true;
246
+ debug.log("sending unsaved changes failed", error);
247
+ }
248
+ await new Promise((resolve) => setTimeout(resolve, RENDER_SETTLE_MS));
249
+ stopWatching();
250
+ inFlight = false;
251
+ if (recovering) return;
252
+ if (!failed) lastGood = log;
253
+ if (pending && !cancelled) {
254
+ const next = pending;
255
+ pending = null;
256
+ void send(next);
257
+ }
258
+ };
259
+ return {
260
+ schedule(log) {
261
+ cancelled = false;
262
+ if (timer) clearTimeout(timer);
263
+ timer = setTimeout(() => {
264
+ timer = null;
265
+ if (inFlight) pending = log;
266
+ else void send(log);
267
+ }, LOG_SYNC_DEBOUNCE_MS);
268
+ },
269
+ cancel() {
270
+ cancelled = true;
271
+ pending = null;
272
+ if (timer) clearTimeout(timer);
273
+ timer = null;
274
+ },
275
+ /** After a save: the stored content is what renders now. */
276
+ reset() {
277
+ this.cancel();
278
+ lastGood = [];
279
+ setQuarantined([]);
280
+ }
281
+ };
282
+ }
283
+ export {
284
+ PreprRefresh,
285
+ initLivePreview,
286
+ useLive
287
+ };