From 43ef9415d21e83f841bee390c0e25e640e2a8658 Mon Sep 17 00:00:00 2001
From: Jin <22962980+JYC333@users.noreply.github.com>
Date: Wed, 19 Mar 2025 14:27:33 +0100
Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20Port=20clone=5Fto=20?=
=?UTF-8?q?to=20ts?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/public/app/components/app_context.ts | 3 ++
.../dialogs/{clone_to.js => clone_to.ts} | 41 ++++++++++++++-----
2 files changed, 33 insertions(+), 11 deletions(-)
rename src/public/app/widgets/dialogs/{clone_to.js => clone_to.ts} (82%)
diff --git a/src/public/app/components/app_context.ts b/src/public/app/components/app_context.ts
index 8b50a8101..53e64085e 100644
--- a/src/public/app/components/app_context.ts
+++ b/src/public/app/components/app_context.ts
@@ -371,6 +371,9 @@ type EventMappings = {
openBulkActionsDialog: {
selectedOrActiveNoteIds: string[];
};
+ cloneNoteIdsTo: {
+ noteIds: string[];
+ };
};
diff --git a/src/public/app/widgets/dialogs/clone_to.js b/src/public/app/widgets/dialogs/clone_to.ts
similarity index 82%
rename from src/public/app/widgets/dialogs/clone_to.js
rename to src/public/app/widgets/dialogs/clone_to.ts
index 0098f340a..3f51942d6 100644
--- a/src/public/app/widgets/dialogs/clone_to.js
+++ b/src/public/app/widgets/dialogs/clone_to.ts
@@ -7,6 +7,8 @@ import branchService from "../../services/branches.js";
import appContext from "../../components/app_context.js";
import BasicWidget from "../basic_widget.js";
import { t } from "../../services/i18n.js";
+import type { EventData } from "../../components/app_context.js";
+
const TPL = `
@@ -48,10 +50,14 @@ const TPL = `
`;
export default class CloneToDialog extends BasicWidget {
+ private $form!: JQuery;
+ private $noteAutoComplete!: JQuery;
+ private $clonePrefix!: JQuery;
+ private $noteList!: JQuery;
+ private clonedNoteIds: string[] | null = null;
+
constructor() {
super();
-
- this.clonedNoteIds = null;
}
doRender() {
@@ -66,7 +72,6 @@ export default class CloneToDialog extends BasicWidget {
if (notePath) {
this.$widget.modal("hide");
-
this.cloneNotesTo(notePath);
} else {
logError(t("clone_to.no_path_to_clone_to"));
@@ -76,9 +81,9 @@ export default class CloneToDialog extends BasicWidget {
});
}
- async cloneNoteIdsToEvent({ noteIds }) {
+ async cloneNoteIdsToEvent({ noteIds }: EventData<"cloneNoteIdsTo">) {
if (!noteIds || noteIds.length === 0) {
- noteIds = [appContext.tabManager.getActiveContextNoteId()];
+ noteIds = [appContext.tabManager.getActiveContextNoteId() ?? ""];
}
this.clonedNoteIds = [];
@@ -90,14 +95,14 @@ export default class CloneToDialog extends BasicWidget {
}
utils.openDialog(this.$widget);
-
this.$noteAutoComplete.val("").trigger("focus");
-
this.$noteList.empty();
for (const noteId of this.clonedNoteIds) {
const note = await froca.getNote(noteId);
-
+ if (!note) {
+ continue;
+ }
this.$noteList.append($("").text(note.title));
}
@@ -105,15 +110,29 @@ export default class CloneToDialog extends BasicWidget {
noteAutocompleteService.showRecentNotes(this.$noteAutoComplete);
}
- async cloneNotesTo(notePath) {
+ async cloneNotesTo(notePath: string) {
const { noteId, parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(notePath);
+ if (!noteId || !parentNoteId) {
+ return;
+ }
+
const targetBranchId = await froca.getBranchId(parentNoteId, noteId);
+ if (!targetBranchId || !this.clonedNoteIds) {
+ return;
+ }
for (const cloneNoteId of this.clonedNoteIds) {
- await branchService.cloneNoteToBranch(cloneNoteId, targetBranchId, this.$clonePrefix.val());
+ await branchService.cloneNoteToBranch(cloneNoteId, targetBranchId, this.$clonePrefix.val() as string);
const clonedNote = await froca.getNote(cloneNoteId);
- const targetNote = await froca.getBranch(targetBranchId).getNote();
+ const targetBranch = froca.getBranch(targetBranchId);
+ if (!clonedNote || !targetBranch) {
+ continue;
+ }
+ const targetNote = await targetBranch.getNote();
+ if (!targetNote) {
+ continue;
+ }
toastService.showMessage(t("clone_to.note_cloned", { clonedTitle: clonedNote.title, targetTitle: targetNote.title }));
}