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