diff --git a/src/ui/settings-tab.ts b/src/ui/settings-tab.ts index 4bc9607..ce585b7 100644 --- a/src/ui/settings-tab.ts +++ b/src/ui/settings-tab.ts @@ -1,6 +1,7 @@ import { App, Plugin, PluginSettingTab, Setting, Notice } from 'obsidian'; import { FormStore } from '../core/form-store'; import { FormBuilderModal } from './form-builder'; +import { exportForms, importForms } from '../utils/form-io'; /** * Interface for the subset of FormfirePlugin that the settings tab needs. @@ -46,6 +47,55 @@ export class FormfireSettingTab extends PluginSettingTab { }); }); + // Import / Export + new Setting(containerEl) + .setName('Import / Export') + .setDesc('Share forms as JSON files.') + .addButton((btn) => { + btn.setButtonText('Export All').onClick(() => { + const forms = this.pluginRef.store.getAll(); + if (forms.length === 0) { + new Notice('No forms to export.'); + return; + } + const json = exportForms(forms); + const blob = new Blob([json], { type: 'application/json' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = 'formfire-export.json'; + a.click(); + URL.revokeObjectURL(url); + }); + }) + .addButton((btn) => { + btn.setButtonText('Import').onClick(() => { + const input = document.createElement('input'); + input.type = 'file'; + input.accept = '.json'; + input.addEventListener('change', async () => { + const file = input.files?.[0]; + if (!file) return; + try { + const text = await file.text(); + const forms = importForms(text); + for (const form of forms) { + this.pluginRef.store.add(form); + } + await this.pluginRef.saveSettings(); + this.pluginRef.refreshSidebar(); + this.display(); + new Notice(`Imported ${forms.length} form(s).`); + } catch (err) { + new Notice( + err instanceof Error ? err.message : 'Import failed.', + ); + } + }); + input.click(); + }); + }); + // Form list const forms = this.pluginRef.store.getAll(); @@ -70,6 +120,18 @@ export class FormfireSettingTab extends PluginSettingTab { new Setting(containerEl) .setName(form.name) .setDesc(desc) + .addButton((btn) => { + btn.setButtonText('Export').onClick(() => { + const json = exportForms([form]); + const blob = new Blob([json], { type: 'application/json' }); + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = `formfire-${form.name.toLowerCase().replace(/\s+/g, '-')}.json`; + a.click(); + URL.revokeObjectURL(url); + }); + }) .addButton((btn) => { btn.setButtonText('Edit').onClick(() => { new FormBuilderModal(this.app, form, async (saved) => {