-
Notifications
You must be signed in to change notification settings - Fork 92
/
index.js
47 lines (39 loc) · 1.31 KB
/
index.js
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
46
47
import fs from 'fs';
import path from 'path';
import { createElement } from '../utils/createElement';
import WordRenderer from '../reconciler/';
// Renders the input component
async function render(element, filePath) {
// Create root container instance
const container = createElement('ROOT');
// Returns the current fiber (flushed fiber)
const node = WordRenderer.createContainer(container);
// Schedules a top level update with current fiber and a priority level (depending upon the context)
WordRenderer.updateContainer(element, node, null);
// WordRenderer.injectIntoDevTools({
// bundleType: 1,
// version: '0.1.0',
// rendererPackageName: 'custom-renderer',
// findHostInstanceByFiber: WordRenderer.findHostInstance
// })
// Officegen generates a output stream and not a file
const stream = fs.createWriteStream(filePath);
await new Promise((resolve, reject) => {
// Generate a word document
container.doc.generate(stream, Events(filePath, resolve, reject));
});
}
// Handle docx events (optional)
function Events(filePath, resolve, reject) {
return {
finalize: () => {
console.log(`✨ Word document created at ${path.resolve(filePath)}.`);
resolve();
},
error: () => {
console.log('An error occurred while generating the document.');
reject();
},
};
}
export default render;