{"version":3,"file":"main.Cz9rPx.js","sources":["theme-118/virtual:theme","theme-118/discourse/initializers/initialize-for-pdf-preview.js","theme-118/virtual:entrypoint:main"],"sourcesContent":["import { getObjectForTheme } from \"discourse/lib/theme-settings-store\";\n\nexport const settings = getObjectForTheme(118);\n\nexport function themePrefix(key) {\n  return `theme_translations.118.${key}`;\n}\n","import { iconHTML } from \"discourse/lib/icon-library\";\nimport { withPluginApi } from \"discourse/lib/plugin-api\";\n\nconst PREVIEW_HEIGHT = 500;\n\nexport default {\n  name: \"pdf-previews\",\n  initialize(container) {\n    withPluginApi((api) => {\n      const capabilities = container.lookup(\"service:capabilities\");\n\n      try {\n        const previewModeSetting = settings.preview_mode;\n        const newTabIcon = () => {\n          const template = document.createElement(\"template\");\n          template.innerHTML = iconHTML(\"up-right-from-square\", {\n            class: \"new-tab-pdf-icon\",\n          });\n          return template.content.firstChild;\n        };\n\n        const createPreviewElement = () => {\n          const iframe = document.createElement(\"iframe\");\n          iframe.src = \"\";\n          iframe.type = \"application/pdf\";\n          iframe.height = PREVIEW_HEIGHT;\n          iframe.loading = \"lazy\";\n          iframe.classList.add(\"pdf-preview\");\n\n          return iframe;\n        };\n\n        const setUpPreviewType = (pdf, renderMode) => {\n          if (renderMode === \"Inline\") {\n            const preview = createPreviewElement();\n            pdf.classList.add(\"pdf-attachment\");\n            pdf.after(preview);\n\n            return preview;\n          }\n\n          if (renderMode === \"New Tab\") {\n            pdf.classList.add(\"new-tab-pdf\");\n            pdf.prepend(newTabIcon());\n          }\n        };\n\n        api.decorateCookedElement(\n          (post) => {\n            if (!capabilities.viewport.sm) {\n              return;\n            }\n\n            const attachments = [...post.querySelectorAll(\".attachment\")];\n\n            const pdfs = attachments.filter((attachment) =>\n              /\\.pdf$/i.test(attachment.href)\n            );\n\n            pdfs.forEach((pdf) => {\n              const fileSize = pdf.nextSibling;\n              if (fileSize) {\n                fileSize.nodeValue = \"\";\n              }\n\n              const startsWithWhitespace = /^\\s+/;\n              const fileName = pdf.innerText;\n\n              // open the pdf in a new tab if either the global setting is\n              // \"New Tab\" or if the pdf description starts with a whitespace\n              // otherwise, render the preview in the inline in the post\n              const renderMode =\n                previewModeSetting === \"New Tab\" ||\n                startsWithWhitespace.test(fileName)\n                  ? \"New Tab\"\n                  : \"Inline\";\n\n              // we don't need the space anymore.\n              pdf.innerText = pdf.innerText.trim();\n\n              // handle preview type\n              const preview = setUpPreviewType(pdf, renderMode);\n\n              // the pdf is set to Content-Disposition: attachment; filename=\"filename.jpg\"\n              // on the server. this means we can't just use the href as the\n              // src for the pdf preview elements.\n              const httpRequest = new XMLHttpRequest();\n              httpRequest.open(\"GET\", pdf.href);\n              httpRequest.responseType = \"blob\";\n\n              httpRequest.onreadystatechange = () => {\n                if (httpRequest.readyState !== XMLHttpRequest.DONE) {\n                  return;\n                }\n\n                if (httpRequest.status === 200) {\n                  const src = URL.createObjectURL(httpRequest.response);\n\n                  if (renderMode === \"Inline\") {\n                    preview.src = src;\n                  }\n\n                  if (renderMode === \"New Tab\") {\n                    pdf.addEventListener(\"click\", (event) => {\n                      event.preventDefault();\n                      event.stopPropagation();\n                      window.open(src);\n                    });\n                  }\n                }\n              };\n              httpRequest.send();\n            });\n          },\n          {\n            id: \"pdf-previews\",\n            onlyStream: true,\n          }\n        );\n      } catch (error) {\n        // eslint-disable-next-line no-console\n        console.error(\n          \"There's an issue in the pdf previews theme component\",\n          error\n        );\n      }\n    });\n  },\n};\n","import * as Mod1 from \"./discourse/initializers/initialize-for-pdf-preview\";\nconst compatModules = {\n  \"discourse/initializers/initialize-for-pdf-preview\": Mod1,\n};\nexport { compatModules };\nexport default compatModules;\n"],"names":["settings","getObjectForTheme","initializeForPdfPreview","name","initialize","container","withPluginApi","api","capabilities","lookup","previewModeSetting","preview_mode","newTabIcon","template","document","createElement","innerHTML","iconHTML","class","content","firstChild","createPreviewElement","iframe","src","type","height","loading","classList","add","setUpPreviewType","pdf","renderMode","preview","after","prepend","decorateCookedElement","post","viewport","sm","querySelectorAll","filter","attachment","test","href","forEach","fileSize","nextSibling","nodeValue","fileName","innerText","trim","httpRequest","XMLHttpRequest","open","responseType","onreadystatechange","readyState","DONE","status","URL","createObjectURL","response","addEventListener","event","preventDefault","stopPropagation","window","send","id","onlyStream","error","console","compatModules"],"mappings":"oTAEaA,EAAWC,EAAkB;ACG1C,IAAAC,EAAe,CACbC,KAAM,eACNC,UAAAA,CAAWC;8BACTC,CAAeC,IACb,MAAMC,EAAeH,EAAUI,OAAO;AAEtC,IACE,MAAMC,EAAqBV,EAASW,aAC9BC,EAAaA,KACjB,MAAMC,EAAWC,SAASC,cAAc;AAIxC,OAHAF,EAASG,UAAYC,EAAS,uBAAwB,CACpDC,MAAO,qBAEFL,EAASM,QAAQC,YAGpBC,EAAuBA,KAC3B,MAAMC,EAASR,SAASC,cAAc;AAOtC,OANAO,EAAOC,IAAM,GACbD,EAAOE,KAAO,kBACdF,EAAOG,OAtBM,IAuBbH,EAAOI,QAAU,OACjBJ,EAAOK,UAAUC,IAAI,eAEdN,GAGHO,EAAmBA,CAACC,EAAKC,KAC7B,GAAmB,WAAfA,EAAyB,CAC3B,MAAMC,EAAUX;AAIhB,OAHAS,EAAIH,UAAUC,IAAI,kBAClBE,EAAIG,MAAMD,GAEHA,CACT,CAEmB,YAAfD,IACFD,EAAIH,UAAUC,IAAI,eAClBE,EAAII,QAAQtB;AAIhBL,EAAI4B,sBACDC,IACC,IAAK5B,EAAa6B,SAASC,GACzB,OAGkB,IAAIF,EAAKG,iBAAiB,gBAErBC,OAAQC,GAC/B,UAAUC,KAAKD,EAAWE,OAGvBC,QAASd,IACZ,MAAMe,EAAWf,EAAIgB;AACjBD,IACFA,EAASE,UAAY;AAGvB,MACMC,EAAWlB,EAAImB,UAKflB,EACmB,YAAvBrB,GAP2B,OAQNgC,KAAKM,GACtB,UACA;AAGNlB,EAAImB,UAAYnB,EAAImB,UAAUC;AAG9B,MAAMlB,EAAUH,EAAiBC,EAAKC,GAKhCoB,EAAc,IAAIC;AAC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