|
1 | | -import React, { useState, useRef, useCallback, useMemo } from 'react'; |
2 | | -import SuperDocTemplateBuilder from '@superdoc-dev/template-builder'; |
| 1 | +import React, { useState, useRef, useCallback, useMemo } from "react"; |
| 2 | +import SuperDocTemplateBuilder from "@superdoc-dev/template-builder"; |
3 | 3 | import type { |
4 | 4 | SuperDocTemplateBuilderHandle, |
5 | 5 | TemplateField, |
6 | | - FieldDefinition |
7 | | -} from '@superdoc-dev/template-builder'; |
8 | | -import 'superdoc/dist/style.css'; |
9 | | -import './App.css'; |
| 6 | + FieldDefinition, |
| 7 | +} from "@superdoc-dev/template-builder"; |
| 8 | +import "superdoc/dist/style.css"; |
| 9 | +import "./App.css"; |
10 | 10 |
|
11 | 11 | const availableFields: FieldDefinition[] = [ |
12 | 12 | // Contact Information |
13 | | - { id: 'customer_name', label: 'Customer Name', category: 'Contact' }, |
14 | | - { id: 'customer_email', label: 'Customer Email', category: 'Contact' }, |
15 | | - { id: 'customer_phone', label: 'Customer Phone', category: 'Contact' }, |
16 | | - { id: 'customer_address', label: 'Customer Address', category: 'Contact' }, |
| 13 | + { id: "customer_name", label: "Customer Name", category: "Contact" }, |
| 14 | + { id: "customer_email", label: "Customer Email", category: "Contact" }, |
| 15 | + { id: "customer_phone", label: "Customer Phone", category: "Contact" }, |
| 16 | + { id: "customer_address", label: "Customer Address", category: "Contact" }, |
17 | 17 |
|
18 | 18 | // Company Information |
19 | 19 | { id: 'company_name', label: 'Company Name', category: 'Company' }, |
@@ -45,8 +45,7 @@ const availableFields: FieldDefinition[] = [ |
45 | 45 | export function App() { |
46 | 46 | const [fields, setFields] = useState<TemplateField[]>([]); |
47 | 47 | const [events, setEvents] = useState<string[]>([]); |
48 | | - const [showExport, setShowExport] = useState(false); |
49 | | - const [exportData, setExportData] = useState<any>(null); |
| 48 | + const [isDownloading, setIsDownloading] = useState(false); |
50 | 49 | const builderRef = useRef<SuperDocTemplateBuilderHandle>(null); |
51 | 50 |
|
52 | 51 | const log = useCallback((msg: string) => { |
@@ -82,11 +81,25 @@ export function App() { |
82 | 81 | log('⌨ Trigger detected'); |
83 | 82 | }, [log]); |
84 | 83 |
|
85 | | - const handleExportTemplate = useCallback(() => { |
86 | | - const data = builderRef.current?.exportTemplate(); |
87 | | - setExportData(data); |
88 | | - setShowExport(true); |
89 | | - log('📤 Template exported'); |
| 84 | + const handleExportTemplate = useCallback(async () => { |
| 85 | + if (!builderRef.current) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + setIsDownloading(true); |
| 91 | + |
| 92 | + await builderRef.current.exportTemplate({ |
| 93 | + fileName: "template.docx", |
| 94 | + }); |
| 95 | + |
| 96 | + log("📤 Template exported"); |
| 97 | + } catch (error) { |
| 98 | + log("⚠️ Export failed"); |
| 99 | + console.error("Failed to export template", error); |
| 100 | + } finally { |
| 101 | + setIsDownloading(false); |
| 102 | + } |
90 | 103 | }, [log]); |
91 | 104 |
|
92 | 105 | const handleKeyDown = (e: React.KeyboardEvent) => { |
@@ -147,8 +160,12 @@ export function App() { |
147 | 160 | <span className="hint">Tab/Shift+Tab to navigate</span> |
148 | 161 | </div> |
149 | 162 | <div className="toolbar-right"> |
150 | | - <button onClick={handleExportTemplate} className="export-button"> |
151 | | - Export Template |
| 163 | + <button |
| 164 | + onClick={handleExportTemplate} |
| 165 | + className="export-button" |
| 166 | + disabled={isDownloading} |
| 167 | + > |
| 168 | + {isDownloading ? "Exporting..." : "Export Template"} |
152 | 169 | </button> |
153 | 170 | </div> |
154 | 171 | </div> |
@@ -177,27 +194,6 @@ export function App() { |
177 | 194 | </div> |
178 | 195 | )} |
179 | 196 |
|
180 | | - {/* Export Modal */} |
181 | | - {showExport && ( |
182 | | - <div className="modal-overlay" onClick={() => setShowExport(false)}> |
183 | | - <div className="modal" onClick={e => e.stopPropagation()}> |
184 | | - <h2>Exported Template</h2> |
185 | | - <div className="export-content"> |
186 | | - <h3>Fields ({exportData?.fields?.length || 0})</h3> |
187 | | - <pre>{JSON.stringify(exportData?.fields || [], null, 2)}</pre> |
188 | | - {exportData?.document && ( |
189 | | - <> |
190 | | - <h3>Document Structure</h3> |
191 | | - <pre>{JSON.stringify(exportData.document, null, 2).substring(0, 500)}...</pre> |
192 | | - </> |
193 | | - )} |
194 | | - </div> |
195 | | - <button onClick={() => setShowExport(false)} className="modal-close"> |
196 | | - Close |
197 | | - </button> |
198 | | - </div> |
199 | | - </div> |
200 | | - )} |
201 | 197 | </div> |
202 | 198 | </div> |
203 | 199 | ); |
|
0 commit comments