{"version":3,"file":"index.vue2.mjs","sources":["../../../../../../packages/preview/supports/ppt-preview/index.vue"],"sourcesContent":["<script lang='ts' setup>\nimport {init} from 'pptx-preview'\nimport {ref, watch, onMounted, onUnmounted} from 'vue'\nimport type {PreviewProps} from '../../preview.interface'\nimport {getFileRenderByFile, getFileRenderByUrl} from '../../utils/utils'\n\nconst props = withDefaults(\n  defineProps<PreviewProps>(),\n  {\n    url: () => null,\n    file: () => null,\n  },\n)\n\nconst emit = defineEmits<{\n  rendered: []\n  error: [error: Error]\n}>()\n\nconst pptContainer = ref<HTMLElement | null>(null)\n\nlet previewer: ReturnType<typeof init> | null = null\nlet arrayBufferData: ArrayBuffer | null = null\nlet slideCount = 0\nlet lastWidth = 0\nlet resizeObserver: ResizeObserver | null = null\nlet resizeTimer: ReturnType<typeof setTimeout> | null = null\n\nfunction getContainerWidth(): number {\n  const el = pptContainer.value\n  if (!el) return 960\n  const w = el.clientWidth\n  const pw = el.parentElement?.clientWidth || 0\n  return w > 100 ? w : (pw > 100 ? pw : 960)\n}\n\nfunction destroyPreviewer(): void {\n  if (previewer) {\n    try {\n      previewer.destroy()\n    } catch {\n      // ignore\n    }\n    previewer = null\n  }\n  if (pptContainer.value) {\n    pptContainer.value.innerHTML = ''\n  }\n}\n\n/** Retrieve using a hidden container.slideCount */\nasync function detectSlideCount(data: ArrayBuffer): Promise<number> {\n  const hidden = document.createElement('div')\n  hidden.style.cssText = 'position:absolute;left:-9999px;top:-9999px;visibility:hidden'\n  document.body.appendChild(hidden)\n  try {\n    const temp = init(hidden, {width: 100, height: 100, mode: 'slide'})\n    await temp.preview(data)\n    const count = temp.slideCount\n    temp.destroy()\n    return count\n  } finally {\n    document.body.removeChild(hidden)\n  }\n}\n\nasync function renderPreview(): Promise<void> {\n  if (!pptContainer.value || !arrayBufferData) return\n\n  destroyPreviewer()\n\n  const width = getContainerWidth()\n  lastWidth = width\n  const slideHeight = Math.floor(width * 9 / 16)\n\n  try {\n    // Retrieved during the initial render slideCount\n    if (slideCount === 0) {\n      slideCount = await detectSlideCount(arrayBufferData)\n    }\n\n    previewer = init(pptContainer.value, {\n      width,\n      height: slideHeight * slideCount,\n      mode: 'list',\n    })\n    await previewer.preview(arrayBufferData)\n    emit('rendered')\n  } catch (e) {\n    emit('error', e instanceof Error ? e : new Error('PPT render failed'))\n  }\n}\n\nfunction onResize(): void {\n  const w = getContainerWidth()\n  if (w > 0 && Math.abs(w - lastWidth) >= 10 && arrayBufferData && slideCount > 0) {\n    if (resizeTimer) clearTimeout(resizeTimer)\n    resizeTimer = setTimeout(() => {\n      renderPreview()\n    }, 300)\n  }\n}\n\nonMounted(() => {\n  if (pptContainer.value) {\n    resizeObserver = new ResizeObserver(() => onResize())\n    resizeObserver.observe(pptContainer.value)\n  }\n})\n\nonUnmounted(() => {\n  resizeObserver?.disconnect()\n  if (resizeTimer) clearTimeout(resizeTimer)\n  destroyPreviewer()\n})\n\nasync function loadData(data: ArrayBuffer | string): Promise<void> {\n  if (data instanceof ArrayBuffer) {\n    arrayBufferData = data\n  } else if (typeof data === 'string') {\n    const response = await fetch(data)\n    arrayBufferData = await response.arrayBuffer()\n  }\n  slideCount = 0\n  await renderPreview()\n}\n\nwatch(\n  () => props.file,\n  (file) => {\n    if (file) {\n      getFileRenderByFile(file).then(loadData).catch((e: Error) => emit('error', e))\n    }\n  },\n  {immediate: true},\n)\n\nwatch(\n  () => props.url,\n  (url) => {\n    if (url && !props.file) {\n      getFileRenderByUrl(url).then(loadData).catch((e: Error) => emit('error', e))\n    }\n  },\n  {immediate: true},\n)\n</script>\n\n<template>\n  <div ref=\"pptContainer\" class=\"ppt-preview\"/>\n</template>\n\n<style scoped lang='scss'>\n.ppt-preview {\n  width: 100%;\n  height: 100%;\n  overflow-y: auto;\n  overflow-x: hidden;\n  background: #333;\n  scrollbar-width: thin;\n  scrollbar-gutter: stable;\n\n  &::-webkit-scrollbar {\n    width: 6px;\n  }\n\n  &::-webkit-scrollbar-track {\n    background: transparent;\n  }\n\n  &::-webkit-scrollbar-thumb {\n    background: rgba(255, 255, 255, 0.3);\n    border-radius: 3px;\n  }\n\n  :deep(.pptx-preview-wrapper) {\n    overflow-y: visible !important;\n  }\n}\n</style>"],"names":["props","__props","emit","__emit","pptContainer","ref","previewer","arrayBufferData","slideCount","lastWidth","resizeObserver","resizeTimer","getContainerWidth","el","w","pw","_a","destroyPreviewer","detectSlideCount","data","hidden","temp","init","count","renderPreview","width","slideHeight","e","onResize","onMounted","onUnmounted","loadData","watch","file","getFileRenderByFile","url","getFileRenderByUrl","_createElementBlock"],"mappings":";;;;;;;;;;;;AAMA,UAAMA,IAAQC,GAQRC,IAAOC,GAKPC,IAAeC,EAAwB,IAAI;AAEjD,QAAIC,IAA4C,MAC5CC,IAAsC,MACtCC,IAAa,GACbC,IAAY,GACZC,IAAwC,MACxCC,IAAoD;AAExD,aAASC,IAA4B;;AACnC,YAAMC,IAAKT,EAAa;AACxB,UAAI,CAACS,EAAI,QAAO;AAChB,YAAMC,IAAID,EAAG,aACPE,MAAKC,IAAAH,EAAG,kBAAH,gBAAAG,EAAkB,gBAAe;AAC5C,aAAOF,IAAI,MAAMA,IAAKC,IAAK,MAAMA,IAAK;AAAA,IACxC;AAEA,aAASE,IAAyB;AAChC,UAAIX,GAAW;AACb,YAAI;AACF,UAAAA,EAAU,QAAA;AAAA,QACZ,QAAQ;AAAA,QAER;AACA,QAAAA,IAAY;AAAA,MACd;AACA,MAAIF,EAAa,UACfA,EAAa,MAAM,YAAY;AAAA,IAEnC;AAGA,mBAAec,EAAiBC,GAAoC;AAClE,YAAMC,IAAS,SAAS,cAAc,KAAK;AAC3C,MAAAA,EAAO,MAAM,UAAU,gEACvB,SAAS,KAAK,YAAYA,CAAM;AAChC,UAAI;AACF,cAAMC,IAAOC,EAAKF,GAAQ,EAAC,OAAO,KAAK,QAAQ,KAAK,MAAM,SAAQ;AAClE,cAAMC,EAAK,QAAQF,CAAI;AACvB,cAAMI,IAAQF,EAAK;AACnB,eAAAA,EAAK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