manual-js.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!doctype html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Flyfish Viewer Manual JS Demo</title>
  7. <style>
  8. :root {
  9. color: #172033;
  10. background: #f4f7f8;
  11. font-family: Inter, "Segoe UI", "PingFang SC", sans-serif;
  12. }
  13. * {
  14. box-sizing: border-box;
  15. }
  16. body {
  17. margin: 0;
  18. }
  19. main {
  20. display: grid;
  21. grid-template-rows: auto minmax(0, 1fr);
  22. min-height: 100vh;
  23. }
  24. header {
  25. display: flex;
  26. align-items: center;
  27. justify-content: space-between;
  28. gap: 16px;
  29. padding: 14px 18px;
  30. border-bottom: 1px solid #d8e1e8;
  31. background: #ffffff;
  32. }
  33. h1,
  34. p {
  35. margin: 0;
  36. }
  37. h1 {
  38. font-size: 18px;
  39. line-height: 1.3;
  40. }
  41. p {
  42. margin-top: 4px;
  43. color: #64748b;
  44. font-size: 13px;
  45. }
  46. code {
  47. padding: 2px 6px;
  48. border-radius: 5px;
  49. background: #edf2f7;
  50. color: #334155;
  51. }
  52. #viewer {
  53. width: 100%;
  54. height: 100%;
  55. min-height: 0;
  56. background: #ffffff;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <main>
  62. <header>
  63. <div>
  64. <h1>Manual Pure JS</h1>
  65. <p>普通页面通过原生 JS 动态加载 <code>mountViewer</code> 浏览器包。</p>
  66. </div>
  67. <p id="status">loading</p>
  68. </header>
  69. <div id="viewer" data-testid="manual-js-viewer"></div>
  70. </main>
  71. <script>
  72. var statusNode = document.getElementById('status')
  73. var host = document.getElementById('viewer')
  74. var bundleUrl = '/vendor/file-viewer-web/flyfish-file-viewer-web.iife.js'
  75. function setStatus(value) {
  76. document.body.setAttribute('data-viewer-status', value)
  77. statusNode.textContent = value
  78. }
  79. function loadViewerApi() {
  80. if (window.FlyfishFileViewerWeb) {
  81. return Promise.resolve(window.FlyfishFileViewerWeb)
  82. }
  83. return new Promise(function (resolve, reject) {
  84. var script = document.createElement('script')
  85. script.src = bundleUrl
  86. script.async = true
  87. script.onload = function () {
  88. if (window.FlyfishFileViewerWeb) {
  89. resolve(window.FlyfishFileViewerWeb)
  90. } else {
  91. reject(new Error('FlyfishFileViewerWeb global is unavailable'))
  92. }
  93. }
  94. script.onerror = function () {
  95. reject(new Error('Failed to load ' + bundleUrl))
  96. }
  97. document.head.appendChild(script)
  98. })
  99. }
  100. loadViewerApi()
  101. .then(function (api) {
  102. api.mountViewer(host, {
  103. url: '/example/word.docx',
  104. options: {
  105. theme: 'light',
  106. toolbar: {
  107. position: 'bottom-right'
  108. }
  109. },
  110. onEvent: function (event) {
  111. if (event && (event.type === 'load-start' || event.type === 'load-complete')) {
  112. setStatus(event.type)
  113. }
  114. }
  115. })
  116. setStatus('mounted')
  117. })
  118. .catch(function (error) {
  119. console.error(error)
  120. setStatus('error')
  121. })
  122. </script>
  123. </body>
  124. </html>