webviewer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const ComPDFKitViewer = {
  2. init (options, element) {
  3. return new Promise((resolve) => {
  4. element.addEventListener("ready", function ready () {
  5. element.removeEventListener('ready', ready)
  6. const iframeWindow = element.querySelector('iframe').contentWindow
  7. Promise.resolve().then(function() {
  8. resolve({
  9. docViewer: iframeWindow.instance,
  10. Core: iframeWindow.instances.Core,
  11. UI: iframeWindow.instances.UI
  12. })
  13. })
  14. })
  15. const viewer = new Viewer(options, element)
  16. })
  17. }
  18. }
  19. const docViewer = new Map()
  20. class Viewer {
  21. static config = {
  22. path: '',
  23. webviewerPath: './webviewer/index.html',
  24. }
  25. constructor (options, element) {
  26. if (docViewer.get(element)) return
  27. this.docId = 0
  28. docViewer.set(element, true)
  29. this.instance = null
  30. this.options = options
  31. this.initialDoc = options.pdfUrl || null
  32. this.element = element
  33. this.options.path = this.options.path || Viewer.config.path
  34. const length = this.options.path.length - 1
  35. length > 0 && "/" !== this.options.path[length] && (this.options.path += "/")
  36. this.options.webviewerPath = this.options.path + Viewer.config.webviewerPath
  37. element.addEventListener("ready", (function ready() {
  38. element.removeEventListener("ready", ready)
  39. }))
  40. this.create()
  41. }
  42. _createEvent (e, t) {
  43. let n;
  44. try {
  45. n = new CustomEvent(e, {
  46. detail: t,
  47. bubbles: true,
  48. cancelable: true
  49. })
  50. } catch (o) {
  51. (n = document.createEvent("Event")).initEvent(e, true, true),
  52. n.detail = t
  53. }
  54. return n
  55. }
  56. create() {
  57. if (this.initialDoc) {
  58. this.initialDoc = encodeURIComponent(JSON.stringify(this.initialDoc))
  59. this._create()
  60. } else {
  61. this._create()
  62. }
  63. }
  64. _create () {
  65. this.docId++
  66. if (!this._trigger) {
  67. this._trigger = function(e, t) {
  68. var n = this._createEvent(e, t);
  69. this.element.dispatchEvent(n)
  70. }
  71. }
  72. this.createViewer()
  73. }
  74. createViewer () {
  75. const self = this
  76. let webviewerUrl = this.options.webviewerPath
  77. if (this.initialDoc) {
  78. webviewerUrl += "#d=".concat(this.initialDoc)
  79. } else {
  80. webviewerUrl += '#'
  81. }
  82. webviewerUrl += "&docId=".concat(this.docId)
  83. this.options.showToolbarControl !== undefined && (webviewerUrl += "&header=".concat(this.options.showToolbarControl ? "true": "false"))
  84. const iframe = document.createElement("iframe")
  85. iframe.id = "webviewer-" + this.docId
  86. iframe.src = webviewerUrl
  87. iframe.frameBorder = 0
  88. iframe.width = "100%"
  89. iframe.height = "100%"
  90. iframe.setAttribute("allowfullscreen", true)
  91. iframe.setAttribute("webkitallowfullscreen", true)
  92. iframe.setAttribute("mozallowfullscreen", true)
  93. this.iframe = iframe
  94. this.options?.backgroundColor && iframe.setAttribute("data-bgcolor", this.options.backgroundColor),
  95. this.options?.assetPath && iframe.setAttribute("data-assetpath", encodeURIComponent(this.options.assetPath)),
  96. this.element.appendChild(iframe)
  97. this.loadListener = async function() {
  98. var $iframe = self.iframe;
  99. self.instance = $iframe.contentWindow.instance;
  100. await self.instance.initOptions(self.options)
  101. }
  102. iframe.addEventListener("load", this.loadListener)
  103. this.messageHandler = function (event) {
  104. if ("documentViewerLoaded" === event.data.type && event.data.docId === self.docId) {
  105. try {
  106. self.instance = self.iframe.contentWindow.instance
  107. } catch(e) {} finally {
  108. window.removeEventListener("message", this.messageHandler),
  109. self._documentViewerLoaded(self.iframe)
  110. }
  111. }
  112. }
  113. window.addEventListener("message", this.messageHandler)
  114. }
  115. _documentViewerLoaded (iframe) {
  116. this._trigger("ready")
  117. if (this.options.pdfUrl) {
  118. this.iframe.contentWindow.instances.UI.loadDocument(this.options.pdfUrl)
  119. }
  120. }
  121. }
  122. export default ComPDFKitViewer