From 8cd6f12557e30b0ac7cd561d08f7c52ecbb2f738 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Fri, 23 Aug 2024 22:14:16 +0800 Subject: [PATCH] add: support multi-select in note picker resolve: #1041 --- src/elements/linkCreator/inboundCreator.ts | 2 +- src/elements/linkCreator/notePicker.ts | 2 +- src/elements/linkCreator/outboundCreator.ts | 66 +++++++++++++-------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/elements/linkCreator/inboundCreator.ts b/src/elements/linkCreator/inboundCreator.ts index d46df2c..4b470d0 100644 --- a/src/elements/linkCreator/inboundCreator.ts +++ b/src/elements/linkCreator/inboundCreator.ts @@ -80,7 +80,7 @@ export class InboundCreator extends PluginCEBase { await this.notePicker.load(); this.notePicker.addEventListener("selectionchange", (event: any) => { - this.targetNote = event.detail.selectedNote; + this.targetNote = event.detail.selectedNotes[0]; this.updatePickerTitle(this.targetNote); this.noteOutline.item = this.targetNote; this.noteOutline.render(); diff --git a/src/elements/linkCreator/notePicker.ts b/src/elements/linkCreator/notePicker.ts index b010754..02ea14a 100644 --- a/src/elements/linkCreator/notePicker.ts +++ b/src/elements/linkCreator/notePicker.ts @@ -433,7 +433,7 @@ export class NotePicker extends PluginCEBase { this.dispatchEvent( new CustomEvent("selectionchange", { detail: { - selectedNote: this.getSelectedNotes(selection)[0], + selectedNotes: this.getSelectedNotes(selection), }, }), ); diff --git a/src/elements/linkCreator/outboundCreator.ts b/src/elements/linkCreator/outboundCreator.ts index e19b61b..36e6745 100644 --- a/src/elements/linkCreator/outboundCreator.ts +++ b/src/elements/linkCreator/outboundCreator.ts @@ -13,7 +13,7 @@ export class OutboundCreator extends PluginCEBase { // Where the link is inserted to currentNote: Zotero.Item | undefined; // Where the link is generated from - targetNote: Zotero.Item | undefined; + targetNotes: Zotero.Item[] | undefined; positionData: NoteNodeData | undefined; @@ -68,7 +68,7 @@ export class OutboundCreator extends PluginCEBase { } async accept(io: any) { - if (!this.targetNote) return; + if (!this.targetNotes) return; const content = await this.getContentToInsert(); this.notePicker.saveRecentNotes(); @@ -83,10 +83,10 @@ export class OutboundCreator extends PluginCEBase { await this.notePicker.load(); this.notePicker.addEventListener("selectionchange", (event: any) => { - this.targetNote = event.detail.selectedNote; - this.updatePickerTitle(this.targetNote); + this.targetNotes = event.detail.selectedNotes; + this.updatePickerTitle(this.targetNotes); this.updateNotePreview(); - if (this.targetNote) this.scrollToSection("outline"); + if (this.targetNotes) this.scrollToSection("outline"); }); const content = document.createElement("span"); @@ -166,9 +166,22 @@ export class OutboundCreator extends PluginCEBase { ?.append(content, fromTitle, middleTitle, toTitle); } - updatePickerTitle(noteItem?: Zotero.Item) { - const title = noteItem ? noteItem.getNoteTitle() : ""; - this.querySelector("#selected-note-title")!.textContent = title; + getPickedNotesTitle(noteItems?: Zotero.Item[]) { + let title = ""; + if (!noteItems?.length) { + title = "-"; + } + if (noteItems?.length === 1) { + title = noteItems[0].getNoteTitle(); + } else { + title = `${noteItems?.length} notes`; + } + return title; + } + + updatePickerTitle(noteItems?: Zotero.Item[]) { + this.querySelector("#selected-note-title")!.textContent = + this.getPickedNotesTitle(noteItems); } updateOutlineTitle() { @@ -183,7 +196,7 @@ export class OutboundCreator extends PluginCEBase { this.querySelector("#preview-note-middle-title") as HTMLElement ).dataset.l10nArgs = `{"show": "true"}`; this.querySelector("#preview-note-to-title")!.textContent = - this.targetNote?.getNoteTitle() || "No title"; + this.getPickedNotesTitle(this.targetNotes); } async updateNotePreview() { @@ -224,21 +237,26 @@ export class OutboundCreator extends PluginCEBase { } async getContentToInsert() { - if (!this.currentNote || !this.targetNote) return ""; - const forwardLink = this._addon.api.convert.note2link(this.targetNote, {}); - const content = await this._addon.api.template.runTemplate( - "[QuickInsertV2]", - "link, linkText, subNoteItem, noteItem", - [ - forwardLink, - this.targetNote.getNoteTitle().trim() || forwardLink, - this.targetNote, - this.currentNote, - ], - { - dryRun: true, - }, - ); + if (!this.currentNote || !this.targetNotes?.length) return ""; + let content = ""; + for (const note of this.targetNotes) { + const forwardLink = this._addon.api.convert.note2link(note, {}); + content += await this._addon.api.template.runTemplate( + "[QuickInsertV2]", + "link, linkText, subNoteItem, noteItem", + [ + forwardLink, + note.getNoteTitle().trim() || forwardLink, + note, + this.currentNote, + ], + { + dryRun: true, + }, + ); + content += "\n"; + } + return content; }