134 lines
2.9 KiB
TypeScript
134 lines
2.9 KiB
TypeScript
|
|
import { App, Modal, Notice } from "obsidian";
|
|||
|
|
import i18n from "../utils/i18n";
|
|||
|
|
|
|||
|
|
export interface CommentModalOptions {
|
|||
|
|
title: string;
|
|||
|
|
initial?: string;
|
|||
|
|
placeholder?: string;
|
|||
|
|
submitText: string;
|
|||
|
|
onSubmit: (text: string) => Promise<void> | void;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 评论回复 / 编辑弹窗
|
|||
|
|
*/
|
|||
|
|
export class CommentModal extends Modal {
|
|||
|
|
private options: CommentModalOptions;
|
|||
|
|
private submitting = false;
|
|||
|
|
|
|||
|
|
constructor(app: App, options: CommentModalOptions) {
|
|||
|
|
super(app);
|
|||
|
|
this.options = options;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
onOpen() {
|
|||
|
|
const { contentEl } = this;
|
|||
|
|
contentEl.empty();
|
|||
|
|
|
|||
|
|
contentEl.createEl("h2", { text: this.options.title });
|
|||
|
|
|
|||
|
|
const textarea = contentEl.createEl("textarea", {
|
|||
|
|
cls: "otm-textarea",
|
|||
|
|
attr: {
|
|||
|
|
rows: "6",
|
|||
|
|
placeholder: this.options.placeholder ?? "",
|
|||
|
|
},
|
|||
|
|
});
|
|||
|
|
textarea.value = this.options.initial ?? "";
|
|||
|
|
|
|||
|
|
const footer = contentEl.createDiv({ cls: "otm-modal-actions" });
|
|||
|
|
|
|||
|
|
const submitButton = footer.createEl("button", {
|
|||
|
|
cls: "mod-cta",
|
|||
|
|
text: this.options.submitText,
|
|||
|
|
});
|
|||
|
|
submitButton.addEventListener("click", async () => {
|
|||
|
|
if (this.submitting) return;
|
|||
|
|
|
|||
|
|
const text = textarea.value.trim();
|
|||
|
|
if (!text) {
|
|||
|
|
new Notice(i18n.t("manager.textRequired"));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
this.submitting = true;
|
|||
|
|
submitButton.disabled = true;
|
|||
|
|
try {
|
|||
|
|
await this.options.onSubmit(text);
|
|||
|
|
this.close();
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error("提交评论操作失败", error);
|
|||
|
|
} finally {
|
|||
|
|
this.submitting = false;
|
|||
|
|
submitButton.disabled = false;
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
const cancelButton = footer.createEl("button", {
|
|||
|
|
text: i18n.t("manager.cancel"),
|
|||
|
|
});
|
|||
|
|
cancelButton.addEventListener("click", () => this.close());
|
|||
|
|
|
|||
|
|
textarea.focus();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
onClose() {
|
|||
|
|
this.contentEl.empty();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 通用确认弹窗(用于删除这类不可恢复操作)
|
|||
|
|
*/
|
|||
|
|
export class ConfirmModal extends Modal {
|
|||
|
|
private titleText: string;
|
|||
|
|
private message: string;
|
|||
|
|
private onConfirm: () => Promise<void> | void;
|
|||
|
|
|
|||
|
|
constructor(
|
|||
|
|
app: App,
|
|||
|
|
titleText: string,
|
|||
|
|
message: string,
|
|||
|
|
onConfirm: () => Promise<void> | void
|
|||
|
|
) {
|
|||
|
|
super(app);
|
|||
|
|
this.titleText = titleText;
|
|||
|
|
this.message = message;
|
|||
|
|
this.onConfirm = onConfirm;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
onOpen() {
|
|||
|
|
const { contentEl } = this;
|
|||
|
|
contentEl.empty();
|
|||
|
|
|
|||
|
|
contentEl.createEl("h2", { text: this.titleText });
|
|||
|
|
contentEl.createEl("p", { text: this.message });
|
|||
|
|
|
|||
|
|
const footer = contentEl.createDiv({ cls: "otm-modal-actions" });
|
|||
|
|
|
|||
|
|
const confirmButton = footer.createEl("button", {
|
|||
|
|
cls: "mod-warning",
|
|||
|
|
text: i18n.t("manager.confirm"),
|
|||
|
|
});
|
|||
|
|
confirmButton.addEventListener("click", async () => {
|
|||
|
|
confirmButton.disabled = true;
|
|||
|
|
try {
|
|||
|
|
await this.onConfirm();
|
|||
|
|
this.close();
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error("确认操作失败", error);
|
|||
|
|
confirmButton.disabled = false;
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
const cancelButton = footer.createEl("button", {
|
|||
|
|
text: i18n.t("manager.cancel"),
|
|||
|
|
});
|
|||
|
|
cancelButton.addEventListener("click", () => this.close());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
onClose() {
|
|||
|
|
this.contentEl.empty();
|
|||
|
|
}
|
|||
|
|
}
|