Skip to content

Commit 55489c4

Browse files
committed
feat(import): Import project to local.
1 parent 30f9b8b commit 55489c4

File tree

2 files changed

+138
-4
lines changed

2 files changed

+138
-4
lines changed

src/components/ProjectImportModal.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { App, Modal, Setting, Notice } from "obsidian";
2+
import type InvioPlugin from "../main"; // unavoidable
3+
import type { TransItemType } from "../i18n";
4+
import { log } from '../moreOnLog';
5+
import { settingsPrefix, settingsSuffix } from '../settings';
6+
import { mkdirpInVault } from "../misc";
7+
8+
export class ProjectImportModal extends Modal {
9+
readonly plugin: InvioPlugin;
10+
restoredStr: string;
11+
confirmCB: any;
12+
constructor(app: App, plugin: InvioPlugin, cb?: any) {
13+
super(app);
14+
this.plugin = plugin;
15+
this.confirmCB = cb;
16+
}
17+
18+
t(x: TransItemType, vars?: any) {
19+
return this.plugin.i18n.t(x, vars);
20+
}
21+
22+
async importProject(plugin: InvioPlugin, restoredStr: string) {
23+
const t = (x: TransItemType, vars?: any) => {
24+
return plugin.i18n.t(x, vars);
25+
};
26+
if (!restoredStr) {
27+
new Notice(t("settings_import_err"));
28+
return;
29+
}
30+
if (!(restoredStr.startsWith(settingsPrefix) && restoredStr.endsWith(settingsSuffix))) {
31+
new Notice(t("settings_import_err"));
32+
return;
33+
}
34+
35+
await plugin.loadSettings(restoredStr.replace(settingsPrefix, '').replace(settingsSuffix, '').trim());
36+
// Create dir if necessary.
37+
const dir = plugin.settings.localWatchDir;
38+
if (dir && (typeof dir === 'string')) {
39+
await mkdirpInVault(dir, plugin.app.vault);
40+
await plugin.switchWorkingDir(dir);
41+
42+
setTimeout(() => {
43+
plugin.syncRun('auto');
44+
}, 100)
45+
} else {
46+
log.error('Imported settings not configured correctly.')
47+
}
48+
await plugin.saveSettings();
49+
return dir;
50+
}
51+
52+
onOpen() {
53+
let { contentEl } = this;
54+
const t = (x: TransItemType, vars?: any) => {
55+
return this.plugin.i18n.t(x, vars);
56+
};
57+
58+
contentEl.createEl("h2", {
59+
text: 'Import project to local'
60+
});
61+
62+
const formContainer = contentEl.createDiv('form-container');
63+
// formContainer.innerHTML = 'FORM'
64+
65+
new Setting(formContainer)
66+
.setName('Import String')
67+
.setDesc('Paste the hash string you got')
68+
.addTextArea((text) =>
69+
text
70+
.setPlaceholder("")
71+
.setValue(this.restoredStr)
72+
.onChange(txt => {
73+
this.restoredStr = txt;
74+
log.info('restoredStr changed: ', this.restoredStr);
75+
})
76+
);
77+
78+
new Setting(formContainer)
79+
.addButton((button) => {
80+
button.setButtonText('cancel');
81+
button.onClick(async () => {
82+
// this.plugin.settings.password = this.newPassword;
83+
// await this.plugin.saveSettings();
84+
// new Notice(t("modal_password_notice"));
85+
if (this.confirmCB) {
86+
this.confirmCB(null, 'cancel');
87+
}
88+
this.close();
89+
});
90+
})
91+
.addButton((button) => {
92+
button.setClass("password-second-confirm");
93+
button.setButtonText('Confirm');
94+
button.onClick(async () => {
95+
await this.importProject(this.plugin, this.restoredStr)
96+
.then(dir => {
97+
if (dir) {
98+
if (this.confirmCB) {
99+
this.confirmCB(dir);
100+
new Notice(`Local project created - ${dir}`, 3500);
101+
}
102+
} else {
103+
if (this.confirmCB) {
104+
this.confirmCB(null, 'fail');
105+
new Notice(`Local project import failed`, 3500);
106+
}
107+
return;
108+
}
109+
this.close();
110+
})
111+
.catch(err => {
112+
log.error('create project failed: ', JSON.stringify(err));
113+
// TODO: Show error info
114+
new Notice(err?.message, 3500);
115+
return err;
116+
})
117+
});
118+
});
119+
}
120+
121+
onClose() {
122+
let { contentEl } = this;
123+
contentEl.empty();
124+
}
125+
}

src/settings.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ import {
4848
restoreLogWritterInplace,
4949
} from "./moreOnLog";
5050
import { DEFAULT_S3_CONFIG } from "./remoteForS3";
51+
import { ProjectImportModal } from './components/ProjectImportModal';
5152

52-
const settingsPrefix = `Invio-Settings>`;
53-
const settingsSuffix = `<&`
53+
export const settingsPrefix = `Invio-Settings>`;
54+
export const settingsSuffix = `<&`
5455

5556
export const DEFAULT_SETTINGS: InvioPluginSettings = {
5657
s3: DEFAULT_S3_CONFIG,
@@ -452,7 +453,15 @@ export class InvioSettingTab extends PluginSettingTab {
452453
.onChange(async (value: string) => {
453454
await this.plugin.switchWorkingDir(value);
454455
})
455-
});
456+
})
457+
.addButton(async (button) => {
458+
button.setButtonText('Import');
459+
button.onClick(async () => {
460+
log.info('importing...');
461+
const modal = new ProjectImportModal(this.app, this.plugin);
462+
modal.open();
463+
});
464+
})
456465

457466
// =============== Hosting Settings ======================
458467

@@ -997,7 +1006,7 @@ export class InvioSettingTab extends PluginSettingTab {
9971006
InvioSettingTab.exportSettings(this.plugin);
9981007
});
9991008
});
1000-
1009+
10011010
let restoredStr = '';
10021011
new Setting(importExportDiv)
10031012
.setName(t("settings_import"))

0 commit comments

Comments
 (0)