@prismicio/svelte 0.1.1 → 1.0.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.
@@ -0,0 +1,30 @@
1
+ import * as prismic from "@prismicio/client";
2
+ /**
3
+ * Configures a Prismic client to automatically query draft content during a
4
+ * preview session.
5
+ *
6
+ * @param config - Configuration for the function.
7
+ */
8
+ export const enableAutoPreviews = (config) => {
9
+ if (!config.cookies) {
10
+ return;
11
+ }
12
+ const cookie = config.cookies.get(prismic.cookie.preview);
13
+ // We only return the cookie if a Prismic Preview session is active.
14
+ //
15
+ // An inactive cookie looks like this (URL encoded):
16
+ // {
17
+ // "_tracker": "abc123"
18
+ // }
19
+ //
20
+ // An active cookie looks like this (URL encoded):
21
+ // {
22
+ // "_tracker": "abc123",
23
+ // "example-prismic-repo.prismic.io": {
24
+ // preview: "https://example-prismic-repo.prismic.io/previews/abc:123?websitePreviewId=xyz"
25
+ // }
26
+ // }
27
+ if (cookie && /\.prismic\.io/.test(cookie)) {
28
+ config.client.queryContentFromRef(cookie);
29
+ }
30
+ };
@@ -0,0 +1,6 @@
1
+ export { default as PrismicPreview } from "./PrismicPreview.svelte";
2
+ export { enableAutoPreviews } from "./enableAutoPreviews";
3
+ export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews";
4
+ export { redirectToPreviewURL } from "./redirectToPreviewURL";
5
+ export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL";
6
+ export type { CreateClientConfig } from "./types";
@@ -0,0 +1,3 @@
1
+ export { default as PrismicPreview } from "./PrismicPreview.svelte";
2
+ export { enableAutoPreviews } from "./enableAutoPreviews";
3
+ export { redirectToPreviewURL } from "./redirectToPreviewURL";
@@ -0,0 +1,38 @@
1
+ import * as prismic from "@prismicio/client";
2
+ export type RedirectToPreviewURLConfig = {
3
+ /**
4
+ * The Prismic client configured for the preview session's repository.
5
+ */
6
+ client: Pick<prismic.Client, "resolvePreviewURL">;
7
+ /**
8
+ * The `request` object from a `+server` file.
9
+ *
10
+ * @see SvelteKit `+server` docs: \<https://kit.svelte.dev/docs/routing#server\>
11
+ */
12
+ request: Request;
13
+ /**
14
+ * The default redirect URL if a URL cannot be determined for the previewed
15
+ * document.
16
+ */
17
+ defaultURL?: string;
18
+ };
19
+ /**
20
+ * Redirects a visitor to the URL of a previewed Prismic document from within a
21
+ * SvelteKit `+server` file.
22
+ *
23
+ * Return the created `Response` in your `+server` file.
24
+ *
25
+ * @example
26
+ *
27
+ * ```typescript
28
+ * import { createClient } from "../lib/prismicio.js";
29
+ * import { redirectToPreviewURL } from "@prismicio/svelte/kit";
30
+ *
31
+ * export async function GET({ fetch, request }) {
32
+ * const client = createClient({ fetch });
33
+ *
34
+ * return await redirectToPreviewURL({ client, request });
35
+ * }
36
+ * ```
37
+ */
38
+ export declare const redirectToPreviewURL: (config: RedirectToPreviewURLConfig) => Promise<Response>;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Redirects a visitor to the URL of a previewed Prismic document from within a
3
+ * SvelteKit `+server` file.
4
+ *
5
+ * Return the created `Response` in your `+server` file.
6
+ *
7
+ * @example
8
+ *
9
+ * ```typescript
10
+ * import { createClient } from "../lib/prismicio.js";
11
+ * import { redirectToPreviewURL } from "@prismicio/svelte/kit";
12
+ *
13
+ * export async function GET({ fetch, request }) {
14
+ * const client = createClient({ fetch });
15
+ *
16
+ * return await redirectToPreviewURL({ client, request });
17
+ * }
18
+ * ```
19
+ */
20
+ export const redirectToPreviewURL = async (config) => {
21
+ const previewToken = new URL(config.request.url).searchParams.get("token") ?? undefined;
22
+ const documentID = new URL(config.request.url).searchParams.get("documentId") ?? undefined;
23
+ const previewURL = await config.client.resolvePreviewURL({
24
+ previewToken,
25
+ documentID,
26
+ defaultURL: config.defaultURL || "/",
27
+ });
28
+ return new Response(undefined, {
29
+ status: 307,
30
+ headers: {
31
+ Location: previewURL,
32
+ },
33
+ });
34
+ };
@@ -0,0 +1,13 @@
1
+ import { ClientConfig } from "@prismicio/client";
2
+ import { Cookies } from "@sveltejs/kit";
3
+ /**
4
+ * Configuration for creating a Prismic client with automatic preview support in
5
+ * SvelteKit apps.
6
+ */
7
+ export type CreateClientConfig = ClientConfig & {
8
+ /**
9
+ * The `cookies` object provided to a route's `load()` function. This parmeter
10
+ * is required to load previews.
11
+ */
12
+ cookies?: Cookies;
13
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/svelte",
3
- "version": "0.1.1",
3
+ "version": "1.0.0",
4
4
  "description": "Svelte components to present Prismic content.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -19,11 +19,26 @@
19
19
  "svelte": "./dist/index.js",
20
20
  "default": "./dist/index.js"
21
21
  },
22
+ "./kit": {
23
+ "types": "./dist/kit/index.d.ts",
24
+ "svelte": "./dist/kit/index.js",
25
+ "default": "./dist/kit/index.js"
26
+ },
22
27
  "./package.json": "./package.json"
23
28
  },
24
29
  "main": "./dist/index.js",
25
30
  "module": "./dist/index.js",
26
31
  "types": "./dist/index.d.ts",
32
+ "typesVersions": {
33
+ "*": {
34
+ "*": [
35
+ "dist/index.d.ts"
36
+ ],
37
+ "kit": [
38
+ "dist/kit/index.d.ts"
39
+ ]
40
+ }
41
+ },
27
42
  "files": [
28
43
  "./dist",
29
44
  "./src"
@@ -50,35 +65,42 @@
50
65
  "esm-env": "^1.0.0"
51
66
  },
52
67
  "devDependencies": {
53
- "@prismicio/client": "^7.1.1",
68
+ "@prismicio/client": "^7.2.0",
54
69
  "@prismicio/mock": "^0.3.1",
55
- "@size-limit/preset-small-lib": "^8.2.6",
56
- "@sveltejs/package": "^2.2.1",
57
- "@sveltejs/vite-plugin-svelte": "^2.4.5",
70
+ "@size-limit/preset-small-lib": "^9.0.0",
71
+ "@sveltejs/kit": "^1.25.1",
72
+ "@sveltejs/package": "^2.2.2",
73
+ "@sveltejs/vite-plugin-svelte": "^2.4.6",
58
74
  "@testing-library/svelte": "^4.0.3",
59
75
  "@trivago/prettier-plugin-sort-imports": "^4.2.0",
60
- "@typescript-eslint/eslint-plugin": "^6.4.1",
61
- "@typescript-eslint/parser": "^6.4.1",
62
- "@vitest/coverage-v8": "^0.34.2",
63
- "eslint": "^8.47.0",
76
+ "@typescript-eslint/eslint-plugin": "^6.7.4",
77
+ "@typescript-eslint/parser": "^6.7.4",
78
+ "@vitest/coverage-v8": "^0.34.6",
79
+ "eslint": "^8.50.0",
64
80
  "eslint-config-prettier": "^9.0.0",
65
81
  "eslint-plugin-prettier": "^5.0.0",
66
- "eslint-plugin-svelte": "^2.33.0",
82
+ "eslint-plugin-svelte": "^2.33.2",
67
83
  "eslint-plugin-tsdoc": "^0.2.17",
68
- "happy-dom": "^10.11.0",
69
- "prettier": "^3.0.2",
70
- "prettier-plugin-jsdoc": "^1.0.1",
84
+ "happy-dom": "^12.7.0",
85
+ "prettier": "^3.0.3",
86
+ "prettier-plugin-jsdoc": "^1.0.2",
71
87
  "prettier-plugin-svelte": "^3.0.3",
72
- "size-limit": "^8.2.6",
88
+ "size-limit": "^9.0.0",
73
89
  "standard-version": "^9.5.0",
74
90
  "svelte": "^4.2.0",
75
- "typescript": "^5.1.6",
76
- "vitest": "^0.34.2"
91
+ "typescript": "^5.2.2",
92
+ "vitest": "^0.34.6"
77
93
  },
78
94
  "peerDependencies": {
79
95
  "@prismicio/client": ">=7",
96
+ "@sveltejs/kit": ">=1",
80
97
  "svelte": ">=4"
81
98
  },
99
+ "peerDependenciesMeta": {
100
+ "@sveltejs/kit": {
101
+ "optional": true
102
+ }
103
+ },
82
104
  "engines": {
83
105
  "node": ">=14.15.0"
84
106
  },
@@ -48,9 +48,7 @@
48
48
  <span class={node.data.label}><slot /></span>
49
49
  {:else}
50
50
  {#each node.text.split("\n") as line, index}
51
- {#if index > 0}
52
- <br />
53
- {/if}
54
- {line}
51
+ <!-- This formatting is intentional to prevent unwanted whitespace between elements. -->
52
+ {#if index > 0}<br />{/if}{line}
55
53
  {/each}
56
54
  {/if}
@@ -1,5 +1,4 @@
1
1
  <script lang="ts">
2
- import type { RichTextNodeType } from "@prismicio/client";
3
2
  import type { asTree } from "@prismicio/richtext";
4
3
 
5
4
  import type { SvelteRichTextSerializer } from "../types";
@@ -9,28 +8,29 @@
9
8
  export let components: SvelteRichTextSerializer = {};
10
9
  export let children: ReturnType<typeof asTree>["children"];
11
10
 
12
- const rewrittenNodeTypes: Partial<
13
- Record<
14
- (typeof RichTextNodeType)[keyof typeof RichTextNodeType],
15
- keyof typeof RichTextNodeType
16
- >
17
- > = {
11
+ const CHILD_TYPE_RENAMES = {
18
12
  "list-item": "listItem",
19
13
  "o-list-item": "oListItem",
20
14
  "group-list-item": "list",
21
15
  "group-o-list-item": "oList",
22
- };
16
+ } as const;
17
+
18
+ function getComponent(child: ReturnType<typeof asTree>["children"][number]) {
19
+ return (
20
+ components[
21
+ CHILD_TYPE_RENAMES[child.type as keyof typeof CHILD_TYPE_RENAMES] ||
22
+ (child.type as keyof typeof components)
23
+ ] || DefaultComponent
24
+ );
25
+ }
23
26
  </script>
24
27
 
25
28
  {#each children as child}
26
- <svelte:component
27
- this={components[rewrittenNodeTypes[child.type] || child.type] ||
28
- DefaultComponent}
29
- node={child.node}
30
- key={child.key}
29
+ <svelte:component this={getComponent(child)} node={child.node}>
30
+ <!-- This formatting is intentional to prevent unwanted whitespace between elements. -->
31
+ {#if child.children.length > 0}<svelte:self
32
+ children={child.children}
33
+ {components}
34
+ />{/if}</svelte:component
31
35
  >
32
- {#if child.children.length > 0}
33
- <svelte:self children={child.children} {components} />
34
- {/if}
35
- </svelte:component>
36
36
  {/each}
@@ -0,0 +1,39 @@
1
+ <script lang="ts">
2
+ import { onMount } from "svelte";
3
+ import { getToolbarSrc } from "@prismicio/client";
4
+ import { invalidateAll } from "$app/navigation";
5
+
6
+ /**
7
+ * The name of your Prismic repository. A Prismic Toolbar will be registered
8
+ * using this repository.
9
+ */
10
+ export let repositoryName: string;
11
+
12
+ $: toolbarSrc = getToolbarSrc(repositoryName);
13
+
14
+ function prismicPreviewEventHandler(event: Event) {
15
+ event.preventDefault();
16
+
17
+ invalidateAll();
18
+ }
19
+
20
+ onMount(() => {
21
+ window.addEventListener("prismicPreviewUpdate", prismicPreviewEventHandler);
22
+ window.addEventListener("prismicPreviewEnd", prismicPreviewEventHandler);
23
+
24
+ return () => {
25
+ window.removeEventListener(
26
+ "prismicPreviewUpdate",
27
+ prismicPreviewEventHandler,
28
+ );
29
+ window.removeEventListener(
30
+ "prismicPreviewEnd",
31
+ prismicPreviewEventHandler,
32
+ );
33
+ };
34
+ });
35
+ </script>
36
+
37
+ <svelte:head>
38
+ <script defer src={toolbarSrc}></script>
39
+ </svelte:head>
@@ -0,0 +1,52 @@
1
+ import * as prismic from "@prismicio/client";
2
+ import { Cookies } from "@sveltejs/kit";
3
+
4
+ /**
5
+ * Configuration for `enableAutoPreviews`.
6
+ */
7
+ export type EnableAutoPreviewsConfig = {
8
+ /**
9
+ * Prismic client with which automatic previews will be enabled.
10
+ */
11
+ // `Pick` is used to use the smallest possible subset of `Client`. Doing
12
+ // this reduces the surface area for breaking type changes.
13
+ client: Pick<prismic.Client, "queryContentFromRef">;
14
+
15
+ /**
16
+ * The `cookies` object provided to a route's `load()` function. This parmeter
17
+ * is required to load previews.
18
+ */
19
+ cookies?: Cookies;
20
+ };
21
+
22
+ /**
23
+ * Configures a Prismic client to automatically query draft content during a
24
+ * preview session.
25
+ *
26
+ * @param config - Configuration for the function.
27
+ */
28
+ export const enableAutoPreviews = (config: EnableAutoPreviewsConfig): void => {
29
+ if (!config.cookies) {
30
+ return;
31
+ }
32
+
33
+ const cookie = config.cookies.get(prismic.cookie.preview);
34
+
35
+ // We only return the cookie if a Prismic Preview session is active.
36
+ //
37
+ // An inactive cookie looks like this (URL encoded):
38
+ // {
39
+ // "_tracker": "abc123"
40
+ // }
41
+ //
42
+ // An active cookie looks like this (URL encoded):
43
+ // {
44
+ // "_tracker": "abc123",
45
+ // "example-prismic-repo.prismic.io": {
46
+ // preview: "https://example-prismic-repo.prismic.io/previews/abc:123?websitePreviewId=xyz"
47
+ // }
48
+ // }
49
+ if (cookie && /\.prismic\.io/.test(cookie)) {
50
+ config.client.queryContentFromRef(cookie);
51
+ }
52
+ };
@@ -0,0 +1,9 @@
1
+ export { default as PrismicPreview } from "./PrismicPreview.svelte";
2
+
3
+ export { enableAutoPreviews } from "./enableAutoPreviews";
4
+ export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews";
5
+
6
+ export { redirectToPreviewURL } from "./redirectToPreviewURL";
7
+ export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL";
8
+
9
+ export type { CreateClientConfig } from "./types";
@@ -0,0 +1,65 @@
1
+ import * as prismic from "@prismicio/client";
2
+
3
+ export type RedirectToPreviewURLConfig = {
4
+ /**
5
+ * The Prismic client configured for the preview session's repository.
6
+ */
7
+ // `Pick` is used to use the smallest possible subset of
8
+ // `prismic.Client`. Doing this reduces the surface area for breaking
9
+ // type changes.
10
+ client: Pick<prismic.Client, "resolvePreviewURL">;
11
+
12
+ /**
13
+ * The `request` object from a `+server` file.
14
+ *
15
+ * @see SvelteKit `+server` docs: \<https://kit.svelte.dev/docs/routing#server\>
16
+ */
17
+ request: Request;
18
+
19
+ /**
20
+ * The default redirect URL if a URL cannot be determined for the previewed
21
+ * document.
22
+ */
23
+ defaultURL?: string;
24
+ };
25
+
26
+ /**
27
+ * Redirects a visitor to the URL of a previewed Prismic document from within a
28
+ * SvelteKit `+server` file.
29
+ *
30
+ * Return the created `Response` in your `+server` file.
31
+ *
32
+ * @example
33
+ *
34
+ * ```typescript
35
+ * import { createClient } from "$lib/prismicio.js";
36
+ * import { redirectToPreviewURL } from "@prismicio/svelte/kit";
37
+ *
38
+ * export async function GET({ fetch, request }) {
39
+ * const client = createClient({ fetch });
40
+ *
41
+ * return await redirectToPreviewURL({ client, request });
42
+ * }
43
+ * ```
44
+ */
45
+ export const redirectToPreviewURL = async (
46
+ config: RedirectToPreviewURLConfig,
47
+ ): Promise<Response> => {
48
+ const previewToken =
49
+ new URL(config.request.url).searchParams.get("token") ?? undefined;
50
+ const documentID =
51
+ new URL(config.request.url).searchParams.get("documentId") ?? undefined;
52
+
53
+ const previewURL = await config.client.resolvePreviewURL({
54
+ previewToken,
55
+ documentID,
56
+ defaultURL: config.defaultURL || "/",
57
+ });
58
+
59
+ return new Response(undefined, {
60
+ status: 307,
61
+ headers: {
62
+ Location: previewURL,
63
+ },
64
+ });
65
+ };
@@ -0,0 +1,14 @@
1
+ import { ClientConfig } from "@prismicio/client";
2
+ import { Cookies } from "@sveltejs/kit";
3
+
4
+ /**
5
+ * Configuration for creating a Prismic client with automatic preview support in
6
+ * SvelteKit apps.
7
+ */
8
+ export type CreateClientConfig = ClientConfig & {
9
+ /**
10
+ * The `cookies` object provided to a route's `load()` function. This parmeter
11
+ * is required to load previews.
12
+ */
13
+ cookies?: Cookies;
14
+ };