Skip to content

Commit 0f03a87

Browse files
committed
chore(doc-viewer): reintroduce mammoth
1 parent 4462419 commit 0f03a87

File tree

4 files changed

+84
-20
lines changed

4 files changed

+84
-20
lines changed

packages/pluggableWidgets/document-viewer-web/components/DocxViewer.tsx

+49-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
1-
import { createElement, Fragment } from "react";
2-
import { DocumentViewerContainerProps } from "../typings/DocumentViewerProps";
1+
import { createElement, Fragment, useCallback, useEffect, useState } from "react";
2+
import mammoth from "mammoth";
3+
import { DocumentViewerContainerProps } from "typings/DocumentViewerProps";
34
import { DocRendererElement } from "./documentRenderer";
45

5-
const DocxViewer: DocRendererElement = (_props: DocumentViewerContainerProps) => {
6-
return <Fragment>DOCX</Fragment>;
6+
const DocxViewer: DocRendererElement = (props: DocumentViewerContainerProps) => {
7+
const { file } = props;
8+
const [docxHtml, setDocxHtml] = useState<string | null>(null);
9+
10+
const loadContent = useCallback(async (arrayBuffer: any) => {
11+
try {
12+
mammoth
13+
.convertToHtml(
14+
{ arrayBuffer: arrayBuffer },
15+
{
16+
includeDefaultStyleMap: true
17+
}
18+
)
19+
.then(result => {
20+
if (result) {
21+
setDocxHtml(result.value);
22+
}
23+
});
24+
} catch (error) {}
25+
}, []);
26+
27+
useEffect(() => {
28+
const controller = new AbortController();
29+
const { signal } = controller;
30+
if (file.status === "available" && file.value.uri) {
31+
fetch(file.value.uri, { method: "GET", signal })
32+
.then(res => res.arrayBuffer())
33+
.then(response => {
34+
loadContent(response);
35+
});
36+
}
37+
38+
return () => {
39+
controller.abort();
40+
};
41+
}, [file, file?.status, file?.value?.uri]);
42+
43+
return (
44+
<Fragment>
45+
{docxHtml && (
46+
<div className="widget-document-viewer-content" dangerouslySetInnerHTML={{ __html: docxHtml }}>
47+
{/* {docHtmlStr} */}
48+
</div>
49+
)}
50+
</Fragment>
51+
);
752
};
853

954
DocxViewer.contentTypes = [

packages/pluggableWidgets/document-viewer-web/components/ErrorViewer.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { DocRendererElement } from "./documentRenderer";
44

55
const ErrorViewer: DocRendererElement = () => {
66
const props = useContext(DocumentContext);
7-
console.log("ErrorViewer", props);
87
return (
98
<Fragment>
109
<div className="widget-document-viewer-content">No document selected</div>

packages/pluggableWidgets/document-viewer-web/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@babel/plugin-transform-class-properties": "^7.23.3",
4343
"@babel/plugin-transform-private-methods": "^7.23.3",
4444
"@babel/plugin-transform-private-property-in-object": "^7.23.4",
45-
"@mendix/pluggable-widgets-tools": "^10.0.0"
45+
"@mendix/pluggable-widgets-tools": "^10.0.0",
46+
"@rollup/plugin-replace": "^6.0.2"
4647
}
4748
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import commonjs from "@rollup/plugin-commonjs";
2+
import replace from "@rollup/plugin-replace";
23

34
export default args => {
45
const result = args.configDefaultConfig;
@@ -9,21 +10,39 @@ export default args => {
910
}
1011
return {
1112
...config,
12-
plugins: config.plugins.map(plugin => {
13-
if (plugin && plugin.name === "commonjs") {
14-
// replace common js plugin that transforms
15-
// external requires to imports
16-
// this is needed in order to work with modern client
17-
return commonjs({
18-
extensions: [".js", ".jsx", ".tsx", ".ts"],
19-
transformMixedEsModules: true,
20-
requireReturnsDefault: "auto",
21-
esmExternals: true
22-
});
23-
}
13+
plugins: [
14+
...config.plugins.map(plugin => {
15+
if (plugin && plugin.name === "commonjs") {
16+
// replace common js plugin that transforms
17+
// external requires to imports
18+
// this is needed in order to work with modern client
19+
return commonjs({
20+
extensions: [".js", ".jsx", ".tsx", ".ts"],
21+
transformMixedEsModules: true,
22+
requireReturnsDefault: "auto",
23+
esmExternals: true
24+
});
25+
}
2426

25-
return plugin;
26-
})
27+
return plugin;
28+
}),
29+
// rollup config for pdfjs-dist copying from https://github.com/unjs/unpdf/blob/main/pdfjs.rollup.config.ts
30+
replace({
31+
delimiters: ["", ""],
32+
preventAssignment: true,
33+
values: {
34+
// Disable the `window` check (for requestAnimationFrame).
35+
"typeof window": '"undefined"',
36+
// Imitate the Node.js environment for all serverless environments, unenv will
37+
// take care of the remaining Node.js polyfills. Keep support for browsers.
38+
"const isNodeJS = typeof": 'const isNodeJS = typeof document === "undefined" // typeof',
39+
// Force inlining the PDF.js worker.
40+
"await import(/*webpackIgnore: true*/this.workerSrc)": "__pdfjsWorker__",
41+
// Tree-shake client worker initialization logic.
42+
"!PDFWorkerUtil.isWorkerDisabled && !PDFWorker.#mainThreadWorkerMessageHandler": "false"
43+
}
44+
})
45+
]
2746
};
2847
});
2948
};

0 commit comments

Comments
 (0)