@work-zhanguo/light-file-preview 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +158 -74
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,68 +1,91 @@
|
|
|
1
1
|
# @work-zhanguo/light-file-preview
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
3
|
+
Gitee: [https://gitee.com/hou_zhan_guo/light-file-preview](https://gitee.com/hou_zhan_guo/light-file-preview)
|
|
4
|
+
npm:[https://www.npmjs.com/package/@work-zhanguo/light-file-preview](https://www.npmjs.com/package/@work-zhanguo/light-file-preview)
|
|
5
|
+
|
|
6
|
+
## 项目简介
|
|
7
|
+
|
|
8
|
+
用于业务系统里的附件预览,支持传入文件路径、远程地址和本地文件,接入方式尽量保持简单。
|
|
9
|
+
|
|
10
|
+
## 支持文件
|
|
11
|
+
|
|
12
|
+
支持类型:
|
|
13
|
+
|
|
14
|
+
- `PNG`
|
|
15
|
+
- `JPG`
|
|
16
|
+
- `JPEG`
|
|
17
|
+
- `GIF`
|
|
18
|
+
- `WEBP`
|
|
19
|
+
- `PDF`
|
|
20
|
+
- `TXT`
|
|
21
|
+
- `JSON`
|
|
22
|
+
- `JS`
|
|
23
|
+
- `HTML`
|
|
24
|
+
- `CSS`
|
|
25
|
+
- `MD`
|
|
26
|
+
- `DOCX`
|
|
27
|
+
- `XLS`
|
|
28
|
+
- `XLSX`
|
|
29
|
+
- `CSV`
|
|
30
|
+
- `MP4`
|
|
31
|
+
- `WEBM`
|
|
32
|
+
- `MP3`
|
|
33
|
+
- `WAV`
|
|
34
|
+
|
|
35
|
+
补充说明:
|
|
36
|
+
|
|
37
|
+
- 支持远程 `URL`、本地 `File`、`Blob`
|
|
38
|
+
- 支持新页面预览和弹窗预览
|
|
39
|
+
- 不支持的格式不强行预览,统一保留下载入口
|
|
40
|
+
|
|
41
|
+
## 项目依赖
|
|
42
|
+
|
|
43
|
+
- [pdf.js](https://github.com/mozilla/pdf.js)
|
|
44
|
+
- [docx-preview](https://github.com/VolodymyrBaydalka/docxjs)
|
|
45
|
+
- [SheetJS](https://github.com/SheetJS/sheetjs)
|
|
46
|
+
- [marked](https://github.com/markedjs/marked)
|
|
47
|
+
- [DOMPurify](https://github.com/cure53/DOMPurify)
|
|
26
48
|
|
|
27
49
|
## 安装
|
|
28
50
|
|
|
51
|
+
安装组件:
|
|
52
|
+
|
|
29
53
|
```bash
|
|
30
54
|
npm install @work-zhanguo/light-file-preview
|
|
31
55
|
```
|
|
32
56
|
|
|
33
|
-
|
|
57
|
+
如果是 `Vue3` 项目,注册入口:
|
|
34
58
|
|
|
35
59
|
```ts
|
|
36
|
-
import { createApp } from 'vue';
|
|
37
|
-
import App from './App.vue';
|
|
38
60
|
import LightFilePreview from '@work-zhanguo/light-file-preview';
|
|
39
61
|
import '@work-zhanguo/light-file-preview/style.css';
|
|
40
|
-
|
|
41
|
-
createApp(App).use(LightFilePreview).mount('#app');
|
|
42
62
|
```
|
|
43
63
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
64
|
+
如果是 `Vue2` 项目,注册入口:
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import LightFilePreview from '@work-zhanguo/light-file-preview/vue2';
|
|
68
|
+
import '@work-zhanguo/light-file-preview/style.css';
|
|
48
69
|
```
|
|
49
70
|
|
|
50
|
-
##
|
|
71
|
+
## 远程地址预览
|
|
51
72
|
|
|
52
|
-
|
|
53
|
-
<script setup lang="ts">
|
|
54
|
-
import { FilePreview } from '@work-zhanguo/light-file-preview';
|
|
55
|
-
import '@work-zhanguo/light-file-preview/style.css';
|
|
56
|
-
</script>
|
|
73
|
+
通过 `window.open` 传入网络地址,直接打开新页面预览。建议同时传 `name`,这样扩展名识别更稳定。
|
|
57
74
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
75
|
+
```js
|
|
76
|
+
const remoteUrl = 'https://example.com/files/test.docx';
|
|
77
|
+
const previewUrl =
|
|
78
|
+
'/?preview=1&src=' +
|
|
79
|
+
encodeURIComponent(remoteUrl) +
|
|
80
|
+
'&name=' +
|
|
81
|
+
encodeURIComponent('test.docx');
|
|
82
|
+
|
|
83
|
+
window.open(previewUrl, '_blank');
|
|
61
84
|
```
|
|
62
85
|
|
|
63
|
-
##
|
|
86
|
+
## 原生项目集成
|
|
64
87
|
|
|
65
|
-
|
|
88
|
+
非 Vue 项目可以直接使用 standalone 产物。
|
|
66
89
|
|
|
67
90
|
```html
|
|
68
91
|
<link rel="stylesheet" href="/vendor/light-file-preview/style.css" />
|
|
@@ -70,53 +93,114 @@ import '@work-zhanguo/light-file-preview/style.css';
|
|
|
70
93
|
<script src="/vendor/light-file-preview/light-file-preview.iife.js"></script>
|
|
71
94
|
<script>
|
|
72
95
|
window.LightFilePreview.mount('#preview-root', {
|
|
73
|
-
source: '/uploads
|
|
96
|
+
source: '/uploads/report.xlsx'
|
|
74
97
|
});
|
|
75
98
|
</script>
|
|
76
99
|
```
|
|
77
100
|
|
|
78
|
-
|
|
101
|
+
## Odoo 集成
|
|
79
102
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
103
|
+
Odoo 项目推荐走静态资源加 Owl 组件挂载。
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
/** @odoo-module **/
|
|
107
|
+
|
|
108
|
+
import { Component, onMounted, useRef } from '@odoo/owl';
|
|
109
|
+
|
|
110
|
+
export class FilePreviewBlock extends Component {
|
|
111
|
+
setup() {
|
|
112
|
+
this.rootRef = useRef('root');
|
|
113
|
+
|
|
114
|
+
onMounted(() => {
|
|
115
|
+
window.LightFilePreview.mount(this.rootRef.el, {
|
|
116
|
+
source: this.props.source,
|
|
117
|
+
fileName: this.props.fileName || 'report.pdf'
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
FilePreviewBlock.template = 'your_module.FilePreviewBlock';
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```xml
|
|
127
|
+
<templates xml:space="preserve">
|
|
128
|
+
<t t-name="your_module.FilePreviewBlock">
|
|
129
|
+
<div class="o_file_preview_root" t-ref="root" />
|
|
130
|
+
</t>
|
|
131
|
+
</templates>
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Vue2 集成
|
|
135
|
+
|
|
136
|
+
通过单独适配入口接入:
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
import Vue from 'vue';
|
|
140
|
+
import LightFilePreview from '@work-zhanguo/light-file-preview/vue2';
|
|
141
|
+
import '@work-zhanguo/light-file-preview/style.css';
|
|
142
|
+
|
|
143
|
+
Vue.use(LightFilePreview);
|
|
86
144
|
```
|
|
87
145
|
|
|
88
|
-
|
|
146
|
+
```vue
|
|
147
|
+
<template>
|
|
148
|
+
<light-file-preview :source="fileUrl" />
|
|
149
|
+
</template>
|
|
150
|
+
```
|
|
89
151
|
|
|
90
|
-
|
|
91
|
-
| --- | --- | --- | --- |
|
|
92
|
-
| `source` | `string \| File \| Blob` | - | 文件路径或二进制对象 |
|
|
93
|
-
| `fileName` | `string` | - | 自定义文件名 |
|
|
94
|
-
| `mode` | `'inline' \| 'dialog'` | `inline` | 预览模式 |
|
|
95
|
-
| `visible` | `boolean` | `true` | 弹窗是否显示 |
|
|
96
|
-
| `loadingText` | `string` | `文件加载中...` | 加载提示 |
|
|
97
|
-
| `textEncoding` | `string` | `utf-8` | 文本解码编码 |
|
|
98
|
-
| `maxTextBytes` | `number` | `2097152` | 文本最多读取字节数 |
|
|
99
|
-
| `maxSheetRows` | `number` | `200` | 表格最多展示行数 |
|
|
100
|
-
| `maxSheetCols` | `number` | `50` | 表格最多展示列数 |
|
|
101
|
-
| `pdfScale` | `number` | `1.35` | PDF 渲染缩放 |
|
|
102
|
-
| `showToolbar` | `boolean` | `true` | 是否显示工具栏 |
|
|
152
|
+
## Vue3 集成
|
|
103
153
|
|
|
104
|
-
|
|
154
|
+
```ts
|
|
155
|
+
import { createApp } from 'vue';
|
|
156
|
+
import App from './App.vue';
|
|
157
|
+
import LightFilePreview from '@work-zhanguo/light-file-preview';
|
|
158
|
+
import '@work-zhanguo/light-file-preview/style.css';
|
|
105
159
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
- 大文件文本预览默认只截取前 2MB,避免阻塞页面
|
|
109
|
-
- `.html` 默认按文本展示,不直接执行内容,降低安全风险
|
|
160
|
+
createApp(App).use(LightFilePreview).mount('#app');
|
|
161
|
+
```
|
|
110
162
|
|
|
111
|
-
|
|
163
|
+
```vue
|
|
164
|
+
<template>
|
|
165
|
+
<LightFilePreview source="/files/demo.pdf" />
|
|
166
|
+
</template>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## 部署说明
|
|
170
|
+
|
|
171
|
+
直接复制打包产物:
|
|
112
172
|
|
|
113
173
|
```bash
|
|
114
174
|
npm install
|
|
115
|
-
npm run
|
|
175
|
+
npm run build
|
|
176
|
+
|
|
177
|
+
# 组件库产物
|
|
178
|
+
dist/
|
|
179
|
+
|
|
180
|
+
# 可复制到任意项目的独立产物
|
|
181
|
+
dist/standalone/
|
|
116
182
|
```
|
|
117
183
|
|
|
118
|
-
|
|
184
|
+
如果要把首页演示页和文档页一起部署成静态站点,执行:
|
|
119
185
|
|
|
120
186
|
```bash
|
|
121
|
-
npm run build
|
|
187
|
+
npm run build:site
|
|
122
188
|
```
|
|
189
|
+
|
|
190
|
+
站点部署目录:
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
dist-site/
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
其中会包含:
|
|
197
|
+
|
|
198
|
+
- `dist-site/index.html`
|
|
199
|
+
- `dist-site/docs.html`
|
|
200
|
+
|
|
201
|
+
## 接入说明
|
|
202
|
+
|
|
203
|
+
- 本地上传文件只在当前浏览器中预览,不会保存到服务器
|
|
204
|
+
- 如果是远程文件预览,需要目标地址允许浏览器访问和跨域读取
|
|
205
|
+
- 弹窗预览会挂到 `body`
|
|
206
|
+
- 不支持的文件格式会保留下载入口
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@work-zhanguo/light-file-preview",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "轻量级通用文件预览组件,支持 Vue3、Vue2 适配与独立产物嵌入。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/light-file-preview.umd.cjs",
|
|
@@ -28,8 +28,10 @@
|
|
|
28
28
|
"scripts": {
|
|
29
29
|
"dev": "vite",
|
|
30
30
|
"build": "vite build && vite build --config vite.vue2.config.ts && vite build --config vite.standalone.config.ts && npm run build:types",
|
|
31
|
+
"build:site": "vite build --config vite.site.config.ts",
|
|
31
32
|
"build:types": "vue-tsc -p tsconfig.json --declaration --emitDeclarationOnly --outDir dist/types",
|
|
32
|
-
"preview": "vite preview"
|
|
33
|
+
"preview": "vite preview",
|
|
34
|
+
"preview:site": "vite preview --config vite.site.config.ts --outDir dist-site"
|
|
33
35
|
},
|
|
34
36
|
"keywords": [
|
|
35
37
|
"vue3",
|