@prismicio/svelte 1.6.0-alpha.0 → 2.0.0-alpha.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.
Files changed (44) hide show
  1. package/README.md +1 -1
  2. package/dist/PrismicEmbed.svelte +12 -2
  3. package/dist/PrismicEmbed.svelte.d.ts +8 -19
  4. package/dist/PrismicImage.svelte +140 -59
  5. package/dist/PrismicImage.svelte.d.ts +54 -66
  6. package/dist/PrismicLink.svelte +55 -18
  7. package/dist/PrismicLink.svelte.d.ts +30 -40
  8. package/dist/PrismicRichText/DefaultComponent.svelte +35 -22
  9. package/dist/PrismicRichText/DefaultComponent.svelte.d.ts +7 -19
  10. package/dist/PrismicRichText/PrismicRichText.svelte +23 -5
  11. package/dist/PrismicRichText/PrismicRichText.svelte.d.ts +12 -22
  12. package/dist/PrismicRichText/Serialize.svelte +35 -16
  13. package/dist/PrismicRichText/Serialize.svelte.d.ts +7 -18
  14. package/dist/PrismicTable/DefaultComponent.svelte +12 -2
  15. package/dist/PrismicTable/DefaultComponent.svelte.d.ts +7 -19
  16. package/dist/PrismicTable/PrismicTable.svelte +46 -30
  17. package/dist/PrismicTable/PrismicTable.svelte.d.ts +18 -27
  18. package/dist/PrismicText.svelte +49 -23
  19. package/dist/PrismicText.svelte.d.ts +17 -20
  20. package/dist/SliceZone/SliceZone.svelte +53 -9
  21. package/dist/SliceZone/SliceZone.svelte.d.ts +36 -84
  22. package/dist/SliceZone/TodoComponent.svelte +19 -9
  23. package/dist/SliceZone/TodoComponent.svelte.d.ts +8 -20
  24. package/dist/index.d.ts +1 -1
  25. package/dist/kit/PrismicPreview.svelte +107 -58
  26. package/dist/kit/PrismicPreview.svelte.d.ts +21 -30
  27. package/dist/lib/devMsg.js +1 -1
  28. package/dist/types.d.ts +93 -75
  29. package/package.json +27 -24
  30. package/src/PrismicEmbed.svelte +8 -4
  31. package/src/PrismicImage.svelte +65 -69
  32. package/src/PrismicLink.svelte +23 -19
  33. package/src/PrismicRichText/DefaultComponent.svelte +27 -19
  34. package/src/PrismicRichText/PrismicRichText.svelte +14 -10
  35. package/src/PrismicRichText/Serialize.svelte +12 -6
  36. package/src/PrismicTable/DefaultComponent.svelte +10 -2
  37. package/src/PrismicTable/PrismicTable.svelte +37 -43
  38. package/src/PrismicText.svelte +30 -22
  39. package/src/SliceZone/SliceZone.svelte +42 -91
  40. package/src/SliceZone/TodoComponent.svelte +14 -8
  41. package/src/index.ts +1 -1
  42. package/src/kit/PrismicPreview.svelte +30 -22
  43. package/src/lib/devMsg.ts +1 -1
  44. package/src/types.ts +117 -44
@@ -1,36 +1,44 @@
1
1
  <script lang="ts">
2
2
  import type { RichTextField } from "@prismicio/client";
3
- import { isFilled, asText } from "@prismicio/client";
3
+ import { asText, isFilled } from "@prismicio/client";
4
4
  import { DEV } from "esm-env";
5
5
 
6
6
  import { devMsg } from "./lib/devMsg";
7
7
 
8
- /**
9
- * A Rich Text or Title field from Prismic.
10
- */
11
- export let field: RichTextField;
8
+ type Props = {
9
+ /**
10
+ * A Rich Text or Title field from Prismic.
11
+ */
12
+ field: RichTextField;
12
13
 
13
- /**
14
- * The separator used between blocks. Defaults to `\n`.
15
- */
16
- export let separator: string | undefined = undefined;
14
+ /**
15
+ * The separator used between blocks. Defaults to `\n`.
16
+ */
17
+ separator?: string | undefined;
17
18
 
18
- /**
19
- * The string rendered when the field is empty. If a fallback is not given,
20
- * `null` will be rendered.
21
- */
22
- export let fallback: string | undefined = undefined;
19
+ /**
20
+ * The string rendered when the field is empty. If a fallback is not given,
21
+ * `null` will be rendered.
22
+ */
23
+ fallback?: string | undefined;
24
+ };
23
25
 
24
- if (DEV) {
25
- if ("className" in $$restProps) {
26
- console.warn(
27
- `[PrismicText] className cannot be passed to <PrismicText> since it renders plain text without a wrapping component. For more details, see ${devMsg(
28
- "classname-is-not-a-valid-prop",
29
- )}.`,
30
- field,
31
- );
26
+ const { field, separator, fallback, ...restProps }: Props = $props();
27
+
28
+ $effect(() => {
29
+ if (DEV) {
30
+ if ("className" in restProps) {
31
+ console.warn(
32
+ `[PrismicText] className cannot be passed to <PrismicText> since it renders plain text without a wrapping component. For more details, see ${devMsg(
33
+ "classname-is-not-a-valid-prop",
34
+ )}.`,
35
+ $state.snapshot(field),
36
+ );
37
+ }
32
38
  }
39
+ });
33
40
 
41
+ if (DEV) {
34
42
  if (typeof field === "string") {
35
43
  throw new Error(
36
44
  `[PrismicText] The "field" prop only accepts a Rich Text or Title field's value but was provided a different type of field instead (e.g. a Key Text or Select field). You can resolve this error by rendering the field value inline without <PrismicText>. For more details, see ${devMsg(
@@ -1,102 +1,53 @@
1
1
  <script lang="ts">
2
- import TodoComponent from "./TodoComponent.svelte";
3
-
4
- import { ComponentCompat } from "../types";
5
-
6
- import type * as prismic from "@prismicio/client";
7
- import type { SvelteComponent } from "svelte";
8
-
9
- type SvelteSliceComponent = new (
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- ...args: any[]
12
- ) => SvelteComponent<{
13
- slice?: SliceLike;
14
- slices?: SliceLike[];
15
- context?: unknown;
16
- index?: number;
17
- }>;
2
+ import type { Component } from "svelte";
18
3
 
19
- type SliceComponents = Record<string, SvelteSliceComponent>;
4
+ import type { SliceComponentProps, SliceZoneLike } from "../types";
20
5
 
21
- /**
22
- * The minimum required properties to represent a Prismic Slice from the
23
- * Prismic Rest API V2 for the `<SliceZone>` component.
24
- *
25
- * If using Prismic's Rest API V2, use the `Slice` export from
26
- * `@prismicio/types` for a full interface.
27
- *
28
- * @typeParam SliceType - Type name of the Slice.
29
- */
30
- type SliceLikeRestV2<SliceType extends string = string> = {
31
- slice_type: prismic.Slice<SliceType>["slice_type"];
32
- };
6
+ import TodoComponent from "./TodoComponent.svelte";
33
7
 
34
- /**
35
- * The minimum required properties to represent a Prismic Slice from the
36
- * Prismic GraphQL API for the `<SliceZone>` component.
37
- *
38
- * @typeParam SliceType - Type name of the Slice.
39
- */
40
- type SliceLikeGraphQL<SliceType extends string = string> = {
41
- type: prismic.Slice<SliceType>["slice_type"];
42
- };
8
+ type Props = {
9
+ /**
10
+ * An array of Prismic Slices, such as the `slices` property from a Prismic
11
+ * document.
12
+ */
13
+ slices?: SliceZoneLike;
43
14
 
44
- /**
45
- * The minimum required properties to represent a Prismic Slice for the
46
- * `<SliceZone>` component.
47
- *
48
- * If using Prismic's Rest API V2, use the `Slice` export from
49
- * `@prismicio/types` for a full interface.
50
- *
51
- * @typeParam SliceType - Type name of the Slice.
52
- */
53
- type SliceLike<SliceType extends string = string> = (
54
- | SliceLikeRestV2<SliceType>
55
- | SliceLikeGraphQL<SliceType>
56
- ) & {
57
15
  /**
58
- * If `true`, this Slice has been modified from its original value using a
59
- * mapper and `@prismicio/client`'s `mapSliceZone()`.
16
+ * An object that maps Slice components to their corresponding API IDs.
60
17
  *
61
- * @internal
18
+ * @example An example map:
19
+ *
20
+ * ```js
21
+ * import BlockQuote from "./BlockQuote.svelte";
22
+ * import HeroImage from "./HeroImage.svelte";
23
+ *
24
+ * const components = {
25
+ * block_quote: BlockQuote,
26
+ * hero_image: HeroImage,
27
+ * };
28
+ * ```
62
29
  */
63
- __mapped?: true;
64
- };
30
+ components?: Record<string, Component>;
65
31
 
66
- /**
67
- * An array of Prismic Slices, such as the `slices` property from a Prismic
68
- * document.
69
- */
70
- export let slices: SliceLike[] = [];
71
-
72
- /**
73
- * An object that maps Slice components to their corresponding API IDs.
74
- *
75
- * @example An example map:
76
- *
77
- * ```js
78
- * import BlockQuote from "./BlockQuote.svelte";
79
- * import HeroImage from "./HeroImage.svelte";
80
- *
81
- * const components = {
82
- * block_quote: BlockQuote,
83
- * hero_image: HeroImage,
84
- * };
85
- * ```
86
- */
87
- export let components: Record<string, ComponentCompat<any>> = {};
32
+ /**
33
+ * Arbitrary data passed to all Slice components as a `context` prop.
34
+ */
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
+ context?: any;
88
37
 
89
- /**
90
- * Arbitrary data passed to all Slice components as a `context` prop.
91
- */
92
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
- export let context: any = {};
38
+ /**
39
+ * The Svelte component rendered if a component mapping from the
40
+ * `components` prop cannot be found.
41
+ */
42
+ defaultComponent?: Component<SliceComponentProps> | undefined;
43
+ };
94
44
 
95
- /**
96
- * The Svelte component rendered if a component mapping from the `components`
97
- * prop cannot be found.
98
- */
99
- export let defaultComponent: SvelteSliceComponent | undefined = undefined;
45
+ const {
46
+ slices = [],
47
+ components = {},
48
+ context = {},
49
+ defaultComponent = undefined,
50
+ }: Props = $props();
100
51
  </script>
101
52
 
102
53
  <!--
@@ -115,15 +66,15 @@
115
66
  ```
116
67
  -->
117
68
 
118
- {#each slices as slice, index}
69
+ {#each slices as slice, index ("id" in slice && slice.id ? slice.id : `${index}-${JSON.stringify(slice)}`)}
119
70
  {@const type = "slice_type" in slice ? slice.slice_type : slice.type}
120
71
  {@const Component = components[type] || defaultComponent}
121
72
  {#if Component}
122
73
  {#if slice.__mapped}
123
74
  {@const { __mapped, ...mappedProps } = slice}
124
- <svelte:component this={Component} {...mappedProps} />
75
+ <Component {...mappedProps} />
125
76
  {:else}
126
- <svelte:component this={Component} {slice} {slices} {context} {index} />
77
+ <Component {slice} {slices} {context} {index} />
127
78
  {/if}
128
79
  {:else}
129
80
  <TodoComponent {slice} />
@@ -1,16 +1,22 @@
1
1
  <script lang="ts">
2
2
  import { DEV } from "esm-env";
3
3
 
4
- export let slice: { slice_type: string } | { type: string };
4
+ type Props = {
5
+ slice: { slice_type: string } | { type: string };
6
+ };
5
7
 
6
- const type = "slice_type" in slice ? slice.slice_type : slice.type;
8
+ const { slice }: Props = $props();
7
9
 
8
- if (DEV) {
9
- console.warn(
10
- `[SliceZone] Could not find a component for Slice type "${type}"`,
11
- slice,
12
- );
13
- }
10
+ const type = $derived("slice_type" in slice ? slice.slice_type : slice.type);
11
+
12
+ $effect(() => {
13
+ if (DEV) {
14
+ console.warn(
15
+ `[SliceZone] Could not find a component for Slice type "${type}"`,
16
+ $state.snapshot(slice),
17
+ );
18
+ }
19
+ });
14
20
  </script>
15
21
 
16
22
  {#if DEV}
package/src/index.ts CHANGED
@@ -6,4 +6,4 @@ export { default as PrismicTable } from "./PrismicTable/PrismicTable.svelte";
6
6
  export { default as PrismicText } from "./PrismicText.svelte";
7
7
  export { default as SliceZone } from "./SliceZone/SliceZone.svelte";
8
8
 
9
- export type { SvelteRichTextSerializer } from "./types";
9
+ export type { SvelteRichTextSerializer, SliceComponentProps } from "./types";
@@ -1,35 +1,43 @@
1
1
  <script lang="ts">
2
- import { onMount } from "svelte";
3
- import { getToolbarSrc } from "@prismicio/client";
4
2
  import { beforeNavigate, goto, invalidateAll } from "$app/navigation";
3
+ import { getToolbarSrc } from "@prismicio/client";
4
+ import { onMount } from "svelte";
5
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;
6
+ type Props = {
7
+ /**
8
+ * The name of your Prismic repository. A Prismic Toolbar will be registered
9
+ * using this repository.
10
+ */
11
+ repositoryName: string;
11
12
 
12
- /**
13
- * The route parameter prefixed during preview sessions.
14
- */
15
- export let routePrefix: string = "preview";
13
+ /**
14
+ * The route parameter prefixed during preview sessions.
15
+ */
16
+ routePrefix?: string;
16
17
 
17
- /**
18
- * The name of the preview-specific route parameter prefixed during preview
19
- * sessions.
20
- *
21
- * Only set this value if the route parameter's name differs from the
22
- * parameter's value.
23
- */
24
- export let routePrefixName: string = routePrefix;
18
+ /**
19
+ * The name of the preview-specific route parameter prefixed during preview
20
+ * sessions.
21
+ *
22
+ * Only set this value if the route parameter's name differs from the
23
+ * parameter's value.
24
+ */
25
+ routePrefixName?: string;
26
+ };
27
+
28
+ const {
29
+ repositoryName,
30
+ routePrefix = "preview",
31
+ routePrefixName = routePrefix,
32
+ }: Props = $props();
25
33
 
26
- $: toolbarSrc = getToolbarSrc(repositoryName);
34
+ const toolbarSrc = $derived(getToolbarSrc(repositoryName));
27
35
 
28
36
  /**
29
37
  * Set to `true` when the next `beforeNavigate()` call should not prefix the
30
38
  * route.
31
39
  */
32
- let endingPreview = false;
40
+ let endingPreview = $state(false);
33
41
 
34
42
  // Register Prismic toolbar event handlers to refresh data on content updates.
35
43
  onMount(() => {
@@ -53,7 +61,7 @@
53
61
  goto(
54
62
  new URL(
55
63
  window.location.pathname.replace(
56
- new RegExp(`^(\/${routePrefix}\/?$|\/${routePrefix}\/)`),
64
+ new RegExp(`^(/${routePrefix}/?$|/${routePrefix}/)`),
57
65
  "/",
58
66
  ),
59
67
  window.location.origin,
package/src/lib/devMsg.ts CHANGED
@@ -16,5 +16,5 @@ import { version } from "../../package.json";
16
16
  * @returns The `prismic.dev/msg` URL for the given slug.
17
17
  */
18
18
  export const devMsg = (slug: string): string => {
19
- return `https://prismic.dev/msg/react/v${version}/${slug}`;
19
+ return `https://prismic.dev/msg/svelte/v${version}/${slug}`;
20
20
  };
package/src/types.ts CHANGED
@@ -1,51 +1,124 @@
1
- import type * as prismic from "@prismicio/client";
2
- import type { Component, ComponentType, SvelteComponent } from "svelte";
3
-
4
- export type SvelteRichTextSerializer = {
5
- heading1?: ComponentCompat<{ node: prismic.RTHeading1Node }>;
6
- heading2?: ComponentCompat<{ node: prismic.RTHeading2Node }>;
7
- heading3?: ComponentCompat<{ node: prismic.RTHeading3Node }>;
8
- heading4?: ComponentCompat<{ node: prismic.RTHeading4Node }>;
9
- heading5?: ComponentCompat<{ node: prismic.RTHeading5Node }>;
10
- heading6?: ComponentCompat<{ node: prismic.RTHeading6Node }>;
11
- paragraph?: ComponentCompat<{ node: prismic.RTParagraphNode }>;
12
- preformatted?: ComponentCompat<{ node: prismic.RTPreformattedNode }>;
13
- strong?: ComponentCompat<{ node: prismic.RTStrongNode }>;
14
- em?: ComponentCompat<{ node: prismic.RTEmNode }>;
15
- listItem?: ComponentCompat<{ node: prismic.RTListItemNode }>;
16
- oListItem?: ComponentCompat<{ node: prismic.RTOListItemNode }>;
17
- list?: ComponentCompat<{ node: prismic.RTListNode }>;
18
- oList?: ComponentCompat<{ node: prismic.RTOListNode }>;
19
- image?: ComponentCompat<{ node: prismic.RTImageNode }>;
20
- embed?: ComponentCompat<{ node: prismic.RTEmbedNode }>;
21
- hyperlink?: ComponentCompat<{ node: prismic.RTLinkNode }>;
22
- label?: ComponentCompat<{ node: prismic.RTLabelNode }>;
23
- span?: ComponentCompat<{ node: prismic.RTSpanNode }>;
24
- };
1
+ import type {
2
+ RTAnyNode,
3
+ RichTextNodeType,
4
+ Slice,
5
+ TableField,
6
+ TableFieldBody,
7
+ TableFieldBodyRow,
8
+ TableFieldDataCell,
9
+ TableFieldHead,
10
+ TableFieldHeadRow,
11
+ TableFieldHeaderCell,
12
+ } from "@prismicio/client";
13
+ import type { Component } from "svelte";
14
+
15
+ export type SvelteRichTextSerializer = Partial<
16
+ Record<keyof typeof RichTextNodeType, Component<{ node?: RTAnyNode }>>
17
+ >;
25
18
 
26
19
  // Define the type for the components prop
27
20
  export type TableComponents = {
28
- table?: ComponentTypeCompat<{ table: prismic.TableField<"filled"> }>;
29
- thead?: ComponentTypeCompat<{ head: prismic.TableFieldHead }>;
30
- tbody?: ComponentTypeCompat<{ body: prismic.TableFieldBody }>;
31
- tr?: ComponentTypeCompat<{
32
- row: prismic.TableFieldHeadRow | prismic.TableFieldBodyRow;
33
- }>;
34
- th?: ComponentTypeCompat<{ table: prismic.TableFieldHeaderCell }>;
35
- td?: ComponentTypeCompat<{ table: prismic.TableFieldDataCell }>;
21
+ table?: Component<{ table: TableField<"filled"> }>;
22
+ thead?: Component<{ head: TableFieldHead }>;
23
+ tbody?: Component<{ body: TableFieldBody }>;
24
+ tr?: Component<{ row: TableFieldHeadRow | TableFieldBodyRow }>;
25
+ th?: Component<{ cell: TableFieldHeaderCell }>;
26
+ td?: Component<{ cell: TableFieldDataCell }>;
36
27
  };
37
28
 
38
- // Compatibility type helpers for better compatibility with Svelte 4 and 5.
29
+ /**
30
+ * Props for a component rendering content from a Prismic Slice using the
31
+ * `<SliceZone>` component.
32
+ *
33
+ * @typeParam TSlice - The Slice passed as a prop.
34
+ * @typeParam TContext - Arbitrary data passed to `<SliceZone>` and made
35
+ * available to all Slice components.
36
+ */
37
+ export type SliceComponentProps<
38
+ TSlice extends SliceLike = SliceLike,
39
+ TContext = unknown,
40
+ > = {
41
+ /**
42
+ * Slice data for this component.
43
+ */
44
+ slice: TSlice;
45
+
46
+ /**
47
+ * The index of the Slice in the Slice Zone.
48
+ */
49
+ index: number;
50
+
51
+ /**
52
+ * All Slices from the Slice Zone to which the Slice belongs.
53
+ */
54
+ // TODO: We have to keep this list of Slices general due to circular
55
+ // reference limtiations. If we had another generic to determine the full
56
+ // union of Slice types, it would include TSlice. This causes TypeScript to
57
+ // throw a compilation error.
58
+ slices: SliceZoneLike<
59
+ TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2
60
+ >;
61
+
62
+ /**
63
+ * Arbitrary data passed to `<SliceZone>` and made available to all Slice
64
+ * components.
65
+ */
66
+ context: TContext;
67
+ };
39
68
 
40
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
- type ComponentTypeCompat<Props extends Record<string, any> = any> =
42
- | Component<Props>
43
- | ComponentType<SvelteComponent<Props>>;
69
+ /**
70
+ * The minimum required properties to represent a Prismic Slice from the Prismic
71
+ * Rest API V2 for the `<SliceZone>` component.
72
+ *
73
+ * If using Prismic's Rest API V2, use the `Slice` export from
74
+ * `@prismicio/types` for a full interface.
75
+ *
76
+ * @typeParam SliceType - Type name of the Slice.
77
+ */
78
+ type SliceLikeRestV2<SliceType extends string = string> = {
79
+ slice_type: Slice<SliceType>["slice_type"];
80
+ };
81
+
82
+ /**
83
+ * The minimum required properties to represent a Prismic Slice from the Prismic
84
+ * GraphQL API for the `<SliceZone>` component.
85
+ *
86
+ * @typeParam SliceType - Type name of the Slice.
87
+ */
88
+ type SliceLikeGraphQL<SliceType extends string = string> = {
89
+ type: Slice<SliceType>["slice_type"];
90
+ };
91
+
92
+ /**
93
+ * The minimum required properties to represent a Prismic Slice for the
94
+ * `<SliceZone>` component.
95
+ *
96
+ * If using Prismic's Rest API V2, use the `Slice` export from
97
+ * `@prismicio/types` for a full interface.
98
+ *
99
+ * @typeParam SliceType - Type name of the Slice.
100
+ */
101
+ type SliceLike<SliceType extends string = string> = (
102
+ | SliceLikeRestV2<SliceType>
103
+ | SliceLikeGraphQL<SliceType>
104
+ ) & {
105
+ /**
106
+ * If `true`, this Slice has been modified from its original value using a
107
+ * mapper and `@prismicio/client`'s `mapSliceZone()`.
108
+ *
109
+ * @internal
110
+ */
111
+ __mapped?: true;
112
+ };
44
113
 
45
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
- export type ComponentCompat<Props extends Record<string, any> = any> =
47
- | Component<Props>
48
- | (new (
49
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
- ...args: any[]
51
- ) => SvelteComponent<Partial<Props>>);
114
+ /**
115
+ * A looser version of the `SliceZone` type from `@prismicio/client` using
116
+ * `SliceLike`.
117
+ *
118
+ * If using Prismic's Rest API V2, use the `SliceZone` export from
119
+ * `@prismicio/client` for the full type.
120
+ *
121
+ * @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
122
+ */
123
+ export type SliceZoneLike<TSlice extends SliceLike = SliceLike> =
124
+ readonly TSlice[];