UNPKG

@blinkk/selective-edit

Version:
154 lines 5.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.templateIndex = exports.templatePreviewValue = exports.findOrGuessPreviewValue = exports.findPreviewValue = exports.combinePreviewKeys = exports.PreviewTypes = void 0; /** * Utility for determining the preview value given an object. */ const lit_html_1 = require("lit-html"); const dataType_1 = require("./dataType"); const deepObject_1 = require("./deepObject"); const COMMON_PREVIEW_KEYS = [ // First match wins. 'title', 'label', 'subtitle', 'type', 'text', 'key', 'id', 'url', 'value', 'doc', 'partial', ]; const VIDEO_EXT = [ // Video extensions. 'mp4', 'webm', ]; var PreviewTypes; (function (PreviewTypes) { PreviewTypes["Image"] = "image"; PreviewTypes["Text"] = "text"; })(PreviewTypes = exports.PreviewTypes || (exports.PreviewTypes = {})); /** * In the editor configuration it allows for providing a list of preview keys * or a single preview key to use for finding the preview value. This method * combines them into a single array to normalize the value for other methods. * * @param previewKeys Array of preview keys to use for previewing a field. * @param previewKey Preview key to use for previewing a field. */ function combinePreviewKeys(previewKeys, previewKey) { // Copy the array to prevent modifying the original array. const allKeys = [...(previewKeys ?? [])]; if (previewKey) { allKeys.push(previewKey); } return allKeys; } exports.combinePreviewKeys = combinePreviewKeys; function guessPreviewForObject(obj) { const deepObj = (0, deepObject_1.autoDeepObject)(obj); let previewValue = obj; for (const key of COMMON_PREVIEW_KEYS) { previewValue = deepObj.get(key); if (!previewValue) { // Also check for translation marked keys. previewValue = deepObj.get(`${key}@`); } if (previewValue) { break; } } // If the matched preview is also an object try again. if (dataType_1.DataType.isObject(previewValue)) { return guessPreviewForObject(previewValue); } return previewValue; } function findPreviewValue(value, previewFieldKeys, defaultValue) { value = (0, deepObject_1.autoDeepObject)(value); let previewValue = null; if (previewFieldKeys) { for (const fieldKey of previewFieldKeys) { previewValue = value.get(fieldKey); // First matching field key becomes the value. if (previewValue && dataType_1.DataType.isString(previewValue)) { return previewValue; } } } return defaultValue; } exports.findPreviewValue = findPreviewValue; function findOrGuessPreviewValue(value, previewFieldKeys, defaultValue) { value = (0, deepObject_1.autoDeepObject)(value); let previewValue = null; if (previewFieldKeys) { for (const fieldKey of previewFieldKeys) { previewValue = value.get(fieldKey); // First matching field key becomes the value. if (previewValue) { break; } } } if (!previewValue) { previewValue = value.obj; } if (previewValue) { // Do not return a object as a preview. // Previews need to be something that can be displayed. if (dataType_1.DataType.isObject(previewValue)) { previewValue = guessPreviewForObject(previewValue); } return previewValue || defaultValue; } return defaultValue; } exports.findOrGuessPreviewValue = findOrGuessPreviewValue; function templatePreviewValue(previewValue, previewType, defaultValue, // eslint-disable-next-line @typescript-eslint/no-unused-vars index) { if (previewType === PreviewTypes.Image && dataType_1.DataType.isString(previewValue)) { if (previewValue.startsWith('http') || previewValue.startsWith('//')) { for (const videoExt of VIDEO_EXT) { if (previewValue.endsWith(`.${videoExt}`)) { return (0, lit_html_1.html) `<video playsinline disableremoteplayback muted autoplay loop > <source src="${previewValue}" /> </video>`; } } return (0, lit_html_1.html) `<img src="${previewValue}" class="selective__image__fingernail" />`; } else if (previewValue.startsWith('/')) { return (0, lit_html_1.html) `<img src="${previewValue}" class="selective__image__fingernail" />`; } } // Prevent having `[Object]` style preview values. if (!dataType_1.DataType.isString(previewValue)) { previewValue = defaultValue; } return (0, lit_html_1.html) `${previewValue || defaultValue}`; } exports.templatePreviewValue = templatePreviewValue; function templateIndex(index) { return index !== undefined ? (0, lit_html_1.html) `<span class="selective__index">${index + 1}</span>` : (0, lit_html_1.html) ``; } exports.templateIndex = templateIndex; //# sourceMappingURL=preview.js.map