@preprio/toolkit 0.5.0-rc.4 → 0.5.0-rc.5
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/README.md +226 -153
- package/dist/astro/index.cjs +1 -1
- package/dist/astro/index.js +1 -1
- package/dist/{chunk-AHBN6FQP.js → chunk-3X55JL52.js} +1 -1
- package/dist/{chunk-46ISDRHT.js → chunk-CMUFTCG3.js} +1 -1
- package/dist/{chunk-PDNCNTDB.js → chunk-SBLZANL5.js} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/nextjs/components.cjs +1 -1
- package/dist/nextjs/components.js +2 -2
- package/dist/nextjs/index.cjs +1 -1
- package/dist/nextjs/index.d.cts +2 -0
- package/dist/nextjs/index.d.ts +2 -0
- package/dist/nextjs/index.js +1 -1
- package/dist/nextjs/live-action.js +2 -2
- package/dist/nextjs/live.cjs +4 -1
- package/dist/nextjs/live.d.cts +3 -1
- package/dist/nextjs/live.d.ts +3 -1
- package/dist/nextjs/live.js +4 -1
- package/dist/nuxt/index.cjs +1 -1
- package/dist/nuxt/index.js +1 -1
- package/dist/react/components.cjs +1 -1
- package/dist/react/components.js +1 -1
- package/dist/sveltekit/index.cjs +1 -1
- package/dist/sveltekit/index.js +1 -1
- package/dist/sveltekit/live-preview-O2ZTJNW4.js +1520 -0
- package/dist/sveltekit/live.cjs +1731 -0
- package/dist/sveltekit/live.d.cts +59 -0
- package/dist/sveltekit/live.d.ts +59 -0
- package/dist/sveltekit/live.js +97 -0
- package/package.json +6 -1
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `normalizeQuery` accepts as the query: GraphQL source text, a parsed
|
|
3
|
+
* `DocumentNode` (from `graphql-tag`, Apollo or GraphQL Code Generator), or a
|
|
4
|
+
* string-like object such as codegen's `TypedDocumentString`.
|
|
5
|
+
*/
|
|
6
|
+
type QueryInput = string | {
|
|
7
|
+
kind: 'Document';
|
|
8
|
+
} | {
|
|
9
|
+
toString(): string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
interface NormalizedQueryOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Pass your preview flag; never `true` on a production deployment.
|
|
15
|
+
* Defaults to false: the data is returned unchanged.
|
|
16
|
+
*/
|
|
17
|
+
enabled?: boolean;
|
|
18
|
+
/** Called when the editor saves, e.g. `invalidateAll` from `$app/navigation`. */
|
|
19
|
+
onSave?: () => void;
|
|
20
|
+
/** Exact editor origins, replacing the `*.prepr.io` default. */
|
|
21
|
+
allowedEditorOrigins?: string[];
|
|
22
|
+
/** See the toolbar's `strictMode`. */
|
|
23
|
+
strictMode?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Svelte's store contract, written out so this module needs no build-time
|
|
27
|
+
* dependency on `svelte`. Read it with `$store` in a component.
|
|
28
|
+
*/
|
|
29
|
+
interface NormalizedQueryStore<T> {
|
|
30
|
+
subscribe(run: (value: T) => void): () => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Show the Prepr editor's unsaved changes in a SvelteKit page, applied in the
|
|
34
|
+
* browser while the editor types:
|
|
35
|
+
*
|
|
36
|
+
* ```svelte
|
|
37
|
+
* <script lang="ts">
|
|
38
|
+
* import { invalidateAll } from '$app/navigation';
|
|
39
|
+
* import { normalizedQuery } from '@preprio/toolkit/sveltekit/live';
|
|
40
|
+
*
|
|
41
|
+
* let { data } = $props();
|
|
42
|
+
* const result = $derived(
|
|
43
|
+
* normalizedQuery(GetPageDocument, data.result, {
|
|
44
|
+
* enabled: data.preview,
|
|
45
|
+
* onSave: invalidateAll,
|
|
46
|
+
* }),
|
|
47
|
+
* );
|
|
48
|
+
* </script>
|
|
49
|
+
*
|
|
50
|
+
* <h1>{$result.Page.title}</h1>
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* Returns a store that holds the data unchanged on the server and outside
|
|
54
|
+
* the editor iframe. Select `_id` on every item and component that should
|
|
55
|
+
* update.
|
|
56
|
+
*/
|
|
57
|
+
declare function normalizedQuery<T>(query: QueryInput, data: T, options?: NormalizedQueryOptions): NormalizedQueryStore<T>;
|
|
58
|
+
|
|
59
|
+
export { type NormalizedQueryOptions, type NormalizedQueryStore, normalizedQuery };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `normalizeQuery` accepts as the query: GraphQL source text, a parsed
|
|
3
|
+
* `DocumentNode` (from `graphql-tag`, Apollo or GraphQL Code Generator), or a
|
|
4
|
+
* string-like object such as codegen's `TypedDocumentString`.
|
|
5
|
+
*/
|
|
6
|
+
type QueryInput = string | {
|
|
7
|
+
kind: 'Document';
|
|
8
|
+
} | {
|
|
9
|
+
toString(): string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
interface NormalizedQueryOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Pass your preview flag; never `true` on a production deployment.
|
|
15
|
+
* Defaults to false: the data is returned unchanged.
|
|
16
|
+
*/
|
|
17
|
+
enabled?: boolean;
|
|
18
|
+
/** Called when the editor saves, e.g. `invalidateAll` from `$app/navigation`. */
|
|
19
|
+
onSave?: () => void;
|
|
20
|
+
/** Exact editor origins, replacing the `*.prepr.io` default. */
|
|
21
|
+
allowedEditorOrigins?: string[];
|
|
22
|
+
/** See the toolbar's `strictMode`. */
|
|
23
|
+
strictMode?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Svelte's store contract, written out so this module needs no build-time
|
|
27
|
+
* dependency on `svelte`. Read it with `$store` in a component.
|
|
28
|
+
*/
|
|
29
|
+
interface NormalizedQueryStore<T> {
|
|
30
|
+
subscribe(run: (value: T) => void): () => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Show the Prepr editor's unsaved changes in a SvelteKit page, applied in the
|
|
34
|
+
* browser while the editor types:
|
|
35
|
+
*
|
|
36
|
+
* ```svelte
|
|
37
|
+
* <script lang="ts">
|
|
38
|
+
* import { invalidateAll } from '$app/navigation';
|
|
39
|
+
* import { normalizedQuery } from '@preprio/toolkit/sveltekit/live';
|
|
40
|
+
*
|
|
41
|
+
* let { data } = $props();
|
|
42
|
+
* const result = $derived(
|
|
43
|
+
* normalizedQuery(GetPageDocument, data.result, {
|
|
44
|
+
* enabled: data.preview,
|
|
45
|
+
* onSave: invalidateAll,
|
|
46
|
+
* }),
|
|
47
|
+
* );
|
|
48
|
+
* </script>
|
|
49
|
+
*
|
|
50
|
+
* <h1>{$result.Page.title}</h1>
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* Returns a store that holds the data unchanged on the server and outside
|
|
54
|
+
* the editor iframe. Select `_id` on every item and component that should
|
|
55
|
+
* update.
|
|
56
|
+
*/
|
|
57
|
+
declare function normalizedQuery<T>(query: QueryInput, data: T, options?: NormalizedQueryOptions): NormalizedQueryStore<T>;
|
|
58
|
+
|
|
59
|
+
export { type NormalizedQueryOptions, type NormalizedQueryStore, normalizedQuery };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/core/live-preview/test-reset.ts
|
|
2
|
+
var resets = /* @__PURE__ */ new Set();
|
|
3
|
+
function onTestReset(reset) {
|
|
4
|
+
resets.add(reset);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// src/sveltekit/live.ts
|
|
8
|
+
var starting = null;
|
|
9
|
+
var store = null;
|
|
10
|
+
var bridge = null;
|
|
11
|
+
var applyLog = null;
|
|
12
|
+
var log = [];
|
|
13
|
+
var allowedEditorOrigins;
|
|
14
|
+
var strictMode;
|
|
15
|
+
var logListeners = /* @__PURE__ */ new Set();
|
|
16
|
+
var startListeners = /* @__PURE__ */ new Set();
|
|
17
|
+
function isFramed() {
|
|
18
|
+
return typeof window !== "undefined" && window.self !== window.top;
|
|
19
|
+
}
|
|
20
|
+
function startLive() {
|
|
21
|
+
starting ?? (starting = import("./live-preview-O2ZTJNW4.js").then(
|
|
22
|
+
({ applyPatchLog, createLiveBridge, createLiveStore }) => {
|
|
23
|
+
applyLog = applyPatchLog;
|
|
24
|
+
const started = createLiveStore();
|
|
25
|
+
bridge = createLiveBridge(started, {
|
|
26
|
+
allowedEditorOrigins: () => allowedEditorOrigins,
|
|
27
|
+
strictMode: () => strictMode
|
|
28
|
+
});
|
|
29
|
+
bridge.start();
|
|
30
|
+
started.onLogChange((next) => {
|
|
31
|
+
log = next;
|
|
32
|
+
logListeners.forEach((listener) => listener());
|
|
33
|
+
});
|
|
34
|
+
store = started;
|
|
35
|
+
startListeners.forEach((listener) => listener(started));
|
|
36
|
+
startListeners.clear();
|
|
37
|
+
}
|
|
38
|
+
));
|
|
39
|
+
}
|
|
40
|
+
function whenLive(listener) {
|
|
41
|
+
if (store) {
|
|
42
|
+
listener(store);
|
|
43
|
+
return () => {
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
startListeners.add(listener);
|
|
47
|
+
return () => startListeners.delete(listener);
|
|
48
|
+
}
|
|
49
|
+
function normalizedQuery(query, data, options = {}) {
|
|
50
|
+
if (!options.enabled || !isFramed()) {
|
|
51
|
+
return {
|
|
52
|
+
subscribe(run) {
|
|
53
|
+
run(data);
|
|
54
|
+
return () => {
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (options.strictMode) strictMode = true;
|
|
60
|
+
if (options.allowedEditorOrigins) {
|
|
61
|
+
allowedEditorOrigins = [...options.allowedEditorOrigins];
|
|
62
|
+
}
|
|
63
|
+
startLive();
|
|
64
|
+
const current = () => log.length && applyLog ? applyLog(query, data, log) : data;
|
|
65
|
+
return {
|
|
66
|
+
subscribe(run) {
|
|
67
|
+
run(current());
|
|
68
|
+
const onLog = () => run(current());
|
|
69
|
+
logListeners.add(onLog);
|
|
70
|
+
let offInvalidate = null;
|
|
71
|
+
const stopWaiting = options.onSave ? whenLive((started) => {
|
|
72
|
+
offInvalidate = started.onInvalidate(() => options.onSave?.());
|
|
73
|
+
}) : () => {
|
|
74
|
+
};
|
|
75
|
+
return () => {
|
|
76
|
+
logListeners.delete(onLog);
|
|
77
|
+
stopWaiting();
|
|
78
|
+
offInvalidate?.();
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
onTestReset(() => {
|
|
84
|
+
bridge?.stop();
|
|
85
|
+
bridge = null;
|
|
86
|
+
store = null;
|
|
87
|
+
starting = null;
|
|
88
|
+
applyLog = null;
|
|
89
|
+
log = [];
|
|
90
|
+
logListeners.clear();
|
|
91
|
+
startListeners.clear();
|
|
92
|
+
allowedEditorOrigins = void 0;
|
|
93
|
+
strictMode = void 0;
|
|
94
|
+
});
|
|
95
|
+
export {
|
|
96
|
+
normalizedQuery
|
|
97
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@preprio/toolkit",
|
|
3
|
-
"version": "0.5.0-rc.
|
|
3
|
+
"version": "0.5.0-rc.5",
|
|
4
4
|
"description": "Prepr personalization, A/B testing and preview toolkit for React, Next.js, Nuxt, Astro, SvelteKit and Express",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Prepr B.V. (https://prepr.io)",
|
|
@@ -85,6 +85,11 @@
|
|
|
85
85
|
"import": "./dist/sveltekit/index.js",
|
|
86
86
|
"require": "./dist/sveltekit/index.cjs"
|
|
87
87
|
},
|
|
88
|
+
"./sveltekit/live": {
|
|
89
|
+
"types": "./dist/sveltekit/live.d.ts",
|
|
90
|
+
"import": "./dist/sveltekit/live.js",
|
|
91
|
+
"require": "./dist/sveltekit/live.cjs"
|
|
92
|
+
},
|
|
88
93
|
"./sveltekit/components/*": "./src/sveltekit/*.svelte",
|
|
89
94
|
"./nuxt": {
|
|
90
95
|
"types": "./dist/nuxt/index.d.ts",
|