@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,111 +1,37 @@
1
- import { SvelteComponent } from "svelte";
2
- declare const __propDef: {
3
- props: {
4
- /**
5
- * An array of Prismic Slices, such as the `slices` property from a Prismic
6
- * document.
7
- */ slices?: (({
8
- slice_type: string;
9
- } | {
10
- type: string;
11
- }) & {
12
- /**
13
- * If `true`, this Slice has been modified from its original value using a
14
- * mapper and `@prismicio/client`'s `mapSliceZone()`.
15
- *
16
- * @internal
17
- */
18
- __mapped?: true | undefined;
19
- })[] | undefined;
20
- /**
21
- * An object that maps Slice components to their corresponding API IDs.
22
- *
23
- * @example An example map:
24
- *
25
- * ```js
26
- * import BlockQuote from "./BlockQuote.svelte";
27
- * import HeroImage from "./HeroImage.svelte";
28
- *
29
- * const components = {
30
- * block_quote: BlockQuote,
31
- * hero_image: HeroImage,
32
- * };
33
- * ```
34
- */ components?: Record<string, new (...args: any[]) => SvelteComponent<{
35
- slice?: (({
36
- slice_type: string;
37
- } | {
38
- type: string;
39
- }) & {
40
- /**
41
- * If `true`, this Slice has been modified from its original value using a
42
- * mapper and `@prismicio/client`'s `mapSliceZone()`.
43
- *
44
- * @internal
45
- */
46
- __mapped?: true | undefined;
47
- }) | undefined;
48
- slices?: (({
49
- slice_type: string;
50
- } | {
51
- type: string;
52
- }) & {
53
- /**
54
- * If `true`, this Slice has been modified from its original value using a
55
- * mapper and `@prismicio/client`'s `mapSliceZone()`.
56
- *
57
- * @internal
58
- */
59
- __mapped?: true | undefined;
60
- })[] | undefined;
61
- context?: unknown;
62
- index?: number | undefined;
63
- }, any, any>> | undefined;
64
- /**
65
- * Arbitrary data passed to all Slice components as a `context` prop.
66
- */ context?: any;
67
- /**
68
- * The Svelte component rendered if a component mapping from the `components`
69
- * prop cannot be found.
70
- */ defaultComponent?: (new (...args: any[]) => SvelteComponent<{
71
- slice?: (({
72
- slice_type: string;
73
- } | {
74
- type: string;
75
- }) & {
76
- /**
77
- * If `true`, this Slice has been modified from its original value using a
78
- * mapper and `@prismicio/client`'s `mapSliceZone()`.
79
- *
80
- * @internal
81
- */
82
- __mapped?: true | undefined;
83
- }) | undefined;
84
- slices?: (({
85
- slice_type: string;
86
- } | {
87
- type: string;
88
- }) & {
89
- /**
90
- * If `true`, this Slice has been modified from its original value using a
91
- * mapper and `@prismicio/client`'s `mapSliceZone()`.
92
- *
93
- * @internal
94
- */
95
- __mapped?: true | undefined;
96
- })[] | undefined;
97
- context?: unknown;
98
- index?: number | undefined;
99
- }, any, any>) | undefined;
100
- };
101
- events: {
102
- [evt: string]: CustomEvent<any>;
103
- };
104
- slots: {};
1
+ import type { Component } from "svelte";
2
+ import type { SliceComponentProps, SliceZoneLike } from "../types";
3
+ type Props = {
4
+ /**
5
+ * An array of Prismic Slices, such as the `slices` property from a Prismic
6
+ * document.
7
+ */
8
+ slices?: SliceZoneLike;
9
+ /**
10
+ * An object that maps Slice components to their corresponding API IDs.
11
+ *
12
+ * @example An example map:
13
+ *
14
+ * ```js
15
+ * import BlockQuote from "./BlockQuote.svelte";
16
+ * import HeroImage from "./HeroImage.svelte";
17
+ *
18
+ * const components = {
19
+ * block_quote: BlockQuote,
20
+ * hero_image: HeroImage,
21
+ * };
22
+ * ```
23
+ */
24
+ components?: Record<string, Component>;
25
+ /**
26
+ * Arbitrary data passed to all Slice components as a `context` prop.
27
+ */
28
+ context?: any;
29
+ /**
30
+ * The Svelte component rendered if a component mapping from the
31
+ * `components` prop cannot be found.
32
+ */
33
+ defaultComponent?: Component<SliceComponentProps> | undefined;
105
34
  };
106
- export type SliceZoneProps = typeof __propDef.props;
107
- export type SliceZoneEvents = typeof __propDef.events;
108
- export type SliceZoneSlots = typeof __propDef.slots;
109
35
  /**
110
36
  * Component to render an array of Prismic slices.
111
37
  *
@@ -120,6 +46,6 @@ export type SliceZoneSlots = typeof __propDef.slots;
120
46
  * />
121
47
  * ```
122
48
  */
123
- export default class SliceZone extends SvelteComponent<SliceZoneProps, SliceZoneEvents, SliceZoneSlots> {
124
- }
125
- export {};
49
+ declare const SliceZone: Component<Props, {}, "">;
50
+ type SliceZone = ReturnType<typeof SliceZone>;
51
+ export default SliceZone;
@@ -1,12 +1,22 @@
1
- <script>import { DEV } from "esm-env";
2
- export let slice;
3
- const type = "slice_type" in slice ? slice.slice_type : slice.type;
4
- if (DEV) {
5
- console.warn(
6
- `[SliceZone] Could not find a component for Slice type "${type}"`,
7
- slice
8
- );
9
- }
1
+ <script lang="ts">
2
+ import { DEV } from "esm-env";
3
+
4
+ type Props = {
5
+ slice: { slice_type: string } | { type: string };
6
+ };
7
+
8
+ const { slice }: Props = $props();
9
+
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
+ });
10
20
  </script>
11
21
 
12
22
  {#if DEV}
@@ -1,20 +1,10 @@
1
- import { SvelteComponent } from "svelte";
2
- declare const __propDef: {
3
- props: {
4
- slice: {
5
- slice_type: string;
6
- } | {
7
- type: string;
8
- };
1
+ type Props = {
2
+ slice: {
3
+ slice_type: string;
4
+ } | {
5
+ type: string;
9
6
  };
10
- events: {
11
- [evt: string]: CustomEvent<any>;
12
- };
13
- slots: {};
14
7
  };
15
- export type TodoComponentProps = typeof __propDef.props;
16
- export type TodoComponentEvents = typeof __propDef.events;
17
- export type TodoComponentSlots = typeof __propDef.slots;
18
- export default class TodoComponent extends SvelteComponent<TodoComponentProps, TodoComponentEvents, TodoComponentSlots> {
19
- }
20
- export {};
8
+ declare const TodoComponent: import("svelte").Component<Props, {}, "">;
9
+ type TodoComponent = ReturnType<typeof TodoComponent>;
10
+ export default TodoComponent;
package/dist/index.d.ts CHANGED
@@ -5,4 +5,4 @@ export { default as PrismicRichText } from "./PrismicRichText/PrismicRichText.sv
5
5
  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
- export type { SvelteRichTextSerializer } from "./types";
8
+ export type { SvelteRichTextSerializer, SliceComponentProps } from "./types";
@@ -1,62 +1,110 @@
1
- <script>import { onMount } from "svelte";
2
- import { getToolbarSrc } from "@prismicio/client";
3
- import { beforeNavigate, goto, invalidateAll } from "$app/navigation";
4
- export let repositoryName;
5
- export let routePrefix = "preview";
6
- export let routePrefixName = routePrefix;
7
- $:
8
- toolbarSrc = getToolbarSrc(repositoryName);
9
- let endingPreview = false;
10
- onMount(() => {
11
- const controller = new AbortController();
12
- window.addEventListener(
13
- "prismicPreviewUpdate",
14
- (event) => {
15
- event.preventDefault();
16
- invalidateAll();
17
- },
18
- { signal: controller.signal }
19
- );
20
- window.addEventListener(
21
- "prismicPreviewEnd",
22
- (event) => {
23
- event.preventDefault();
24
- endingPreview = true;
25
- goto(
26
- new URL(
27
- window.location.pathname.replace(
28
- new RegExp(`^(/${routePrefix}/?$|/${routePrefix}/)`),
29
- "/"
30
- ),
31
- window.location.origin
32
- ),
33
- {
34
- invalidateAll: true,
35
- noScroll: true
36
- }
37
- );
38
- },
39
- { signal: controller.signal }
40
- );
41
- return () => {
42
- controller.abort();
43
- };
44
- });
45
- beforeNavigate((navigation) => {
46
- if (navigation.to && navigation.from?.params?.[routePrefixName] === routePrefix && !(routePrefixName in (navigation.to.params || {}))) {
47
- if (endingPreview) {
48
- endingPreview = false;
49
- return;
50
- }
51
- navigation.cancel();
52
- goto(
53
- new URL(
54
- routePrefix + navigation.to.url.pathname,
55
- navigation.to.url.origin
56
- )
57
- );
58
- }
59
- });
1
+ <script lang="ts">
2
+ import { beforeNavigate, goto, invalidateAll } from "$app/navigation";
3
+ import { getToolbarSrc } from "@prismicio/client";
4
+ import { onMount } from "svelte";
5
+
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;
12
+
13
+ /**
14
+ * The route parameter prefixed during preview sessions.
15
+ */
16
+ routePrefix?: string;
17
+
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();
33
+
34
+ const toolbarSrc = $derived(getToolbarSrc(repositoryName));
35
+
36
+ /**
37
+ * Set to `true` when the next `beforeNavigate()` call should not prefix the
38
+ * route.
39
+ */
40
+ let endingPreview = $state(false);
41
+
42
+ // Register Prismic toolbar event handlers to refresh data on content updates.
43
+ onMount(() => {
44
+ const controller = new AbortController();
45
+
46
+ window.addEventListener(
47
+ "prismicPreviewUpdate",
48
+ (event) => {
49
+ event.preventDefault();
50
+ invalidateAll();
51
+ },
52
+ { signal: controller.signal },
53
+ );
54
+ window.addEventListener(
55
+ "prismicPreviewEnd",
56
+ (event) => {
57
+ event.preventDefault();
58
+
59
+ endingPreview = true;
60
+
61
+ goto(
62
+ new URL(
63
+ window.location.pathname.replace(
64
+ new RegExp(`^(/${routePrefix}/?$|/${routePrefix}/)`),
65
+ "/",
66
+ ),
67
+ window.location.origin,
68
+ ),
69
+ {
70
+ invalidateAll: true,
71
+ noScroll: true,
72
+ },
73
+ );
74
+ },
75
+ { signal: controller.signal },
76
+ );
77
+
78
+ return () => {
79
+ controller.abort();
80
+ };
81
+ });
82
+
83
+ beforeNavigate((navigation) => {
84
+ // Prefix links with the preview route parameter if the current
85
+ // route is in a preview session.
86
+ if (
87
+ navigation.to &&
88
+ navigation.from?.params?.[routePrefixName] === routePrefix &&
89
+ !(routePrefixName in (navigation.to.params || {}))
90
+ ) {
91
+ // If this callback is called due to a `prismicPreviewEnd`
92
+ // event, don't prefix the route. The exception is only
93
+ // valid for one call.
94
+ if (endingPreview) {
95
+ endingPreview = false;
96
+ return;
97
+ }
98
+
99
+ navigation.cancel();
100
+ goto(
101
+ new URL(
102
+ routePrefix + navigation.to.url.pathname,
103
+ navigation.to.url.origin,
104
+ ),
105
+ );
106
+ }
107
+ });
60
108
  </script>
61
109
 
62
110
  <svelte:head>
@@ -1,29 +1,22 @@
1
- import { SvelteComponent } from "svelte";
2
- declare const __propDef: {
3
- props: {
4
- /**
5
- * The name of your Prismic repository. A Prismic Toolbar will be registered
6
- * using this repository.
7
- */ repositoryName: string;
8
- /**
9
- * The route parameter prefixed during preview sessions.
10
- */ routePrefix?: string | undefined;
11
- /**
12
- * The name of the preview-specific route parameter prefixed during preview
13
- * sessions.
14
- *
15
- * Only set this value if the route parameter's name differs from the
16
- * parameter's value.
17
- */ routePrefixName?: string | undefined;
18
- };
19
- events: {
20
- [evt: string]: CustomEvent<any>;
21
- };
22
- slots: {};
1
+ type Props = {
2
+ /**
3
+ * The name of your Prismic repository. A Prismic Toolbar will be registered
4
+ * using this repository.
5
+ */
6
+ repositoryName: string;
7
+ /**
8
+ * The route parameter prefixed during preview sessions.
9
+ */
10
+ routePrefix?: string;
11
+ /**
12
+ * The name of the preview-specific route parameter prefixed during preview
13
+ * sessions.
14
+ *
15
+ * Only set this value if the route parameter's name differs from the
16
+ * parameter's value.
17
+ */
18
+ routePrefixName?: string;
23
19
  };
24
- export type PrismicPreviewProps = typeof __propDef.props;
25
- export type PrismicPreviewEvents = typeof __propDef.events;
26
- export type PrismicPreviewSlots = typeof __propDef.slots;
27
- export default class PrismicPreview extends SvelteComponent<PrismicPreviewProps, PrismicPreviewEvents, PrismicPreviewSlots> {
28
- }
29
- export {};
20
+ declare const PrismicPreview: import("svelte").Component<Props, {}, "">;
21
+ type PrismicPreview = ReturnType<typeof PrismicPreview>;
22
+ export default PrismicPreview;
@@ -15,5 +15,5 @@ import { version } from "../../package.json";
15
15
  * @returns The `prismic.dev/msg` URL for the given slug.
16
16
  */
17
17
  export const devMsg = (slug) => {
18
- return `https://prismic.dev/msg/react/v${version}/${slug}`;
18
+ return `https://prismic.dev/msg/svelte/v${version}/${slug}`;
19
19
  };
package/dist/types.d.ts CHANGED
@@ -1,27 +1,102 @@
1
- import type { RTAnyNode, RichTextNodeType, TableField, TableFieldBody, TableFieldBodyRow, TableFieldDataCell, TableFieldHead, TableFieldHeadRow, TableFieldHeaderCell } from "@prismicio/client";
2
- import type { ComponentType, SvelteComponent } from "svelte";
3
- export type SvelteRichTextSerializer = Partial<Record<keyof typeof RichTextNodeType, SvelteRichTextComponent>>;
4
- type SvelteRichTextComponent = new (...args: any[]) => SvelteComponent<Partial<{
5
- node: RTAnyNode;
6
- }>>;
1
+ import type { RTAnyNode, RichTextNodeType, Slice, TableField, TableFieldBody, TableFieldBodyRow, TableFieldDataCell, TableFieldHead, TableFieldHeadRow, TableFieldHeaderCell } from "@prismicio/client";
2
+ import type { Component } from "svelte";
3
+ export type SvelteRichTextSerializer = Partial<Record<keyof typeof RichTextNodeType, Component<{
4
+ node?: RTAnyNode;
5
+ }>>>;
7
6
  export type TableComponents = {
8
- table?: ComponentType<SvelteComponent<{
7
+ table?: Component<{
9
8
  table: TableField<"filled">;
10
- }>>;
11
- thead?: ComponentType<SvelteComponent<{
9
+ }>;
10
+ thead?: Component<{
12
11
  head: TableFieldHead;
13
- }>>;
14
- tbody?: ComponentType<SvelteComponent<{
12
+ }>;
13
+ tbody?: Component<{
15
14
  body: TableFieldBody;
16
- }>>;
17
- tr?: ComponentType<SvelteComponent<{
15
+ }>;
16
+ tr?: Component<{
18
17
  row: TableFieldHeadRow | TableFieldBodyRow;
19
- }>>;
20
- th?: ComponentType<SvelteComponent<{
21
- table: TableFieldHeaderCell;
22
- }>>;
23
- td?: ComponentType<SvelteComponent<{
24
- table: TableFieldDataCell;
25
- }>>;
18
+ }>;
19
+ th?: Component<{
20
+ cell: TableFieldHeaderCell;
21
+ }>;
22
+ td?: Component<{
23
+ cell: TableFieldDataCell;
24
+ }>;
26
25
  };
26
+ /**
27
+ * Props for a component rendering content from a Prismic Slice using the
28
+ * `<SliceZone>` component.
29
+ *
30
+ * @typeParam TSlice - The Slice passed as a prop.
31
+ * @typeParam TContext - Arbitrary data passed to `<SliceZone>` and made
32
+ * available to all Slice components.
33
+ */
34
+ export type SliceComponentProps<TSlice extends SliceLike = SliceLike, TContext = unknown> = {
35
+ /**
36
+ * Slice data for this component.
37
+ */
38
+ slice: TSlice;
39
+ /**
40
+ * The index of the Slice in the Slice Zone.
41
+ */
42
+ index: number;
43
+ /**
44
+ * All Slices from the Slice Zone to which the Slice belongs.
45
+ */
46
+ slices: SliceZoneLike<TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2>;
47
+ /**
48
+ * Arbitrary data passed to `<SliceZone>` and made available to all Slice
49
+ * components.
50
+ */
51
+ context: TContext;
52
+ };
53
+ /**
54
+ * The minimum required properties to represent a Prismic Slice from the Prismic
55
+ * Rest API V2 for the `<SliceZone>` component.
56
+ *
57
+ * If using Prismic's Rest API V2, use the `Slice` export from
58
+ * `@prismicio/types` for a full interface.
59
+ *
60
+ * @typeParam SliceType - Type name of the Slice.
61
+ */
62
+ type SliceLikeRestV2<SliceType extends string = string> = {
63
+ slice_type: Slice<SliceType>["slice_type"];
64
+ };
65
+ /**
66
+ * The minimum required properties to represent a Prismic Slice from the Prismic
67
+ * GraphQL API for the `<SliceZone>` component.
68
+ *
69
+ * @typeParam SliceType - Type name of the Slice.
70
+ */
71
+ type SliceLikeGraphQL<SliceType extends string = string> = {
72
+ type: Slice<SliceType>["slice_type"];
73
+ };
74
+ /**
75
+ * The minimum required properties to represent a Prismic Slice for the
76
+ * `<SliceZone>` component.
77
+ *
78
+ * If using Prismic's Rest API V2, use the `Slice` export from
79
+ * `@prismicio/types` for a full interface.
80
+ *
81
+ * @typeParam SliceType - Type name of the Slice.
82
+ */
83
+ type SliceLike<SliceType extends string = string> = (SliceLikeRestV2<SliceType> | SliceLikeGraphQL<SliceType>) & {
84
+ /**
85
+ * If `true`, this Slice has been modified from its original value using a
86
+ * mapper and `@prismicio/client`'s `mapSliceZone()`.
87
+ *
88
+ * @internal
89
+ */
90
+ __mapped?: true;
91
+ };
92
+ /**
93
+ * A looser version of the `SliceZone` type from `@prismicio/client` using
94
+ * `SliceLike`.
95
+ *
96
+ * If using Prismic's Rest API V2, use the `SliceZone` export from
97
+ * `@prismicio/client` for the full type.
98
+ *
99
+ * @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
100
+ */
101
+ export type SliceZoneLike<TSlice extends SliceLike = SliceLike> = readonly TSlice[];
27
102
  export {};