Skip to content

Commit d8ca686

Browse files
committed
feat: 迁移项目
0 parents  commit d8ca686

11 files changed

+8302
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.DS_Store
2+
node_modules
3+
bower_components
4+
.sass-cache
5+
npm-debug.log
6+
.idea
7+
.vscode

README.md

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
## Word-file-transform
2+
3+
### 一、简介
4+
5+
本项目是在开发**富文本编辑器项目**中的一个需求项,结合团队大佬的经验分享,整理成这个项目。
6+
7+
Word-file-transform 是基于 [mammoth.js](https://github.com/mwilliamson/mammoth.js) 插件进行二次开发。
8+
9+
目的在于实现通过上传并解析 Word 文件,并将 Word 文件中的各种格式的图片上传到指定接口。目前支持绝大部分格式的图片,如 **.png****.jpg****.jpeg****.gif**等,目前已知不支持 **.wmf****.emf**格式文件。
10+
11+
目前本项目支持 Word 文件转 HTML 字符串输出,如果需要转成 Markdow 或 其他格式,可以参考[mammoth.js](https://github.com/mwilliamson/mammoth.js) 插件 API 进行开发。可以将解析规则在 `/src/config/transformFn.js` 中拓展。
12+
13+
### 二、快速上手
14+
15+
这里以 React + AntDesign 项目为例:
16+
17+
1. 引入 `WordFileTransform`
18+
19+
暂时还没提交 npmjs,所以先从 dist 目录拷贝并引入。
20+
21+
```js
22+
import WordFileTransform from '@util/common/word-file-transform.umd.js';
23+
```
24+
25+
2. 配置需要使用的参数
26+
27+
在 AntDesign 中,建议在 `Upload` 组件的 `beforeUpload` 方法中使用,具体参数可查看后面**参数列表**介绍。
28+
29+
```js
30+
export default class ImportDocsComponent extends React.Component {
31+
// ...
32+
beforeUpload(file, fileList) {
33+
return new Promise(async (resolve, reject) => {
34+
const _this = this;
35+
const transformSuccessFn = function (data) {
36+
const {title, content } = data;
37+
// 插入富文本操作
38+
message.success('文档加载成功!', 2);
39+
};
40+
const transformErrorFn = function(err){
41+
message.error(err.msg);
42+
};
43+
const imageTransformErrorFn = function(err){
44+
message.error(err.msg);
45+
};
46+
const imageUrlTransformFn = res => res.data.url;
47+
const imageUploadUrl = "https://www.pingan8787.com";
48+
const userToken = await UTIL.getUserToken();
49+
const props = {
50+
file,
51+
config: {
52+
imageTransform: {
53+
imageUrlTransformFn,
54+
imageUploadUrl,
55+
imageTransformErrorFn,
56+
axiosPostConfig: {headers: {token:userToken}}
57+
},
58+
wordTransform:{
59+
transformSuccessFn,
60+
transformErrorFn
61+
}
62+
}
63+
};
64+
let WordTransform = new WordFileTransform(props);
65+
WordTransform.wordTransform();
66+
reject();
67+
})
68+
}
69+
// ...
70+
}
71+
```
72+
73+
74+
### 三、参数列表
75+
76+
**file**
77+
|参数|类型|说明|是否必传|默认值|返回值|
78+
|---|---|---|---|---|---|
79+
|`file`|`File`|word 文件数据(即 `file` 值)|**必传**|||
80+
81+
**config**
82+
|参数|类型|说明|是否必传|默认值|返回值|
83+
|---|---|---|---|---|---|
84+
|`convertType`|`String`|word 文件数据(即 `file` 值)|非必传|`html`||
85+
|`wordTransform`|`Object`|`wordTransform` 是一个对象,详细查看下表介绍||`html`||
86+
|`imageTransform`|`Object`|`imageTransform` 是一个对象,详细查看下表介绍||`html`||
87+
|`mammothOption`|` Object`|用于设置 mammoth 自带的 options。|非必传|||
88+
89+
**config.wordTransform**
90+
|参数|类型|说明|是否必传|默认值|返回值|
91+
|---|---|---|---|---|---|
92+
|`transformSuccessFn`|`Function`|设置 word 文件转换成功后的回调,一般可能是弹框提示,插入编辑区等操作。|非必传||`Function({title, content})`|
93+
|`transformErrorFn`|`Function`|设置 word 文件转换失败后的回调,一般可能是弹框提示等操作。|非必传||`Function({msg})`|
94+
95+
**config.imageTransform**
96+
|参数|类型|说明|是否必传|默认值|返回值|
97+
|---|---|---|---|---|---|
98+
|`imageUrlTransformFn`|`Function`|设置图片上传成功后数据格式。|**必传**||`Function(): Object`,如 `{url: 'www.demo.com'}`|
99+
|`imageTransformErrorFn`|`Function`|图片上传失败的回调方法。|非必传||`Function(errorData)`|
100+
|`imageTransformSuccessFn`|`Function`|图片上传成功的回调方法。|非必传||`Function(imageData)`|
101+
|`imageUploadFn`|`Function`|自定义图片上传方法。|非必传|||
102+
|`imageUploadUrl`|`String`|设置图片上传地址。|**必传**|||
103+
|`axiosPostConfig`|`Object`|设置上传方法的请求头信息。|非必传|||
104+
105+
参数案例如下:
106+
107+
```js
108+
const params = {
109+
file : {}, // File类型,必传,word 文件数据(即 file 值)
110+
config : {
111+
convertType?: '', // String类型,非必传,表示需要转成的目标类型,默认 html
112+
wordTransform:{ // Object类型,设置 word 文件转换的相关操作
113+
transformSuccessFn?, // Function类型,非必传,设置 word 文件转换成功后的回调,一般可能是弹框提示,插入编辑区等操作。返回值类型 {title, content}
114+
transformErrorFn?, // Function类型,非必传,设置 word 文件转换失败后的回调,一般可能是弹框提示等操作。返回值类型 {msg}
115+
},
116+
imageTransform: { // Object类型,设置 word 文件中 image 处理的相关操作
117+
imageUrlTransformFn, // Function类型,必传,设置图片上传成功后数据格式,需要转成类似 {url: 'www.demo.com'} 的格式返回
118+
imageTransformErrorFn?, // Function类型,非必传,图片上传失败的回调方法,Function(errorData)
119+
imageTransformSuccessFn?, // Function类型,非必传,图片上传成功的回调方法,Function(imageData)
120+
imageUploadFn?, // Function类型,非必传,自定义图片上传方法
121+
imageUploadUrl, // String类型,必传,设置图片上传地址
122+
axiosPostConfig?, // Object类型,非必传,设置 axios 上传方法的请求头信息,可参考 axios 参数,如{headers:{token:''}, timeout:''}
123+
},
124+
mammothOption, // Object类型,非必传,用于设置 mammoth 自带的 options
125+
}
126+
}
127+
```
128+
129+
## 四、WordFileTransform API介绍
130+
131+
* **new WordFileTransform(props)**
132+
133+
实例化转换方法。
134+
135+
* **.wordTransform()**
136+
137+
执行转换操作。
138+
139+
```js
140+
import WordFileTransform from "word-file-transform";
141+
const props = {
142+
// ... 详见参考前面使用案例
143+
}
144+
const WordTransform = new WordFileTransform(props);
145+
WordTransform.wordTransform();
146+
```
147+

dist/word-file-transform.umd.js

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)