/
raiden
/
obsidian-git-encrypt
Обзор
Документация
Войти
/
raiden
/
obsidian-git-encrypt
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/ui/passwordModals.ts
99 строк
3 KB
Robert Kuzhin
feat: add Obsidian session and desktop UX
09 авг 2026, 13:07
09 авг 2026, 13:07
14fe125
Код
Авторство
О чём код?
import { Modal, type App } from "obsidian"; type PasswordMode = "create" | "unlock" | "change"; export function openCreateVaultModal(app: App): Promise<Uint8Array | undefined> { return openPasswordModal(app, "create"); } export function openUnlockModal(app: App): Promise<Uint8Array | undefined> { return openPasswordModal(app, "unlock"); } export function openChangePasswordModal(app: App): Promise<Uint8Array | undefined> { return openPasswordModal(app, "change"); } function openPasswordModal(app: App, mode: PasswordMode): Promise<Uint8Array | undefined> { return new Promise((resolve) => new PasswordModal(app, mode, resolve).open()); } class PasswordModal extends Modal { private settled = false; public constructor( app: App, private readonly mode: PasswordMode, private readonly resolveResult: (password: Uint8Array | undefined) => void, ) { super(app); } public override onOpen(): void { this.setTitle(titleFor(this.mode)); this.contentEl.empty(); if (this.mode === "create") { const warning = this.contentEl.createEl("p", { cls: "encrypted-git-warning" }); warning.setText( "There is no password recovery. The working Obsidian vault remains plaintext unless it is stored on an encrypted filesystem.", ); } const form = this.contentEl.createEl("form", { cls: "encrypted-git-password-form" }); const password = createPasswordField(form, this.mode === "change" ? "New password" : "Password"); const confirmation = this.mode === "unlock" ? undefined : createPasswordField(form, "Confirm password"); const error = form.createEl("div", { cls: "encrypted-git-form-error" }); const actions = form.createEl("div", { cls: "encrypted-git-modal-actions" }); const cancel = actions.createEl("button", { text: "Cancel", attr: { type: "button" } }); const submit = actions.createEl("button", { text: this.mode === "unlock" ? "Unlock" : this.mode === "create" ? "Create" : "Change", cls: "mod-cta", attr: { type: "submit" }, }); cancel.addEventListener("click", () => this.close()); form.addEventListener("submit", (event) => { event.preventDefault(); const value = password.value; if (value.length < 12) { error.setText("Use at least 12 characters."); return; } if (confirmation !== undefined && value !== confirmation.value) { error.setText("Passwords do not match."); return; } const bytes = new TextEncoder().encode(value); password.value = ""; if (confirmation !== undefined) confirmation.value = ""; submit.disabled = true; this.settle(bytes); this.close(); }); password.focus(); } public override onClose(): void { this.contentEl.empty(); this.settle(undefined); } private settle(value: Uint8Array | undefined): void { if (this.settled) return; this.settled = true; this.resolveResult(value); } } function createPasswordField(container: HTMLElement, labelText: string): HTMLInputElement { const label = container.createEl("label", { cls: "encrypted-git-password-field" }); label.createEl("span", { text: labelText }); return label.createEl("input", { attr: { type: "password", autocomplete: "off", spellcheck: "false" }, }); } function titleFor(mode: PasswordMode): string { if (mode === "create") return "Create encrypted vault"; if (mode === "change") return "Change encrypted vault password"; return "Unlock encrypted vault"; }