@prismicio/vue 2.0.9 → 2.1.0-alpha.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;
@@ -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: {
@@ -196,10 +223,22 @@ var Link = (function (_ref) {
196
223
  },
197
224
  linkResolver: {
198
225
  type: Function,
199
- required: false,
200
- "default": function _default() {
201
- return null;
202
- }
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",
241
+ required: false
203
242
  }
204
243
  },
205
244
  render: function render(h, _ref2) {
@@ -214,9 +253,11 @@ var Link = (function (_ref) {
214
253
  return null;
215
254
  }
216
255
 
217
- var url = parent.$prismic ? parent.$prismic.asLink(field, linkResolver) : PrismicDOM__default.Link.url(field, linkResolver);
256
+ var url = parent.$prismic ? parent.$prismic.asLink(field, linkResolver) : PrismicDOM__default["default"].Link.url(field, linkResolver || function () {
257
+ return null;
258
+ });
218
259
 
219
- if (url.indexOf('/') === 0) {
260
+ if (isInternalURL(url) && !props.target) {
220
261
  data.props = data.props || {};
221
262
  data.props.to = url;
222
263
  return h(component, data, children);
@@ -224,23 +265,35 @@ var Link = (function (_ref) {
224
265
 
225
266
  data.attrs = _objectSpread2(_objectSpread2({}, data.attrs), {}, {
226
267
  href: url
227
- }, field.target && {
228
- target: field.target,
229
- rel: 'noopener'
230
268
  });
231
- return h('a', data, children);
269
+
270
+ if (typeof props.target !== "undefined" || field.target) {
271
+ data.attrs.target = typeof props.target !== "undefined" ? props.target : field.target;
272
+ }
273
+
274
+ if (data.attrs.target === "_blank") {
275
+ data.attrs.rel = typeof props.rel !== "undefined" ? props.rel : props.blankTargetRelAttribute;
276
+ } else if (typeof props.rel !== "undefined") {
277
+ data.attrs.rel = props.rel;
278
+ }
279
+
280
+ return h("a", data, children);
232
281
  }
233
282
  };
234
283
  });
235
284
 
236
285
  var RichText = {
237
- name: 'PrismicRichText',
286
+ name: "PrismicRichText",
238
287
  functional: true,
239
288
  props: {
240
289
  field: {
241
290
  type: Array,
242
291
  required: true
243
292
  },
293
+ linkResolver: {
294
+ type: Function,
295
+ required: false
296
+ },
244
297
  htmlSerializer: {
245
298
  type: Function,
246
299
  required: false
@@ -248,15 +301,15 @@ var RichText = {
248
301
  wrapper: {
249
302
  type: String,
250
303
  required: false,
251
- "default": 'div'
304
+ "default": "div"
252
305
  }
253
306
  },
254
307
  render: function render(h, _ref) {
255
308
  var props = _ref.props,
256
309
  data = _ref.data,
257
- children = _ref.children,
258
310
  parent = _ref.parent;
259
311
  var field = props.field,
312
+ linkResolver = props.linkResolver,
260
313
  htmlSerializer = props.htmlSerializer,
261
314
  wrapper = props.wrapper;
262
315
 
@@ -264,7 +317,7 @@ var RichText = {
264
317
  return null;
265
318
  }
266
319
 
267
- var innerHTML = PrismicDOM.RichText.asHtml(field, parent.$prismic.linkResolver, htmlSerializer || parent.$prismic.htmlSerializer);
320
+ var innerHTML = PrismicDOM.RichText.asHtml(field, linkResolver ? linkResolver : parent.$prismic ? parent.$prismic.linkResolver : undefined, htmlSerializer ? htmlSerializer : parent.$prismic ? parent.$prismic.htmlSerializer : undefined);
268
321
  return h(wrapper, _objectSpread2(_objectSpread2({}, data), {}, {
269
322
  domProps: {
270
323
  innerHTML: innerHTML
@@ -273,17 +326,198 @@ var RichText = {
273
326
  }
274
327
  };
275
328
 
329
+ var Text = {
330
+ name: "PrismicText",
331
+ functional: true,
332
+ props: {
333
+ field: {
334
+ type: Array,
335
+ required: true
336
+ },
337
+ separator: {
338
+ type: String,
339
+ "default": undefined,
340
+ required: false
341
+ },
342
+ wrapper: {
343
+ type: String,
344
+ required: false,
345
+ "default": "div"
346
+ }
347
+ },
348
+ render: function render(h, _ref) {
349
+ var props = _ref.props,
350
+ data = _ref.data;
351
+ _ref.children;
352
+ _ref.parent;
353
+ var field = props.field,
354
+ separator = props.separator,
355
+ wrapper = props.wrapper;
356
+
357
+ if (!field) {
358
+ return null;
359
+ }
360
+
361
+ var innerHTML = PrismicDOM.RichText.asText(field, separator);
362
+ return h(wrapper, _objectSpread2(_objectSpread2({}, data), {}, {
363
+ domProps: {
364
+ innerHTML: innerHTML
365
+ }
366
+ }));
367
+ }
368
+ };
369
+
370
+ // We need to polyfill process if it doesn't exist, such as in the browser.
371
+ if (typeof process === "undefined") {
372
+ globalThis.process = {
373
+ env: {}
374
+ };
375
+ }
376
+ /**
377
+ * `true` if in the production environment, `false` otherwise.
378
+ *
379
+ * This boolean can be used to perform actions only in development environments,
380
+ * such as logging.
381
+ */
382
+
383
+
384
+ var __PRODUCTION__ = process.env.NODE_ENV === "production";
385
+
386
+ var getSliceComponentProps = function getSliceComponentProps(propsHint) {
387
+ return {
388
+ slice: {
389
+ type: Object,
390
+ required: true
391
+ },
392
+ index: {
393
+ type: Number,
394
+ required: true
395
+ },
396
+ slices: {
397
+ type: Array,
398
+ required: true
399
+ },
400
+ context: {
401
+ type: null,
402
+ required: true
403
+ }
404
+ };
405
+ };
406
+ var TODOSliceComponent = __PRODUCTION__ ? null : {
407
+ name: "TODOSliceCOmponent",
408
+ functional: true,
409
+ props: getSliceComponentProps(),
410
+ render: function render(h, _ref) {
411
+ var props = _ref.props,
412
+ data = _ref.data;
413
+ console.warn("[SliceZone] Could not find a component for Slice type \"".concat(props.slice.slice_type, "\""), props.slice);
414
+ return h("section", _objectSpread2(_objectSpread2({}, data), {}, {
415
+ attrs: _objectSpread2(_objectSpread2({}, data.attrs), {}, {
416
+ "data-slice-zone-todo-component": "",
417
+ "data-slice-type": props.slice.slice_type
418
+ })
419
+ }), ["Could not find a component for Slice type \"".concat(props.slice.slice_type, "\"")]);
420
+ }
421
+ }; // Just mimiced to prevent confusion but doesn't provide any value with version 2 of this kit
422
+ var SliceZone = {
423
+ name: "SliceZone",
424
+ props: {
425
+ slices: {
426
+ type: Array,
427
+ required: true
428
+ },
429
+ components: {
430
+ type: Object,
431
+ "default": undefined,
432
+ required: false
433
+ },
434
+ resolver: {
435
+ type: Function,
436
+ "default": undefined,
437
+ required: false
438
+ },
439
+ context: {
440
+ type: null,
441
+ "default": undefined,
442
+ required: false
443
+ },
444
+ defaultComponent: {
445
+ type: Object,
446
+ "default": undefined,
447
+ required: false
448
+ },
449
+ wrapper: {
450
+ type: [String, Object, Function],
451
+ "default": "div",
452
+ required: false
453
+ }
454
+ },
455
+ computed: {
456
+ renderedSlices: function renderedSlices() {
457
+ var _this = this;
458
+
459
+ if (!this.slices) {
460
+ return null;
461
+ }
462
+
463
+ return this.slices.map(function (slice, index) {
464
+ 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`.
465
+
466
+ if (_this.resolver) {
467
+ var resolvedComponent = _this.resolver({
468
+ slice: slice,
469
+ sliceName: slice.slice_type,
470
+ i: index
471
+ });
472
+
473
+ if (resolvedComponent) {
474
+ component = resolvedComponent;
475
+ }
476
+ }
477
+
478
+ var p = {
479
+ key: "".concat(slice.slice_type, "-").concat(index),
480
+ props: {
481
+ slice: slice,
482
+ index: index,
483
+ context: _this.context,
484
+ slices: _this.slices
485
+ }
486
+ };
487
+ return {
488
+ component: component,
489
+ p: p
490
+ };
491
+ });
492
+ }
493
+ },
494
+ render: function render(h) {
495
+ // Prevent fatal if user didn't check for field, throws `Invalid prop` warn
496
+ if (!this.slices) {
497
+ return null;
498
+ }
499
+
500
+ return h(this.wrapper, this.renderedSlices.map(function (_ref2) {
501
+ var component = _ref2.component,
502
+ p = _ref2.p;
503
+ return h(component, p);
504
+ }));
505
+ }
506
+ };
507
+
276
508
  var NuxtLink = Link({
277
- component: 'nuxt-link'
509
+ component: "nuxt-link"
278
510
  });
279
511
  var VueRouterLink = Link({
280
- component: 'router-link'
512
+ component: "router-link"
281
513
  });
282
514
  var exp = {
283
515
  common: {
284
516
  Embed: Embed,
285
517
  Image: Image,
286
- RichText: RichText
518
+ RichText: RichText,
519
+ Text: Text,
520
+ SliceZone: SliceZone
287
521
  },
288
522
  nuxt: {
289
523
  Link: NuxtLink
@@ -295,32 +529,35 @@ var exp = {
295
529
 
296
530
  function asHtml(richText, linkResolver, htmlSerializer) {
297
531
  if (richText) {
298
- return PrismicDOM__default.RichText.asHtml(richText, linkResolver, htmlSerializer);
532
+ return PrismicDOM__default["default"].RichText.asHtml(richText, linkResolver, htmlSerializer);
299
533
  }
300
534
  }
301
535
  function asText(richText, joinString) {
302
536
  if (richText) {
303
- return PrismicDOM__default.RichText.asText(richText, joinString);
537
+ return PrismicDOM__default["default"].RichText.asText(richText, joinString);
304
538
  }
305
539
 
306
- return '';
540
+ return "";
307
541
  }
308
542
  function asLink(link, linkResolver) {
309
543
  if (link) {
310
- return PrismicDOM__default.Link.url(link, linkResolver);
544
+ return PrismicDOM__default["default"].Link.url(link, linkResolver);
311
545
  }
312
546
  }
313
547
  function asDate(date) {
314
548
  if (date) {
315
- return PrismicDOM__default.Date(date);
549
+ return PrismicDOM__default["default"].Date(date);
316
550
  }
317
551
  }
318
552
 
553
+ var client = prismicJS__default["default"].client;
554
+
319
555
  function attachMethods(Vue, options) {
320
556
  Vue.prototype.$prismic.asHtml = function (richText, linkResolver, htmlSerializer) {
321
557
  return asHtml(richText, linkResolver || options.linkResolver, htmlSerializer || options.htmlSerializer);
322
558
  };
323
559
 
560
+ Vue.prototype.$prismic.asHTML = Vue.prototype.$prismic.asHtml;
324
561
  Vue.prototype.$prismic.asText = asText;
325
562
  Vue.prototype.$prismic.richTextAsPlain = asText;
326
563
  Vue.prototype.$prismic.asDate = asDate;
@@ -333,12 +570,12 @@ function attachMethods(Vue, options) {
333
570
  var PrismicVue = {
334
571
  install: function install(Vue, options) {
335
572
  var _options$linkType = options.linkType,
336
- linkType = _options$linkType === void 0 ? 'vueRouter' : _options$linkType;
337
- Vue.prototype.$prismic = prismicJS;
573
+ linkType = _options$linkType === void 0 ? "vueRouter" : _options$linkType;
574
+ Vue.prototype.$prismic = prismicJS__default["default"];
338
575
  Vue.prototype.$prismic.endpoint = options.endpoint;
339
576
  Vue.prototype.$prismic.linkResolver = options.linkResolver;
340
577
  Vue.prototype.$prismic.htmlSerializer = options.htmlSerializer;
341
- Vue.prototype.$prismic.client = prismicJS.client(options.endpoint, options.apiOptions);
578
+ Vue.prototype.$prismic.client = client(options.endpoint, options.apiOptions);
342
579
  attachMethods(Vue, options);
343
580
 
344
581
  var components = _objectSpread2(_objectSpread2({}, exp.common), exp[linkType]);
@@ -349,9 +586,9 @@ var PrismicVue = {
349
586
 
350
587
 
351
588
  Object.entries(components).forEach(function (_ref) {
352
- var _ref2 = _slicedToArray(_ref, 2),
353
- _ = _ref2[0],
354
- c = _ref2[1];
589
+ var _ref2 = _slicedToArray(_ref, 2);
590
+ _ref2[0];
591
+ var c = _ref2[1];
355
592
 
356
593
  Vue.component(c.name, c);
357
594
  });