Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit 7e2a03d

Browse files
authored
feat: enhance exportTemplate functionality with configurable options (#17)
This commit updates the exportTemplate method to support a new ExportConfig interface, allowing users to specify a custom filename and control whether the download is triggered automatically or if the template is returned as a Blob for manual handling. Additionally, the README has been updated to reflect these changes, providing clear examples for both download and Blob modes.
1 parent f752754 commit 7e2a03d

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ examples/**/yarn.lock
2525

2626
# Dev
2727
dev/**
28+
CLAUDE.md

README.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,77 @@ function TemplateEditor() {
206206
207207
## Export Template
208208
209-
Get the complete template data for saving:
209+
The `exportTemplate` method supports two modes of operation via the `ExportConfig` interface:
210+
211+
### 1. Download Mode (Default)
212+
213+
Automatically downloads the template as a file in the browser:
214+
215+
```jsx
216+
const handleDownload = async () => {
217+
// Download with default filename "document.docx"
218+
await ref.current?.exportTemplate();
219+
220+
// Or with custom filename
221+
await ref.current?.exportTemplate({
222+
fileName: 'invoice-template.docx'
223+
});
224+
};
225+
```
226+
227+
### 2. Blob Mode (for Database/API)
228+
229+
Get the template as a Blob for saving to your database or API:
210230
211231
```jsx
212232
const handleSave = async () => {
213-
await ref.current?.exportTemplate({ fileName: 'invoice.docx' });
233+
// Get the blob without triggering download
234+
const blob = await ref.current?.exportTemplate({
235+
fileName: 'invoice-template.docx',
236+
triggerDownload: false
237+
});
238+
239+
if (blob) {
240+
// Send to your API/database
241+
const formData = new FormData();
242+
formData.append('template', blob, 'invoice-template.docx');
243+
244+
await fetch('/api/templates', {
245+
method: 'POST',
246+
body: formData
247+
});
248+
}
214249
};
215250
```
216251
252+
### ExportConfig Interface
253+
254+
```typescript
255+
interface ExportConfig {
256+
fileName?: string; // Default: "document"
257+
triggerDownload?: boolean; // Default: true
258+
}
259+
260+
// Method signature
261+
exportTemplate(config?: ExportConfig): Promise<void | Blob>
262+
```
263+
264+
**Return value:**
265+
- `Promise<void>` when `triggerDownload: true` (download happens automatically)
266+
- `Promise<Blob>` when `triggerDownload: false` (returns the docx data)
267+
217268
## TypeScript
218269
219270
Full TypeScript support included:
220271
221272
```typescript
222273
import SuperDocTemplateBuilder from '@superdoc-dev/template-builder';
223-
import type {
274+
import type {
224275
TemplateField,
225276
FieldDefinition,
226277
TriggerEvent,
227-
SuperDocTemplateBuilderHandle
278+
ExportConfig,
279+
SuperDocTemplateBuilderHandle
228280
} from '@superdoc-dev/template-builder';
229281

230282
const ref = useRef<SuperDocTemplateBuilderHandle>(null);

src/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,17 @@ const SuperDocTemplateBuilder = forwardRef<
588588
}, [templateFields, selectedFieldId, selectField]);
589589

590590
const exportTemplate = useCallback(
591-
async (options?: { fileName?: string }): Promise<void> => {
591+
async (config?: Types.ExportConfig): Promise<void | Blob> => {
592+
const { fileName = "document", triggerDownload = true } = config || {};
593+
592594
try {
593-
await superdocRef.current?.export({
595+
const result = await superdocRef.current?.export({
594596
exportType: ["docx"],
595-
exportedName: options?.fileName ? options?.fileName : "document",
597+
exportedName: fileName,
598+
triggerDownload,
596599
});
600+
601+
return result;
597602
} catch (error) {
598603
console.error("Failed to export DOCX", error);
599604
throw error;

src/types.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ export interface ToolbarConfig {
7676
icons?: Record<string, any>;
7777
}
7878

79+
/**
80+
* Configuration options for exporting templates
81+
*/
82+
export interface ExportConfig {
83+
/**
84+
* The name of the exported file (without extension)
85+
* @default "document"
86+
*/
87+
fileName?: string;
88+
/**
89+
* Whether to trigger an automatic download in the browser
90+
* - true: Automatically downloads the file
91+
* - false: Returns the Blob data for manual handling (e.g., saving to database)
92+
* @default true
93+
*/
94+
triggerDownload?: boolean;
95+
}
96+
7997
export interface SuperDocTemplateBuilderProps {
8098
document?: DocumentConfig;
8199
fields?: FieldsConfig;
@@ -115,5 +133,5 @@ export interface SuperDocTemplateBuilderHandle {
115133
nextField: () => void;
116134
previousField: () => void;
117135
getFields: () => TemplateField[];
118-
exportTemplate: (options?: { fileName?: string }) => Promise<void>;
136+
exportTemplate: (config?: ExportConfig) => Promise<void | Blob>;
119137
}

0 commit comments

Comments
 (0)