Skip to content

Commit 425887a

Browse files
committed
feat(ex/import): Settings export and import by command and menu.
1 parent 049d816 commit 425887a

File tree

5 files changed

+80
-30
lines changed

5 files changed

+80
-30
lines changed

src/langs/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
"command_startsync": "start sync the folder",
3838
"command_startsync_file": "start sync current file",
3939
"command_forcesync": "Force full synchronization",
40+
"command_import_settings": "Import settings from clipboard",
41+
"command_export_settings": "Export settings to clipboard",
4042
"command_drynrun": "start sync (dry run only)",
4143
"command_exportsyncplans_json": "export sync plans in json format",
4244
"command_exportsyncplans_table": "export sync plans in table format",

src/langs/zh_cn.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
"command_startsync": "开始同步文件夹",
3838
"command_startsync_file": "开始同步当前文件",
3939
"command_forcesync": "强制全量同步",
40+
"command_import_settings": "剪切板导入配置",
41+
"command_export_settings": "导出配置到剪贴板",
4042
"command_drynrun": "开始同步(空跑模式)",
4143
"command_exportsyncplans_json": "导出同步计划为 json 格式(export sync plans in json format)",
4244
"command_exportsyncplans_table": "导出同步计划为表格格式(export sync plans in table format)",

src/langs/zh_tw.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
"command_startsync": "开始同步文件夹",
3838
"command_startsync_file": "开始同步当前文件",
3939
"command_forcesync": "强制全量同步",
40+
"command_import_settings": "剪切板导入配置",
41+
"command_export_settings": "导出配置到剪贴板",
4042
"command_drynrun": "开始同步(空跑模式)",
4143
"command_exportsyncplans_json": "匯出同步計劃為 json 格式(export sync plans in json format)",
4244
"command_exportsyncplans_table": "匯出同步計劃為表格格式(export sync plans in table format)",

src/main.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ export default class InvioPlugin extends Plugin {
709709
.setIcon("document")
710710
})
711711

712-
if (file.path !== this.settings.localWatchDir) {
712+
if ((file.path !== this.settings.localWatchDir) && (file.path.indexOf('/') < 0) ) {
713713
menu.addItem((item) => {
714714
item
715715
.setTitle(`${Menu_Tab}Set as working folder`)
@@ -727,6 +727,14 @@ export default class InvioPlugin extends Plugin {
727727
this.syncRun("manual")
728728
});
729729
})
730+
menu.addItem((item) => {
731+
item
732+
.setTitle(`${Menu_Tab}Share This Folder`)
733+
.setIcon("document")
734+
.onClick(async () => {
735+
await InvioSettingTab.exportSettings(this)
736+
});
737+
})
730738
}
731739
menu.addSeparator();
732740
} else if (file instanceof TFile) {
@@ -886,6 +894,26 @@ export default class InvioPlugin extends Plugin {
886894
}
887895
})
888896

897+
this.addCommand({
898+
id: 'export-settings',
899+
name: t('command_export_settings'),
900+
icon: iconNameSyncLogo,
901+
callback: async () => {
902+
await InvioSettingTab.exportSettings(this);
903+
}
904+
})
905+
906+
907+
this.addCommand({
908+
id: 'import-settings',
909+
name: t('command_import_settings'),
910+
icon: iconNameSyncLogo,
911+
callback: async () => {
912+
const str = await navigator.clipboard.readText();
913+
InvioSettingTab.importSettings(this, str);
914+
}
915+
})
916+
889917
this.addSettingTab(new InvioSettingTab(this.app, this));
890918

891919
// this.registerDomEvent(document, "click", (evt: MouseEvent) => {

src/settings.ts

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
insertLoggerOutputByVault,
3333
clearExpiredLoggerOutputRecords,
3434
} from "./localdb";
35-
import type InvioPlugin from "./main"; // unavoidable
35+
import InvioPlugin from "./main"; // unavoidable
3636
import { RemoteClient } from "./remote";
3737
import { messyConfigToNormal } from "./configPersist";
3838
import type { TransItemType } from "./i18n";
@@ -46,6 +46,8 @@ import {
4646
restoreLogWritterInplace,
4747
} from "./moreOnLog";
4848

49+
const settingsPrefix = `Invio-Settings>`;
50+
const settingsSuffix = `<&`
4951
class PasswordModal extends Modal {
5052
plugin: InvioPlugin;
5153
newPassword: string;
@@ -354,6 +356,45 @@ export class InvioSettingTab extends PluginSettingTab {
354356
plugin.saveSettings();
355357
}
356358

359+
360+
static async exportSettings(plugin: InvioPlugin) {
361+
const t = (x: TransItemType, vars?: any) => {
362+
return plugin.i18n.t(x, vars);
363+
};
364+
const data = await plugin.loadData();
365+
await navigator.clipboard.writeText(`${settingsPrefix} ${data.d} ${settingsSuffix}`);
366+
new Notice(t("settings_export_msg"));
367+
}
368+
369+
static async importSettings(plugin: InvioPlugin, restoredStr: string) {
370+
const t = (x: TransItemType, vars?: any) => {
371+
return plugin.i18n.t(x, vars);
372+
};
373+
if (!restoredStr) {
374+
new Notice(t("settings_import_err"));
375+
return;
376+
}
377+
if (!(restoredStr.startsWith(settingsPrefix) && restoredStr.endsWith(settingsSuffix))) {
378+
new Notice(t("settings_import_err"));
379+
return;
380+
}
381+
382+
await plugin.loadSettings(restoredStr.replace(settingsPrefix, '').replace(settingsSuffix, '').trim());
383+
// Create dir if necessary.
384+
const dir = plugin.settings.localWatchDir;
385+
if (dir && (typeof dir === 'string')) {
386+
await mkdirpInVault(dir, plugin.app.vault);
387+
await plugin.switchWorkingDir(dir);
388+
389+
setTimeout(() => {
390+
plugin.syncRun('auto');
391+
}, 100)
392+
} else {
393+
log.error('Imported settings not configured correctly.')
394+
}
395+
await plugin.saveSettings();
396+
}
397+
357398
display(): void {
358399
let { containerEl } = this;
359400

@@ -838,18 +879,15 @@ export class InvioSettingTab extends PluginSettingTab {
838879
text: t("settings_importexport"),
839880
});
840881

841-
const settingsPrefix = `Invio-Settings>`;
842-
const settingsSuffix = `<&`
882+
843883
new Setting(importExportDiv)
844884
.setName(t("settings_export"))
845885
.setDesc(t("settings_export_desc"))
846886
.addButton(async (button) => {
847887
button.setButtonText(t("settings_export_desc_button"));
848888
button.onClick(async () => {
849889
// new ExportSettingsQrCodeModal(this.app, this.plugin).open();
850-
const data = await this.plugin.loadData();
851-
await navigator.clipboard.writeText(`${settingsPrefix} ${data.d} ${settingsSuffix}`);
852-
new Notice(t("settings_export_msg"));
890+
InvioSettingTab.exportSettings(this.plugin);
853891
});
854892
});
855893

@@ -868,29 +906,7 @@ export class InvioSettingTab extends PluginSettingTab {
868906
.addButton(async (button) => {
869907
button.setButtonText(t("settings_import_desc_button"));
870908
button.onClick(async () => {
871-
if (!restoredStr) {
872-
new Notice(t("settings_import_err"));
873-
return;
874-
}
875-
if (!(restoredStr.startsWith(settingsPrefix) && restoredStr.endsWith(settingsSuffix))) {
876-
new Notice(t("settings_import_err"));
877-
return;
878-
}
879-
880-
await this.plugin.loadSettings(restoredStr.replace(settingsPrefix, '').replace(settingsSuffix, '').trim());
881-
// Create dir if necessary.
882-
const dir = this.plugin.settings.localWatchDir;
883-
if (dir && (typeof dir === 'string')) {
884-
await mkdirpInVault(dir, this.plugin.app.vault);
885-
await this.plugin.switchWorkingDir(dir);
886-
887-
setTimeout(() => {
888-
this.plugin.syncRun('auto');
889-
}, 100)
890-
} else {
891-
log.error('Imported settings not configured correctly.')
892-
}
893-
await this.plugin.saveSettings();
909+
await InvioSettingTab.importSettings(this.plugin, restoredStr);
894910
this.hide();
895911
this.display();
896912
});

0 commit comments

Comments
 (0)