@prismicio/vue 2.0.11 → 2.1.1

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