forked from datocms/vue-datocms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.esm.js
507 lines (502 loc) · 17.3 KB
/
index.esm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import hypenateStyleName from 'hyphenate-style-name';
import { isVue3, h, defineComponent, ref, onMounted, onBeforeUnmount } from 'vue-demi';
import { render, renderNodeRule, defaultMetaTransformer } from 'datocms-structured-text-generic-html-renderer';
export { renderMarkRule, renderNodeRule, renderNodeRule as renderRule } from 'datocms-structured-text-generic-html-renderer';
import { isRoot, isInlineItem, RenderError, isStructuredText, isItemLink, isBlock } from 'datocms-structured-text-utils';
export { RenderError } from 'datocms-structured-text-utils';
var __defProp$2 = Object.defineProperty;
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value;
var __objSpread$2 = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp$3.call(b, prop))
__defNormalProp$2(a, prop, b[prop]);
if (__getOwnPropSymbols$3)
for (var prop of __getOwnPropSymbols$3(b)) {
if (__propIsEnum$3.call(b, prop))
__defNormalProp$2(a, prop, b[prop]);
}
return a;
};
var __objRest$1 = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp$3.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols$3)
for (var prop of __getOwnPropSymbols$3(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum$3.call(source, prop))
target[prop] = source[prop];
}
return target;
};
function crossH(tag, data, ...rest) {
if (isVue3) {
let vue3Data = null;
if (data) {
const _a = data, {domProps, attrs, props, on} = _a, other = __objRest$1(_a, ["domProps", "attrs", "props", "on"]);
vue3Data = __objSpread$2(__objSpread$2(__objSpread$2(__objSpread$2(__objSpread$2({}, other), attrs), props), domProps), Object.entries(on || {}).reduce((acc, [key, value]) => __objSpread$2(__objSpread$2({}, acc), {
[`on${key.charAt(0).toUpperCase() + key.slice(1)}`]: value
}), {}));
}
return h(tag, vue3Data, ...rest);
}
return h(tag, data, ...rest);
}
var __defProp$1 = Object.defineProperty;
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value;
var __objSpread$1 = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp$2.call(b, prop))
__defNormalProp$1(a, prop, b[prop]);
if (__getOwnPropSymbols$2)
for (var prop of __getOwnPropSymbols$2(b)) {
if (__propIsEnum$2.call(b, prop))
__defNormalProp$1(a, prop, b[prop]);
}
return a;
};
const escape = (s) => {
s = "" + s;
s = s.replace(/&/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/"/g, """);
s = s.replace(/'/g, "'");
return s;
};
const toCss = (object) => {
if (!object) {
return null;
}
let result = "";
for (var styleName in object) {
if (Object.prototype.hasOwnProperty.call(object, styleName) && object[styleName]) {
result += `${hypenateStyleName(styleName)}: ${object[styleName]}; `;
}
}
return result.length > 0 ? result : null;
};
const tag = (tagName, attrs, content) => {
const serializedAttrs = [];
if (attrs) {
for (var attrName in attrs) {
if (Object.prototype.hasOwnProperty.call(attrs, attrName)) {
const value = attrs[attrName];
if (value) {
serializedAttrs.push(`${escape(attrName)}="${escape(value)}"`);
}
}
}
}
const attrsString = serializedAttrs.length > 0 ? ` ${serializedAttrs.join(" ")}` : "";
return content ? `<${tagName}${attrsString}>${content.join("")}</${tagName}>` : `<${tagName}${attrsString} />`;
};
const isSsr = typeof window === "undefined";
const isIntersectionObserverAvailable = isSsr ? false : !!window.IntersectionObserver;
const universalBtoa = isSsr ? (str) => Buffer.from(str.toString(), "binary").toString("base64") : window.btoa;
const absolutePositioning = {
position: "absolute",
left: "0px",
top: "0px",
width: "100%",
height: "100%"
};
const useInView = ({threshold, rootMargin}) => {
const observer = ref(null);
const elRef = ref(null);
const inView = ref(false);
onMounted(() => {
if (isIntersectionObserverAvailable) {
observer.value = new IntersectionObserver((entries) => {
const image = entries[0];
if (image.isIntersecting && observer.value) {
inView.value = true;
observer.value.disconnect();
}
}, {
threshold,
rootMargin
});
if (elRef.value) {
observer.value.observe(elRef.value);
}
}
});
onBeforeUnmount(() => {
if (isIntersectionObserverAvailable && observer.value) {
observer.value.disconnect();
}
});
return {inView, elRef};
};
const imageAddStrategy = ({lazyLoad, inView, loaded}) => {
if (!lazyLoad) {
return true;
}
if (isSsr) {
return false;
}
if (isIntersectionObserverAvailable) {
return inView || loaded;
}
return true;
};
const imageShowStrategy = ({lazyLoad, loaded}) => {
if (!lazyLoad) {
return true;
}
if (isSsr) {
return false;
}
if (isIntersectionObserverAvailable) {
return loaded;
}
return true;
};
const Image = defineComponent({
name: "DatocmsImage",
props: {
data: {
type: Object,
required: true
},
pictureClass: {
type: String
},
fadeInDuration: {
type: Number
},
intersectionTreshold: {
type: Number,
default: 0
},
intersectionThreshold: {
type: Number
},
intersectionMargin: {
type: String,
default: "0px 0px 0px 0px"
},
lazyLoad: {
type: Boolean,
default: true
},
pictureStyle: {
type: Object,
default: () => ({})
},
explicitWidth: {
type: Boolean
}
},
setup(props) {
const loaded = ref(false);
function handleLoad() {
loaded.value = true;
}
const {inView, elRef} = useInView({
threshold: props.intersectionThreshold || props.intersectionTreshold || 0,
rootMargin: props.intersectionMargin || "0px 0px 0px 0px"
});
return {
inView,
elRef,
loaded,
handleLoad
};
},
render() {
const addImage = imageAddStrategy({
lazyLoad: this.lazyLoad,
inView: this.inView,
loaded: this.loaded
});
const showImage = imageShowStrategy({
lazyLoad: this.lazyLoad,
inView: this.inView,
loaded: this.loaded
});
const webpSource = this.data.webpSrcSet && crossH("source", {
attrs: {
srcset: this.data.webpSrcSet,
sizes: this.data.sizes,
type: "image/webp"
}
});
const regularSource = this.data.srcSet && crossH("source", {
attrs: {
srcset: this.data.srcSet,
sizes: this.data.sizes
}
});
const transition = typeof this.fadeInDuration === "undefined" || this.fadeInDuration > 0 ? `opacity ${this.fadeInDuration || 500}ms ${this.fadeInDuration || 500}ms` : void 0;
const placeholder = crossH("div", {
style: __objSpread$1({
backgroundImage: this.data.base64 ? `url(${this.data.base64})` : null,
backgroundColor: this.data.bgColor,
backgroundSize: "cover",
opacity: showImage ? 0 : 1,
transition
}, absolutePositioning)
});
const {width, aspectRatio} = this.data;
const height = this.data.height || width / aspectRatio;
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}"></svg>`;
const sizer = crossH("img", {
class: this.pictureClass,
style: __objSpread$1({
display: "block",
width: this.explicitWidth ? `${width}px` : "100%"
}, this.pictureStyle),
attrs: {
src: `data:image/svg+xml;base64,${universalBtoa(svg)}`,
role: "presentation"
}
});
return crossH("div", {
style: {
display: this.explicitWidth ? "inline-block" : "block",
overflow: "hidden",
position: "relative"
},
ref: "elRef"
}, [
sizer,
placeholder,
addImage && crossH("picture", null, [
webpSource,
regularSource,
this.data.src && crossH("img", {
attrs: {
src: this.data.src,
alt: this.data.alt,
title: this.data.title
},
on: {
load: this.handleLoad
},
class: this.pictureClass,
style: __objSpread$1(__objSpread$1(__objSpread$1({}, absolutePositioning), this.pictureStyle), {
opacity: showImage ? 1 : 0,
transition
})
})
]),
crossH("noscript", {
domProps: {
innerHTML: tag("picture", {}, [
this.data.webpSrcSet && tag("source", {
srcset: this.data.webpSrcSet,
sizes: this.data.sizes,
type: "image/webp"
}),
this.data.srcSet && tag("source", {
srcset: this.data.srcSet,
sizes: this.data.sizes
}),
tag("img", {
src: this.data.src,
alt: this.data.alt,
title: this.data.title,
class: this.pictureClass,
style: toCss(__objSpread$1(__objSpread$1({}, this.pictureStyle), absolutePositioning)),
loading: "lazy"
})
])
}
})
]);
}
});
const DatocmsImagePlugin = {
install: (Vue) => {
Vue.component("DatocmsImage", Image);
}
};
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp$1.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols$1)
for (var prop of __getOwnPropSymbols$1(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum$1.call(source, prop))
target[prop] = source[prop];
}
return target;
};
const hAdapter = (tagName, props, childOrChildren) => {
let data = props;
if (props) {
const _a = props, {key} = _a, attrs = __objRest(_a, ["key"]);
data = {key, attrs};
}
return crossH(tagName, data, Array.isArray(childOrChildren) ? childOrChildren : [childOrChildren]);
};
const defaultAdapter = {
renderNode: hAdapter,
renderMark: hAdapter,
renderFragment: (children, key) => children,
renderText: (text, key) => text
};
function appendKeyToValidElement(element, key) {
if (isVue3) {
const {isVNode, cloneVNode} = require("vue");
if (isVNode(element) && element.key === null) {
return cloneVNode(element, {key});
}
} else if (element && typeof element === "object" && (element.key === null || element.key === void 0)) {
element.key = key;
return element;
}
return element;
}
const StructuredText = defineComponent({
name: "DatocmsStructuredText",
props: {
data: {
type: Object
},
customRules: {
type: Array
},
customNodeRules: {
type: Array
},
customMarkRules: {
type: Array
},
renderInlineRecord: {
type: Function
},
renderLinkToRecord: {
type: Function
},
renderBlock: {
type: Function
},
metaTransformer: {type: Function},
renderText: {type: Function},
renderNode: {type: Function},
renderFragment: {type: Function}
},
setup(props) {
return () => {
return render(props.data, {
adapter: {
renderText: props.renderText || defaultAdapter.renderText,
renderNode: props.renderNode || defaultAdapter.renderNode,
renderFragment: props.renderFragment || defaultAdapter.renderFragment
},
metaTransformer: props.metaTransformer,
customMarkRules: props.customMarkRules,
customNodeRules: [
renderNodeRule(isRoot, ({adapter: {renderNode}, key, children}) => {
return renderNode("div", {key}, children);
}),
renderNodeRule(isInlineItem, ({node, key}) => {
if (!props.renderInlineRecord) {
throw new RenderError(`The Structured Text document contains an 'inlineItem' node, but no 'renderInlineRecord' prop is specified!`, node);
}
if (!isStructuredText(props.data) || !props.data.links) {
throw new RenderError(`The Structured Text document contains an 'inlineItem' node, but .links is not present!`, node);
}
const item = props.data.links.find((item2) => item2.id === node.item);
if (!item) {
throw new RenderError(`The Structured Text document contains an 'inlineItem' node, but cannot find a record with ID ${node.item} inside .links!`, node);
}
return appendKeyToValidElement(props.renderInlineRecord({record: item}), key);
}),
renderNodeRule(isItemLink, ({node, key, children}) => {
if (!props.renderLinkToRecord) {
throw new RenderError(`The Structured Text document contains an 'itemLink' node, but no 'renderLinkToRecord' prop is specified!`, node);
}
if (!isStructuredText(props.data) || !props.data.links) {
throw new RenderError(`The Structured Text document contains an 'itemLink' node, but .links is not present!`, node);
}
const item = props.data.links.find((item2) => item2.id === node.item);
if (!item) {
throw new RenderError(`The Structured Text document contains an 'itemLink' node, but cannot find a record with ID ${node.item} inside .links!`, node);
}
return appendKeyToValidElement(props.renderLinkToRecord({
record: item,
children,
transformedMeta: node.meta ? (props.metaTransformer || defaultMetaTransformer)({
node,
meta: node.meta
}) : null
}), key);
}),
renderNodeRule(isBlock, ({node, key}) => {
if (!props.renderBlock) {
throw new RenderError(`The Structured Text document contains a 'block' node, but no 'renderBlock' prop is specified!`, node);
}
if (!isStructuredText(props.data) || !props.data.blocks) {
throw new RenderError(`The Structured Text document contains a 'block' node, but .blocks is not present!`, node);
}
const item = props.data.blocks.find((item2) => item2.id === node.item);
if (!item) {
throw new RenderError(`The Structured Text document contains a 'block' node, but cannot find a record with ID ${node.item} inside .blocks!`, node);
}
return appendKeyToValidElement(props.renderBlock({record: item}), key);
}),
...props.customNodeRules || props.customRules || []
]
});
};
}
});
const DatocmsStructuredTextPlugin = {
install: (Vue) => {
Vue.component("DatocmsStructuredText", StructuredText);
}
};
var __defProp = Object.defineProperty;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {enumerable: true, configurable: true, writable: true, value}) : obj[key] = value;
var __objSpread = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
const toHead = (...args) => {
const tags = [].concat(...args);
const titleTag = tags && tags.find((t) => t.tag === "title");
const metaTags = tags ? tags.filter((t) => t.tag === "meta") : [];
const linkTags = tags ? tags.filter((t) => t.tag === "link") : [];
return {
title: titleTag && titleTag.content,
meta: metaTags.map((tag) => {
var _a, _b, _c, _d;
return __objSpread(__objSpread({}, tag.attributes), {
hid: ((_a = tag.attributes) == null ? void 0 : _a.name) || ((_b = tag.attributes) == null ? void 0 : _b.property),
vmid: ((_c = tag.attributes) == null ? void 0 : _c.name) || ((_d = tag.attributes) == null ? void 0 : _d.property)
});
}),
link: linkTags.map((tag) => {
var _a, _b, _c, _d, _e, _f;
return __objSpread(__objSpread({}, tag.attributes), {
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}`,
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}`
});
})
};
};
export { DatocmsImagePlugin, DatocmsStructuredTextPlugin, Image, StructuredText, appendKeyToValidElement, defaultAdapter, toHead };