Implemented:

- Add new features for setting Self-hosted LiveSync up more easier.
This commit is contained in:
vorotamoroz
2022-09-27 17:58:13 +09:00
parent 728edac283
commit 4b32365694
3 changed files with 130 additions and 49 deletions
+29 -1
View File
@@ -164,6 +164,24 @@ export class ObsidianLiveSyncSettingTab extends PluginSettingTab {
}) })
}) })
const infoWarnForSubsequent = setupWizardEl.createEl("div", { text: `To set up second or subsequent device, please use 'Copy setup URI' and 'Open setup URI'` });
infoWarnForSubsequent.addClass("op-warn-info");
new Setting(setupWizardEl)
.setName("Copy setup URI")
.addButton((text) => {
text.setButtonText("Copy setup URI").onClick(() => {
// @ts-ignore
this.plugin.app.commands.executeCommandById("obsidian-livesync:livesync-copysetupuri")
})
})
.addButton((text) => {
text.setButtonText("Open setup URI").onClick(() => {
// @ts-ignore
this.plugin.app.commands.executeCommandById("obsidian-livesync:livesync-opensetupuri")
})
})
addScreenElement("110", setupWizardEl); addScreenElement("110", setupWizardEl);
@@ -361,7 +379,17 @@ export class ObsidianLiveSyncSettingTab extends PluginSettingTab {
.onClick(async () => { .onClick(async () => {
await applyEncryption(true); await applyEncryption(true);
}) })
); )
.addButton((button) =>
button
.setButtonText("Apply w/o rebuilding")
.setWarning()
.setDisabled(false)
.setClass("sls-btn-right")
.onClick(async () => {
await applyEncryption(false);
})
);
const rebuildDB = async (method: "localOnly" | "remoteOnly" | "rebuildBothByThisDevice") => { const rebuildDB = async (method: "localOnly" | "remoteOnly" | "rebuildBothByThisDevice") => {
+1 -1
View File
@@ -98,7 +98,7 @@ export class PopoverSelectString extends FuzzySuggestModal<string> {
constructor(app: App, note: string, placeholder: string | null, getItemsFun: () => string[], callback: (e: string) => void) { constructor(app: App, note: string, placeholder: string | null, getItemsFun: () => string[], callback: (e: string) => void) {
super(app); super(app);
this.app = app; this.app = app;
this.setPlaceholder(placeholder ?? "y/n) " + note); this.setPlaceholder((placeholder ?? "y/n) ") + note);
if (getItemsFun) this.getItemsFun = getItemsFun; if (getItemsFun) this.getItemsFun = getItemsFun;
this.callback = callback; this.callback = callback;
} }
+100 -47
View File
@@ -82,7 +82,7 @@ const askYesNo = (app: App, message: string): Promise<"yes" | "no"> => {
const askSelectString = (app: App, message: string, items: string[]): Promise<string> => { const askSelectString = (app: App, message: string, items: string[]): Promise<string> => {
const getItemsFun = () => items; const getItemsFun = () => items;
return new Promise((res) => { return new Promise((res) => {
const popover = new PopoverSelectString(app, message, "Select file)", getItemsFun, (result) => res(result)); const popover = new PopoverSelectString(app, message, "", getItemsFun, (result) => res(result));
popover.open(); popover.open();
}); });
}; };
@@ -340,11 +340,31 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
const configURIBase = "obsidian://setuplivesync?settings="; const configURIBase = "obsidian://setuplivesync?settings=";
this.addCommand({ this.addCommand({
id: "livesync-copysetupuri", id: "livesync-copysetupuri",
name: "Copy setup URI (beta)", name: "Copy setup URI",
callback: async () => { callback: async () => {
const encryptingPassphrase = await askString(this.app, "Encrypt your settings", "Passphrase", ""); const encryptingPassphrase = await askString(this.app, "Encrypt your settings", "Passphrase", "");
if (encryptingPassphrase === false) return; if (encryptingPassphrase === false) return;
const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(this.settings), encryptingPassphrase)); const setting = { ...this.settings };
const keys = Object.keys(setting) as (keyof ObsidianLiveSyncSettings)[];
for (const k of keys) {
if (JSON.stringify(k in setting ? setting[k] : "") == JSON.stringify(k in DEFAULT_SETTINGS ? DEFAULT_SETTINGS[k] : "*")) {
delete setting[k];
}
}
const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(setting), encryptingPassphrase));
const uri = `${configURIBase}${encryptedSetting}`;
await navigator.clipboard.writeText(uri);
Logger("Setup URI copied to clipboard", LOG_LEVEL.NOTICE);
},
});
this.addCommand({
id: "livesync-copysetupurifull",
name: "Copy setup URI (Full)",
callback: async () => {
const encryptingPassphrase = await askString(this.app, "Encrypt your settings", "Passphrase", "");
if (encryptingPassphrase === false) return;
const setting = { ...this.settings };
const encryptedSetting = encodeURIComponent(await encrypt(JSON.stringify(setting), encryptingPassphrase));
const uri = `${configURIBase}${encryptedSetting}`; const uri = `${configURIBase}${encryptedSetting}`;
await navigator.clipboard.writeText(uri); await navigator.clipboard.writeText(uri);
Logger("Setup URI copied to clipboard", LOG_LEVEL.NOTICE); Logger("Setup URI copied to clipboard", LOG_LEVEL.NOTICE);
@@ -352,9 +372,9 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
}); });
this.addCommand({ this.addCommand({
id: "livesync-opensetupuri", id: "livesync-opensetupuri",
name: "Open setup URI (beta)", name: "Open setup URI",
callback: async () => { callback: async () => {
const setupURI = await askString(this.app, "Set up manually", "Set up URI", `${configURIBase}aaaaa`); const setupURI = await askString(this.app, "Easy setup", "Set up URI", `${configURIBase}aaaaa`);
if (setupURI === false) return; if (setupURI === false) return;
if (!setupURI.startsWith(`${configURIBase}`)) { if (!setupURI.startsWith(`${configURIBase}`)) {
Logger("Set up URI looks wrong.", LOG_LEVEL.NOTICE); Logger("Set up URI looks wrong.", LOG_LEVEL.NOTICE);
@@ -374,58 +394,91 @@ export default class ObsidianLiveSyncPlugin extends Plugin {
if (newConf) { if (newConf) {
const result = await askYesNo(this.app, "Importing LiveSync's conf, OK?"); const result = await askYesNo(this.app, "Importing LiveSync's conf, OK?");
if (result == "yes") { if (result == "yes") {
const newSettingW = Object.assign({}, this.settings, newConf); const newSettingW = Object.assign({}, DEFAULT_SETTINGS, newConf);
// stopping once.
this.localDatabase.closeReplication(); this.localDatabase.closeReplication();
this.settings.suspendFileWatching = true; this.settings.suspendFileWatching = true;
console.dir(newSettingW); console.dir(newSettingW);
const keepLocalDB = await askYesNo(this.app, "Keep local DB?"); const setupJustImport = "Just import setting";
const keepRemoteDB = await askYesNo(this.app, "Keep remote DB?"); const setupAsNew = "Set it up as secondary or subsequent device";
if (keepLocalDB == "yes" && keepRemoteDB == "yes") { const setupAgain = "Reconfigure and reconstitute the data";
// nothing to do. so peaceful. const setupManually = "Leave everything to me";
const setupType = await askSelectString(this.app, "How would you like to set it up?", [setupAsNew, setupAgain, setupJustImport, setupManually]);
if (setupType == setupJustImport) {
this.settings = newSettingW; this.settings = newSettingW;
await this.saveSettings(); await this.saveSettings();
const replicate = await askYesNo(this.app, "Unlock and replicate?"); } else if (setupType == setupAsNew) {
if (replicate == "yes") { this.settings = newSettingW;
await this.replicate(true); await this.saveSettings();
await this.markRemoteUnlocked(); await this.resetLocalOldDatabase();
} await this.resetLocalDatabase();
Logger("Configuration loaded.", LOG_LEVEL.NOTICE); await this.localDatabase.initializeDatabase();
return; await this.markRemoteResolved();
} await this.replicate(true);
if (keepLocalDB == "no" && keepRemoteDB == "no") { } else if (setupType == setupAgain) {
const reset = await askYesNo(this.app, "Drop everything?"); const confirm = "I know this operation will rebuild all my databases with files on this device, and files that are on the remote database and I didn't synchronize to any other devices will be lost and want to proceed indeed.";
if (reset != "yes") { if (await askSelectString(this.app, "Do you really want to do this?", ["Cancel", confirm]) != confirm) {
Logger("Cancelled", LOG_LEVEL.NOTICE);
this.settings = oldConf;
return; return;
} }
} await this.saveSettings();
let initDB; await this.resetLocalOldDatabase();
this.settings = newSettingW; await this.resetLocalDatabase();
await this.saveSettings(); await this.localDatabase.initializeDatabase();
if (keepLocalDB == "no") { await this.initializeDatabase(true);
this.resetLocalOldDatabase();
this.resetLocalDatabase();
this.localDatabase.initializeDatabase();
const rebuild = await askYesNo(this.app, "Rebuild the database?");
if (rebuild == "yes") {
initDB = this.initializeDatabase(true);
} else {
this.markRemoteResolved();
}
}
if (keepRemoteDB == "no") {
await this.tryResetRemoteDatabase(); await this.tryResetRemoteDatabase();
await this.markRemoteLocked(); await this.markRemoteLocked();
} await this.markRemoteResolved();
if (keepLocalDB == "no" || keepRemoteDB == "no") { await this.replicate(true);
const replicate = await askYesNo(this.app, "Replicate once?");
if (replicate == "yes") { } else if (setupType == setupManually) {
if (initDB != null) { const keepLocalDB = await askYesNo(this.app, "Keep local DB?");
await initDB; const keepRemoteDB = await askYesNo(this.app, "Keep remote DB?");
if (keepLocalDB == "yes" && keepRemoteDB == "yes") {
// nothing to do. so peaceful.
this.settings = newSettingW;
await this.saveSettings();
const replicate = await askYesNo(this.app, "Unlock and replicate?");
if (replicate == "yes") {
await this.replicate(true);
await this.markRemoteUnlocked();
}
Logger("Configuration loaded.", LOG_LEVEL.NOTICE);
return;
}
if (keepLocalDB == "no" && keepRemoteDB == "no") {
const reset = await askYesNo(this.app, "Drop everything?");
if (reset != "yes") {
Logger("Cancelled", LOG_LEVEL.NOTICE);
this.settings = oldConf;
return;
}
}
let initDB;
this.settings = newSettingW;
await this.saveSettings();
if (keepLocalDB == "no") {
this.resetLocalOldDatabase();
this.resetLocalDatabase();
this.localDatabase.initializeDatabase();
const rebuild = await askYesNo(this.app, "Rebuild the database?");
if (rebuild == "yes") {
initDB = this.initializeDatabase(true);
} else {
this.markRemoteResolved();
}
}
if (keepRemoteDB == "no") {
await this.tryResetRemoteDatabase();
await this.markRemoteLocked();
}
if (keepLocalDB == "no" || keepRemoteDB == "no") {
const replicate = await askYesNo(this.app, "Replicate once?");
if (replicate == "yes") {
if (initDB != null) {
await initDB;
}
await this.replicate(true);
} }
await this.replicate(true);
} }
} }
} }