{"version":3,"file":"index.vue2.mjs","sources":["../../../../../../packages/preview/supports/vue-files-preview/index.vue"],"sourcesContent":["<script lang=\"ts\" setup>\nimport { ref, shallowRef, watch } from 'vue'\nimport { PreviewRules, getPreviewTypeByFileType } from '../../preview.const'\nimport type { IPreviewRule, PreviewProps } from '../../preview.interface'\nimport { PreviewType } from '../../preview.interface'\nimport { getFileName, getFileNameFromUrl, getFileType, getFileTypeFromUrl } from '../../utils/utils'\n\nconst props = withDefaults(\n  defineProps<PreviewProps & {\n    width?: string\n    height?: string\n    overflow?: string\n  }>(),\n  {\n    file: () => null,\n    url: () => null,\n    width: () => '100%',\n    height: () => '100%',\n    overflow: () => 'auto',\n  },\n)\n\nconst emit = defineEmits<{\n  rendered: []\n  error: [error: Error]\n}>()\n\nconst loading = ref(true)\nconst currentPreview = shallowRef<IPreviewRule>(PreviewRules[PreviewType.NONE])\n\nfunction syncPreviewByFile(file: File | Blob): void {\n  const rule = PreviewRules[getPreviewTypeByFileType(getFileType(file, props.name))]\n  if (rule) {\n    loading.value = true\n    currentPreview.value = { ...rule, name: getFileName(file, props.name) }\n  }\n}\n\nfunction applyPreviewRule(fileType: string, name: string): void {\n  const rule = PreviewRules[getPreviewTypeByFileType(fileType)]\n  if (rule) {\n    loading.value = true\n    currentPreview.value = { ...rule, name }\n  }\n}\n\nasync function syncPreviewByUrl(url: string): Promise<void> {\n  const fileType = getFileTypeFromUrl(url, props.name)\n  const fileName = getFileNameFromUrl(url, props.name)\n\n  applyPreviewRule(fileType, fileName)\n}\n\nfunction onRendered() {\n  loading.value = false\n  emit('rendered')\n}\n\nfunction onError(e: Error) {\n  loading.value = false\n  emit('error', e)\n}\n\nwatch(\n  () => props.file,\n  (file) => {\n    if (file) {\n      syncPreviewByFile(file)\n    }\n  },\n  { immediate: true },\n)\n\nwatch(\n  () => props.url,\n  (url) => {\n    // Identify via URL only when no file is present.\n    if (url && !props.file) {\n      syncPreviewByUrl(url)\n    }\n  },\n  { immediate: true },\n)\n</script>\n\n<template>\n  <div class=\"vue-files-preview\" :style=\"{ width, height, overflow }\">\n    <!-- Loading -->\n    <div v-if=\"loading\" class=\"vfp-loading\">\n      <div class=\"vfp-spinner\" />\n    </div>\n    <component\n      :is=\"currentPreview.component\"\n      :name=\"currentPreview.name\"\n      :file=\"file\"\n      :url=\"url\"\n      @rendered=\"onRendered\"\n      @error=\"onError\"\n    />\n  </div>\n</template>\n\n<style scoped lang=\"scss\">\n.vfp-loading {\n  position: absolute;\n  inset: 0;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  background: #fff;\n  z-index: 10;\n}\n\n.vfp-spinner {\n  width: 32px;\n  height: 32px;\n  border: 3px solid #e8ecf0;\n  border-top-color: #667eea;\n  border-radius: 50%;\n  animation: vfp-spin 0.8s linear infinite;\n}\n\n@keyframes vfp-spin {\n  to {\n    transform: rotate(360deg);\n  }\n}\n\n.vue-files-preview {\n  position: relative;\n}\n</style>\n"],"names":["props","__props","emit","__emit","loading","ref","currentPreview","shallowRef","PreviewRules","PreviewType","syncPreviewByFile","file","rule","getPreviewTypeByFileType","getFileType","getFileName","applyPreviewRule","fileType","name","syncPreviewByUrl","url","getFileTypeFromUrl","fileName","getFileNameFromUrl","onRendered","onError","watch","_createElementBlock","_normalizeStyle","_openBlock","_hoisted_1","_cache","_createElementVNode","_createBlock","_resolveDynamicComponent"],"mappings":";;;;;;;;;;;;;;;;;;;AAOA,UAAMA,IAAQC,GAeRC,IAAOC,GAKPC,IAAUC,EAAI,EAAI,GAClBC,IAAiBC,EAAyBC,EAAaC,EAAY,IAAI,CAAC;AAE9E,aAASC,EAAkBC,GAAyB;AAClD,YAAMC,IAAOJ,EAAaK,EAAyBC,EAAYH,GAAMX,EAAM,IAAI,CAAC,CAAC;AACjF,MAAIY,MACFR,EAAQ,QAAQ,IAChBE,EAAe,QAAQ,EAAE,GAAGM,GAAM,MAAMG,EAAYJ,GAAMX,EAAM,IAAI,EAAA;AAAA,IAExE;AAEA,aAASgB,EAAiBC,GAAk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