|
| 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 | +} |
0 commit comments