@prismicio/svelte 1.5.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 -17
  4. package/dist/PrismicImage.svelte +140 -59
  5. package/dist/PrismicImage.svelte.d.ts +54 -64
  6. package/dist/PrismicLink.svelte +55 -20
  7. package/dist/PrismicLink.svelte.d.ts +30 -38
  8. package/dist/PrismicRichText/DefaultComponent.svelte +35 -23
  9. package/dist/PrismicRichText/DefaultComponent.svelte.d.ts +7 -17
  10. package/dist/PrismicRichText/PrismicRichText.svelte +23 -6
  11. package/dist/PrismicRichText/PrismicRichText.svelte.d.ts +13 -22
  12. package/dist/PrismicRichText/Serialize.svelte +35 -16
  13. package/dist/PrismicRichText/Serialize.svelte.d.ts +9 -19
  14. package/dist/PrismicTable/DefaultComponent.svelte +12 -2
  15. package/dist/PrismicTable/DefaultComponent.svelte.d.ts +7 -17
  16. package/dist/PrismicTable/PrismicTable.svelte +46 -30
  17. package/dist/PrismicTable/PrismicTable.svelte.d.ts +19 -31
  18. package/dist/PrismicText.svelte +49 -23
  19. package/dist/PrismicText.svelte.d.ts +17 -18
  20. package/dist/SliceZone/SliceZone.svelte +53 -8
  21. package/dist/SliceZone/SliceZone.svelte.d.ts +36 -110
  22. package/dist/SliceZone/TodoComponent.svelte +19 -9
  23. package/dist/SliceZone/TodoComponent.svelte.d.ts +8 -18
  24. package/dist/index.d.ts +1 -1
  25. package/dist/kit/PrismicPreview.svelte +107 -59
  26. package/dist/kit/PrismicPreview.svelte.d.ts +21 -28
  27. package/dist/lib/devMsg.js +1 -1
  28. package/dist/types.d.ts +95 -20
  29. package/package.json +35 -33
  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 +13 -7
  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 -89
  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 +105 -18
@@ -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,100 +1,53 @@
1
1
  <script lang="ts">
2
- import TodoComponent from "./TodoComponent.svelte";
3
-
4
- import type * as prismic from "@prismicio/client";
5
- import type { SvelteComponent } from "svelte";
2
+ import type { Component } from "svelte";
6
3
 
7
- type SvelteSliceComponent = new (
8
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
- ...args: any[]
10
- ) => SvelteComponent<{
11
- slice?: SliceLike;
12
- slices?: SliceLike[];
13
- context?: unknown;
14
- index?: number;
15
- }>;
4
+ import type { SliceComponentProps, SliceZoneLike } from "../types";
16
5
 
17
- type SliceComponents = Record<string, SvelteSliceComponent>;
18
-
19
- /**
20
- * The minimum required properties to represent a Prismic Slice from the
21
- * Prismic Rest API V2 for the `<SliceZone>` component.
22
- *
23
- * If using Prismic's Rest API V2, use the `Slice` export from
24
- * `@prismicio/types` for a full interface.
25
- *
26
- * @typeParam SliceType - Type name of the Slice.
27
- */
28
- type SliceLikeRestV2<SliceType extends string = string> = {
29
- slice_type: prismic.Slice<SliceType>["slice_type"];
30
- };
6
+ import TodoComponent from "./TodoComponent.svelte";
31
7
 
32
- /**
33
- * The minimum required properties to represent a Prismic Slice from the
34
- * Prismic GraphQL API for the `<SliceZone>` component.
35
- *
36
- * @typeParam SliceType - Type name of the Slice.
37
- */
38
- type SliceLikeGraphQL<SliceType extends string = string> = {
39
- type: prismic.Slice<SliceType>["slice_type"];
40
- };
8
+ type Props = {
9
+ /**
10
+ * An array of Prismic Slices, such as the `slices` property from a Prismic
11
+ * document.
12
+ */
13
+ slices?: SliceZoneLike;
41
14
 
42
- /**
43
- * The minimum required properties to represent a Prismic Slice for the
44
- * `<SliceZone>` component.
45
- *
46
- * If using Prismic's Rest API V2, use the `Slice` export from
47
- * `@prismicio/types` for a full interface.
48
- *
49
- * @typeParam SliceType - Type name of the Slice.
50
- */
51
- type SliceLike<SliceType extends string = string> = (
52
- | SliceLikeRestV2<SliceType>
53
- | SliceLikeGraphQL<SliceType>
54
- ) & {
55
15
  /**
56
- * If `true`, this Slice has been modified from its original value using a
57
- * mapper and `@prismicio/client`'s `mapSliceZone()`.
16
+ * An object that maps Slice components to their corresponding API IDs.
17
+ *
18
+ * @example An example map:
58
19
  *
59
- * @internal
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
+ * ```
60
29
  */
61
- __mapped?: true;
62
- };
63
-
64
- /**
65
- * An array of Prismic Slices, such as the `slices` property from a Prismic
66
- * document.
67
- */
68
- export let slices: SliceLike[] = [];
30
+ components?: Record<string, Component>;
69
31
 
70
- /**
71
- * An object that maps Slice components to their corresponding API IDs.
72
- *
73
- * @example An example map:
74
- *
75
- * ```js
76
- * import BlockQuote from "./BlockQuote.svelte";
77
- * import HeroImage from "./HeroImage.svelte";
78
- *
79
- * const components = {
80
- * block_quote: BlockQuote,
81
- * hero_image: HeroImage,
82
- * };
83
- * ```
84
- */
85
- export let components: SliceComponents = {};
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;
86
37
 
87
- /**
88
- * Arbitrary data passed to all Slice components as a `context` prop.
89
- */
90
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
- 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
+ };
92
44
 
93
- /**
94
- * The Svelte component rendered if a component mapping from the `components`
95
- * prop cannot be found.
96
- */
97
- export let defaultComponent: SvelteSliceComponent | undefined = undefined;
45
+ const {
46
+ slices = [],
47
+ components = {},
48
+ context = {},
49
+ defaultComponent = undefined,
50
+ }: Props = $props();
98
51
  </script>
99
52
 
100
53
  <!--
@@ -113,15 +66,15 @@
113
66
  ```
114
67
  -->
115
68
 
116
- {#each slices as slice, index}
69
+ {#each slices as slice, index ("id" in slice && slice.id ? slice.id : `${index}-${JSON.stringify(slice)}`)}
117
70
  {@const type = "slice_type" in slice ? slice.slice_type : slice.type}
118
71
  {@const Component = components[type] || defaultComponent}
119
72
  {#if Component}
120
73
  {#if slice.__mapped}
121
74
  {@const { __mapped, ...mappedProps } = slice}
122
- <svelte:component this={Component} {...mappedProps} />
75
+ <Component {...mappedProps} />
123
76
  {:else}
124
- <svelte:component this={Component} {slice} {slices} {context} {index} />
77
+ <Component {slice} {slices} {context} {index} />
125
78
  {/if}
126
79
  {:else}
127
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,6 +1,7 @@
1
1
  import type {
2
2
  RTAnyNode,
3
3
  RichTextNodeType,
4
+ Slice,
4
5
  TableField,
5
6
  TableFieldBody,
6
7
  TableFieldBodyRow,
@@ -9,29 +10,115 @@ import type {
9
10
  TableFieldHeadRow,
10
11
  TableFieldHeaderCell,
11
12
  } from "@prismicio/client";
12
- import type { ComponentType, SvelteComponent } from "svelte";
13
+ import type { Component } from "svelte";
13
14
 
14
15
  export type SvelteRichTextSerializer = Partial<
15
- Record<keyof typeof RichTextNodeType, SvelteRichTextComponent>
16
- >;
17
-
18
- type SvelteRichTextComponent = new (
19
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
- ...args: any[]
21
- ) => SvelteComponent<
22
- Partial<{
23
- node: RTAnyNode;
24
- }>
16
+ Record<keyof typeof RichTextNodeType, Component<{ node?: RTAnyNode }>>
25
17
  >;
26
18
 
27
19
  // Define the type for the components prop
28
20
  export type TableComponents = {
29
- table?: ComponentType<SvelteComponent<{ table: TableField<"filled"> }>>;
30
- thead?: ComponentType<SvelteComponent<{ head: TableFieldHead }>>;
31
- tbody?: ComponentType<SvelteComponent<{ body: TableFieldBody }>>;
32
- tr?: ComponentType<
33
- SvelteComponent<{ row: TableFieldHeadRow | TableFieldBodyRow }>
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 }>;
27
+ };
28
+
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
34
60
  >;
35
- th?: ComponentType<SvelteComponent<{ table: TableFieldHeaderCell }>>;
36
- td?: ComponentType<SvelteComponent<{ table: TableFieldDataCell }>>;
61
+
62
+ /**
63
+ * Arbitrary data passed to `<SliceZone>` and made available to all Slice
64
+ * components.
65
+ */
66
+ context: TContext;
37
67
  };
68
+
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
+ };
113
+
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[];