-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathsuperdoc.js
More file actions
72 lines (62 loc) · 2 KB
/
superdoc.js
File metadata and controls
72 lines (62 loc) · 2 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use client';
import { useEffect, useRef, useCallback } from 'react';
import './superdoc.css';
import '@harbour-enterprises/superdoc/style.css';
export default function SuperDocEditor() {
const superdocContainerRef = useRef(null);
const superdoc = useRef(null);
const editor = useRef(null);
const onReady = () => {
editor.current = superdoc.current.activeEditor;
console.log('SuperDoc is ready');
};
const initSuperDoc = async (fileToLoad = null) => {
const { SuperDoc } = await import('@harbour-enterprises/superdoc');
superdoc.current = new SuperDoc({
selector: superdocContainerRef.current,
modules: {
toolbar: {
selector: '#toolbar',
toolbarGroups: ['center'],
},
},
document: fileToLoad ? { data: fileToLoad } : '/sample-document.docx',
pagination: true,
rulers: true,
onReady,
onEditorCreate: (event) => {
console.log('Editor is created', event);
},
});
};
useEffect(() => {
initSuperDoc();
}, []);
const handleImport = useCallback(async () => {
if (!superdocContainerRef.current) return;
const [fileHandle] = await window.showOpenFilePicker({
types: [{
description: 'Word document',
accept: { 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['.docx'] }
}]
});
const file = await fileHandle.getFile();
initSuperDoc(file);
}, []);
const handleExport = useCallback(async () => {
console.debug('Exporting document', superdoc.current);
superdoc.current.export();
});
return (
<div className="example-container">
<div id="toolbar" />
<div className="editor-and-button">
<div id="superdoc" ref={superdocContainerRef} />
<div className="editor-buttons">
<button className="custom-button" onClick={handleImport}>Import</button>
<button className="custom-button" onClick={handleExport}>Export</button>
</div>
</div>
</div>
);
}