@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,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;
@@ -115,7 +122,7 @@ var Embed = {
115
122
  required: true
116
123
  },
117
124
  wrapper: {
118
- type: String,
125
+ type: [String, Object, Function],
119
126
  required: false,
120
127
  "default": 'div'
121
128
  }
@@ -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: {
@@ -192,6 +217,21 @@ var Link = (function (_ref) {
192
217
  linkResolver: {
193
218
  type: Function,
194
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
195
235
  }
196
236
  },
197
237
  render: function render(h, _ref2) {
@@ -210,7 +250,7 @@ var Link = (function (_ref) {
210
250
  return null;
211
251
  });
212
252
 
213
- if (url.indexOf('/') === 0) {
253
+ if (isInternalURL(url) && !props.target) {
214
254
  data.props = data.props || {};
215
255
  data.props.to = url;
216
256
  return h(component, data, children);
@@ -218,39 +258,53 @@ var Link = (function (_ref) {
218
258
 
219
259
  data.attrs = _objectSpread2(_objectSpread2({}, data.attrs), {}, {
220
260
  href: url
221
- }, field.target && {
222
- target: field.target,
223
- rel: 'noopener'
224
261
  });
225
- 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);
226
276
  }
227
277
  };
228
278
  });
229
279
 
230
- var RichText$1 = {
231
- name: 'PrismicRichText',
280
+ var RichText = {
281
+ name: "PrismicRichText",
232
282
  functional: true,
233
283
  props: {
234
284
  field: {
235
285
  type: Array,
236
286
  required: true
237
287
  },
288
+ linkResolver: {
289
+ type: Function,
290
+ required: false
291
+ },
238
292
  htmlSerializer: {
239
293
  type: Function,
240
294
  required: false
241
295
  },
242
296
  wrapper: {
243
- type: String,
297
+ type: [String, Object, Function],
244
298
  required: false,
245
- "default": 'div'
299
+ "default": "div"
246
300
  }
247
301
  },
248
302
  render: function render(h, _ref) {
249
303
  var props = _ref.props,
250
304
  data = _ref.data,
251
- children = _ref.children,
252
305
  parent = _ref.parent;
253
306
  var field = props.field,
307
+ linkResolver = props.linkResolver,
254
308
  htmlSerializer = props.htmlSerializer,
255
309
  wrapper = props.wrapper;
256
310
 
@@ -258,7 +312,7 @@ var RichText$1 = {
258
312
  return null;
259
313
  }
260
314
 
261
- 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);
262
316
  return h(wrapper, _objectSpread2(_objectSpread2({}, data), {}, {
263
317
  domProps: {
264
318
  innerHTML: innerHTML
@@ -267,17 +321,198 @@ var RichText$1 = {
267
321
  }
268
322
  };
269
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, Object, Function],
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);
357
+ return h(wrapper, _objectSpread2(_objectSpread2({}, data), {}, {
358
+ domProps: {
359
+ innerHTML: innerHTML
360
+ }
361
+ }));
362
+ }
363
+ };
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
+
270
503
  var NuxtLink = Link({
271
- component: 'nuxt-link'
504
+ component: "nuxt-link"
272
505
  });
273
506
  var VueRouterLink = Link({
274
- component: 'router-link'
507
+ component: "router-link"
275
508
  });
276
509
  var exp = {
277
510
  common: {
278
511
  Embed: Embed,
279
512
  Image: Image,
280
- RichText: RichText$1
513
+ RichText: RichText,
514
+ Text: Text,
515
+ SliceZone: SliceZone
281
516
  },
282
517
  nuxt: {
283
518
  Link: NuxtLink
@@ -297,7 +532,7 @@ function asText(richText, joinString) {
297
532
  return PrismicDOM.RichText.asText(richText, joinString);
298
533
  }
299
534
 
300
- return '';
535
+ return "";
301
536
  }
302
537
  function asLink(link, linkResolver) {
303
538
  if (link) {
@@ -310,11 +545,14 @@ function asDate(date) {
310
545
  }
311
546
  }
312
547
 
548
+ var client = prismicJS.client;
549
+
313
550
  function attachMethods(Vue, options) {
314
551
  Vue.prototype.$prismic.asHtml = function (richText, linkResolver, htmlSerializer) {
315
552
  return asHtml(richText, linkResolver || options.linkResolver, htmlSerializer || options.htmlSerializer);
316
553
  };
317
554
 
555
+ Vue.prototype.$prismic.asHTML = Vue.prototype.$prismic.asHtml;
318
556
  Vue.prototype.$prismic.asText = asText;
319
557
  Vue.prototype.$prismic.richTextAsPlain = asText;
320
558
  Vue.prototype.$prismic.asDate = asDate;
@@ -327,12 +565,16 @@ function attachMethods(Vue, options) {
327
565
  var PrismicVue = {
328
566
  install: function install(Vue, options) {
329
567
  var _options$linkType = options.linkType,
330
- linkType = _options$linkType === void 0 ? 'vueRouter' : _options$linkType;
568
+ linkType = _options$linkType === void 0 ? "vueRouter" : _options$linkType;
331
569
  Vue.prototype.$prismic = prismicJS;
570
+ Vue.prototype.$prismic.predicate = prismicJS.Predicates;
571
+ Vue.prototype.$prismic.cookie = {
572
+ preview: prismicJS.previewCookie
573
+ };
332
574
  Vue.prototype.$prismic.endpoint = options.endpoint;
333
575
  Vue.prototype.$prismic.linkResolver = options.linkResolver;
334
576
  Vue.prototype.$prismic.htmlSerializer = options.htmlSerializer;
335
- Vue.prototype.$prismic.client = prismicJS.client(options.endpoint, options.apiOptions);
577
+ Vue.prototype.$prismic.client = client(options.endpoint, options.apiOptions);
336
578
  attachMethods(Vue, options);
337
579
 
338
580
  var components = _objectSpread2(_objectSpread2({}, exp.common), exp[linkType]);
@@ -343,13 +585,13 @@ var PrismicVue = {
343
585
 
344
586
 
345
587
  Object.entries(components).forEach(function (_ref) {
346
- var _ref2 = _slicedToArray(_ref, 2),
347
- _ = _ref2[0],
348
- c = _ref2[1];
588
+ var _ref2 = _slicedToArray(_ref, 2);
589
+ _ref2[0];
590
+ var c = _ref2[1];
349
591
 
350
592
  Vue.component(c.name, c);
351
593
  });
352
594
  }
353
595
  };
354
596
 
355
- export default PrismicVue;
597
+ export { PrismicVue as default };