@bareblends/vsf-prismic
Version:
Prismic CMS integration with Vue Storefront
112 lines (106 loc) • 4.04 kB
JavaScript
import { __assign, __awaiter, __generator } from 'tslib';
import PrismicDOM from 'prismic-dom';
import { useContentFactory } from '@vue-storefront/core';
var getType = function (block) {
if (!block) {
return undefined;
}
if (typeof block === 'string' || typeof block === 'number') {
return 'text';
}
if (Array.isArray(block)) {
return 'html';
}
if (block.dimensions && block.url) {
return 'image';
}
if (block.embed_url) {
return 'embed';
}
if (block.link_type) {
return block.kind === 'image' ? 'image' : 'link';
}
return undefined;
};
var getElement = function (block) {
if (typeof block === 'object') {
return block;
}
return [{
type: 'paragraph',
text: block,
spans: []
}];
};
var transformBlock = function (block) {
var blockType = getType(block);
var element = getElement(block);
switch (blockType) {
case 'html':
return PrismicDOM.RichText.asHtml(element.map(function (elem) { return (__assign(__assign({}, elem), { type: elem.type || 'paragraph', text: elem.text || '', spans: Array.isArray(elem.spans) ? elem.spans : [] })); }));
case 'image':
var height = element.height, width = element.width, url = element.url, alt = element.alt, name_1 = element.name;
return "<img src=\"" + (url || '') + "\" height=\"" + (height || '') + "\" width=\"" + (width || '') + "\" alt=\"" + (alt || name_1 || '') + "\" />";
case 'text':
return PrismicDOM.RichText.asText(element);
case 'embed':
return element.html || '';
case 'link':
return PrismicDOM.Link.url(element, function (doc) { return "<a href=\"" + doc.url + "\">" + doc.url + "</a>"; });
default:
return '';
}
};
var search = function (context, params) { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, context.$pris.api.getContent(params)];
});
}); };
var useContent = useContentFactory({ search: search });
var buildImageObject = function (items, actions) {
if (actions === void 0) { actions = { replace: false }; }
return items.map(function (item) {
var replace = actions.replace;
if (item.image) {
var image = item.image;
var itemObject = replace
? __assign(__assign({}, item), { url: image }) : __assign(__assign({}, item), { image: {
url: image
} });
if (replace) {
delete itemObject.image;
}
return itemObject;
}
return item;
});
};
var extractComponents = function (contentData) {
if (contentData === void 0) { contentData = []; }
var content = contentData;
if (!Array.isArray(contentData)) {
content = [content];
}
return content.map(function (component) {
var props = Object.keys(component).reduce(function (res, key) {
var _a;
return (__assign(__assign({}, res), (_a = {}, _a[key] = component[key], _a)));
}, {});
if (props.items && Array.isArray(props.items)) {
props.items = buildImageObject(props.items);
}
if (props.images && Array.isArray(props.images)) {
props.images = buildImageObject(props.images, { replace: true });
}
return {
componentName: component.component || 'CustomComponent',
props: props
};
});
};
var getContent = function (prismicContent) { return (prismicContent === null || prismicContent === void 0 ? void 0 : prismicContent.results[0]) || []; };
var contentGetters = {
getContent: getContent
};
export { contentGetters, extractComponents, transformBlock, useContent };
//# sourceMappingURL=index.es.js.map