refactor: 💡 Port branch prefix to ts

This commit is contained in:
Jin 2025-03-19 13:59:36 +01:00
parent 96e2cc29e0
commit 91a963fafd

View File

@ -8,8 +8,6 @@ import appContext from "../../components/app_context.js";
import { t } from "../../services/i18n.js"; import { t } from "../../services/i18n.js";
import { Modal } from "bootstrap"; import { Modal } from "bootstrap";
let branchId;
const TPL = `<div class="branch-prefix-dialog modal fade mx-auto" tabindex="-1" role="dialog"> const TPL = `<div class="branch-prefix-dialog modal fade mx-auto" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg" role="document"> <div class="modal-dialog modal-lg" role="document">
<form class="branch-prefix-form"> <form class="branch-prefix-form">
@ -38,61 +36,70 @@ const TPL = `<div class="branch-prefix-dialog modal fade mx-auto" tabindex="-1"
</div>`; </div>`;
export default class BranchPrefixDialog extends BasicWidget { export default class BranchPrefixDialog extends BasicWidget {
doRender() { private modal!: Modal;
private $form!: JQuery<HTMLElement>;
private $treePrefixInput!: JQuery<HTMLElement>;
private $noteTitle!: JQuery<HTMLElement>;
private branchId: string | null = null;
doRender(): void {
this.$widget = $(TPL); this.$widget = $(TPL);
this.modal = Modal.getOrCreateInstance(this.$widget); this.modal = Modal.getOrCreateInstance(this.$widget[0]);
this.$form = this.$widget.find(".branch-prefix-form"); this.$form = this.$widget.find(".branch-prefix-form");
this.$treePrefixInput = this.$widget.find(".branch-prefix-input"); this.$treePrefixInput = this.$widget.find(".branch-prefix-input");
this.$noteTitle = this.$widget.find(".branch-prefix-note-title"); this.$noteTitle = this.$widget.find(".branch-prefix-note-title");
this.$form.on("submit", () => { this.$form.on("submit", () => {
this.savePrefix(); this.savePrefix();
return false; return false;
}); });
this.$widget.on("shown.bs.modal", () => this.$treePrefixInput.trigger("focus")); this.$widget.on("shown.bs.modal", () => this.$treePrefixInput.trigger("focus"));
} }
async refresh(notePath) { async refresh(notePath: string) {
const { noteId, parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(notePath); const { noteId, parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(notePath);
if (!noteId || !parentNoteId) { if (!noteId || !parentNoteId) {
return; return;
} }
branchId = await froca.getBranchId(parentNoteId, noteId); const newBranchId = await froca.getBranchId(parentNoteId, noteId);
const branch = froca.getBranch(branchId); if (!newBranchId) {
return;
}
this.branchId = newBranchId;
const branch = froca.getBranch(this.branchId);
if (!branch || branch.noteId === "root") { if (!branch || branch.noteId === "root") {
return; return;
} }
const parentNote = await froca.getNote(branch.parentNoteId); const parentNote = await froca.getNote(branch.parentNoteId);
if (!parentNote || parentNote.type === "search") {
if (parentNote.type === "search") {
return; return;
} }
this.$treePrefixInput.val(branch.prefix); this.$treePrefixInput.val(branch.prefix || "");
const noteTitle = await treeService.getNoteTitle(noteId); const noteTitle = await treeService.getNoteTitle(noteId);
this.$noteTitle.text(` - ${noteTitle}`); this.$noteTitle.text(` - ${noteTitle}`);
} }
async editBranchPrefixEvent() { async editBranchPrefixEvent() {
const notePath = appContext.tabManager.getActiveContextNotePath(); const notePath = appContext.tabManager.getActiveContextNotePath();
if (!notePath) {
return;
}
await this.refresh(notePath); await this.refresh(notePath);
utils.openDialog(this.$widget); utils.openDialog(this.$widget);
} }
async savePrefix() { async savePrefix() {
const prefix = this.$treePrefixInput.val(); const prefix = this.$treePrefixInput.val();
await server.put(`branches/${branchId}/set-prefix`, { prefix: prefix }); await server.put(`branches/${this.branchId}/set-prefix`, { prefix: prefix });
this.modal.hide(); this.modal.hide();