阅行客电子档案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.8 KiB

  1. import { defaultOptions, renderAsync } from 'docx-preview'
  2. import renderPptx from '@/vendors/pptx'
  3. import renderSheet from '@/vendors/xlsx'
  4. import renderOfdView from '@/vendors/ofd'
  5. import renderPdf from '@/vendors/pdf'
  6. import renderImage from '@/vendors/image'
  7. import renderText from '@/vendors/text'
  8. import renderMp4 from '@/vendors/mp4'
  9. // 假装构造一个vue的包装,让上层统一处理销毁和替换节点
  10. const VueWrapper = (el) => ({
  11. $el: el,
  12. $destroy() {
  13. // 什么也不需要 nothing to do
  14. }
  15. })
  16. const handlers = [
  17. // 使用docxjs支持,目前效果最好的渲染器
  18. {
  19. accepts: ['docx'],
  20. handler: async(buffer, target) => {
  21. console.log(defaultOptions)
  22. const docxOptions = Object.assign(defaultOptions, {
  23. debug: true,
  24. experimental: true
  25. })
  26. console.log(target)
  27. await renderAsync(buffer, target, null, docxOptions)
  28. return VueWrapper(target)
  29. }
  30. },
  31. // 使用pptx2html,已通过默认值更替
  32. {
  33. accepts: ['pptx'],
  34. handler: async(buffer, target) => {
  35. await renderPptx(buffer, target, null)
  36. window.dispatchEvent(new Event('resize'))
  37. return VueWrapper(target)
  38. }
  39. },
  40. // 使用sheetjs + handsontable,无样式
  41. {
  42. accepts: ['xlsx'],
  43. handler: async(buffer, target) => {
  44. return renderSheet(buffer, target)
  45. }
  46. },
  47. // 使用pdfjs,渲染pdf,效果最好
  48. {
  49. accepts: ['pdf'],
  50. handler: async(buffer, target) => {
  51. return renderPdf(buffer, target)
  52. }
  53. },
  54. // ofd
  55. {
  56. accepts: ['ofd'],
  57. handler: async(buffer, target) => {
  58. return renderOfdView(buffer, target)
  59. }
  60. },
  61. // 图片过滤器
  62. {
  63. accepts: ['gif', 'jpg', 'jpeg', 'bmp', 'tiff', 'tif', 'png', 'svg'],
  64. handler: async(buffer, target) => {
  65. return renderImage(buffer, target)
  66. }
  67. },
  68. // 纯文本预览
  69. {
  70. accepts: [
  71. 'txt',
  72. 'json',
  73. 'js',
  74. 'css',
  75. 'java',
  76. 'py',
  77. 'html',
  78. 'jsx',
  79. 'ts',
  80. 'tsx',
  81. 'xml',
  82. 'md',
  83. 'log'
  84. ],
  85. handler: async(buffer, target) => {
  86. return renderText(buffer, target)
  87. }
  88. },
  89. // 视频预览,仅支持MP4
  90. {
  91. accepts: ['mp4'],
  92. handler: async(buffer, target) => {
  93. renderMp4(buffer, target)
  94. return VueWrapper(target)
  95. }
  96. },
  97. // 错误处理
  98. {
  99. accepts: ['error'],
  100. handler: async(buffer, target, type) => {
  101. target.innerHTML = `<div style="text-align: center; margin-top: 80px">不支持.${type}格式的在线预览,请下载后预览或转换为支持的格式</div>
  102. <div style="text-align: center">支持docx, xlsx, pptx, pdf, ofd 以及纯文本格式和各种图片格式的在线预览</div>`
  103. return VueWrapper(target)
  104. }
  105. }
  106. ]
  107. // 匹配
  108. export default handlers.reduce((result, { accepts, handler }) => {
  109. accepts.forEach((type) => (result[type] = handler))
  110. return result
  111. }, {})