@prismicio/vue 3.0.0-beta.1 → 3.0.0-beta.2

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/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { getEndpoint, createClient, predicate, cookie } from '@prismicio/client';
2
- import { asLink, asText, asHTML, asDate, documentToLinkField } from '@prismicio/helpers';
2
+ import { isFilled, asImageWidthSrcSet, asImagePixelDensitySrcSet, asImageSrc, asLink, asText, asHTML, asDate, documentToLinkField } from '@prismicio/helpers';
3
3
  import { resolveDynamicComponent, defineComponent, h, inject, computed, unref, reactive, ref, watch, nextTick, onBeforeUnmount, watchEffect, markRaw, shallowRef, isRef } from 'vue';
4
4
  import { routerKey } from 'vue-router';
5
5
 
@@ -44,6 +44,62 @@ const usePrismic = () => {
44
44
  };
45
45
 
46
46
  const defaultImageComponent = "img";
47
+ const usePrismicImage = (props) => {
48
+ const asImage = computed(() => {
49
+ const field = unref(props.field);
50
+ if (!isFilled.image(field)) {
51
+ return {
52
+ src: null,
53
+ srcset: null
54
+ };
55
+ }
56
+ const imgixParams = unref(props.imgixParams);
57
+ const widths = unref(props.widths);
58
+ const pixelDensities = unref(props.pixelDensities);
59
+ if (widths) {
60
+ if (pixelDensities) {
61
+ console.warn("[PrismicImage] `widths` and `pixelDensities` props should not be use alongside each others, only `widths` will be applied", props);
62
+ }
63
+ if (widths === "auto") {
64
+ return asImageWidthSrcSet(field, imgixParams);
65
+ } else {
66
+ const { url, dimensions, alt: alt2, copyright: copyright2 } = field;
67
+ return asImageWidthSrcSet({ url, dimensions, alt: alt2, copyright: copyright2 }, {
68
+ ...imgixParams,
69
+ widths: widths === "defaults" ? void 0 : widths
70
+ });
71
+ }
72
+ } else if (pixelDensities) {
73
+ return asImagePixelDensitySrcSet(field, {
74
+ ...imgixParams,
75
+ pixelDensities: pixelDensities === "defaults" ? void 0 : pixelDensities
76
+ });
77
+ } else {
78
+ return {
79
+ src: asImageSrc(field, imgixParams),
80
+ srcset: null
81
+ };
82
+ }
83
+ });
84
+ const src = computed(() => {
85
+ return asImage.value.src;
86
+ });
87
+ const srcset = computed(() => {
88
+ return asImage.value.srcset;
89
+ });
90
+ const alt = computed(() => {
91
+ return unref(props.field).alt || null;
92
+ });
93
+ const copyright = computed(() => {
94
+ return unref(props.field).copyright || null;
95
+ });
96
+ return {
97
+ src,
98
+ srcset,
99
+ alt,
100
+ copyright
101
+ };
102
+ };
47
103
  const PrismicImageImpl = /* @__PURE__ */ defineComponent({
48
104
  name: "PrismicImage",
49
105
  props: {
@@ -56,10 +112,20 @@ const PrismicImageImpl = /* @__PURE__ */ defineComponent({
56
112
  default: void 0,
57
113
  required: false
58
114
  },
59
- imageComponentAdditionalProps: {
115
+ imgixParams: {
60
116
  type: Object,
61
117
  default: void 0,
62
118
  required: false
119
+ },
120
+ widths: {
121
+ type: [String, Object],
122
+ default: void 0,
123
+ required: false
124
+ },
125
+ pixelDensities: {
126
+ type: [String, Object],
127
+ default: void 0,
128
+ required: false
63
129
  }
64
130
  },
65
131
  setup(props) {
@@ -71,10 +137,12 @@ const PrismicImageImpl = /* @__PURE__ */ defineComponent({
71
137
  var _a;
72
138
  return props.imageComponent || ((_a = options.components) == null ? void 0 : _a.imageComponent) || defaultImageComponent;
73
139
  });
140
+ const { src, srcset, alt, copyright } = usePrismicImage(props);
74
141
  return () => {
75
142
  const attributes = {
76
- src: props.field.url || null,
77
- alt: props.field.alt || null
143
+ src: src.value,
144
+ srcset: srcset.value,
145
+ alt: alt.value
78
146
  };
79
147
  switch (type.value) {
80
148
  case "img":
@@ -82,8 +150,7 @@ const PrismicImageImpl = /* @__PURE__ */ defineComponent({
82
150
  default:
83
151
  return h(simplyResolveComponent(type.value), {
84
152
  ...attributes,
85
- copyright: props.field.copyright || null,
86
- ...props.imageComponentAdditionalProps
153
+ copyright: copyright.value
87
154
  });
88
155
  }
89
156
  };
@@ -221,7 +288,7 @@ const PrismicLink = PrismicLinkImpl;
221
288
  const defaultWrapper$1 = "div";
222
289
  const usePrismicText = (props) => {
223
290
  const text = computed(() => {
224
- return asText(unref(props.field), unref(props.separator));
291
+ return asText(unref(props.field), unref(props.separator)) || "";
225
292
  });
226
293
  return {
227
294
  text
@@ -267,7 +334,7 @@ const usePrismicRichText = (props) => {
267
334
  var _a, _b;
268
335
  const linkResolver = (_a = unref(props.linkResolver)) != null ? _a : options.linkResolver;
269
336
  const htmlSerializer = (_b = unref(props.htmlSerializer)) != null ? _b : options.htmlSerializer;
270
- return asHTML(unref(props.field), linkResolver, htmlSerializer);
337
+ return asHTML(unref(props.field), linkResolver, htmlSerializer) || "";
271
338
  });
272
339
  return {
273
340
  html
@@ -510,6 +577,9 @@ const createPrismic = (options) => {
510
577
  return asLink(linkField, linkResolver || options.linkResolver);
511
578
  },
512
579
  asDate,
580
+ asImageSrc,
581
+ asImageWidthSrcSet,
582
+ asImagePixelDensitySrcSet,
513
583
  documentToLinkField
514
584
  };
515
585
  const prismic = {
@@ -590,5 +660,5 @@ const usePrismicDocumentsBySomeTags = (...args) => useStatefulPrismicClientMetho
590
660
  const useAllPrismicDocumentsBySomeTags = (...args) => useStatefulPrismicClientMethod("getAllBySomeTags", args);
591
661
  const dangerouslyUseAllPrismicDocuments = (...args) => useStatefulPrismicClientMethod("dangerouslyGetAll", args);
592
662
 
593
- export { PrismicClientComposableState, PrismicEmbed, PrismicImage, PrismicLink, PrismicRichText, PrismicText, SliceZone, SliceZoneImpl, TODOSliceComponent, createPrismic, dangerouslyUseAllPrismicDocuments, defineSliceZoneComponents, getSliceComponentProps, prismicKey, useAllPrismicDocumentsByEveryTag, useAllPrismicDocumentsByIDs, useAllPrismicDocumentsBySomeTags, useAllPrismicDocumentsByTag, useAllPrismicDocumentsByType, useAllPrismicDocumentsByUIDs, useFirstPrismicDocument, usePrismic, usePrismicDocumentByID, usePrismicDocumentByUID, usePrismicDocuments, usePrismicDocumentsByEveryTag, usePrismicDocumentsByIDs, usePrismicDocumentsBySomeTags, usePrismicDocumentsByTag, usePrismicDocumentsByType, usePrismicDocumentsByUIDs, usePrismicLink, usePrismicRichText, usePrismicText, useSinglePrismicDocument };
663
+ export { PrismicClientComposableState, PrismicEmbed, PrismicImage, PrismicLink, PrismicRichText, PrismicText, SliceZone, SliceZoneImpl, TODOSliceComponent, createPrismic, dangerouslyUseAllPrismicDocuments, defineSliceZoneComponents, getSliceComponentProps, prismicKey, useAllPrismicDocumentsByEveryTag, useAllPrismicDocumentsByIDs, useAllPrismicDocumentsBySomeTags, useAllPrismicDocumentsByTag, useAllPrismicDocumentsByType, useAllPrismicDocumentsByUIDs, useFirstPrismicDocument, usePrismic, usePrismicDocumentByID, usePrismicDocumentByUID, usePrismicDocuments, usePrismicDocumentsByEveryTag, usePrismicDocumentsByIDs, usePrismicDocumentsBySomeTags, usePrismicDocumentsByTag, usePrismicDocumentsByType, usePrismicDocumentsByUIDs, usePrismicImage, usePrismicLink, usePrismicRichText, usePrismicText, useSinglePrismicDocument };
594
664
  //# sourceMappingURL=index.js.map