webviewer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const ComPDFKitViewer = {
  2. init (options, element, optionsUrl) {
  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, optionsUrl)
  16. })
  17. }
  18. }
  19. const docViewer = new Map()
  20. class Viewer {
  21. constructor (options, element, optionsUrl) {
  22. if (docViewer.get(element)) return
  23. docViewer.set(element, true)
  24. this.instance = null
  25. this.options = options
  26. this.initialDoc = options.pdfUrl || null
  27. this.element = element
  28. this.optionsUrl = optionsUrl
  29. element.addEventListener("ready", (function ready() {
  30. element.removeEventListener("ready", ready)
  31. }))
  32. this.create()
  33. }
  34. _createEvent (e, t) {
  35. let n;
  36. try {
  37. n = new CustomEvent(e, {
  38. detail: t,
  39. bubbles: true,
  40. cancelable: true
  41. })
  42. } catch (o) {
  43. (n = document.createEvent("Event")).initEvent(e, true, true),
  44. n.detail = t
  45. }
  46. return n
  47. }
  48. create() {
  49. if (this.initialDoc) {
  50. this.initialDoc = encodeURIComponent(this.initialDoc)
  51. this._create()
  52. } else {
  53. this._create()
  54. }
  55. }
  56. _create () {
  57. if (!this._trigger) {
  58. this._trigger = function(e, t) {
  59. var n = this._createEvent(e, t);
  60. this.element.dispatchEvent(n)
  61. }
  62. }
  63. this.createViewer()
  64. }
  65. createViewer () {
  66. const self = this
  67. let webviewerUrl = './webviewer/index.html'
  68. if (this.initialDoc) {
  69. webviewerUrl += "#d=\"".concat(decodeURIComponent(this.initialDoc)).concat("\"")
  70. }
  71. const iframe = document.createElement("iframe")
  72. iframe.id = "webviewer-".concat(this.id)
  73. iframe.src = webviewerUrl
  74. iframe.title = "webviewer"
  75. iframe.frameBorder = 0
  76. iframe.width = "100%"
  77. iframe.height = "100%"
  78. iframe.setAttribute("allowfullscreen", true)
  79. iframe.setAttribute("webkitallowfullscreen", true)
  80. iframe.setAttribute("mozallowfullscreen", true)
  81. this.iframe = iframe
  82. this.options?.backgroundColor && iframe.setAttribute("data-bgcolor", this.options.backgroundColor),
  83. this.options?.assetPath && iframe.setAttribute("data-assetpath", encodeURIComponent(this.options.assetPath)),
  84. this.loadListener = function() {
  85. var $iframe = self.iframe;
  86. self.instance = $iframe.contentWindow.instance;
  87. self.instance.initOptions(self.options)
  88. self._trigger("ready");
  89. },
  90. iframe.addEventListener("load", this.loadListener),
  91. this.element.appendChild(iframe)
  92. }
  93. }
  94. export default ComPDFKitViewer