webviewer.js 2.7 KB

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