From a249203048807bef1631330addb179985b02e516 Mon Sep 17 00:00:00 2001 From: Paul Tirk Date: Thu, 3 Oct 2024 22:11:40 +0200 Subject: [PATCH] add button for exporting data --- i18n/en.json | 6 ++++ src/plugins/ImportExportPlugin.ts | 52 +++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/i18n/en.json b/i18n/en.json index 839e67c..a67038b 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -50,6 +50,12 @@ "name": "Name" } }, + "plugins": { + "importExportPlugin": { + "export": "Export", + "label": "Import / Export" + } + }, "routes": { "label": "Label", "settings": "Settings", diff --git a/src/plugins/ImportExportPlugin.ts b/src/plugins/ImportExportPlugin.ts index 9fadc83..4f7b95b 100644 --- a/src/plugins/ImportExportPlugin.ts +++ b/src/plugins/ImportExportPlugin.ts @@ -1,11 +1,59 @@ +import { useStorageManager } from '../stores/StorageManager'; +import { SettingInputType } from '../types/SettingsTypes'; import { Plugin } from './Plugin'; +import type { PluginInitOptions } from './PluginManager'; export class ImportExportPlugin extends Plugin { public getPluginId(): string { - return 'import-export'; + return 'importExportPlugin'; } - public async init(): Promise { + public async init({ registerSettings }: PluginInitOptions): Promise { + registerSettings([ + { + name: this.getPluginId(), + labelTk: 'plugins.importExportPlugin.label', + settings: { + importExportButtons: { + name: 'importExportButtons', + type: SettingInputType.INPUT_GROUP, + children: [ + { + name: 'export', + labelTk: 'plugins.importExportPlugin.export', + type: SettingInputType.BUTTON, + handler: async (): Promise => { + const storageManager = useStorageManager(); + + const entities = await storageManager.exportData(); + + this.downloadFile( + JSON.stringify(entities, undefined, 2), + 'export.json' + ); + } + } + ] + } + } + } + ]); return Promise.resolve(); } + + private downloadFile(content: string, fileName: string): void { + const element = document.createElement('a'); + element.setAttribute( + 'href', + 'data:text/plain;charset=utf-8,' + encodeURIComponent(content) + ); + element.setAttribute('download', fileName); + + element.style.display = 'none'; + document.body.appendChild(element); + + element.click(); + + document.body.removeChild(element); + } }