All files / src/views remember-modal.ts

100% Statements 44/44
100% Branches 4/4
100% Functions 8/8
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                8x 8x 8x     8x 8x 8x       7x 7x 7x 7x   7x 7x 7x 7x 4x     7x 7x 7x 7x 7x 7x 7x 2x     7x       7x 1x 1x     7x 7x 7x 7x 7x       5x 5x 1x 1x   4x 4x 3x 3x 3x   1x            
import { App, Modal, Notice } from "obsidian";
import type LilbeePlugin from "../main";
import { MEMORY_KIND, type MemoryKind } from "../types";
import { MESSAGES } from "../locales/en";
import { bindEscapeToClose, errorMessage } from "../utils";
 
export class RememberModal extends Modal {
    private plugin: LilbeePlugin;
    private text = "";
    private kind: MemoryKind = MEMORY_KIND.FACT;
    private shared = false;
 
    constructor(app: App, plugin: LilbeePlugin) {
        super(app);
        this.plugin = plugin;
        bindEscapeToClose(this);
    }
 
    onOpen(): void {
        const { contentEl } = this;
        contentEl.empty();
        contentEl.addClass("lilbee-remember-modal");
        contentEl.createEl("h3", { text: MESSAGES.LABEL_REMEMBER_TITLE });
 
        contentEl.createEl("label", { text: MESSAGES.LABEL_REMEMBER_TEXT_LABEL, cls: "lilbee-remember-label" });
        const textEl = contentEl.createEl("textarea", { cls: "lilbee-remember-text" });
        textEl.placeholder = MESSAGES.LABEL_REMEMBER_TEXT_PLACEHOLDER;
        textEl.addEventListener("input", () => {
            this.text = textEl.value;
        });
 
        const kindRow = contentEl.createDiv({ cls: "lilbee-remember-row" });
        kindRow.createEl("label", { text: MESSAGES.LABEL_REMEMBER_KIND_LABEL, cls: "lilbee-remember-label" });
        const kindEl = kindRow.createEl("select", { cls: "lilbee-remember-kind" });
        kindEl.createEl("option", { text: MESSAGES.LABEL_REMEMBER_KIND_FACT, value: MEMORY_KIND.FACT });
        kindEl.createEl("option", { text: MESSAGES.LABEL_REMEMBER_KIND_PREFERENCE, value: MEMORY_KIND.PREFERENCE });
        kindEl.value = MEMORY_KIND.FACT;
        kindEl.addEventListener("change", () => {
            this.kind = kindEl.value === MEMORY_KIND.PREFERENCE ? MEMORY_KIND.PREFERENCE : MEMORY_KIND.FACT;
        });
 
        const sharedBtn = kindRow.createEl("button", {
            text: MESSAGES.LABEL_REMEMBER_SHARED_LABEL,
            cls: "lilbee-remember-shared",
        });
        sharedBtn.addEventListener("click", () => {
            this.shared = !this.shared;
            sharedBtn.toggleClass("is-active", this.shared);
        });
 
        const actions = contentEl.createDiv({ cls: "lilbee-remember-actions" });
        const saveBtn = actions.createEl("button", { text: MESSAGES.BUTTON_REMEMBER, cls: "mod-cta" });
        saveBtn.addEventListener("click", () => void this.submit());
        const cancelBtn = actions.createEl("button", { text: MESSAGES.BUTTON_CANCEL });
        cancelBtn.addEventListener("click", () => this.close());
    }
 
    private async submit(): Promise<void> {
        const text = this.text.trim();
        if (!text) {
            new Notice(MESSAGES.REMEMBER_EMPTY);
            return;
        }
        try {
            await this.plugin.api.remember(text, this.kind, this.shared);
            new Notice(MESSAGES.REMEMBER_SUCCESS(this.kind));
            this.plugin.refreshMemoryViews();
            this.close();
        } catch (err) {
            new Notice(
                MESSAGES.REMEMBER_FAILED(errorMessage(err, MESSAGES.ERROR_UNKNOWN, this.plugin.settings.serverMode)),
            );
        }
    }
}