-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathdocx-viewer.jsx
45 lines (41 loc) · 1.19 KB
/
docx-viewer.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2017 PlanGrid, Inc.
import React, { Component } from 'react';
import mammoth from 'mammoth';
import 'styles/docx.scss';
import Loading from '../loading';
export default class extends Component {
divRef = React.createRef();
componentDidMount() {
const jsonFile = new XMLHttpRequest();
jsonFile.open('GET', this.props.filePath, true);
jsonFile.send();
jsonFile.responseType = 'arraybuffer';
jsonFile.onreadystatechange = () => {
if (jsonFile.readyState === 4 && jsonFile.status === 200) {
mammoth
.convertToHtml(
{ arrayBuffer: jsonFile.response },
{ includeDefaultStyleMap: true },
)
.then(result => {
const docEl = document.createElement('div');
docEl.className = 'document-container';
docEl.innerHTML = result.value;
if (this.divRef.current)
this.divRef.current.innerHTML = docEl.outerHTML;
})
.catch(a => {
console.log('alexei: something went wrong', a);
})
.done();
}
};
}
render() {
return (
<div ref={this.divRef}>
<Loading />
</div>
);
}
}