2022-06-16 14:02:43 +02:00
|
|
|
import noteAutocompleteService from "../../services/note_autocomplete.js";
|
|
|
|
import treeService from "../../services/tree.js";
|
|
|
|
import toastService from "../../services/toast.js";
|
|
|
|
import froca from "../../services/froca.js";
|
|
|
|
import branchService from "../../services/branches.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../../components/app_context.js";
|
2022-06-16 14:02:43 +02:00
|
|
|
import BasicWidget from "../basic_widget.js";
|
2024-07-23 17:38:22 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2025-03-19 14:27:33 +01:00
|
|
|
import type { EventData } from "../../components/app_context.js";
|
2025-06-19 22:44:02 +03:00
|
|
|
import { openDialog } from "../../services/dialog.js";
|
2025-03-19 14:27:33 +01:00
|
|
|
|
2022-06-16 14:02:43 +02:00
|
|
|
|
2025-04-01 23:24:21 +03:00
|
|
|
const TPL = /*html*/`
|
2022-06-16 14:02:43 +02:00
|
|
|
<div class="clone-to-dialog modal mx-auto" tabindex="-1" role="dialog">
|
|
|
|
<div class="modal-dialog modal-lg" style="max-width: 1000px" role="document">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2025-01-09 18:07:02 +02:00
|
|
|
<h5 class="modal-title flex-grow-1">${t("clone_to.clone_notes_to")}</h5>
|
|
|
|
<button type="button" class="help-button" title="${t("clone_to.help_on_links")}" data-help-page="cloning-notes.html">?</button>
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t("clone_to.close")}"></button>
|
2022-06-16 14:02:43 +02:00
|
|
|
</div>
|
|
|
|
<form class="clone-to-form">
|
|
|
|
<div class="modal-body">
|
2025-01-09 18:07:02 +02:00
|
|
|
<h5>${t("clone_to.notes_to_clone")}</h5>
|
2022-06-16 14:02:43 +02:00
|
|
|
|
|
|
|
<ul class="clone-to-note-list" style="max-height: 200px; overflow: auto;"></ul>
|
|
|
|
|
|
|
|
<div class="form-group">
|
|
|
|
<label style="width: 100%">
|
2025-01-09 18:07:02 +02:00
|
|
|
${t("clone_to.target_parent_note")}
|
2022-06-16 14:02:43 +02:00
|
|
|
<div class="input-group">
|
2025-01-09 18:07:02 +02:00
|
|
|
<input class="clone-to-note-autocomplete form-control" placeholder="${t("clone_to.search_for_note_by_its_name")}">
|
2022-06-16 14:02:43 +02:00
|
|
|
</div>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
<div class="form-group" title="${t("clone_to.cloned_note_prefix_title")}">
|
2022-06-16 14:02:43 +02:00
|
|
|
<label style="width: 100%">
|
2025-01-09 18:07:02 +02:00
|
|
|
${t("clone_to.prefix_optional")}
|
2022-06-16 14:02:43 +02:00
|
|
|
<input class="clone-prefix form-control" style="width: 100%;">
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
2025-01-09 18:07:02 +02:00
|
|
|
<button type="submit" class="btn btn-primary">${t("clone_to.clone_to_selected_note")}</button>
|
2022-06-16 14:02:43 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
export default class CloneToDialog extends BasicWidget {
|
2025-03-19 14:27:33 +01:00
|
|
|
private $form!: JQuery<HTMLElement>;
|
|
|
|
private $noteAutoComplete!: JQuery<HTMLElement>;
|
|
|
|
private $clonePrefix!: JQuery<HTMLElement>;
|
|
|
|
private $noteList!: JQuery<HTMLElement>;
|
|
|
|
private clonedNoteIds: string[] | null = null;
|
|
|
|
|
2022-06-16 14:02:43 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
|
|
|
this.$form = this.$widget.find(".clone-to-form");
|
|
|
|
this.$noteAutoComplete = this.$widget.find(".clone-to-note-autocomplete");
|
|
|
|
this.$clonePrefix = this.$widget.find(".clone-prefix");
|
|
|
|
this.$noteList = this.$widget.find(".clone-to-note-list");
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$form.on("submit", () => {
|
2022-06-16 14:02:43 +02:00
|
|
|
const notePath = this.$noteAutoComplete.getSelectedNotePath();
|
|
|
|
|
|
|
|
if (notePath) {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$widget.modal("hide");
|
2022-06-16 14:02:43 +02:00
|
|
|
this.cloneNotesTo(notePath);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
|
|
|
logError(t("clone_to.no_path_to_clone_to"));
|
2022-06-16 14:02:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-03-19 14:27:33 +01:00
|
|
|
async cloneNoteIdsToEvent({ noteIds }: EventData<"cloneNoteIdsTo">) {
|
2022-06-16 14:02:43 +02:00
|
|
|
if (!noteIds || noteIds.length === 0) {
|
2025-03-19 14:27:33 +01:00
|
|
|
noteIds = [appContext.tabManager.getActiveContextNoteId() ?? ""];
|
2022-06-16 14:02:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.clonedNoteIds = [];
|
|
|
|
|
|
|
|
for (const noteId of noteIds) {
|
|
|
|
if (!this.clonedNoteIds.includes(noteId)) {
|
|
|
|
this.clonedNoteIds.push(noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-19 22:44:02 +03:00
|
|
|
openDialog(this.$widget);
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$noteAutoComplete.val("").trigger("focus");
|
2022-06-16 14:02:43 +02:00
|
|
|
this.$noteList.empty();
|
|
|
|
|
|
|
|
for (const noteId of this.clonedNoteIds) {
|
|
|
|
const note = await froca.getNote(noteId);
|
2025-03-19 14:27:33 +01:00
|
|
|
if (!note) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-16 14:02:43 +02:00
|
|
|
this.$noteList.append($("<li>").text(note.title));
|
|
|
|
}
|
|
|
|
|
|
|
|
noteAutocompleteService.initNoteAutocomplete(this.$noteAutoComplete);
|
|
|
|
noteAutocompleteService.showRecentNotes(this.$noteAutoComplete);
|
|
|
|
}
|
|
|
|
|
2025-03-19 14:27:33 +01:00
|
|
|
async cloneNotesTo(notePath: string) {
|
2024-09-03 18:15:10 +02:00
|
|
|
const { noteId, parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(notePath);
|
2025-03-19 14:27:33 +01:00
|
|
|
if (!noteId || !parentNoteId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-16 14:02:43 +02:00
|
|
|
const targetBranchId = await froca.getBranchId(parentNoteId, noteId);
|
2025-03-19 14:27:33 +01:00
|
|
|
if (!targetBranchId || !this.clonedNoteIds) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-16 14:02:43 +02:00
|
|
|
|
|
|
|
for (const cloneNoteId of this.clonedNoteIds) {
|
2025-03-19 14:27:33 +01:00
|
|
|
await branchService.cloneNoteToBranch(cloneNoteId, targetBranchId, this.$clonePrefix.val() as string);
|
2022-06-16 14:02:43 +02:00
|
|
|
|
|
|
|
const clonedNote = await froca.getNote(cloneNoteId);
|
2025-03-19 14:27:33 +01:00
|
|
|
const targetBranch = froca.getBranch(targetBranchId);
|
|
|
|
if (!clonedNote || !targetBranch) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const targetNote = await targetBranch.getNote();
|
|
|
|
if (!targetNote) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-16 14:02:43 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
toastService.showMessage(t("clone_to.note_cloned", { clonedTitle: clonedNote.title, targetTitle: targetNote.title }));
|
2022-06-16 14:02:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|