@prismicio/vue 2.0.10 → 2.1.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.
@@ -1,29 +1,18 @@
1
- import PrismicDOM, { RichText } from 'prismic-dom';
2
1
  import prismicJS from '@prismicio/client';
3
-
4
- function _defineProperty(obj, key, value) {
5
- if (key in obj) {
6
- Object.defineProperty(obj, key, {
7
- value: value,
8
- enumerable: true,
9
- configurable: true,
10
- writable: true
11
- });
12
- } else {
13
- obj[key] = value;
14
- }
15
-
16
- return obj;
17
- }
2
+ import PrismicDOM, { RichText as RichText$1 } from 'prismic-dom';
18
3
 
19
4
  function ownKeys(object, enumerableOnly) {
20
5
  var keys = Object.keys(object);
21
6
 
22
7
  if (Object.getOwnPropertySymbols) {
23
8
  var symbols = Object.getOwnPropertySymbols(object);
24
- if (enumerableOnly) symbols = symbols.filter(function (sym) {
25
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
26
- });
9
+
10
+ if (enumerableOnly) {
11
+ symbols = symbols.filter(function (sym) {
12
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
13
+ });
14
+ }
15
+
27
16
  keys.push.apply(keys, symbols);
28
17
  }
29
18
 
@@ -50,6 +39,21 @@ function _objectSpread2(target) {
50
39
  return target;
51
40
  }
52
41
 
42
+ function _defineProperty(obj, key, value) {
43
+ if (key in obj) {
44
+ Object.defineProperty(obj, key, {
45
+ value: value,
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true
49
+ });
50
+ } else {
51
+ obj[key] = value;
52
+ }
53
+
54
+ return obj;
55
+ }
56
+
53
57
  function _slicedToArray(arr, i) {
54
58
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
55
59
  }
@@ -59,14 +63,17 @@ function _arrayWithHoles(arr) {
59
63
  }
60
64
 
61
65
  function _iterableToArrayLimit(arr, i) {
62
- if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
66
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
67
+
68
+ if (_i == null) return;
63
69
  var _arr = [];
64
70
  var _n = true;
65
71
  var _d = false;
66
- var _e = undefined;
72
+
73
+ var _s, _e;
67
74
 
68
75
  try {
69
- for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
76
+ for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
70
77
  _arr.push(_s.value);
71
78
 
72
79
  if (i && _arr.length === i) break;
@@ -164,10 +171,15 @@ var Image = {
164
171
  render: function render(h, _ref) {
165
172
  var props = _ref.props,
166
173
  data = _ref.data;
167
- var _props$field = props.field,
168
- url = _props$field.url,
169
- alt = _props$field.alt,
170
- copyright = _props$field.copyright;
174
+ var field = props.field;
175
+
176
+ if (!field) {
177
+ return null;
178
+ }
179
+
180
+ var url = field.url,
181
+ alt = field.alt,
182
+ copyright = field.copyright;
171
183
  return h('img', Object.assign(data, {
172
184
  attrs: _objectSpread2(_objectSpread2({}, data.attrs), {}, {
173
185
  src: url,
@@ -178,11 +190,24 @@ var Image = {
178
190
  }
179
191
  };
180
192
 
193
+ var isInternalURL = function isInternalURL(url) {
194
+ /**
195
+ * @see Regex101 expression: {@link https://regex101.com/r/1y7iod/1}
196
+ */
197
+ var isInternal = /^\/(?!\/)/.test(url);
198
+ /**
199
+ * @see Regex101 expression: {@link https://regex101.com/r/RnUseS/1}
200
+ */
201
+
202
+ var isSpecialLink = !isInternal && !/^https?:\/\//i.test(url);
203
+ return isInternal && !isSpecialLink;
204
+ };
205
+
181
206
  var Link = (function (_ref) {
182
207
  var _ref$component = _ref.component,
183
- component = _ref$component === void 0 ? 'a' : _ref$component;
208
+ component = _ref$component === void 0 ? "a" : _ref$component;
184
209
  return {
185
- name: 'PrismicLink',
210
+ name: "PrismicLink",
186
211
  functional: true,
187
212
  props: {
188
213
  field: {
@@ -191,10 +216,22 @@ var Link = (function (_ref) {
191
216
  },
192
217
  linkResolver: {
193
218
  type: Function,
194
- required: false,
195
- "default": function _default() {
196
- return null;
197
- }
219
+ required: false
220
+ },
221
+ target: {
222
+ type: String,
223
+ "default": undefined,
224
+ required: false
225
+ },
226
+ rel: {
227
+ type: String,
228
+ "default": undefined,
229
+ required: false
230
+ },
231
+ blankTargetRelAttribute: {
232
+ type: String,
233
+ "default": "noopener noreferrer",
234
+ required: false
198
235
  }
199
236
  },
200
237
  render: function render(h, _ref2) {
@@ -209,9 +246,11 @@ var Link = (function (_ref) {
209
246
  return null;
210
247
  }
211
248
 
212
- var url = parent.$prismic ? parent.$prismic.asLink(field, linkResolver) : PrismicDOM.Link.url(field, linkResolver);
249
+ var url = parent.$prismic ? parent.$prismic.asLink(field, linkResolver) : PrismicDOM.Link.url(field, linkResolver || function () {
250
+ return null;
251
+ });
213
252
 
214
- if (url.indexOf('/') === 0) {
253
+ if (isInternalURL(url) && !props.target) {
215
254
  data.props = data.props || {};
216
255
  data.props.to = url;
217
256
  return h(component, data, children);
@@ -219,23 +258,37 @@ var Link = (function (_ref) {
219
258
 
220
259
  data.attrs = _objectSpread2(_objectSpread2({}, data.attrs), {}, {
221
260
  href: url
222
- }, field.target && {
223
- target: field.target,
224
- rel: 'noopener'
225
261
  });
226
- return h('a', data, children);
262
+
263
+ if (typeof props.target !== "undefined") {
264
+ data.attrs.target = props.target;
265
+ } else if (field.target) {
266
+ data.attrs.target = field.target;
267
+ }
268
+
269
+ if (typeof props.rel !== "undefined") {
270
+ data.attrs.rel = props.rel;
271
+ } else if (data.attrs.target === "_blank") {
272
+ data.attrs.rel = props.blankTargetRelAttribute;
273
+ }
274
+
275
+ return h("a", data, children);
227
276
  }
228
277
  };
229
278
  });
230
279
 
231
- var RichText$1 = {
232
- name: 'PrismicRichText',
280
+ var RichText = {
281
+ name: "PrismicRichText",
233
282
  functional: true,
234
283
  props: {
235
284
  field: {
236
285
  type: Array,
237
286
  required: true
238
287
  },
288
+ linkResolver: {
289
+ type: Function,
290
+ required: false
291
+ },
239
292
  htmlSerializer: {
240
293
  type: Function,
241
294
  required: false
@@ -243,15 +296,15 @@ var RichText$1 = {
243
296
  wrapper: {
244
297
  type: String,
245
298
  required: false,
246
- "default": 'div'
299
+ "default": "div"
247
300
  }
248
301
  },
249
302
  render: function render(h, _ref) {
250
303
  var props = _ref.props,
251
304
  data = _ref.data,
252
- children = _ref.children,
253
305
  parent = _ref.parent;
254
306
  var field = props.field,
307
+ linkResolver = props.linkResolver,
255
308
  htmlSerializer = props.htmlSerializer,
256
309
  wrapper = props.wrapper;
257
310
 
@@ -259,7 +312,48 @@ var RichText$1 = {
259
312
  return null;
260
313
  }
261
314
 
262
- var innerHTML = RichText.asHtml(field, parent.$prismic.linkResolver, htmlSerializer || parent.$prismic.htmlSerializer);
315
+ var innerHTML = RichText$1.asHtml(field, linkResolver ? linkResolver : parent.$prismic ? parent.$prismic.linkResolver : undefined, htmlSerializer ? htmlSerializer : parent.$prismic ? parent.$prismic.htmlSerializer : undefined);
316
+ return h(wrapper, _objectSpread2(_objectSpread2({}, data), {}, {
317
+ domProps: {
318
+ innerHTML: innerHTML
319
+ }
320
+ }));
321
+ }
322
+ };
323
+
324
+ var Text = {
325
+ name: "PrismicText",
326
+ functional: true,
327
+ props: {
328
+ field: {
329
+ type: Array,
330
+ required: true
331
+ },
332
+ separator: {
333
+ type: String,
334
+ "default": undefined,
335
+ required: false
336
+ },
337
+ wrapper: {
338
+ type: String,
339
+ required: false,
340
+ "default": "div"
341
+ }
342
+ },
343
+ render: function render(h, _ref) {
344
+ var props = _ref.props,
345
+ data = _ref.data;
346
+ _ref.children;
347
+ _ref.parent;
348
+ var field = props.field,
349
+ separator = props.separator,
350
+ wrapper = props.wrapper;
351
+
352
+ if (!field) {
353
+ return null;
354
+ }
355
+
356
+ var innerHTML = RichText$1.asText(field, separator);
263
357
  return h(wrapper, _objectSpread2(_objectSpread2({}, data), {}, {
264
358
  domProps: {
265
359
  innerHTML: innerHTML
@@ -268,17 +362,157 @@ var RichText$1 = {
268
362
  }
269
363
  };
270
364
 
365
+ // We need to polyfill process if it doesn't exist, such as in the browser.
366
+ if (typeof process === "undefined") {
367
+ globalThis.process = {
368
+ env: {}
369
+ };
370
+ }
371
+ /**
372
+ * `true` if in the production environment, `false` otherwise.
373
+ *
374
+ * This boolean can be used to perform actions only in development environments,
375
+ * such as logging.
376
+ */
377
+
378
+
379
+ var __PRODUCTION__ = process.env.NODE_ENV === "production";
380
+
381
+ var getSliceComponentProps = function getSliceComponentProps(propsHint) {
382
+ return {
383
+ slice: {
384
+ type: Object,
385
+ required: true
386
+ },
387
+ index: {
388
+ type: Number,
389
+ required: true
390
+ },
391
+ slices: {
392
+ type: Array,
393
+ required: true
394
+ },
395
+ context: {
396
+ type: null,
397
+ required: true
398
+ }
399
+ };
400
+ };
401
+ var TODOSliceComponent = __PRODUCTION__ ? null : {
402
+ name: "TODOSliceCOmponent",
403
+ functional: true,
404
+ props: getSliceComponentProps(),
405
+ render: function render(h, _ref) {
406
+ var props = _ref.props,
407
+ data = _ref.data;
408
+ console.warn("[SliceZone] Could not find a component for Slice type \"".concat(props.slice.slice_type, "\""), props.slice);
409
+ return h("section", _objectSpread2(_objectSpread2({}, data), {}, {
410
+ attrs: _objectSpread2(_objectSpread2({}, data.attrs), {}, {
411
+ "data-slice-zone-todo-component": "",
412
+ "data-slice-type": props.slice.slice_type
413
+ })
414
+ }), ["Could not find a component for Slice type \"".concat(props.slice.slice_type, "\"")]);
415
+ }
416
+ }; // Just mimiced to prevent confusion but doesn't provide any value with version 2 of this kit
417
+ var SliceZone = {
418
+ name: "SliceZone",
419
+ props: {
420
+ slices: {
421
+ type: Array,
422
+ required: true
423
+ },
424
+ components: {
425
+ type: Object,
426
+ "default": undefined,
427
+ required: false
428
+ },
429
+ resolver: {
430
+ type: Function,
431
+ "default": undefined,
432
+ required: false
433
+ },
434
+ context: {
435
+ type: null,
436
+ "default": undefined,
437
+ required: false
438
+ },
439
+ defaultComponent: {
440
+ type: Object,
441
+ "default": undefined,
442
+ required: false
443
+ },
444
+ wrapper: {
445
+ type: [String, Object, Function],
446
+ "default": "div",
447
+ required: false
448
+ }
449
+ },
450
+ computed: {
451
+ renderedSlices: function renderedSlices() {
452
+ var _this = this;
453
+
454
+ if (!this.slices) {
455
+ return null;
456
+ }
457
+
458
+ return this.slices.map(function (slice, index) {
459
+ var component = _this.components && slice.slice_type in _this.components ? _this.components[slice.slice_type] : _this.defaultComponent || TODOSliceComponent; // TODO: Remove `resolver` in v3 in favor of `components`.
460
+
461
+ if (_this.resolver) {
462
+ var resolvedComponent = _this.resolver({
463
+ slice: slice,
464
+ sliceName: slice.slice_type,
465
+ i: index
466
+ });
467
+
468
+ if (resolvedComponent) {
469
+ component = resolvedComponent;
470
+ }
471
+ }
472
+
473
+ var p = {
474
+ key: "".concat(slice.slice_type, "-").concat(index),
475
+ props: {
476
+ slice: slice,
477
+ index: index,
478
+ context: _this.context,
479
+ slices: _this.slices
480
+ }
481
+ };
482
+ return {
483
+ component: component,
484
+ p: p
485
+ };
486
+ });
487
+ }
488
+ },
489
+ render: function render(h) {
490
+ // Prevent fatal if user didn't check for field, throws `Invalid prop` warn
491
+ if (!this.slices) {
492
+ return null;
493
+ }
494
+
495
+ return h(this.wrapper, this.renderedSlices.map(function (_ref2) {
496
+ var component = _ref2.component,
497
+ p = _ref2.p;
498
+ return h(component, p);
499
+ }));
500
+ }
501
+ };
502
+
271
503
  var NuxtLink = Link({
272
- component: 'nuxt-link'
504
+ component: "nuxt-link"
273
505
  });
274
506
  var VueRouterLink = Link({
275
- component: 'router-link'
507
+ component: "router-link"
276
508
  });
277
509
  var exp = {
278
510
  common: {
279
511
  Embed: Embed,
280
512
  Image: Image,
281
- RichText: RichText$1
513
+ RichText: RichText,
514
+ Text: Text,
515
+ SliceZone: SliceZone
282
516
  },
283
517
  nuxt: {
284
518
  Link: NuxtLink
@@ -298,7 +532,7 @@ function asText(richText, joinString) {
298
532
  return PrismicDOM.RichText.asText(richText, joinString);
299
533
  }
300
534
 
301
- return '';
535
+ return "";
302
536
  }
303
537
  function asLink(link, linkResolver) {
304
538
  if (link) {
@@ -311,11 +545,14 @@ function asDate(date) {
311
545
  }
312
546
  }
313
547
 
548
+ var client = prismicJS.client;
549
+
314
550
  function attachMethods(Vue, options) {
315
551
  Vue.prototype.$prismic.asHtml = function (richText, linkResolver, htmlSerializer) {
316
552
  return asHtml(richText, linkResolver || options.linkResolver, htmlSerializer || options.htmlSerializer);
317
553
  };
318
554
 
555
+ Vue.prototype.$prismic.asHTML = Vue.prototype.$prismic.asHtml;
319
556
  Vue.prototype.$prismic.asText = asText;
320
557
  Vue.prototype.$prismic.richTextAsPlain = asText;
321
558
  Vue.prototype.$prismic.asDate = asDate;
@@ -328,12 +565,16 @@ function attachMethods(Vue, options) {
328
565
  var PrismicVue = {
329
566
  install: function install(Vue, options) {
330
567
  var _options$linkType = options.linkType,
331
- linkType = _options$linkType === void 0 ? 'vueRouter' : _options$linkType;
568
+ linkType = _options$linkType === void 0 ? "vueRouter" : _options$linkType;
332
569
  Vue.prototype.$prismic = prismicJS;
570
+ Vue.prototype.$prismic.predicate = prismicJS.Predicates;
571
+ Vue.prototype.$prismic.cookie = {
572
+ preview: prismicJS.previewCookie
573
+ };
333
574
  Vue.prototype.$prismic.endpoint = options.endpoint;
334
575
  Vue.prototype.$prismic.linkResolver = options.linkResolver;
335
576
  Vue.prototype.$prismic.htmlSerializer = options.htmlSerializer;
336
- Vue.prototype.$prismic.client = prismicJS.client(options.endpoint, options.apiOptions);
577
+ Vue.prototype.$prismic.client = client(options.endpoint, options.apiOptions);
337
578
  attachMethods(Vue, options);
338
579
 
339
580
  var components = _objectSpread2(_objectSpread2({}, exp.common), exp[linkType]);
@@ -344,13 +585,13 @@ var PrismicVue = {
344
585
 
345
586
 
346
587
  Object.entries(components).forEach(function (_ref) {
347
- var _ref2 = _slicedToArray(_ref, 2),
348
- _ = _ref2[0],
349
- c = _ref2[1];
588
+ var _ref2 = _slicedToArray(_ref, 2);
589
+ _ref2[0];
590
+ var c = _ref2[1];
350
591
 
351
592
  Vue.component(c.name, c);
352
593
  });
353
594
  }
354
595
  };
355
596
 
356
- export default PrismicVue;
597
+ export { PrismicVue as default };