@prismicio/svelte 0.0.5 → 0.0.7

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/CHANGELOG.md CHANGED
@@ -2,12 +2,30 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
- ### [0.0.5](https://github.com/prismicio/prismic-svelte/compare/v0.0.4...v0.0.5) (2023-02-23)
5
+ ### [0.0.7](https://github.com/prismicio/prismic-svelte/compare/v0.0.6...v0.0.7) (2023-03-02)
6
+
7
+
8
+ ### Features
6
9
 
10
+ * create `<PrismicImage>` component ([#5](https://github.com/prismicio/prismic-svelte/issues/5)) ([9b29b58](https://github.com/prismicio/prismic-svelte/commit/9b29b58d771ae0d6cf6960a28d35035c7fff23b0))
11
+
12
+ ### [0.0.6](https://github.com/prismicio/prismic-svelte/compare/v0.0.5...v0.0.6) (2023-02-24)
13
+
14
+
15
+ ### Features
16
+
17
+ * add `<PrismicLink>` component ([1db9dc0](https://github.com/prismicio/prismic-svelte/commit/1db9dc0cd6a24de87505b5e1486fe9458cb7be01))
18
+
19
+
20
+ ### Chore
21
+
22
+ * fix slice typing ([16340b3](https://github.com/prismicio/prismic-svelte/commit/16340b320396f69127e89d26dae498baff8fa299))
23
+
24
+ ### [0.0.5](https://github.com/prismicio/prismic-svelte/compare/v0.0.4...v0.0.5) (2023-02-23)
7
25
 
8
26
  ### Chore
9
27
 
10
- * upgrade SvelteKit to 1.0 ([#3](https://github.com/prismicio/prismic-svelte/issues/3)) ([8d5addc](https://github.com/prismicio/prismic-svelte/commit/8d5addcdd669895ccacb666286b57af7825f4869))
28
+ - upgrade SvelteKit to 1.0 ([#3](https://github.com/prismicio/prismic-svelte/issues/3)) ([8d5addc](https://github.com/prismicio/prismic-svelte/commit/8d5addcdd669895ccacb666286b57af7825f4869))
11
29
 
12
30
  ### [0.0.4](https://github.com/prismicio/prismic-svelte/compare/v0.0.3...v0.0.4) (2022-05-27)
13
31
 
@@ -0,0 +1,11 @@
1
+ // We need to polyfill process if it doesn't exist, such as in the browser.
2
+ if (typeof process === "undefined") {
3
+ globalThis.process = { env: {} };
4
+ }
5
+ /**
6
+ * `true` if in the production environment, `false` otherwise.
7
+ *
8
+ * This boolean can be used to perform actions only in development environments,
9
+ * such as logging.
10
+ */
11
+ export const __PRODUCTION__ = process.env.NODE_ENV === "production";
@@ -1,17 +1,91 @@
1
1
  <script>import * as prismicH from "@prismicio/helpers";
2
+ import { __PRODUCTION__ } from "../__PRODUCTION__";
2
3
  export let field;
3
- let className;
4
- // creates a `class` property, even
5
- // though it is a reserved word
6
- export { className as class };
4
+ export let imgixParams = {};
5
+ export let widths = undefined;
6
+ export let pixelDensities = undefined;
7
+ export let width = undefined;
8
+ export let height = undefined;
9
+ export let alt = undefined;
10
+ export let fallbackAlt = undefined;
11
+ const castInt = (input) => {
12
+ if (typeof input === "number" ||
13
+ typeof input === "undefined" ||
14
+ input === null) {
15
+ return input;
16
+ }
17
+ else {
18
+ const parsed = Number.parseInt(input);
19
+ if (Number.isNaN(parsed)) {
20
+ return undefined;
21
+ }
22
+ else {
23
+ return parsed;
24
+ }
25
+ }
26
+ };
27
+ const ar = field.dimensions && field.dimensions.width / field.dimensions.height;
28
+ const castedWidth = castInt(width);
29
+ const castedHeight = castInt(height);
30
+ let resolvedWidth = castedWidth ?? (field.dimensions && field.dimensions.width);
31
+ let resolvedHeight = castedHeight ?? (field.dimensions && field.dimensions.height);
32
+ if (ar &&
33
+ castedWidth != null &&
34
+ (castedHeight == null || typeof castedWidth !== "undefined")) {
35
+ resolvedHeight = castedWidth / ar;
36
+ }
37
+ else if (ar &&
38
+ castedHeight != null &&
39
+ (castedWidth == null || typeof castedWidth !== "undefined")) {
40
+ resolvedWidth = castedHeight * ar;
41
+ }
42
+ if (!__PRODUCTION__) {
43
+ if (widths && pixelDensities) {
44
+ console.warn(`[PrismicImage] Only one of "widths" or "pixelDensities" props can be provided. You can resolve this warning by removing either the "widths" or "pixelDensities" prop. "widths" will be used in this case.`);
45
+ }
46
+ if (typeof alt === "string" && alt !== "") {
47
+ console.warn(`[PrismicImage] The "alt" prop can only be used to declare an image as decorative by passing an empty string (alt="") but was provided a non-empty string. You can resolve this warning by removing the "alt" prop or changing it to alt="".`);
48
+ }
49
+ if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
50
+ console.warn(`[PrismicImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="".`);
51
+ }
52
+ }
7
53
  </script>
8
54
 
55
+ <!--
56
+ @component
57
+ Component to render a Prismic Image field as an `img` element with `width`, `height`, `alt`, `src`, and `srcset` attributes.
58
+
59
+ @example Rendering an Image field:
60
+ ```svelte
61
+ <PrismicImage
62
+ field={document.data.example_image}
63
+ imgixParams={{ sat: -100 }}
64
+ />
65
+ ```
66
+ -->
67
+
9
68
  {#if prismicH.isFilled.image(field)}
10
- {@const { alt, dimensions } = field}
11
- {@const { width, height } = dimensions}
12
- {@const { src, srcset } = prismicH.asImageWidthSrcSet(field)}
69
+ {@const { src, srcset } =
70
+ pixelDensities && !widths
71
+ ? prismicH.asImagePixelDensitySrcSet(field, {
72
+ pixelDensities:
73
+ pixelDensities === "defaults" ? undefined : pixelDensities,
74
+ ...imgixParams,
75
+ })
76
+ : prismicH.asImageWidthSrcSet(field, {
77
+ widths: widths === "defaults" ? undefined : widths,
78
+ ...imgixParams,
79
+ })}
13
80
 
14
- <img {src} {srcset} {alt} {width} {height} class={className} />
81
+ <img
82
+ {src}
83
+ {srcset}
84
+ alt={alt ?? field.alt ?? fallbackAlt}
85
+ width={width || height ? resolvedWidth : field.dimensions.width}
86
+ height={height || width ? resolvedHeight : field.dimensions.height}
87
+ {...$$restProps}
88
+ />
15
89
  {:else}
16
- <img alt={field.alt || ""} class={className} />
90
+ <img alt={field.alt || ""} {...$$restProps} />
17
91
  {/if}
@@ -1,44 +1,25 @@
1
1
  <script>import * as prismicH from "@prismicio/helpers";
2
- import { usePrismic } from "../usePrismic";
3
- // const defaultBlankTargetRelAttribute = "noopener noreferrer";
4
- export let field, linkResolver, download, hreflang, referrerpolicy, rel, target, type, prefetch, noscroll;
5
- const setRel = () => {
6
- let rels = [];
7
- const defaultBlankTargetRelAttribute = ["noopener", "noreferrer"];
8
- if (rel)
9
- rels.push(...rel?.split(" "));
10
- if ((field && "target" in field && field?.target === "_blank") ||
11
- target === "_blank")
12
- defaultBlankTargetRelAttribute.forEach((item) => {
13
- if (!rels.includes(item))
14
- rels.push(item);
15
- });
16
- if (field &&
17
- "link_type" in field &&
18
- field?.link_type === "Web" &&
19
- !rels.includes("external"))
20
- rels.push("external");
21
- return rels.join(" ");
22
- };
23
- const attrs = {
24
- download,
25
- hreflang,
26
- referrerpolicy,
27
- target: target || field?.target || undefined,
28
- rel: setRel(),
29
- type,
30
- };
31
- const link = field
32
- ? prismicH.asLink(field, linkResolver || usePrismic().linkResolver)
33
- : "";
34
- const href = link || "";
2
+ export let field;
3
+ export let linkResolver = undefined;
4
+ export let target = undefined;
5
+ const resolvedTarget = target || (field && "target" in field && field.target) || undefined;
6
+ export let rel = undefined;
7
+ const resolvedRel = rel || (target === "_blank" ? "noopener noreferrer" : undefined);
8
+ const href = (field ? prismicH.asLink(field, linkResolver) : "") || "";
35
9
  </script>
36
10
 
37
- <a
38
- {href}
39
- {...attrs}
40
- sveltekit:prefetch={prefetch}
41
- sveltekit:noscroll={noscroll}
42
- >
11
+ <!--
12
+ @component
13
+ Component to render a Prismic Link, Link to Media, Content Relationship, or whole Document as an `a` tag.
14
+
15
+ @example Rendering a Link field:
16
+ ```svelte
17
+ <PrismicLink field={document.data.example_link}>
18
+ Example anchor text.
19
+ </PrismicLink>
20
+ ```
21
+ -->
22
+
23
+ <a {href} {...$$restProps} rel={resolvedRel} target={resolvedTarget}>
43
24
  <slot />
44
25
  </a>
@@ -1,6 +1,6 @@
1
1
  // export { default as PrismicEmbed } from "./PrismicEmbed.svelte";
2
- // export { default as PrismicImage } from "./PrismicImage.svelte";
3
- // export { default as PrismicLink } from "./PrismicLink.svelte";
2
+ export { default as PrismicImage } from "./PrismicImage.svelte";
3
+ export { default as PrismicLink } from "./PrismicLink.svelte";
4
4
  // export { default as PrismicText } from "./PrismicText.svelte";
5
5
  // export { default as PrismicRichText } from "./PrismicRichText.svelte";
6
6
  // export { default as PrismicPreview } from "./PrismicPreview.svelte";
package/index.js CHANGED
@@ -2,8 +2,7 @@
2
2
  export {
3
3
  // Components
4
4
  // PrismicEmbed,
5
- // PrismicImage,
6
- // PrismicLink,
5
+ PrismicImage, PrismicLink,
7
6
  // PrismicText,
8
7
  // PrismicRichText,
9
8
  // Slice Zone
@@ -0,0 +1,7 @@
1
+ /**
2
+ * `true` if in the production environment, `false` otherwise.
3
+ *
4
+ * This boolean can be used to perform actions only in development environments,
5
+ * such as logging.
6
+ */
7
+ export declare const __PRODUCTION__: boolean;
@@ -1,9 +1,51 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
+ import * as prismicH from "@prismicio/helpers";
2
3
  import type * as prismicT from "@prismicio/types";
3
4
  declare const __propDef: {
4
5
  props: {
6
+ /**
7
+ * A Prismic Image field.
8
+ */
5
9
  field: prismicT.ImageField;
6
- class: string;
10
+ /**
11
+ * An object of Imgix URL API parameters to transform the image.
12
+ *
13
+ * See: https://docs.imgix.com/apis/rendering
14
+ */
15
+ imgixParams?: Parameters<typeof prismicH.asImageSrc>[1];
16
+ /**
17
+ * Widths used to build a `srcset` value for the Image field.
18
+ *
19
+ * If a `widths` prop is not given or `"defaults"` is passed, the following
20
+ * widths will be used: 640, 750, 828, 1080, 1200, 1920, 2048, 3840.
21
+ *
22
+ * If the Image field contains responsive views, each responsive view can be
23
+ * used as a width in the resulting `srcset` by passing `"thumbnails"` as
24
+ * the `widths` prop.
25
+ */
26
+ widths?: NonNullable<Parameters<typeof prismicH.asImageWidthSrcSet>[1]>["widths"] | "defaults";
27
+ /**
28
+ * Pixel densities used to build a `srcset` value for the Image field.
29
+ *
30
+ * If a `pixelDensities` prop is passed `"defaults"`, the following pixel
31
+ * densities will be used: 1, 2, 3.
32
+ */
33
+ pixelDensities?: NonNullable<Parameters<typeof prismicH.asImagePixelDensitySrcSet>[1]>["pixelDensities"] | "defaults";
34
+ /**
35
+ * Declare an image as decorative by providing `alt=""`.
36
+ *
37
+ * See:
38
+ * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
39
+ */
40
+ alt?: "" | undefined;
41
+ /**
42
+ * Declare an image as decorative only if the Image field does not have
43
+ * alternative text by providing `fallbackAlt=""`.
44
+ *
45
+ * See:
46
+ * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
47
+ */
48
+ fallbackAlt?: "" | undefined;
7
49
  };
8
50
  events: {
9
51
  [evt: string]: CustomEvent<any>;
@@ -13,6 +55,17 @@ declare const __propDef: {
13
55
  export type PrismicImageProps = typeof __propDef.props;
14
56
  export type PrismicImageEvents = typeof __propDef.events;
15
57
  export type PrismicImageSlots = typeof __propDef.slots;
58
+ /**
59
+ * Component to render a Prismic Image field as an `img` element with `width`, `height`, `alt`, `src`, and `srcset` attributes.
60
+ *
61
+ * @example Rendering an Image field:
62
+ * ```svelte
63
+ * <PrismicImage
64
+ * field={document.data.example_image}
65
+ * imgixParams={{ sat: -100 }}
66
+ * />
67
+ * ```
68
+ */
16
69
  export default class PrismicImage extends SvelteComponentTyped<PrismicImageProps, PrismicImageEvents, PrismicImageSlots> {
17
70
  }
18
71
  export {};
@@ -1,19 +1,6 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
- import * as prismicH from "@prismicio/helpers";
3
- import type * as prismicT from "@prismicio/types";
4
2
  declare const __propDef: {
5
- props: {
6
- field: prismicT.LinkField | prismicT.PrismicDocument | null | undefined;
7
- linkResolver: prismicH.LinkResolverFunction | null | undefined;
8
- download: boolean | null | undefined;
9
- hreflang: string | undefined;
10
- referrerpolicy: string | undefined;
11
- rel: string | undefined;
12
- target: string | undefined;
13
- type: string | undefined;
14
- prefetch: true | undefined;
15
- noscroll: true | undefined;
16
- };
3
+ props: any;
17
4
  events: {
18
5
  [evt: string]: CustomEvent<any>;
19
6
  };
@@ -24,6 +11,16 @@ declare const __propDef: {
24
11
  export type PrismicLinkProps = typeof __propDef.props;
25
12
  export type PrismicLinkEvents = typeof __propDef.events;
26
13
  export type PrismicLinkSlots = typeof __propDef.slots;
14
+ /**
15
+ * Component to render a Prismic Link, Link to Media, Content Relationship, or whole Document as an `a` tag.
16
+ *
17
+ * @example Rendering a Link field:
18
+ * ```svelte
19
+ * <PrismicLink field={document.data.example_link}>
20
+ * Example anchor text.
21
+ * </PrismicLink>
22
+ * ```
23
+ */
27
24
  export default class PrismicLink extends SvelteComponentTyped<PrismicLinkProps, PrismicLinkEvents, PrismicLinkSlots> {
28
25
  }
29
26
  export {};
@@ -1 +1,3 @@
1
+ export { default as PrismicImage } from "./PrismicImage.svelte";
2
+ export { default as PrismicLink } from "./PrismicLink.svelte";
1
3
  export { default as SliceZone } from "./SliceZone.svelte";
package/lib/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { SliceZone, } from "./components";
1
+ export { PrismicImage, PrismicLink, SliceZone, } from "./components";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/svelte",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Svelte plugin, components, and composables to fetch and present Prismic content",
5
5
  "keywords": [
6
6
  "typescript",
@@ -0,0 +1,12 @@
1
+ // We need to polyfill process if it doesn't exist, such as in the browser.
2
+ if (typeof process === "undefined") {
3
+ globalThis.process = { env: {} } as typeof process;
4
+ }
5
+
6
+ /**
7
+ * `true` if in the production environment, `false` otherwise.
8
+ *
9
+ * This boolean can be used to perform actions only in development environments,
10
+ * such as logging.
11
+ */
12
+ export const __PRODUCTION__ = process.env.NODE_ENV === "production";
@@ -1,22 +1,179 @@
1
1
  <script lang="ts">
2
2
  import * as prismicH from "@prismicio/helpers";
3
3
  import type * as prismicT from "@prismicio/types";
4
+ import { __PRODUCTION__ } from "$lib/__PRODUCTION__";
4
5
 
5
- export let field: prismicT.ImageField;
6
+ type PrismicImageProps = {
7
+ /**
8
+ * A Prismic Image field.
9
+ */
10
+ field: prismicT.ImageField;
11
+ /**
12
+ * An object of Imgix URL API parameters to transform the image.
13
+ *
14
+ * See: https://docs.imgix.com/apis/rendering
15
+ */
16
+ imgixParams?: Parameters<typeof prismicH.asImageSrc>[1];
17
+ /**
18
+ * Widths used to build a `srcset` value for the Image field.
19
+ *
20
+ * If a `widths` prop is not given or `"defaults"` is passed, the following
21
+ * widths will be used: 640, 750, 828, 1080, 1200, 1920, 2048, 3840.
22
+ *
23
+ * If the Image field contains responsive views, each responsive view can be
24
+ * used as a width in the resulting `srcset` by passing `"thumbnails"` as
25
+ * the `widths` prop.
26
+ */
27
+ widths?:
28
+ | NonNullable<Parameters<typeof prismicH.asImageWidthSrcSet>[1]>["widths"]
29
+ | "defaults";
30
+ /**
31
+ * Pixel densities used to build a `srcset` value for the Image field.
32
+ *
33
+ * If a `pixelDensities` prop is passed `"defaults"`, the following pixel
34
+ * densities will be used: 1, 2, 3.
35
+ */
36
+ pixelDensities?:
37
+ | NonNullable<
38
+ Parameters<typeof prismicH.asImagePixelDensitySrcSet>[1]
39
+ >["pixelDensities"]
40
+ | "defaults";
41
+ /**
42
+ * Declare an image as decorative by providing `alt=""`.
43
+ *
44
+ * See:
45
+ * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
46
+ */
47
+ alt?: "";
48
+ /**
49
+ * Declare an image as decorative only if the Image field does not have
50
+ * alternative text by providing `fallbackAlt=""`.
51
+ *
52
+ * See:
53
+ * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
54
+ */
55
+ fallbackAlt?: "";
56
+ };
6
57
 
7
- let className: string;
58
+ type $$Props = Omit<svelteHTML.IntrinsicElements["img"], "alt"> &
59
+ PrismicImageProps;
8
60
 
9
- // creates a `class` property, even
10
- // though it is a reserved word
11
- export { className as class };
61
+ export let field: $$Props["field"];
62
+
63
+ export let imgixParams: $$Props["imgixParams"] = {};
64
+
65
+ export let widths: $$Props["widths"] = undefined;
66
+
67
+ export let pixelDensities: $$Props["pixelDensities"] = undefined;
68
+
69
+ export let width: $$Props["width"] = undefined;
70
+
71
+ export let height: $$Props["height"] = undefined;
72
+
73
+ export let alt: $$Props["alt"] = undefined;
74
+
75
+ export let fallbackAlt: $$Props["fallbackAlt"] = undefined;
76
+
77
+ const castInt = (
78
+ input: string | number | null | undefined,
79
+ ): number | undefined | null => {
80
+ if (
81
+ typeof input === "number" ||
82
+ typeof input === "undefined" ||
83
+ input === null
84
+ ) {
85
+ return input;
86
+ } else {
87
+ const parsed = Number.parseInt(input);
88
+
89
+ if (Number.isNaN(parsed)) {
90
+ return undefined;
91
+ } else {
92
+ return parsed;
93
+ }
94
+ }
95
+ };
96
+
97
+ const ar =
98
+ field.dimensions && field.dimensions.width / field.dimensions.height;
99
+
100
+ const castedWidth = castInt(width);
101
+ const castedHeight = castInt(height);
102
+
103
+ let resolvedWidth =
104
+ castedWidth ?? (field.dimensions && field.dimensions.width);
105
+ let resolvedHeight =
106
+ castedHeight ?? (field.dimensions && field.dimensions.height);
107
+
108
+ if (
109
+ ar &&
110
+ castedWidth != null &&
111
+ (castedHeight == null || typeof castedWidth !== "undefined")
112
+ ) {
113
+ resolvedHeight = castedWidth / ar;
114
+ } else if (
115
+ ar &&
116
+ castedHeight != null &&
117
+ (castedWidth == null || typeof castedWidth !== "undefined")
118
+ ) {
119
+ resolvedWidth = castedHeight * ar;
120
+ }
121
+
122
+ if (!__PRODUCTION__) {
123
+ if (widths && pixelDensities) {
124
+ console.warn(
125
+ `[PrismicImage] Only one of "widths" or "pixelDensities" props can be provided. You can resolve this warning by removing either the "widths" or "pixelDensities" prop. "widths" will be used in this case.`,
126
+ );
127
+ }
128
+
129
+ if (typeof alt === "string" && alt !== "") {
130
+ console.warn(
131
+ `[PrismicImage] The "alt" prop can only be used to declare an image as decorative by passing an empty string (alt="") but was provided a non-empty string. You can resolve this warning by removing the "alt" prop or changing it to alt="".`,
132
+ );
133
+ }
134
+
135
+ if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
136
+ console.warn(
137
+ `[PrismicImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="".`,
138
+ );
139
+ }
140
+ }
12
141
  </script>
13
142
 
143
+ <!--
144
+ @component
145
+ Component to render a Prismic Image field as an `img` element with `width`, `height`, `alt`, `src`, and `srcset` attributes.
146
+
147
+ @example Rendering an Image field:
148
+ ```svelte
149
+ <PrismicImage
150
+ field={document.data.example_image}
151
+ imgixParams={{ sat: -100 }}
152
+ />
153
+ ```
154
+ -->
155
+
14
156
  {#if prismicH.isFilled.image(field)}
15
- {@const { alt, dimensions } = field}
16
- {@const { width, height } = dimensions}
17
- {@const { src, srcset } = prismicH.asImageWidthSrcSet(field)}
157
+ {@const { src, srcset } =
158
+ pixelDensities && !widths
159
+ ? prismicH.asImagePixelDensitySrcSet(field, {
160
+ pixelDensities:
161
+ pixelDensities === "defaults" ? undefined : pixelDensities,
162
+ ...imgixParams,
163
+ })
164
+ : prismicH.asImageWidthSrcSet(field, {
165
+ widths: widths === "defaults" ? undefined : widths,
166
+ ...imgixParams,
167
+ })}
18
168
 
19
- <img {src} {srcset} {alt} {width} {height} class={className} />
169
+ <img
170
+ {src}
171
+ {srcset}
172
+ alt={alt ?? field.alt ?? fallbackAlt}
173
+ width={width || height ? resolvedWidth : field.dimensions.width}
174
+ height={height || width ? resolvedHeight : field.dimensions.height}
175
+ {...$$restProps}
176
+ />
20
177
  {:else}
21
- <img alt={field.alt || ""} class={className} />
178
+ <img alt={field.alt || ""} {...$$restProps} />
22
179
  {/if}
@@ -1,67 +1,53 @@
1
1
  <script lang="ts">
2
2
  import * as prismicH from "@prismicio/helpers";
3
3
  import type * as prismicT from "@prismicio/types";
4
+ // import { usePrismic } from "../usePrismic";
4
5
 
5
- import { usePrismic } from "../usePrismic";
6
+ type PrismicLinkProps = {
7
+ /**
8
+ * A Prismic Link field, Content Relationship field, Link to Media field or Document.
9
+ */
10
+ field: prismicT.LinkField | prismicT.PrismicDocument;
6
11
 
7
- // const defaultBlankTargetRelAttribute = "noopener noreferrer";
12
+ /**
13
+ * The Link Resolver used to resolve links.
14
+ *
15
+ * @remarks
16
+ * If your app uses Route Resolvers when querying for your Prismic
17
+ * repository's content, a Link Resolver does not need to be provided.
18
+ * @see Learn about Link Resolvers and Route Resolvers {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
19
+ */
20
+ linkResolver?: prismicH.LinkResolverFunction | undefined;
21
+ };
8
22
 
9
- export let field:
10
- | prismicT.LinkField
11
- | prismicT.PrismicDocument
12
- | null
13
- | undefined,
14
- linkResolver: prismicH.LinkResolverFunction | null | undefined,
15
- download: boolean | null | undefined,
16
- hreflang: string | undefined,
17
- referrerpolicy: string | undefined,
18
- rel: string | undefined,
19
- target: string | undefined,
20
- type: string | undefined,
21
- prefetch: true | undefined,
22
- noscroll: true | undefined;
23
+ type $$Props = svelteHTML.IntrinsicElements["a"] & PrismicLinkProps;
23
24
 
24
- const setRel = () => {
25
- let rels: string[] = [];
26
- const defaultBlankTargetRelAttribute = ["noopener", "noreferrer"];
27
- if (rel) rels.push(...rel?.split(" "));
28
- if (
29
- (field && "target" in field && field?.target === "_blank") ||
30
- target === "_blank"
31
- )
32
- defaultBlankTargetRelAttribute.forEach((item) => {
33
- if (!rels.includes(item)) rels.push(item);
34
- });
35
- if (
36
- field &&
37
- "link_type" in field &&
38
- field?.link_type === "Web" &&
39
- !rels.includes("external")
40
- )
41
- rels.push("external");
42
- return rels.join(" ");
43
- };
25
+ export let field: $$Props["field"];
26
+ export let linkResolver: $$Props["linkResolver"] = undefined;
44
27
 
45
- const attrs = {
46
- download,
47
- hreflang,
48
- referrerpolicy,
49
- target: target || field?.target || undefined,
50
- rel: setRel(),
51
- type,
52
- };
28
+ export let target: $$Props["target"] = undefined;
29
+ const resolvedTarget =
30
+ target || (field && "target" in field && field.target) || undefined;
53
31
 
54
- const link: string | null = field
55
- ? prismicH.asLink(field, linkResolver || usePrismic().linkResolver)
56
- : "";
57
- const href: string = link || "";
32
+ export let rel: $$Props["rel"] = undefined;
33
+ const resolvedRel =
34
+ rel || (target === "_blank" ? "noopener noreferrer" : undefined);
35
+
36
+ const href = (field ? prismicH.asLink(field, linkResolver) : "") || "";
58
37
  </script>
59
38
 
60
- <a
61
- {href}
62
- {...attrs}
63
- sveltekit:prefetch={prefetch}
64
- sveltekit:noscroll={noscroll}
65
- >
39
+ <!--
40
+ @component
41
+ Component to render a Prismic Link, Link to Media, Content Relationship, or whole Document as an `a` tag.
42
+
43
+ @example Rendering a Link field:
44
+ ```svelte
45
+ <PrismicLink field={document.data.example_link}>
46
+ Example anchor text.
47
+ </PrismicLink>
48
+ ```
49
+ -->
50
+
51
+ <a {href} {...$$restProps} rel={resolvedRel} target={resolvedTarget}>
66
52
  <slot />
67
53
  </a>
@@ -1,8 +1,8 @@
1
1
  // export { default as PrismicEmbed } from "./PrismicEmbed.svelte";
2
2
 
3
- // export { default as PrismicImage } from "./PrismicImage.svelte";
3
+ export { default as PrismicImage } from "./PrismicImage.svelte";
4
4
 
5
- // export { default as PrismicLink } from "./PrismicLink.svelte";
5
+ export { default as PrismicLink } from "./PrismicLink.svelte";
6
6
 
7
7
  // export { default as PrismicText } from "./PrismicText.svelte";
8
8
 
package/src/lib/index.ts CHANGED
@@ -3,8 +3,8 @@
3
3
  export {
4
4
  // Components
5
5
  // PrismicEmbed,
6
- // PrismicImage,
7
- // PrismicLink,
6
+ PrismicImage,
7
+ PrismicLink,
8
8
  // PrismicText,
9
9
  // PrismicRichText,
10
10
  // Slice Zone
@@ -3,13 +3,18 @@ import type { PageDocument } from "../types/PageDocument.type"
3
3
  import type { PageServerLoad } from "./$types";
4
4
 
5
5
  export const load = (async ({ fetch }) => {
6
- const endpoint = prismic.getEndpoint("svelte-package-dev");
6
+ const endpoint = prismic.getRepositoryEndpoint("svelte-package-dev");
7
7
  const client = prismic.createClient(endpoint, { fetch });
8
8
 
9
- const document: PageDocument = await client.getSingle("homepage");
9
+ const pageDocument: PageDocument = await client.getSingle("homepage");
10
+
11
+ const exampleClient = prismic.createClient("example-prismic-repo", { fetch })
12
+ const exampleDocument = exampleClient.getFirst()
13
+
10
14
 
11
15
  return {
12
- document,
16
+ pageDocument,
17
+ exampleDocument
13
18
  };
14
19
  // eslint-disable-next-line prettier/prettier
15
20
  }) satisfies PageServerLoad;
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
- import { SliceZone } from "$lib";
3
- import { RichText, CodeSnippet } from "src/routes/slices";
2
+ import { SliceZone, PrismicLink, PrismicImage } from "$lib";
3
+ import { RichText, CodeSnippet } from "./slices";
4
4
  import type { PageData } from "./$types";
5
5
 
6
6
  const components = {
@@ -13,7 +13,25 @@
13
13
 
14
14
  <main>
15
15
  <h1>Welcome to SvelteKit</h1>
16
- <SliceZone slices={data.document.data.body} {components} />
16
+ <h2><code>PrismicImage</code></h2>
17
+ <PrismicImage
18
+ field={data.exampleDocument.data.example_image}
19
+ pixelDensities="defaults"
20
+ height="200"
21
+ alt=""
22
+ />
23
+ <h2><code>PrismicLink</code></h2>
24
+ <PrismicLink
25
+ data-sveltekit-noscroll
26
+ data-sveltekit-preload-code
27
+ data-sveltekit-preload-data
28
+ data-sveltekit-reload
29
+ rel="external"
30
+ class="doggo"
31
+ field={data.exampleDocument.data.example_link}>Link</PrismicLink
32
+ >
33
+ <h2><code>SliceZone</code></h2>
34
+ <SliceZone slices={data.pageDocument.data.body} {components} />
17
35
  </main>
18
36
 
19
37
  <style>
@@ -21,4 +39,9 @@
21
39
  max-width: min(90%, 800px);
22
40
  margin: auto;
23
41
  }
42
+
43
+ :global(img) {
44
+ /* width: 100%; */
45
+ height: auto;
46
+ }
24
47
  </style>
@@ -1,5 +1,7 @@
1
1
  import type * as prismicT from "@prismicio/types";
2
2
 
3
- export type CodeSnippetSlice = prismicT.Slice<{
3
+ type PrimaryFields = {
4
4
  content: prismicT.RichTextField;
5
- }>;
5
+ };
6
+
7
+ export type CodeSnippetSlice = prismicT.Slice<"code_snippet", PrimaryFields>;
@@ -0,0 +1,7 @@
1
+ import type * as prismicT from "@prismicio/types";
2
+
3
+ export type ExampleDocument = prismicT.PrismicDocument<{
4
+ example_link: prismicT.LinkField;
5
+ example_content_relationship: prismicT.ContentRelationshipField;
6
+ example_image: prismicT.ImageField;
7
+ }>;
@@ -1,5 +1,6 @@
1
1
  export type { CodeSnippetSlice } from "./CodeSnippetSlice.type";
2
- export type { PageData } from "./PageDocument.type";
2
+ export type { PageDocument } from "./PageDocument.type";
3
+ export type { ExampleDocument } from "./ExampleDocument.type";
3
4
  export type { PrismicConfig } from "./PrismicConfig.type";
4
5
  export type { PrismicContext } from "./PrismicContext.type";
5
6
  export type { RichTextSlice } from "./RichTextSlice.type";