-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
159a6df
commit 5d00e4d
Showing
2 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> { | ||
public async init({ registerSettings }: PluginInitOptions): Promise<void> { | ||
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<void> => { | ||
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); | ||
} | ||
} |