|
| 1 | +// Imports |
| 2 | +import { SuperDoc } from '@harbour-enterprises/superdoc'; |
| 3 | +import '@harbour-enterprises/superdoc/style.css'; |
| 4 | +import './style.css'; |
| 5 | + |
| 6 | +// Init |
| 7 | +let editor = null; |
| 8 | + |
| 9 | +//Init SuperDoc from DOCX file |
| 10 | +function initSuperDocFromFile(file = null) { |
| 11 | + if (editor) { |
| 12 | + editor = null; |
| 13 | + } |
| 14 | + |
| 15 | + editor = new SuperDoc({ |
| 16 | + selector: '#superdoc', |
| 17 | + toolbar: '#superdoc-toolbar', |
| 18 | + document: file, // DOCX URL, File object, or document config |
| 19 | + documentMode: 'editing', |
| 20 | + mode: 'docx', |
| 21 | + pagination: true, |
| 22 | + rulers: true, |
| 23 | + onReady: (event) => { |
| 24 | + const docJSON = event.superdoc.activeEditor.getJSON(); |
| 25 | + console.log('SuperDoc ready - JSON', docJSON); |
| 26 | + }, |
| 27 | + onEditorUpdate: (event) => { |
| 28 | + const docJSON = event.editor.getJSON(); |
| 29 | + console.log('SuperDoc updated - JSON', docJSON); |
| 30 | + }, |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +//Init SuperDoc from JSON |
| 35 | +function initSuperDocFromJSON() { |
| 36 | + if (editor) { |
| 37 | + editor = null; |
| 38 | + } |
| 39 | + |
| 40 | + //Hardcoded demo JSON |
| 41 | + const demoJSON = { |
| 42 | + type: 'doc', |
| 43 | + content: [ |
| 44 | + { |
| 45 | + type: 'paragraph', |
| 46 | + content: [ |
| 47 | + { |
| 48 | + type: 'text', |
| 49 | + text: 'Hello, SuperDoc~!!', |
| 50 | + }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + ], |
| 54 | + }; |
| 55 | + |
| 56 | + editor = new SuperDoc({ |
| 57 | + selector: '#superdoc', |
| 58 | + toolbar: '#superdoc-toolbar', |
| 59 | + documentMode: 'editing', |
| 60 | + |
| 61 | + /* LOADING JSON */ |
| 62 | + //https://docs.superdoc.dev/core/supereditor/configuration#param-options-content |
| 63 | + mode: 'docx', |
| 64 | + jsonOverride: demoJSON, |
| 65 | + |
| 66 | + pagination: true, |
| 67 | + rulers: true, |
| 68 | + onReady: (event) => { |
| 69 | + const docJSON = event.superdoc.activeEditor.getJSON(); |
| 70 | + console.log('SuperDoc ready - JSON', docJSON); |
| 71 | + }, |
| 72 | + onEditorUpdate: (event) => { |
| 73 | + const docJSON = event.editor.getJSON(); |
| 74 | + console.log('SuperDoc updated - JSON', docJSON); |
| 75 | + }, |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +// Setup file input handling |
| 80 | +const fileInput = document.getElementById('fileInput'); |
| 81 | +const loadButton = document.getElementById('loadButton'); |
| 82 | +const loadJSON = document.getElementById('loadJSON'); |
| 83 | + |
| 84 | +loadButton.addEventListener('click', () => { |
| 85 | + fileInput.click(); |
| 86 | +}); |
| 87 | + |
| 88 | +loadJSON.addEventListener('click', () => { |
| 89 | + initSuperDocFromJSON(); |
| 90 | +}); |
| 91 | + |
| 92 | +fileInput.addEventListener('change', (event) => { |
| 93 | + const file = event.target.files?.[0]; |
| 94 | + if (file) { |
| 95 | + initSuperDocFromFile(file); |
| 96 | + } |
| 97 | +}); |
| 98 | + |
| 99 | +// Initialize empty editor on page load |
| 100 | +initSuperDocFromFile(null); |
0 commit comments