Skip to content

Commit

Permalink
new: use mammoth to render docx
Browse files Browse the repository at this point in the history
  • Loading branch information
imzlh committed Oct 5, 2024
1 parent 241a3a1 commit 5d02c5a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,27 @@
"build-only": "vite build",
"type-check": "vue-tsc --build --force"
},
"dependencies": {},
"devDependencies": {
"@types/node": "^20.14.11",
"cheap": "https://github.com/zhaohappy/cheap.git",
"libmedia-common": "https://github.com/zhaohappy/common.git",
"npm-run-all2": "^6.1.2",
"@muyajs/core": "^0.0.32",
"@petamoriken/float16": "^3.8.7",
"@tldraw/assets": "^2.4.6",
"@types/ini": "^4.1.1",
"@types/node": "^20.14.11",
"@vitejs/plugin-vue": "^5.0.4",
"@vue/tsconfig": "^0.5.1",
"@webtoon/psd": "^0.4.0",
"artplayer": "^5.1.5",
"asciinema-player": "^3.8.0",
"assjs": "^0.1.0",
"cheap": "https://github.com/zhaohappy/cheap.git",
"ini": "^5.0.0",
"jssha": "^3.3.1",
"libmedia": "https://github.com/zhaohappy/libmedia.git",
"libmedia-common": "https://github.com/zhaohappy/common.git",
"lrc-kit": "^1.1.1",
"mammoth": "^1.8.0",
"monaco-editor": "^0.50.0",
"npm-run-all2": "^6.1.2",
"react-filerobot-image-editor": "^4.8.1",
"sass": "^1.77.4",
"tldraw": "^2.4.6",
Expand All @@ -59,8 +60,9 @@
"vite-plugin-pwa": "^0.20.0",
"vue": "^3.4.21",
"vue-reader": "^1.2.15",
"vue-tsc": "^2.0.11",
"@types/ini": "^4.1.1"
"vue-tsc": "^2.0.11"
},
"files": ["dist/**"]
"files": [
"dist/**"
]
}
Binary file added public/app/word.webp
Binary file not shown.
20 changes: 20 additions & 0 deletions src/opener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import I_MEDIA from '/app/video.webp';
import I_PS from '/app/ps.webp';
import I_ASCIINEMA from '/app/asciinema.svg';
import I_NOTES from '/app/notes.webp';
import I_WORD from '/app/word.webp';

export const OPENER:Array<OpenerOption> = [
// Monaco-Editor(VsCode)
Expand Down Expand Up @@ -76,6 +77,25 @@ export const OPENER:Array<OpenerOption> = [
});
},
},
// mammoth.js
// @link https://github.com/mwilliamson/mammoth.js
{
"name": "Docx Viewer",
"type": "text/docx",
"typeDesc": "在线读取docx文档,基于mammoth.js",
"icon": I_WORD,
"format": [
"docx"
],
async open(file) {
createWindow({
"content": (await import('@/opener/docx.vue')).default,
"icon": I_WORD,
"name": file.name + " - Docx Viewer",
"option": file
});
},
},
// asciinema
// @link https://asciinema.org/
{
Expand Down
50 changes: 50 additions & 0 deletions src/opener/docx.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<script lang="ts" setup>
import type { vFile } from '@/env';
import { convertToHtml, images } from 'mammoth';
import { onUnmounted, type PropType } from 'vue';
const { option: file } = defineProps({
option: {
type: Object as PropType<vFile>,
required: true
}
}),
imgtmp: Array<string> = [],
data = await (await fetch(file.url)).arrayBuffer(),
html = await convertToHtml({ arrayBuffer: data }, {
convertImage: images.imgElement(async img => {
const data = new Blob([await img.readAsArrayBuffer()]),
url = URL.createObjectURL(data);
imgtmp.push(url);
return { altText: img.contentType, src: url };
}),
ignoreEmptyParagraphs: false,
includeEmbeddedStyleMap: true,
});
onUnmounted(() => imgtmp.forEach(url => URL.revokeObjectURL(url)));
</script>

<template>

<div class="wrapper">
<div class="document" v-html="html.value" />
</div>
</template>

<style lang="scss" scoped>
.wrapper {
width: 100%;
height: 100%;
overflow: auto;
> .document {
width: 80%;
margin: 6rem auto;
padding: 2rem;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border: solid .1rem #ccc;
}
}
</style>
5 changes: 3 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ export default defineConfig({
'/vscode',
'/epub',
'/psd',
'/whiteboard'
'/whiteboard',
'/docx'
].some(item => id.includes(item)))
) return 'main';
// monaco
Expand All @@ -238,7 +239,7 @@ export default defineConfig({
if(id.includes('/asciinema'))
return 'asciinema';
// additional pack
if(id.includes('/psd') || id.includes('/artplayer') || id.includes('/epub.vue') || id.includes('vue-reader'))
if(id.includes('/psd') || id.includes('/artplayer') || id.includes('/epub.vue') || id.includes('vue-reader') || id.includes('docx'))
return 'additional';
},
},
Expand Down

0 comments on commit 5d02c5a

Please sign in to comment.