docviewhelper 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.mjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../packages/docviewhelper/src/lib/helper.ts","../../../packages/docviewhelper/src/lib/model.ts"],"sourcesContent":["// eslint-disable-next-line no-var\ndeclare var mammoth: any;\nimport { IFrameReloader, ViewerRecoveryPlan, ViewerType } from './model';\n\nexport const fileToArray = (url: string): Promise<ArrayBuffer> => {\n return new Promise<ArrayBuffer>((resolve, reject) => {\n try {\n const request = new XMLHttpRequest();\n request.open('GET', url, true);\n request.responseType = 'blob';\n request.onload = () => {\n const reader = new FileReader();\n reader.readAsArrayBuffer(request.response);\n reader.onloadend = () => {\n resolve(reader.result as ArrayBuffer);\n };\n };\n request.send();\n } catch {\n reject(`error while retrieving file ${url}.`);\n }\n });\n};\n\nexport const timeout = (ms: number) => {\n return new Promise((resolve) => setTimeout(resolve, ms));\n};\n\nconst reloadIFrame = (iframe: HTMLIFrameElement) => {\n if (iframe) {\n const url = iframe.src;\n iframe.src = 'about:blank';\n setTimeout(() => {\n if (iframe) {\n iframe.src = url;\n }\n }, 100);\n }\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const handleFileUpload = (fileInput: any) => {\n return new Promise<string>((resolve, reject) => {\n if (fileInput.target.files && fileInput.target.files[0]) {\n const reader = new FileReader();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n reader.onload = (e: any) => {\n resolve(e.target.result);\n };\n reader.readAsDataURL(fileInput.target.files[0]);\n } else {\n reject('no files selected');\n }\n });\n};\n\nexport const getbaseUrl = (): string => {\n const pathArray = window.location.href.split('/');\n const protocol = pathArray[0];\n const host = pathArray[2];\n return protocol + '//' + host;\n};\n\nexport const getLocation = (href: string) => {\n // eslint-disable-next-line no-useless-escape\n const match = href.match(\n /^(https?\\:)\\/\\/(([^:\\/?#]*)(?:\\:([0-9]+))?)([\\/]{0,1}[^?#]*)(\\?[^#]*|)(#.*|)$/,\n );\n return (\n match && {\n href,\n protocol: match[1],\n host: match[2],\n hostname: match[3],\n port: match[4],\n pathname: match[5],\n search: match[6],\n hash: match[7],\n }\n );\n};\n\nexport const getDocxToHtml = async (url: string) => {\n if (!mammoth) {\n console.error(\n 'Please install mammoth and make sure mammoth.browser.min.js is loaded.',\n );\n }\n const arrayBuffer = await fileToArray(url);\n const resultObject = await mammoth.convertToHtml({ arrayBuffer });\n return resultObject.value;\n};\n\nexport const googleCheckSubscription = (): IFrameReloader => {\n let subscription: any = null;\n let checkCount = 0;\n return {\n subscribe: (iframe: HTMLIFrameElement, interval = 3000, maxChecks = 5) => {\n if (!iframeIsLoaded(iframe)) {\n subscription = setInterval(() => {\n checkCount++;\n if (checkCount >= maxChecks) {\n clearInterval(subscription);\n }\n reloadIFrame(iframe);\n }, interval);\n return subscription;\n } else {\n if (subscription) {\n clearInterval(subscription);\n }\n }\n },\n unsubscribe: () => {\n if (subscription) {\n clearInterval(subscription);\n }\n },\n };\n};\n\nexport const iframeIsLoaded = (iframe: HTMLIFrameElement) => {\n // its #document <html><head></head><body></body></html> when google is returning a 204\n // so if contentDocument = null then it's loaded.\n let isLoaded = false;\n try {\n if (!internetExplorer()) {\n isLoaded = !iframe?.contentDocument;\n } else {\n isLoaded = !iframe?.contentWindow?.document;\n }\n } catch {\n // ignore message Blocked a frame with origin \"http://...\" from accessing a cross-origin frame.\n }\n return isLoaded;\n};\n\nconst internetExplorer = () =>\n /MSIE (\\d+\\.\\d+);/.test(navigator.userAgent) ||\n navigator.userAgent.indexOf('Trident/') > -1;\n\nexport const getViewerDetails = (\n url: string,\n configuredViewer: ViewerType = 'google',\n queryParams = '',\n viewerUrl = '',\n) => {\n switch (configuredViewer) {\n case 'google':\n viewerUrl = `https://docs.google.com/gview?url=%URL%&embedded=true`;\n break;\n case 'office': {\n viewerUrl = `https://view.officeapps.live.com/op/embed.aspx?src=%URL%`;\n break;\n }\n case 'pdf': {\n viewerUrl = '';\n break;\n }\n }\n const externalViewer =\n configuredViewer === 'google' ||\n configuredViewer === 'office' ||\n configuredViewer === 'url';\n\n const u = url?.indexOf('/') ? encodeURIComponent(url) : url;\n let fullUrl = viewerUrl ? viewerUrl.replace('%URL%', u) : url;\n if (queryParams && externalViewer && configuredViewer !== 'url') {\n const start = queryParams.startsWith('&') ? '' : '&';\n fullUrl = `${fullUrl}${start}${queryParams}`;\n }\n return {\n url: fullUrl,\n externalViewer,\n };\n};\n\nexport const getViewerRecoveryPlan = ({\n viewer,\n googleCheckContentLoaded = true,\n googleFinalRetryDelay = 0,\n officeAutoRetry = false,\n}: {\n viewer: ViewerType;\n googleCheckContentLoaded?: boolean;\n googleFinalRetryDelay?: number;\n officeAutoRetry?: boolean;\n}): ViewerRecoveryPlan => {\n const modes: ViewerRecoveryPlan['modes'] = [];\n\n if (viewer === 'google' && googleCheckContentLoaded) {\n modes.push('google-probe');\n }\n\n if (viewer === 'google' && googleFinalRetryDelay > 0) {\n modes.push('google-final-retry');\n }\n\n if (viewer === 'office' && officeAutoRetry) {\n modes.push('office-auto-retry');\n }\n\n return { modes };\n};\n\nexport const replaceLocalUrl = (url: string, overrideLocalhost: string) => {\n const loc = getLocation(url);\n const locReplace = getLocation(overrideLocalhost);\n if (loc && locReplace) {\n return url.replace(\n loc.port ? `${loc.hostname}:${loc.port}` : loc.hostname,\n locReplace.port\n ? `${locReplace.hostname}:${locReplace.port}`\n : locReplace.hostname,\n );\n }\n return url;\n};\n\nconst getBlobFromUrl = (url: string) => {\n return new Promise<File>((resolve, reject) => {\n const request = new XMLHttpRequest();\n request.open('GET', url, true);\n request.responseType = 'blob';\n request.onload = () => {\n resolve(request.response as File);\n };\n request.onerror = reject;\n request.send();\n });\n};\n\nexport const uploadToCloud = (fileUrl: string, api: string) =>\n new Promise((resolve, reject) => {\n getBlobFromUrl(fileUrl).then((blob) => {\n const loc = getLocation(fileUrl);\n const name = loc?.pathname\n ? loc?.pathname?.split('/')[loc?.pathname?.split('/').length - 1]\n : '';\n const formData = new FormData();\n const request = new XMLHttpRequest();\n formData.append('file', blob, name);\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n request.onreadystatechange = (e) => {\n if (request.readyState === XMLHttpRequest.DONE) {\n if (request.status === 200) {\n resolve(request.responseText);\n } else {\n reject(request.responseText);\n }\n }\n };\n request.onerror = reject;\n request.open('post', api, true);\n request.send(formData);\n });\n });\n\nexport const isLocalFile = (file: string) => {\n const loc = getLocation(file);\n const hostname = loc?.hostname || '';\n return (\n ['localhost', '127.0.0.1', '', '::1'].includes(hostname) ||\n hostname.startsWith('192.168.') ||\n hostname.startsWith('10.0.') ||\n hostname.endsWith('.local')\n );\n};\n","export type ViewerType = 'google' | 'office' | 'mammoth' | 'pdf' | 'url';\nexport type ViewerRenderPhase = 'idle' | 'loading' | 'ready' | 'error';\nexport type ViewerRecoveryMode =\n | 'google-probe'\n | 'google-final-retry'\n | 'office-auto-retry';\n\nexport interface ViewerRecoveryPlan {\n modes: ViewerRecoveryMode[];\n}\n\ninterface Props {\n loaded?: () => void;\n url: string;\n queryParams?: string;\n viewerUrl?: string;\n googleCheckInterval?: number;\n disableContent?: 'none' | 'all' | 'poput' | 'popout-hide';\n googleCheckContentLoaded?: boolean;\n viewer?: ViewerType;\n}\n\nexport interface IFrameReloader {\n subscribe: (\n iframe: HTMLIFrameElement,\n interval?: number,\n maxChecks?: number\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ) => any;\n unsubscribe: () => void;\n}\n\nexport const defaultProps: Props = {\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n loaded: () => {},\n disableContent: 'none',\n googleCheckContentLoaded: true,\n googleCheckInterval: 3000,\n queryParams: '',\n url: '',\n viewer: 'google',\n viewerUrl: '',\n};\n"],"mappings":";AAIO,IAAM,cAAc,CAAC,QAAsC;AAChE,SAAO,IAAI,QAAqB,CAAC,SAAS,WAAW;AACnD,QAAI;AACF,YAAM,UAA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,YAAY;AAClC,SACE,CAAC,aAAa,aAAa,IAAI,KAAK,EAAE,SAAS,QAAQ,KACvD,SAAS,WAAW,UAAU,KAC9B,SAAS,WAAW,OAAO,KAC3B,SAAS,SAAS,QAAQ;AAE9B;;;AC3OO,IAAM,eAAsB;AAAA;AAAA,EAEjC,QAAQ,MAAM;AAAA,EAAC;AAAA,EACf,gBAAgB;AAAA,EAChB,0BAA0B;AAAA,EAC1B,qBAAqB;AAAA,EACrB,aAAa;AAAA,EACb,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,WAAW;AACb;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "docviewhelper",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "license": "MIT",
5
5
  "description": "Helper for ngx-doc-viewer and react-documents.",
6
6
  "author": {
@@ -16,12 +16,18 @@
16
16
  "ngx-doc-viewer",
17
17
  "react-documents"
18
18
  ],
19
- "main": "./index.umd.js",
20
- "types": "packages/react-documents/src/index.d.ts",
19
+ "main": "./index.js",
20
+ "module": "./index.mjs",
21
+ "types": "./index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./index.d.ts",
25
+ "import": "./index.mjs",
26
+ "require": "./index.js"
27
+ }
28
+ },
21
29
  "bugs": {
22
30
  "url": "https://github.com/Marcelh1983/document-viewer/issues"
23
31
  },
24
- "homepage": "https://github.com/Marcelh1983/document-viewer/tree/main/packages/docviewhelper",
25
- "module": "./index.esm.js",
26
- "typings": "./index.d.ts"
32
+ "homepage": "https://github.com/Marcelh1983/document-viewer/tree/main/packages/docviewhelper"
27
33
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # docviewer-helper
2
-
3
- This library is created to share helper functions to view documents in as well ngx-doc-viewer and react-documents