feat: add form import/export buttons to settings tab

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Luca Oelfke 2026-02-13 15:01:48 +01:00
parent b0d66c9660
commit bad48e5f5e

View file

@ -1,6 +1,7 @@
import { App, Plugin, PluginSettingTab, Setting, Notice } from 'obsidian'; import { App, Plugin, PluginSettingTab, Setting, Notice } from 'obsidian';
import { FormStore } from '../core/form-store'; import { FormStore } from '../core/form-store';
import { FormBuilderModal } from './form-builder'; import { FormBuilderModal } from './form-builder';
import { exportForms, importForms } from '../utils/form-io';
/** /**
* Interface for the subset of FormfirePlugin that the settings tab needs. * 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 // Form list
const forms = this.pluginRef.store.getAll(); const forms = this.pluginRef.store.getAll();
@ -70,6 +120,18 @@ export class FormfireSettingTab extends PluginSettingTab {
new Setting(containerEl) new Setting(containerEl)
.setName(form.name) .setName(form.name)
.setDesc(desc) .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) => { .addButton((btn) => {
btn.setButtonText('Edit').onClick(() => { btn.setButtonText('Edit').onClick(() => {
new FormBuilderModal(this.app, form, async (saved) => { new FormBuilderModal(this.app, form, async (saved) => {