vue-datocms 2.0.0 → 3.0.0-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.
@@ -0,0 +1,890 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var hypenateStyleName = require('hyphenate-style-name');
6
+ var vueDemi = require('vue-demi');
7
+ var datocmsStructuredTextGenericHtmlRenderer = require('datocms-structured-text-generic-html-renderer');
8
+ var datocmsStructuredTextUtils = require('datocms-structured-text-utils');
9
+ var datocmsListen = require('datocms-listen');
10
+
11
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
+
13
+ var hypenateStyleName__default = /*#__PURE__*/_interopDefaultLegacy(hypenateStyleName);
14
+
15
+ const isSsr = () => {
16
+ return typeof window === "undefined";
17
+ };
18
+ const isIntersectionObserverAvailable = () => {
19
+ return isSsr() ? false : !!window.IntersectionObserver;
20
+ };
21
+
22
+ const useInView = ({ threshold, rootMargin }) => {
23
+ const observer = vueDemi.ref(null);
24
+ const elRef = vueDemi.ref(null);
25
+ const inView = vueDemi.ref(false);
26
+ vueDemi.onMounted(() => {
27
+ if (isIntersectionObserverAvailable()) {
28
+ observer.value = new IntersectionObserver(
29
+ (entries) => {
30
+ const image = entries[0];
31
+ if (image.isIntersecting && observer.value) {
32
+ inView.value = true;
33
+ observer.value.disconnect();
34
+ }
35
+ },
36
+ {
37
+ threshold,
38
+ rootMargin
39
+ }
40
+ );
41
+ if (elRef.value) {
42
+ observer.value.observe(elRef.value);
43
+ }
44
+ }
45
+ });
46
+ vueDemi.onBeforeUnmount(() => {
47
+ if (isIntersectionObserverAvailable() && observer.value) {
48
+ observer.value.disconnect();
49
+ }
50
+ });
51
+ return { inView, elRef };
52
+ };
53
+
54
+ var __defProp$4 = Object.defineProperty;
55
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
56
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
57
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
58
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
59
+ var __spreadValues$4 = (a, b) => {
60
+ for (var prop in b || (b = {}))
61
+ if (__hasOwnProp$4.call(b, prop))
62
+ __defNormalProp$4(a, prop, b[prop]);
63
+ if (__getOwnPropSymbols$4)
64
+ for (var prop of __getOwnPropSymbols$4(b)) {
65
+ if (__propIsEnum$4.call(b, prop))
66
+ __defNormalProp$4(a, prop, b[prop]);
67
+ }
68
+ return a;
69
+ };
70
+ const Source = vueDemi.defineComponent({
71
+ props: {
72
+ srcset: {
73
+ type: String
74
+ },
75
+ sizes: {
76
+ type: String
77
+ },
78
+ type: {
79
+ type: String
80
+ }
81
+ },
82
+ setup({ srcset, sizes, type }) {
83
+ return () => vueDemi.h("source", __spreadValues$4(__spreadValues$4({}, vueDemi.isVue2 && {
84
+ attrs: {
85
+ srcset,
86
+ sizes,
87
+ type
88
+ }
89
+ }), vueDemi.isVue3 && {
90
+ srcset,
91
+ sizes,
92
+ type
93
+ }));
94
+ }
95
+ });
96
+
97
+ var __defProp$3 = Object.defineProperty;
98
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
99
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
100
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
101
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
102
+ var __spreadValues$3 = (a, b) => {
103
+ for (var prop in b || (b = {}))
104
+ if (__hasOwnProp$3.call(b, prop))
105
+ __defNormalProp$3(a, prop, b[prop]);
106
+ if (__getOwnPropSymbols$3)
107
+ for (var prop of __getOwnPropSymbols$3(b)) {
108
+ if (__propIsEnum$3.call(b, prop))
109
+ __defNormalProp$3(a, prop, b[prop]);
110
+ }
111
+ return a;
112
+ };
113
+ const universalBtoa = (str) => isSsr() ? Buffer.from(str.toString(), "binary").toString("base64") : window.btoa(str);
114
+ const Sizer = vueDemi.defineComponent({
115
+ props: {
116
+ sizerClass: {
117
+ type: String
118
+ },
119
+ sizerStyle: {
120
+ type: Object,
121
+ default: () => ({})
122
+ },
123
+ width: {
124
+ type: Number
125
+ },
126
+ height: {
127
+ type: Number
128
+ },
129
+ explicitWidth: {
130
+ type: Boolean
131
+ }
132
+ },
133
+ setup({ sizerClass, sizerStyle, width, height, explicitWidth }) {
134
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}"></svg>`;
135
+ return () => vueDemi.h("img", __spreadValues$3(__spreadValues$3({
136
+ class: sizerClass,
137
+ style: __spreadValues$3({
138
+ display: "block",
139
+ width: explicitWidth ? `${width}px` : "100%"
140
+ }, sizerStyle)
141
+ }, vueDemi.isVue2 && {
142
+ attrs: {
143
+ src: `data:image/svg+xml;base64,${universalBtoa(svg)}`,
144
+ role: "presentation"
145
+ }
146
+ }), vueDemi.isVue3 && {
147
+ src: `data:image/svg+xml;base64,${universalBtoa(svg)}`,
148
+ role: "presentation"
149
+ }));
150
+ }
151
+ });
152
+
153
+ var __defProp$2 = Object.defineProperty;
154
+ var __defProps$2 = Object.defineProperties;
155
+ var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
156
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
157
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
158
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
159
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
160
+ var __spreadValues$2 = (a, b) => {
161
+ for (var prop in b || (b = {}))
162
+ if (__hasOwnProp$2.call(b, prop))
163
+ __defNormalProp$2(a, prop, b[prop]);
164
+ if (__getOwnPropSymbols$2)
165
+ for (var prop of __getOwnPropSymbols$2(b)) {
166
+ if (__propIsEnum$2.call(b, prop))
167
+ __defNormalProp$2(a, prop, b[prop]);
168
+ }
169
+ return a;
170
+ };
171
+ var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
172
+ const escape = (s) => {
173
+ s = "" + s;
174
+ s = s.replace(/&/g, "&amp;");
175
+ s = s.replace(/</g, "&lt;");
176
+ s = s.replace(/>/g, "&gt;");
177
+ s = s.replace(/"/g, "&quot;");
178
+ s = s.replace(/'/g, "&#39;");
179
+ return s;
180
+ };
181
+ const toCss = (object) => {
182
+ if (!object) {
183
+ return null;
184
+ }
185
+ let result = "";
186
+ for (var styleName in object) {
187
+ if (Object.prototype.hasOwnProperty.call(object, styleName) && object[styleName]) {
188
+ result += `${hypenateStyleName__default["default"](styleName)}: ${object[styleName]}; `;
189
+ }
190
+ }
191
+ return result.length > 0 ? result : null;
192
+ };
193
+ const tag = (tagName, attrs, content) => {
194
+ const serializedAttrs = [];
195
+ if (attrs) {
196
+ for (var attrName in attrs) {
197
+ if (Object.prototype.hasOwnProperty.call(attrs, attrName)) {
198
+ const value = attrs[attrName];
199
+ if (value) {
200
+ serializedAttrs.push(`${escape(attrName)}="${escape(value)}"`);
201
+ }
202
+ }
203
+ }
204
+ }
205
+ const attrsString = serializedAttrs.length > 0 ? ` ${serializedAttrs.join(" ")}` : "";
206
+ return content ? `<${tagName}${attrsString}>${content.join("")}</${tagName}>` : `<${tagName}${attrsString} />`;
207
+ };
208
+ const absolutePositioning = {
209
+ position: "absolute",
210
+ left: "0px",
211
+ top: "0px",
212
+ width: "100%",
213
+ height: "100%"
214
+ };
215
+ const imageAddStrategy = ({ lazyLoad, inView, loaded }) => {
216
+ if (!lazyLoad) {
217
+ return true;
218
+ }
219
+ if (isSsr()) {
220
+ return false;
221
+ }
222
+ if (isIntersectionObserverAvailable()) {
223
+ return inView || loaded;
224
+ }
225
+ return true;
226
+ };
227
+ const imageShowStrategy = ({ lazyLoad, loaded }) => {
228
+ if (!lazyLoad) {
229
+ return true;
230
+ }
231
+ if (isSsr()) {
232
+ return false;
233
+ }
234
+ if (isIntersectionObserverAvailable()) {
235
+ return loaded;
236
+ }
237
+ return true;
238
+ };
239
+ const Image = vueDemi.defineComponent({
240
+ name: "DatocmsImage",
241
+ props: {
242
+ data: {
243
+ type: Object,
244
+ required: true
245
+ },
246
+ pictureClass: {
247
+ type: String
248
+ },
249
+ fadeInDuration: {
250
+ type: Number
251
+ },
252
+ intersectionTreshold: {
253
+ type: Number,
254
+ default: 0
255
+ },
256
+ intersectionThreshold: {
257
+ type: Number
258
+ },
259
+ intersectionMargin: {
260
+ type: String,
261
+ default: "0px 0px 0px 0px"
262
+ },
263
+ lazyLoad: {
264
+ type: Boolean,
265
+ default: true
266
+ },
267
+ rootStyle: {
268
+ type: Object,
269
+ default: () => ({})
270
+ },
271
+ pictureStyle: {
272
+ type: Object,
273
+ default: () => ({})
274
+ },
275
+ explicitWidth: {
276
+ type: Boolean
277
+ },
278
+ layout: {
279
+ type: String,
280
+ default: () => "responsive",
281
+ validator: (value) => ["intrinsic", "fixed", "responsive", "fill"].includes(value)
282
+ },
283
+ objectFit: {
284
+ type: String
285
+ },
286
+ objectPosition: {
287
+ type: String
288
+ }
289
+ },
290
+ setup(props) {
291
+ const loaded = vueDemi.ref(false);
292
+ function handleLoad() {
293
+ loaded.value = true;
294
+ }
295
+ const { inView, elRef } = useInView({
296
+ threshold: props.intersectionThreshold || props.intersectionTreshold || 0,
297
+ rootMargin: props.intersectionMargin || "0px 0px 0px 0px"
298
+ });
299
+ return {
300
+ inView,
301
+ elRef,
302
+ loaded,
303
+ handleLoad
304
+ };
305
+ },
306
+ render() {
307
+ const addImage = imageAddStrategy({
308
+ lazyLoad: this.lazyLoad,
309
+ inView: this.inView,
310
+ loaded: this.loaded
311
+ });
312
+ const showImage = imageShowStrategy({
313
+ lazyLoad: this.lazyLoad,
314
+ inView: this.inView,
315
+ loaded: this.loaded
316
+ });
317
+ const webpSource = this.data.webpSrcSet && vueDemi.h(Source, __spreadValues$2(__spreadValues$2({}, vueDemi.isVue2 && {
318
+ props: {
319
+ srcset: this.data.webpSrcSet,
320
+ sizes: this.data.sizes,
321
+ type: "image/webp"
322
+ }
323
+ }), vueDemi.isVue3 && {
324
+ srcset: this.data.webpSrcSet,
325
+ sizes: this.data.sizes,
326
+ type: "image/webp"
327
+ }));
328
+ const regularSource = this.data.srcSet && vueDemi.h(Source, __spreadValues$2(__spreadValues$2({}, vueDemi.isVue2 && {
329
+ props: {
330
+ srcset: this.data.srcSet,
331
+ sizes: this.data.sizes
332
+ }
333
+ }), vueDemi.isVue3 && {
334
+ srcset: this.data.srcSet,
335
+ sizes: this.data.sizes
336
+ }));
337
+ const transition = typeof this.fadeInDuration === "undefined" || this.fadeInDuration > 0 ? `opacity ${this.fadeInDuration || 500}ms ${this.fadeInDuration || 500}ms` : void 0;
338
+ const placeholder = vueDemi.h("div", {
339
+ style: __spreadValues$2({
340
+ backgroundImage: this.data.base64 ? `url(${this.data.base64})` : null,
341
+ backgroundColor: this.data.bgColor,
342
+ backgroundSize: "cover",
343
+ opacity: showImage ? 0 : 1,
344
+ objectFit: this.objectFit,
345
+ objectPosition: this.objectPosition,
346
+ transition
347
+ }, absolutePositioning)
348
+ });
349
+ const { width, aspectRatio } = this.data;
350
+ const height = this.data.height || width / aspectRatio;
351
+ const sizer = this.layout !== "fill" ? vueDemi.h(Sizer, __spreadValues$2(__spreadValues$2({}, vueDemi.isVue2 && {
352
+ props: {
353
+ sizerClass: this.pictureClass,
354
+ sizerStyle: this.pictureStyle,
355
+ width,
356
+ height,
357
+ explicitWidth: this.explicitWidth
358
+ }
359
+ }), vueDemi.isVue3 && {
360
+ sizerClass: this.pictureClass,
361
+ sizerStyle: this.pictureStyle,
362
+ width,
363
+ height,
364
+ explicitWidth: this.explicitWidth
365
+ })) : null;
366
+ return vueDemi.h(
367
+ "div",
368
+ {
369
+ style: __spreadValues$2(__spreadValues$2({
370
+ display: this.explicitWidth ? "inline-block" : "block",
371
+ overflow: "hidden"
372
+ }, this.layout === "fill" ? absolutePositioning : this.layout === "intrinsic" ? { position: "relative", width: "100%", maxWidth: `${width}px` } : this.layout === "fixed" ? { position: "relative", width: `${width}px` } : { position: "relative" }), this.rootStyle),
373
+ ref: "elRef"
374
+ },
375
+ [
376
+ sizer,
377
+ placeholder,
378
+ addImage && vueDemi.h("picture", null, [
379
+ webpSource,
380
+ regularSource,
381
+ this.data.src && vueDemi.h("img", __spreadProps$2(__spreadValues$2(__spreadValues$2({}, vueDemi.isVue2 && {
382
+ attrs: {
383
+ src: this.data.src,
384
+ alt: this.data.alt,
385
+ title: this.data.title
386
+ },
387
+ on: {
388
+ load: this.handleLoad
389
+ }
390
+ }), vueDemi.isVue3 && {
391
+ src: this.data.src,
392
+ alt: this.data.alt,
393
+ title: this.data.title,
394
+ onLoad: this.handleLoad
395
+ }), {
396
+ class: this.pictureClass,
397
+ style: __spreadProps$2(__spreadValues$2(__spreadValues$2({}, absolutePositioning), this.pictureStyle), {
398
+ opacity: showImage ? 1 : 0,
399
+ transition,
400
+ objectFit: this.objectFit,
401
+ objectPosition: this.objectPosition
402
+ })
403
+ }))
404
+ ]),
405
+ vueDemi.h("noscript", __spreadValues$2(__spreadValues$2({}, vueDemi.isVue2 && {
406
+ domProps: {
407
+ innerHTML: tag("picture", {}, [
408
+ this.data.webpSrcSet && tag("source", {
409
+ srcset: this.data.webpSrcSet,
410
+ sizes: this.data.sizes,
411
+ type: "image/webp"
412
+ }),
413
+ this.data.srcSet && tag("source", {
414
+ srcset: this.data.srcSet,
415
+ sizes: this.data.sizes
416
+ }),
417
+ tag("img", {
418
+ src: this.data.src,
419
+ alt: this.data.alt,
420
+ title: this.data.title,
421
+ class: this.pictureClass,
422
+ style: toCss(__spreadValues$2(__spreadValues$2({}, this.pictureStyle), absolutePositioning)),
423
+ loading: "lazy"
424
+ })
425
+ ])
426
+ }
427
+ }), vueDemi.isVue3 && {
428
+ innerHTML: tag("picture", {}, [
429
+ this.data.webpSrcSet && tag("source", {
430
+ srcset: this.data.webpSrcSet,
431
+ sizes: this.data.sizes,
432
+ type: "image/webp"
433
+ }),
434
+ this.data.srcSet && tag("source", {
435
+ srcset: this.data.srcSet,
436
+ sizes: this.data.sizes
437
+ }),
438
+ tag("img", {
439
+ src: this.data.src,
440
+ alt: this.data.alt,
441
+ title: this.data.title,
442
+ class: this.pictureClass,
443
+ style: toCss(__spreadValues$2(__spreadValues$2({}, this.pictureStyle), absolutePositioning)),
444
+ loading: "lazy"
445
+ })
446
+ ])
447
+ }))
448
+ ]
449
+ );
450
+ }
451
+ });
452
+ const DatocmsImagePlugin = {
453
+ install: (Vue) => {
454
+ Vue.component("DatocmsImage", Image);
455
+ }
456
+ };
457
+
458
+ const hAdapter = (tagName, props, childOrChildren) => {
459
+ return vueDemi.h(
460
+ tagName,
461
+ props,
462
+ Array.isArray(childOrChildren) ? childOrChildren : [childOrChildren]
463
+ );
464
+ };
465
+ const defaultAdapter = {
466
+ renderNode: hAdapter,
467
+ renderMark: hAdapter,
468
+ renderFragment: (children, key) => children,
469
+ renderText: (text, key) => text
470
+ };
471
+ function appendKeyToValidElement(element, key) {
472
+ if (element !== null && typeof element !== "string" && element.key === null) {
473
+ element.key = key;
474
+ }
475
+ return element;
476
+ }
477
+ const StructuredText = vueDemi.defineComponent({
478
+ name: "DatocmsStructuredText",
479
+ props: {
480
+ data: {
481
+ type: Object
482
+ },
483
+ customRules: {
484
+ type: Array
485
+ },
486
+ customNodeRules: {
487
+ type: Array
488
+ },
489
+ customMarkRules: {
490
+ type: Array
491
+ },
492
+ renderInlineRecord: {
493
+ type: Function
494
+ },
495
+ renderLinkToRecord: {
496
+ type: Function
497
+ },
498
+ renderBlock: {
499
+ type: Function
500
+ },
501
+ metaTransformer: { type: Function },
502
+ renderText: { type: Function },
503
+ renderNode: { type: Function },
504
+ renderFragment: { type: Function }
505
+ },
506
+ setup(props) {
507
+ return () => {
508
+ return datocmsStructuredTextGenericHtmlRenderer.render(props.data, {
509
+ adapter: {
510
+ renderText: props.renderText || defaultAdapter.renderText,
511
+ renderNode: props.renderNode || defaultAdapter.renderNode,
512
+ renderFragment: props.renderFragment || defaultAdapter.renderFragment
513
+ },
514
+ metaTransformer: props.metaTransformer,
515
+ customMarkRules: props.customMarkRules,
516
+ customNodeRules: [
517
+ datocmsStructuredTextGenericHtmlRenderer.renderNodeRule(
518
+ datocmsStructuredTextUtils.isRoot,
519
+ ({ adapter: { renderNode }, key, children }) => {
520
+ return renderNode("div", { key }, children);
521
+ }
522
+ ),
523
+ datocmsStructuredTextGenericHtmlRenderer.renderNodeRule(datocmsStructuredTextUtils.isInlineItem, ({ node, key }) => {
524
+ if (!props.renderInlineRecord) {
525
+ throw new datocmsStructuredTextUtils.RenderError(
526
+ `The Structured Text document contains an 'inlineItem' node, but no 'renderInlineRecord' prop is specified!`,
527
+ node
528
+ );
529
+ }
530
+ if (!datocmsStructuredTextUtils.isStructuredText(props.data) || !props.data.links) {
531
+ throw new datocmsStructuredTextUtils.RenderError(
532
+ `The Structured Text document contains an 'inlineItem' node, but .links is not present!`,
533
+ node
534
+ );
535
+ }
536
+ const item = props.data.links.find((item2) => item2.id === node.item);
537
+ if (!item) {
538
+ throw new datocmsStructuredTextUtils.RenderError(
539
+ `The Structured Text document contains an 'inlineItem' node, but cannot find a record with ID ${node.item} inside .links!`,
540
+ node
541
+ );
542
+ }
543
+ return appendKeyToValidElement(
544
+ props.renderInlineRecord({ record: item }),
545
+ key
546
+ );
547
+ }),
548
+ datocmsStructuredTextGenericHtmlRenderer.renderNodeRule(datocmsStructuredTextUtils.isItemLink, ({ node, key, children }) => {
549
+ if (!props.renderLinkToRecord) {
550
+ throw new datocmsStructuredTextUtils.RenderError(
551
+ `The Structured Text document contains an 'itemLink' node, but no 'renderLinkToRecord' prop is specified!`,
552
+ node
553
+ );
554
+ }
555
+ if (!datocmsStructuredTextUtils.isStructuredText(props.data) || !props.data.links) {
556
+ throw new datocmsStructuredTextUtils.RenderError(
557
+ `The Structured Text document contains an 'itemLink' node, but .links is not present!`,
558
+ node
559
+ );
560
+ }
561
+ const item = props.data.links.find((item2) => item2.id === node.item);
562
+ if (!item) {
563
+ throw new datocmsStructuredTextUtils.RenderError(
564
+ `The Structured Text document contains an 'itemLink' node, but cannot find a record with ID ${node.item} inside .links!`,
565
+ node
566
+ );
567
+ }
568
+ return appendKeyToValidElement(
569
+ props.renderLinkToRecord({
570
+ record: item,
571
+ children,
572
+ transformedMeta: node.meta ? (props.metaTransformer || datocmsStructuredTextGenericHtmlRenderer.defaultMetaTransformer)({
573
+ node,
574
+ meta: node.meta
575
+ }) : null
576
+ }),
577
+ key
578
+ );
579
+ }),
580
+ datocmsStructuredTextGenericHtmlRenderer.renderNodeRule(datocmsStructuredTextUtils.isBlock, ({ node, key }) => {
581
+ if (!props.renderBlock) {
582
+ throw new datocmsStructuredTextUtils.RenderError(
583
+ `The Structured Text document contains a 'block' node, but no 'renderBlock' prop is specified!`,
584
+ node
585
+ );
586
+ }
587
+ if (!datocmsStructuredTextUtils.isStructuredText(props.data) || !props.data.blocks) {
588
+ throw new datocmsStructuredTextUtils.RenderError(
589
+ `The Structured Text document contains a 'block' node, but .blocks is not present!`,
590
+ node
591
+ );
592
+ }
593
+ const item = props.data.blocks.find(
594
+ (item2) => item2.id === node.item
595
+ );
596
+ if (!item) {
597
+ throw new datocmsStructuredTextUtils.RenderError(
598
+ `The Structured Text document contains a 'block' node, but cannot find a record with ID ${node.item} inside .blocks!`,
599
+ node
600
+ );
601
+ }
602
+ return appendKeyToValidElement(
603
+ props.renderBlock({ record: item }),
604
+ key
605
+ );
606
+ }),
607
+ ...props.customNodeRules || props.customRules || []
608
+ ]
609
+ });
610
+ };
611
+ }
612
+ });
613
+ const DatocmsStructuredTextPlugin = {
614
+ install: (Vue) => {
615
+ Vue.component("DatocmsStructuredText", StructuredText);
616
+ }
617
+ };
618
+
619
+ var __defProp$1 = Object.defineProperty;
620
+ var __defProps$1 = Object.defineProperties;
621
+ var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
622
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
623
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
624
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
625
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
626
+ var __spreadValues$1 = (a, b) => {
627
+ for (var prop in b || (b = {}))
628
+ if (__hasOwnProp$1.call(b, prop))
629
+ __defNormalProp$1(a, prop, b[prop]);
630
+ if (__getOwnPropSymbols$1)
631
+ for (var prop of __getOwnPropSymbols$1(b)) {
632
+ if (__propIsEnum$1.call(b, prop))
633
+ __defNormalProp$1(a, prop, b[prop]);
634
+ }
635
+ return a;
636
+ };
637
+ var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
638
+ var __objRest = (source, exclude) => {
639
+ var target = {};
640
+ for (var prop in source)
641
+ if (__hasOwnProp$1.call(source, prop) && exclude.indexOf(prop) < 0)
642
+ target[prop] = source[prop];
643
+ if (source != null && __getOwnPropSymbols$1)
644
+ for (var prop of __getOwnPropSymbols$1(source)) {
645
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$1.call(source, prop))
646
+ target[prop] = source[prop];
647
+ }
648
+ return target;
649
+ };
650
+ var __async$1 = (__this, __arguments, generator) => {
651
+ return new Promise((resolve, reject) => {
652
+ var fulfilled = (value) => {
653
+ try {
654
+ step(generator.next(value));
655
+ } catch (e) {
656
+ reject(e);
657
+ }
658
+ };
659
+ var rejected = (value) => {
660
+ try {
661
+ step(generator.throw(value));
662
+ } catch (e) {
663
+ reject(e);
664
+ }
665
+ };
666
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
667
+ step((generator = generator.apply(__this, __arguments)).next());
668
+ });
669
+ };
670
+ function useQuerySubscription(options) {
671
+ const _a = options, { enabled, initialData } = _a, other = __objRest(_a, ["enabled", "initialData"]);
672
+ const error = vueDemi.ref(null);
673
+ const data = vueDemi.ref(null);
674
+ const status = vueDemi.ref(
675
+ enabled ? "connecting" : "closed"
676
+ );
677
+ const subscribeToQueryOptions = other;
678
+ vueDemi.watchEffect((onCleanup) => {
679
+ if (enabled === false) {
680
+ status.value = "closed";
681
+ return () => {
682
+ };
683
+ }
684
+ let unsubscribe;
685
+ function subscribe() {
686
+ return __async$1(this, null, function* () {
687
+ unsubscribe = yield datocmsListen.subscribeToQuery(__spreadProps$1(__spreadValues$1({}, subscribeToQueryOptions), {
688
+ onStatusChange: (connectionStatus) => {
689
+ status.value = connectionStatus;
690
+ },
691
+ onUpdate: (updateData) => {
692
+ error.value = null;
693
+ data.value = updateData.response.data;
694
+ },
695
+ onChannelError: (errorData) => {
696
+ data.value = null;
697
+ error.value = errorData;
698
+ }
699
+ }));
700
+ });
701
+ }
702
+ subscribe();
703
+ onCleanup(() => {
704
+ if (unsubscribe) {
705
+ unsubscribe();
706
+ }
707
+ });
708
+ });
709
+ return { error, status, data: data || initialData };
710
+ }
711
+
712
+ var __async = (__this, __arguments, generator) => {
713
+ return new Promise((resolve, reject) => {
714
+ var fulfilled = (value) => {
715
+ try {
716
+ step(generator.next(value));
717
+ } catch (e) {
718
+ reject(e);
719
+ }
720
+ };
721
+ var rejected = (value) => {
722
+ try {
723
+ step(generator.throw(value));
724
+ } catch (e) {
725
+ reject(e);
726
+ }
727
+ };
728
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
729
+ step((generator = generator.apply(__this, __arguments)).next());
730
+ });
731
+ };
732
+ const highlightPieces = (textWithHighlightMarker) => {
733
+ return textWithHighlightMarker.split(/\[h\](.+?)\[\/h\]/g).map((text, index) => ({
734
+ text,
735
+ isMatch: index % 2 === 1
736
+ }));
737
+ };
738
+ function useSiteSearch(config) {
739
+ var _a, _b, _c;
740
+ const state = vueDemi.reactive({
741
+ query: ((_a = config.initialState) == null ? void 0 : _a.query) || "",
742
+ page: ((_b = config.initialState) == null ? void 0 : _b.page) || 0,
743
+ locale: (_c = config.initialState) == null ? void 0 : _c.locale
744
+ });
745
+ const error = vueDemi.ref();
746
+ const response = vueDemi.reactive({
747
+ data: [],
748
+ meta: { total_count: 0 }
749
+ });
750
+ error.value = void 0;
751
+ const resultsPerPage = config.resultsPerPage || 8;
752
+ vueDemi.watchEffect((onCleanup) => {
753
+ let isCancelled = false;
754
+ const run = () => __async(this, null, function* () {
755
+ try {
756
+ if (!state.query) {
757
+ response.data = [];
758
+ response.meta = { total_count: 0 };
759
+ return;
760
+ }
761
+ const request = {
762
+ filter: {
763
+ query: state.query,
764
+ locale: state.locale,
765
+ build_trigger_id: config.buildTriggerId
766
+ },
767
+ page: {
768
+ limit: resultsPerPage,
769
+ offset: resultsPerPage * state.page
770
+ }
771
+ };
772
+ if (config.fuzzySearch) {
773
+ request.fuzzy = "true";
774
+ }
775
+ const results = yield config.client.searchResults.rawList(request);
776
+ if (!isCancelled) {
777
+ response.data = results.data;
778
+ response.meta.total_count = results.meta.total_count;
779
+ console.log("!isCancelled", response);
780
+ }
781
+ } catch (e) {
782
+ if (isCancelled) {
783
+ return;
784
+ }
785
+ if (e instanceof Error) {
786
+ error.value = e.message;
787
+ } else {
788
+ error.value = "Unknown error!";
789
+ }
790
+ }
791
+ });
792
+ run();
793
+ onCleanup(() => {
794
+ isCancelled = true;
795
+ });
796
+ });
797
+ const data = vueDemi.computed(() => {
798
+ return state.query && response.data.length > 0 ? {
799
+ pageResults: response.data.map((rawSearchResult) => ({
800
+ id: rawSearchResult.id,
801
+ url: rawSearchResult.attributes.url,
802
+ title: rawSearchResult.attributes.title,
803
+ titleHighlights: rawSearchResult.attributes.highlight.title ? rawSearchResult.attributes.highlight.title.map(highlightPieces) : [],
804
+ bodyExcerpt: rawSearchResult.attributes.body_excerpt,
805
+ bodyHighlights: rawSearchResult.attributes.highlight.body ? rawSearchResult.attributes.highlight.body.map(highlightPieces) : [],
806
+ raw: vueDemi.toRaw(rawSearchResult)
807
+ })),
808
+ totalResults: response.meta.total_count,
809
+ totalPages: Math.ceil(response.meta.total_count / resultsPerPage)
810
+ } : {
811
+ pageResults: [],
812
+ totalResults: 0,
813
+ totalPages: 0
814
+ };
815
+ });
816
+ return {
817
+ state,
818
+ error,
819
+ data
820
+ };
821
+ }
822
+
823
+ var __defProp = Object.defineProperty;
824
+ var __defProps = Object.defineProperties;
825
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
826
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
827
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
828
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
829
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
830
+ var __spreadValues = (a, b) => {
831
+ for (var prop in b || (b = {}))
832
+ if (__hasOwnProp.call(b, prop))
833
+ __defNormalProp(a, prop, b[prop]);
834
+ if (__getOwnPropSymbols)
835
+ for (var prop of __getOwnPropSymbols(b)) {
836
+ if (__propIsEnum.call(b, prop))
837
+ __defNormalProp(a, prop, b[prop]);
838
+ }
839
+ return a;
840
+ };
841
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
842
+ const toHead = (...args) => {
843
+ const tags = [].concat(...args);
844
+ const titleTag = tags && tags.find((t) => t.tag === "title");
845
+ const metaTags = tags ? tags.filter((t) => t.tag === "meta") : [];
846
+ const linkTags = tags ? tags.filter((t) => t.tag === "link") : [];
847
+ return {
848
+ title: titleTag && titleTag.content,
849
+ meta: metaTags.map((tag) => {
850
+ var _a, _b, _c, _d;
851
+ return __spreadProps(__spreadValues({}, tag.attributes), {
852
+ hid: ((_a = tag.attributes) == null ? void 0 : _a.name) || ((_b = tag.attributes) == null ? void 0 : _b.property),
853
+ vmid: ((_c = tag.attributes) == null ? void 0 : _c.name) || ((_d = tag.attributes) == null ? void 0 : _d.property)
854
+ });
855
+ }),
856
+ link: linkTags.map((tag) => {
857
+ var _a, _b, _c, _d, _e, _f;
858
+ return __spreadProps(__spreadValues({}, tag.attributes), {
859
+ hid: ((_a = tag.attributes) == null ? void 0 : _a.name) || `${(_b = tag.attributes) == null ? void 0 : _b.rel}-${(_c = tag.attributes) == null ? void 0 : _c.sizes}`,
860
+ vmid: ((_d = tag.attributes) == null ? void 0 : _d.name) || `${(_e = tag.attributes) == null ? void 0 : _e.rel}-${(_f = tag.attributes) == null ? void 0 : _f.sizes}`
861
+ });
862
+ })
863
+ };
864
+ };
865
+
866
+ Object.defineProperty(exports, 'renderMarkRule', {
867
+ enumerable: true,
868
+ get: function () { return datocmsStructuredTextGenericHtmlRenderer.renderMarkRule; }
869
+ });
870
+ Object.defineProperty(exports, 'renderNodeRule', {
871
+ enumerable: true,
872
+ get: function () { return datocmsStructuredTextGenericHtmlRenderer.renderNodeRule; }
873
+ });
874
+ Object.defineProperty(exports, 'renderRule', {
875
+ enumerable: true,
876
+ get: function () { return datocmsStructuredTextGenericHtmlRenderer.renderNodeRule; }
877
+ });
878
+ Object.defineProperty(exports, 'RenderError', {
879
+ enumerable: true,
880
+ get: function () { return datocmsStructuredTextUtils.RenderError; }
881
+ });
882
+ exports.DatocmsImagePlugin = DatocmsImagePlugin;
883
+ exports.DatocmsStructuredTextPlugin = DatocmsStructuredTextPlugin;
884
+ exports.Image = Image;
885
+ exports.StructuredText = StructuredText;
886
+ exports.appendKeyToValidElement = appendKeyToValidElement;
887
+ exports.defaultAdapter = defaultAdapter;
888
+ exports.toHead = toHead;
889
+ exports.useQuerySubscription = useQuerySubscription;
890
+ exports.useSiteSearch = useSiteSearch;