@prismicio/vue 4.3.0 → 5.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 (245) hide show
  1. package/README.md +4 -4
  2. package/dist/PrismicEmbed.vue.cjs +25 -0
  3. package/dist/PrismicEmbed.vue.cjs.map +1 -0
  4. package/dist/PrismicEmbed.vue.d.ts +19 -0
  5. package/dist/PrismicEmbed.vue.js +26 -0
  6. package/dist/PrismicEmbed.vue.js.map +1 -0
  7. package/dist/PrismicEmbed.vue2.cjs +4 -0
  8. package/dist/PrismicEmbed.vue2.cjs.map +1 -0
  9. package/dist/PrismicEmbed.vue2.js +5 -0
  10. package/dist/PrismicEmbed.vue2.js.map +1 -0
  11. package/dist/PrismicImage.vue.cjs +84 -0
  12. package/dist/PrismicImage.vue.cjs.map +1 -0
  13. package/dist/PrismicImage.vue.d.ts +224 -0
  14. package/dist/PrismicImage.vue.js +85 -0
  15. package/dist/PrismicImage.vue.js.map +1 -0
  16. package/dist/PrismicImage.vue2.cjs +4 -0
  17. package/dist/PrismicImage.vue2.cjs.map +1 -0
  18. package/dist/PrismicImage.vue2.js +5 -0
  19. package/dist/PrismicImage.vue2.js.map +1 -0
  20. package/dist/PrismicLink.vue.cjs +100 -0
  21. package/dist/PrismicLink.vue.cjs.map +1 -0
  22. package/dist/PrismicLink.vue.d.ts +278 -0
  23. package/dist/PrismicLink.vue.js +101 -0
  24. package/dist/PrismicLink.vue.js.map +1 -0
  25. package/dist/PrismicLink.vue2.cjs +4 -0
  26. package/dist/PrismicLink.vue2.cjs.map +1 -0
  27. package/dist/PrismicLink.vue2.js +5 -0
  28. package/dist/PrismicLink.vue2.js.map +1 -0
  29. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue.cjs +90 -0
  30. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue.cjs.map +1 -0
  31. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue.d.ts +7 -0
  32. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue.js +91 -0
  33. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue.js.map +1 -0
  34. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue2.cjs +4 -0
  35. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue2.cjs.map +1 -0
  36. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue2.js +5 -0
  37. package/dist/PrismicRichText/DeprecatedPrismicRichText.vue2.js.map +1 -0
  38. package/dist/PrismicRichText/PrismicRichText.vue.cjs +85 -0
  39. package/dist/PrismicRichText/PrismicRichText.vue.cjs.map +1 -0
  40. package/dist/PrismicRichText/PrismicRichText.vue.d.ts +57 -0
  41. package/dist/PrismicRichText/PrismicRichText.vue.js +86 -0
  42. package/dist/PrismicRichText/PrismicRichText.vue.js.map +1 -0
  43. package/dist/PrismicRichText/PrismicRichText.vue2.cjs +4 -0
  44. package/dist/PrismicRichText/PrismicRichText.vue2.cjs.map +1 -0
  45. package/dist/PrismicRichText/PrismicRichText.vue2.js +5 -0
  46. package/dist/PrismicRichText/PrismicRichText.vue2.js.map +1 -0
  47. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue.cjs +133 -0
  48. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue.cjs.map +1 -0
  49. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue.d.ts +37 -0
  50. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue.js +134 -0
  51. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue.js.map +1 -0
  52. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue2.cjs +4 -0
  53. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue2.cjs.map +1 -0
  54. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue2.js +5 -0
  55. package/dist/PrismicRichText/PrismicRichTextDefaultComponent.vue2.js.map +1 -0
  56. package/dist/PrismicRichText/PrismicRichTextSerialize.vue.cjs +47 -0
  57. package/dist/PrismicRichText/PrismicRichTextSerialize.vue.cjs.map +1 -0
  58. package/dist/PrismicRichText/PrismicRichTextSerialize.vue.d.ts +10 -0
  59. package/dist/PrismicRichText/PrismicRichTextSerialize.vue.js +48 -0
  60. package/dist/PrismicRichText/PrismicRichTextSerialize.vue.js.map +1 -0
  61. package/dist/PrismicRichText/PrismicRichTextSerialize.vue2.cjs +4 -0
  62. package/dist/PrismicRichText/PrismicRichTextSerialize.vue2.cjs.map +1 -0
  63. package/dist/PrismicRichText/PrismicRichTextSerialize.vue2.js +5 -0
  64. package/dist/PrismicRichText/PrismicRichTextSerialize.vue2.js.map +1 -0
  65. package/dist/PrismicRichText/getRichTextComponentProps.cjs +10 -0
  66. package/dist/PrismicRichText/getRichTextComponentProps.cjs.map +1 -0
  67. package/dist/PrismicRichText/getRichTextComponentProps.d.ts +49 -0
  68. package/dist/PrismicRichText/getRichTextComponentProps.js +10 -0
  69. package/dist/PrismicRichText/getRichTextComponentProps.js.map +1 -0
  70. package/dist/PrismicRichText/index.d.ts +2 -0
  71. package/dist/PrismicRichText/types.d.ts +22 -0
  72. package/dist/PrismicText.vue.cjs +43 -0
  73. package/dist/PrismicText.vue.cjs.map +1 -0
  74. package/dist/PrismicText.vue.d.ts +31 -0
  75. package/dist/PrismicText.vue.js +44 -0
  76. package/dist/PrismicText.vue.js.map +1 -0
  77. package/dist/PrismicText.vue2.cjs +4 -0
  78. package/dist/PrismicText.vue2.cjs.map +1 -0
  79. package/dist/PrismicText.vue2.js +5 -0
  80. package/dist/PrismicText.vue2.js.map +1 -0
  81. package/dist/SliceZone/SliceZone.vue.cjs +63 -0
  82. package/dist/SliceZone/SliceZone.vue.cjs.map +1 -0
  83. package/dist/SliceZone/SliceZone.vue.d.ts +40 -0
  84. package/dist/SliceZone/SliceZone.vue.js +64 -0
  85. package/dist/SliceZone/SliceZone.vue.js.map +1 -0
  86. package/dist/SliceZone/SliceZone.vue2.cjs +4 -0
  87. package/dist/SliceZone/SliceZone.vue2.cjs.map +1 -0
  88. package/dist/SliceZone/SliceZone.vue2.js +5 -0
  89. package/dist/SliceZone/SliceZone.vue2.js.map +1 -0
  90. package/dist/SliceZone/TODOSliceComponent.cjs +29 -0
  91. package/dist/SliceZone/TODOSliceComponent.cjs.map +1 -0
  92. package/dist/SliceZone/TODOSliceComponent.d.ts +9 -0
  93. package/dist/SliceZone/TODOSliceComponent.js +29 -0
  94. package/dist/SliceZone/TODOSliceComponent.js.map +1 -0
  95. package/dist/SliceZone/defineSliceZoneComponents.cjs +14 -0
  96. package/dist/SliceZone/defineSliceZoneComponents.cjs.map +1 -0
  97. package/dist/SliceZone/defineSliceZoneComponents.d.ts +36 -0
  98. package/dist/SliceZone/defineSliceZoneComponents.js +14 -0
  99. package/dist/SliceZone/defineSliceZoneComponents.js.map +1 -0
  100. package/dist/SliceZone/getSliceComponentProps.cjs +22 -0
  101. package/dist/SliceZone/getSliceComponentProps.cjs.map +1 -0
  102. package/dist/SliceZone/getSliceComponentProps.d.ts +66 -0
  103. package/dist/SliceZone/getSliceComponentProps.js +22 -0
  104. package/dist/SliceZone/getSliceComponentProps.js.map +1 -0
  105. package/dist/SliceZone/index.d.ts +4 -0
  106. package/dist/SliceZone/types.d.ts +97 -0
  107. package/dist/createPrismic.cjs +17 -50
  108. package/dist/createPrismic.cjs.map +1 -1
  109. package/dist/createPrismic.d.ts +10 -0
  110. package/dist/createPrismic.js +16 -27
  111. package/dist/createPrismic.js.map +1 -1
  112. package/dist/index.cjs +21 -40
  113. package/dist/index.cjs.map +1 -1
  114. package/dist/index.d.ts +19 -9
  115. package/dist/index.js +19 -38
  116. package/dist/index.js.map +1 -1
  117. package/dist/lib/Wrapper.vue.cjs +20 -0
  118. package/dist/lib/Wrapper.vue.cjs.map +1 -0
  119. package/dist/lib/Wrapper.vue.d.ts +34 -0
  120. package/dist/lib/Wrapper.vue.js +21 -0
  121. package/dist/lib/Wrapper.vue.js.map +1 -0
  122. package/dist/lib/Wrapper.vue2.cjs +4 -0
  123. package/dist/lib/Wrapper.vue2.cjs.map +1 -0
  124. package/dist/lib/Wrapper.vue2.js +5 -0
  125. package/dist/lib/Wrapper.vue2.js.map +1 -0
  126. package/dist/lib/devMsg.cjs +8 -0
  127. package/dist/lib/devMsg.cjs.map +1 -0
  128. package/dist/lib/devMsg.d.ts +16 -0
  129. package/dist/lib/devMsg.js +8 -0
  130. package/dist/lib/devMsg.js.map +1 -0
  131. package/dist/lib/isInternalURL.cjs.map +1 -1
  132. package/dist/lib/isInternalURL.js.map +1 -1
  133. package/dist/package.json.cjs +5 -0
  134. package/dist/package.json.cjs.map +1 -0
  135. package/dist/package.json.js +5 -0
  136. package/dist/package.json.js.map +1 -0
  137. package/dist/types.d.ts +42 -58
  138. package/dist/usePrismic.cjs +3 -2
  139. package/dist/usePrismic.cjs.map +1 -1
  140. package/dist/usePrismic.d.ts +13 -5
  141. package/dist/usePrismic.js +2 -1
  142. package/dist/usePrismic.js.map +1 -1
  143. package/package.json +32 -35
  144. package/src/PrismicEmbed.vue +42 -0
  145. package/src/PrismicImage.vue +160 -0
  146. package/src/PrismicLink.vue +191 -0
  147. package/src/PrismicRichText/DeprecatedPrismicRichText.vue +130 -0
  148. package/src/PrismicRichText/PrismicRichText.vue +157 -0
  149. package/src/PrismicRichText/PrismicRichTextDefaultComponent.vue +63 -0
  150. package/src/PrismicRichText/PrismicRichTextSerialize.vue +52 -0
  151. package/src/PrismicRichText/getRichTextComponentProps.ts +63 -0
  152. package/src/PrismicRichText/index.ts +3 -0
  153. package/src/PrismicRichText/types.ts +37 -0
  154. package/src/PrismicText.vue +63 -0
  155. package/src/SliceZone/SliceZone.vue +113 -0
  156. package/src/SliceZone/TODOSliceComponent.ts +49 -0
  157. package/src/SliceZone/defineSliceZoneComponents.ts +67 -0
  158. package/src/SliceZone/getSliceComponentProps.ts +98 -0
  159. package/src/SliceZone/index.ts +13 -0
  160. package/src/SliceZone/types.ts +163 -0
  161. package/src/createPrismic.ts +54 -53
  162. package/src/index.ts +33 -58
  163. package/src/lib/Wrapper.vue +22 -0
  164. package/src/lib/devMsg.ts +20 -0
  165. package/src/lib/isInternalURL.ts +4 -4
  166. package/src/types.ts +91 -109
  167. package/src/usePrismic.ts +16 -9
  168. package/dist/components/PrismicEmbed.cjs +0 -36
  169. package/dist/components/PrismicEmbed.cjs.map +0 -1
  170. package/dist/components/PrismicEmbed.d.ts +0 -59
  171. package/dist/components/PrismicEmbed.js +0 -36
  172. package/dist/components/PrismicEmbed.js.map +0 -1
  173. package/dist/components/PrismicImage.cjs +0 -122
  174. package/dist/components/PrismicImage.cjs.map +0 -1
  175. package/dist/components/PrismicImage.d.ts +0 -163
  176. package/dist/components/PrismicImage.js +0 -122
  177. package/dist/components/PrismicImage.js.map +0 -1
  178. package/dist/components/PrismicLink.cjs +0 -120
  179. package/dist/components/PrismicLink.cjs.map +0 -1
  180. package/dist/components/PrismicLink.d.ts +0 -196
  181. package/dist/components/PrismicLink.js +0 -120
  182. package/dist/components/PrismicLink.js.map +0 -1
  183. package/dist/components/PrismicRichText.cjs +0 -108
  184. package/dist/components/PrismicRichText.cjs.map +0 -1
  185. package/dist/components/PrismicRichText.d.ts +0 -161
  186. package/dist/components/PrismicRichText.js +0 -108
  187. package/dist/components/PrismicRichText.js.map +0 -1
  188. package/dist/components/PrismicText.cjs +0 -57
  189. package/dist/components/PrismicText.cjs.map +0 -1
  190. package/dist/components/PrismicText.d.ts +0 -119
  191. package/dist/components/PrismicText.js +0 -57
  192. package/dist/components/PrismicText.js.map +0 -1
  193. package/dist/components/SliceZone.cjs +0 -156
  194. package/dist/components/SliceZone.cjs.map +0 -1
  195. package/dist/components/SliceZone.d.ts +0 -375
  196. package/dist/components/SliceZone.js +0 -156
  197. package/dist/components/SliceZone.js.map +0 -1
  198. package/dist/components/index.d.ts +0 -12
  199. package/dist/composables.cjs +0 -40
  200. package/dist/composables.cjs.map +0 -1
  201. package/dist/composables.d.ts +0 -330
  202. package/dist/composables.js +0 -40
  203. package/dist/composables.js.map +0 -1
  204. package/dist/globalExtensions.d.ts +0 -11
  205. package/dist/injectionSymbols.cjs +0 -5
  206. package/dist/injectionSymbols.cjs.map +0 -1
  207. package/dist/injectionSymbols.d.ts +0 -9
  208. package/dist/injectionSymbols.js +0 -5
  209. package/dist/injectionSymbols.js.map +0 -1
  210. package/dist/lib/__PRODUCTION__.cjs +0 -8
  211. package/dist/lib/__PRODUCTION__.cjs.map +0 -1
  212. package/dist/lib/__PRODUCTION__.d.ts +0 -7
  213. package/dist/lib/__PRODUCTION__.js +0 -8
  214. package/dist/lib/__PRODUCTION__.js.map +0 -1
  215. package/dist/lib/getSlots.cjs +0 -19
  216. package/dist/lib/getSlots.cjs.map +0 -1
  217. package/dist/lib/getSlots.d.ts +0 -14
  218. package/dist/lib/getSlots.js +0 -19
  219. package/dist/lib/getSlots.js.map +0 -1
  220. package/dist/lib/simplyResolveComponent.cjs +0 -8
  221. package/dist/lib/simplyResolveComponent.cjs.map +0 -1
  222. package/dist/lib/simplyResolveComponent.d.ts +0 -12
  223. package/dist/lib/simplyResolveComponent.js +0 -8
  224. package/dist/lib/simplyResolveComponent.js.map +0 -1
  225. package/dist/useStatefulPrismicClientMethod.cjs +0 -39
  226. package/dist/useStatefulPrismicClientMethod.cjs.map +0 -1
  227. package/dist/useStatefulPrismicClientMethod.d.ts +0 -64
  228. package/dist/useStatefulPrismicClientMethod.js +0 -39
  229. package/dist/useStatefulPrismicClientMethod.js.map +0 -1
  230. package/src/components/PrismicEmbed.ts +0 -91
  231. package/src/components/PrismicImage.ts +0 -309
  232. package/src/components/PrismicLink.ts +0 -326
  233. package/src/components/PrismicRichText.ts +0 -282
  234. package/src/components/PrismicText.ts +0 -167
  235. package/src/components/SliceZone.ts +0 -619
  236. package/src/components/index.ts +0 -46
  237. package/src/composables.ts +0 -474
  238. package/src/globalExtensions.ts +0 -12
  239. package/src/injectionSymbols.ts +0 -17
  240. package/src/lib/__PRODUCTION__.ts +0 -12
  241. package/src/lib/getSlots.ts +0 -37
  242. package/src/lib/simplyResolveComponent.ts +0 -23
  243. package/src/useStatefulPrismicClientMethod.ts +0 -156
  244. package/vetur/attributes.json +0 -113
  245. package/vetur/tags.json +0 -60
@@ -0,0 +1,63 @@
1
+ <script lang="ts" setup>
2
+ import type { RichTextField } from "@prismicio/client"
3
+ import { asText, isFilled } from "@prismicio/client"
4
+ import { DEV } from "esm-env"
5
+ import { watchEffect } from "vue"
6
+
7
+ import Wrapper from "./lib/Wrapper.vue"
8
+ import { devMsg } from "./lib/devMsg"
9
+
10
+ import type { ComponentOrTagName } from "./types"
11
+
12
+ /**
13
+ * Props for `<PrismicText />`.
14
+ */
15
+ export type PrismicTextProps = {
16
+ /**
17
+ * The Prismic rich text field to render.
18
+ */
19
+ field: RichTextField | null | undefined
20
+
21
+ /**
22
+ * The string value to be rendered when the field is empty. If a fallback is
23
+ * not given, `""` (nothing) will be rendered.
24
+ */
25
+ fallback?: string
26
+
27
+ /**
28
+ * The separator used between blocks.
29
+ *
30
+ * @defaultValue `" "` (a whitespace)
31
+ */
32
+ separator?: string
33
+
34
+ /**
35
+ * An HTML tag name or a component used to wrap the output. `<PrismicText />`
36
+ * is not wrapped by default.
37
+ *
38
+ * @defaultValue `"template"` (no wrapper)
39
+ */
40
+ wrapper?: ComponentOrTagName
41
+ }
42
+
43
+ const props = defineProps<PrismicTextProps>()
44
+ defineOptions({ name: "PrismicText" })
45
+
46
+ if (DEV) {
47
+ watchEffect(() => {
48
+ if (typeof props.field === "string") {
49
+ throw new Error(
50
+ `[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(
51
+ "prismictext-works-only-with-rich-text-and-title-fields",
52
+ )}`,
53
+ )
54
+ }
55
+ }, {})
56
+ }
57
+ </script>
58
+
59
+ <template>
60
+ <Wrapper v-if="isFilled.richText(field) || fallback" :wrapper="wrapper">
61
+ {{ asText(field) || fallback }}
62
+ </Wrapper>
63
+ </template>
@@ -0,0 +1,113 @@
1
+ <script lang="ts" setup>
2
+ import { computed } from "vue"
3
+
4
+ import Wrapper from "../lib/Wrapper.vue"
5
+
6
+ import type { ComponentOrTagName } from "../types"
7
+ import type {
8
+ SliceComponentType,
9
+ SliceLike,
10
+ SliceZoneComponents,
11
+ SliceZoneLike,
12
+ } from "./types"
13
+
14
+ import { usePrismic } from "../usePrismic"
15
+
16
+ import { TODOSliceComponent } from "./TODOSliceComponent"
17
+
18
+ /**
19
+ * Props for `<SliceZone />`.
20
+ *
21
+ * @typeParam TContext - Arbitrary data made available to all Slice components
22
+ */
23
+ export type SliceZoneProps<TContext = unknown> = {
24
+ /**
25
+ * List of Slice data from the Slice Zone.
26
+ */
27
+ slices: SliceZoneLike<SliceLike & Record<string, unknown>>
28
+
29
+ /**
30
+ * A record mapping Slice types to Vue components.
31
+ */
32
+ components?: SliceZoneComponents
33
+
34
+ /**
35
+ * The Vue component rendered if a component mapping from the `components`
36
+ * prop cannot be found.
37
+ *
38
+ * @remarks
39
+ * Components will be rendered using the {@link SliceComponentProps} interface.
40
+ *
41
+ * @defaultValue The Slice Zone default component provided to `@prismicio/vue` plugin if configured, otherwise `null` when `process.env.NODE_ENV === "production"` else {@link TODOSliceComponent}.
42
+ */
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ defaultComponent?: SliceComponentType<any, TContext>
45
+
46
+ /**
47
+ * Arbitrary data made available to all Slice components.
48
+ */
49
+ context?: TContext
50
+
51
+ /**
52
+ * An HTML tag name or a component used to wrap the output. `<SliceZone />` is
53
+ * not wrapped by default.
54
+ *
55
+ * @defaultValue `"template"` (no wrapper)
56
+ */
57
+ wrapper?: ComponentOrTagName
58
+ }
59
+
60
+ const props = defineProps<SliceZoneProps>()
61
+ defineOptions({ name: "SliceZone" })
62
+
63
+ const { options } = usePrismic()
64
+
65
+ const renderedSlices = computed(() => {
66
+ return props.slices.map((slice, index) => {
67
+ const type =
68
+ "slice_type" in slice ? (slice.slice_type as string) : slice.type
69
+
70
+ const key =
71
+ "id" in slice && typeof slice.id === "string"
72
+ ? slice.id
73
+ : `${index}-${JSON.stringify(slice)}`
74
+
75
+ const is =
76
+ props.components?.[type] ||
77
+ props.defaultComponent ||
78
+ options.components?.sliceZoneDefaultComponent
79
+
80
+ if (!is) {
81
+ return { is: TODOSliceComponent, key, props: { slice } }
82
+ }
83
+
84
+ if (slice.__mapped) {
85
+ const { __mapped, ...mappedProps } = slice
86
+
87
+ return { is, key, props: mappedProps }
88
+ }
89
+
90
+ return {
91
+ is,
92
+ key,
93
+ props: {
94
+ slice,
95
+ index,
96
+ context: props.context,
97
+ slices: props.slices,
98
+ },
99
+ }
100
+ })
101
+ })
102
+ </script>
103
+
104
+ <template>
105
+ <Wrapper v-if="slices" :wrapper="wrapper">
106
+ <component
107
+ v-for="renderedSlice in renderedSlices"
108
+ :is="renderedSlice.is"
109
+ :key="renderedSlice.key"
110
+ v-bind="renderedSlice.props"
111
+ />
112
+ </Wrapper>
113
+ </template>
@@ -0,0 +1,49 @@
1
+ import { DEV } from "esm-env"
2
+ import type { PropType } from "vue"
3
+ import { computed, defineComponent, h, watchEffect } from "vue"
4
+
5
+ import type { SliceComponentType, SliceLike } from "./types"
6
+
7
+ /**
8
+ * This Slice component can be used as a reminder to provide a proper
9
+ * implementation.
10
+ *
11
+ * This is also the default Vue component rendered when a component mapping
12
+ * cannot be found in `<SliceZone />`.
13
+ */
14
+ export const TODOSliceComponent = DEV
15
+ ? /*#__PURE__*/ (defineComponent({
16
+ name: "TODOSliceComponent",
17
+ props: {
18
+ slice: {
19
+ type: Object as PropType<SliceLike>,
20
+ required: true,
21
+ },
22
+ },
23
+ setup(props) {
24
+ const type = computed(() => {
25
+ return "slice_type" in props.slice
26
+ ? props.slice.slice_type
27
+ : props.slice.type
28
+ })
29
+
30
+ watchEffect(() => {
31
+ console.warn(
32
+ `[SliceZone] Could not find a component for Slice type "${type.value}"`,
33
+ props.slice,
34
+ )
35
+ })
36
+
37
+ return () => {
38
+ return h(
39
+ "section",
40
+ {
41
+ "data-slice-zone-todo-component": "",
42
+ "data-slice-type": type.value,
43
+ },
44
+ [`Could not find a component for Slice type "${type.value}"`],
45
+ )
46
+ }
47
+ },
48
+ }) as SliceComponentType)
49
+ : ((() => null) as SliceComponentType)
@@ -0,0 +1,67 @@
1
+ import { markRaw } from "vue"
2
+
3
+ import type {
4
+ SliceComponentType,
5
+ SliceLike,
6
+ SliceZoneComponents,
7
+ } from "./types"
8
+
9
+ /**
10
+ * Gets an optimized record of Slice types mapped to Vue components. Each
11
+ * components will be rendered for each instance of their Slice type.
12
+ *
13
+ * @remarks
14
+ * This is essentially an helper function to ensure {@link markRaw} is correctly
15
+ * applied on each components, improving performances.
16
+ *
17
+ * @example
18
+ *
19
+ * ```javascript
20
+ * // Defining a slice components
21
+ * import { defineSliceZoneComponents } from "@prismicio/vue";
22
+ *
23
+ * export default {
24
+ * data() {
25
+ * components: defineSliceZoneComponents({
26
+ * foo: Foo,
27
+ * bar: defineAsyncComponent(
28
+ * () => new Promise((res) => res(Bar)),
29
+ * ),
30
+ * baz: "Baz",
31
+ * }),
32
+ * }
33
+ * };
34
+ * ```
35
+ *
36
+ * @typeParam TSlice - The type(s) of slices in the Slice Zone
37
+ * @typeParam TContext - Arbitrary data made available to all Slice components
38
+ *
39
+ * @param components - {@link SliceZoneComponents}
40
+ *
41
+ * @returns A new optimized record of {@link SliceZoneComponents}
42
+ */
43
+ export const defineSliceZoneComponents = <
44
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
+ TSlice extends SliceLike = any,
46
+ TContext = unknown,
47
+ >(
48
+ components: SliceZoneComponents<TSlice, TContext>,
49
+ ): SliceZoneComponents<TSlice, TContext> => {
50
+ const result = {} as SliceZoneComponents<TSlice, TContext>
51
+
52
+ let type: keyof typeof components
53
+ for (type in components) {
54
+ const component = components[type]
55
+ result[type] =
56
+ typeof component === "string"
57
+ ? component
58
+ : markRaw(
59
+ component as SliceComponentType<
60
+ Extract<TSlice, SliceLike<typeof type>>,
61
+ TContext
62
+ >,
63
+ )
64
+ }
65
+
66
+ return result
67
+ }
@@ -0,0 +1,98 @@
1
+ import type { PropType } from "vue"
2
+
3
+ import type { SliceComponentProps, SliceLike } from "./types"
4
+
5
+ /**
6
+ * Native Vue props for a component rendering content from a Prismic Slice using
7
+ * the `<SliceZone />` component.
8
+ *
9
+ * @typeParam TSlice - The Slice type
10
+ * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made
11
+ * available to all Slice components
12
+ */
13
+ type DefineComponentSliceComponentProps<
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ TSlice extends SliceLike = any,
16
+ TContext = unknown,
17
+ > = {
18
+ slice: {
19
+ type: PropType<SliceComponentProps<TSlice, TContext>["slice"]>
20
+ required: true
21
+ }
22
+ index: {
23
+ type: PropType<SliceComponentProps<TSlice, TContext>["index"]>
24
+ required: true
25
+ }
26
+ slices: {
27
+ type: PropType<SliceComponentProps<TSlice, TContext>["slices"]>
28
+ required: true
29
+ }
30
+ context: {
31
+ type: PropType<SliceComponentProps<TSlice, TContext>["context"]>
32
+ required: true
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Gets native Vue props for a component rendering content from a Prismic Slice
38
+ * using the `<SliceZone />` component.
39
+ *
40
+ * Props are: `["slice", "index", "slices", "context"]`
41
+ *
42
+ * @example
43
+ *
44
+ * ```typescript
45
+ * // Defining a new slice component
46
+ * import { getSliceComponentProps } from "@prismicio/vue"
47
+ *
48
+ * defineProps(getSliceComponentProps())
49
+ * ```
50
+ *
51
+ * @example
52
+ *
53
+ * ```typescript
54
+ * // Defining a new slice component with visual hint
55
+ * import { getSliceComponentProps } from "@prismicio/vue"
56
+ *
57
+ * defineProps(
58
+ * getSliceComponentProps(["slice", "index", "slices", "context"]),
59
+ * )
60
+ * ```
61
+ *
62
+ * @typeParam TSlice - The Slice type
63
+ * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made
64
+ * available to all Slice components
65
+ *
66
+ * @param propsHint - An optional array of prop names used for the sole purpose
67
+ * of having a visual hint of which props are made available to the slice,
68
+ * this parameters doesn't have any effect
69
+ *
70
+ * @returns Props object to use with `defineProps()`
71
+ */
72
+ export const getSliceComponentProps = <
73
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
+ TSlice extends SliceLike = any,
75
+ TContext = unknown,
76
+ >(
77
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
78
+ propsHint?: ["slice", "index", "slices", "context"],
79
+ ): DefineComponentSliceComponentProps<TSlice, TContext> => ({
80
+ slice: {
81
+ type: Object as PropType<SliceComponentProps<TSlice, TContext>["slice"]>,
82
+ required: true,
83
+ },
84
+ index: {
85
+ type: Number as PropType<SliceComponentProps<TSlice, TContext>["index"]>,
86
+ required: true,
87
+ },
88
+ slices: {
89
+ type: Array as PropType<SliceComponentProps<TSlice, TContext>["slices"]>,
90
+ required: true,
91
+ },
92
+ context: {
93
+ type: null as unknown as PropType<
94
+ SliceComponentProps<TSlice, TContext>["context"]
95
+ >,
96
+ required: true,
97
+ },
98
+ })
@@ -0,0 +1,13 @@
1
+ export { TODOSliceComponent } from "./TODOSliceComponent"
2
+ export { getSliceComponentProps } from "./getSliceComponentProps"
3
+ export { defineSliceZoneComponents } from "./defineSliceZoneComponents"
4
+
5
+ export type {
6
+ SliceComponentProps,
7
+ SliceComponentType,
8
+ SliceLike,
9
+ SliceLikeGraphQL,
10
+ SliceLikeRestV2,
11
+ SliceZoneComponents,
12
+ SliceZoneLike,
13
+ } from "./types"
@@ -0,0 +1,163 @@
1
+ import type { Slice } from "@prismicio/client"
2
+ import type {
3
+ DefineComponent,
4
+ FunctionalComponent,
5
+ defineAsyncComponent,
6
+ } from "vue"
7
+
8
+ /**
9
+ * Returns the type of a `SliceLike` type.
10
+ *
11
+ * @typeParam TSlice - The Slice from which the type will be extracted.
12
+ */
13
+ type ExtractSliceType<TSlice extends SliceLike> = TSlice extends SliceLikeRestV2
14
+ ? TSlice["slice_type"]
15
+ : TSlice extends SliceLikeGraphQL
16
+ ? TSlice["type"]
17
+ : never
18
+
19
+ /**
20
+ * The minimum required properties to represent a Prismic Slice from the Prismic
21
+ * Rest API V2 for the `unstable_mapSliceZone()` helper.
22
+ *
23
+ * @typeParam SliceType - Type name of the Slice.
24
+ */
25
+ export type SliceLikeRestV2<TSliceType extends string = string> = Pick<
26
+ Slice<TSliceType>,
27
+ "id" | "slice_type"
28
+ >
29
+
30
+ /**
31
+ * The minimum required properties to represent a Prismic Slice from the Prismic
32
+ * GraphQL API for the `unstable_mapSliceZone()` helper.
33
+ *
34
+ * @typeParam SliceType - Type name of the Slice.
35
+ */
36
+ export type SliceLikeGraphQL<TSliceType extends string = string> = {
37
+ type: Slice<TSliceType>["slice_type"]
38
+ }
39
+
40
+ /**
41
+ * The minimum required properties to represent a Prismic Slice for the
42
+ * `<SliceZone />` component.
43
+ *
44
+ * If using Prismic's Rest API V2, use the `Slice` export from
45
+ * `@prismicio/client` for a full interface.
46
+ *
47
+ * @typeParam TSliceType - Type name of the Slice
48
+ */
49
+ export type SliceLike<TSliceType extends string = string> = (
50
+ | SliceLikeRestV2<TSliceType>
51
+ | SliceLikeGraphQL<TSliceType>
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
60
+ }
61
+
62
+ /**
63
+ * A looser version of the `SliceZone` type from `@prismicio/client` using
64
+ * `SliceLike`.
65
+ *
66
+ * If using Prismic's REST API, use the `SliceZone` export from
67
+ * `@prismicio/client` for the full type.
68
+ *
69
+ * @typeParam TSlice - The type(s) of slices in the Slice Zone
70
+ */
71
+ export type SliceZoneLike<TSlice extends SliceLike = SliceLike> =
72
+ readonly TSlice[]
73
+
74
+ /**
75
+ * Vue props for a component rendering content from a Prismic Slice using the
76
+ * `<SliceZone />` component.
77
+ *
78
+ * @typeParam TSlice - The type(s) of slices in the Slice Zone
79
+ * @typeParam TContext - Arbitrary data passed to `<SliceZone />` and made
80
+ * available to all Slice components
81
+ */
82
+ export type SliceComponentProps<
83
+ TSlice extends SliceLike = SliceLike,
84
+ TContext = unknown,
85
+ > = {
86
+ /**
87
+ * Slice data for this component.
88
+ */
89
+ slice: TSlice
90
+
91
+ /**
92
+ * The index of the Slice in the Slice Zone.
93
+ */
94
+ index: number
95
+
96
+ /**
97
+ * All Slices from the Slice Zone to which the Slice belongs.
98
+ */
99
+ // TODO: We have to keep this list of Slices general due to circular
100
+ // reference limtiations. If we had another generic to determine the full
101
+ // union of Slice types, it would include TSlice. This causes TypeScript to
102
+ // throw a compilation error.
103
+ slices: SliceZoneLike<
104
+ TSlice extends SliceLikeGraphQL ? SliceLikeGraphQL : SliceLikeRestV2
105
+ >
106
+
107
+ /**
108
+ * Arbitrary data passed to `<SliceZone />` and made available to all Slice
109
+ * components.
110
+ */
111
+ context: TContext
112
+ }
113
+
114
+ /**
115
+ * A Vue component to be rendered for each instance of its Slice.
116
+ *
117
+ * @typeParam TSlice - The type(s) of slices in the Slice Zone
118
+ * @typeParam TContext - Arbitrary data made available to all Slice components
119
+ */
120
+ export type SliceComponentType<
121
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
122
+ TSlice extends SliceLike = any,
123
+ TContext = unknown,
124
+ > =
125
+ // For reference within TypeScript files when `*.vue` type cannot be inferred
126
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-explicit-any
127
+ | DefineComponent<{}, {}, any>
128
+ // Likewise, for reference with TypeScript files.
129
+ | ReturnType<typeof defineAsyncComponent>
130
+ | DefineComponent<SliceComponentProps<TSlice, TContext>>
131
+ | FunctionalComponent
132
+
133
+ /**
134
+ * A record of Slice types mapped to Vue components. Each components will be
135
+ * rendered for each instance of their Slice type.
136
+ *
137
+ * @typeParam TSlice - The type(s) of slices in the Slice Zone
138
+ * @typeParam TContext - Arbitrary data made available to all Slice components
139
+ */
140
+ export type SliceZoneComponents<
141
+ TSlice extends SliceLike = SliceLike,
142
+ TContext = unknown,
143
+ > =
144
+ // This is purposely not wrapped in Partial to ensure a component is provided
145
+ // for all Slice types. <SliceZone /> will render a default component if one is
146
+ // not provided, but it *should* be a type error if an explicit component is
147
+ // missing.
148
+ //
149
+ // If a developer purposely does not want to provide a component, they can
150
+ // assign it to the TODOSliceComponent exported from this package. This
151
+ // signals to future developers that it is a placeholder and should be
152
+ // implemented.
153
+ {
154
+ [SliceType in ExtractSliceType<TSlice>]:
155
+ | SliceComponentType<
156
+ Extract<TSlice, SliceLike<SliceType>> extends never
157
+ ? SliceLike
158
+ : Extract<TSlice, SliceLike<SliceType>>,
159
+ TContext
160
+ >
161
+ // Global components
162
+ | string
163
+ }