chore: move link creator content generation outside window scope

fix: content generation abort after window closed

fix: #1225
This commit is contained in:
windingwind 2024-12-13 13:15:43 +01:00
parent a3b5ac8050
commit 6c8b8dc988
3 changed files with 23 additions and 33 deletions

View File

@ -66,11 +66,10 @@ export class InboundCreator extends PluginCEBase {
async accept(io: any) { async accept(io: any) {
if (!this.targetNote) return; if (!this.targetNote) return;
const content = await this.getContentToInsert();
this.notePicker.saveRecentNotes(); this.notePicker.saveRecentNotes();
io.targetNoteID = this.targetNote.id; io.targetNoteID = this.targetNote.id;
io.content = content; io.sourceNoteIDs = [this.currentNote!.id];
io.lineIndex = this.getIndexToInsert(); io.lineIndex = this.getIndexToInsert();
} }
@ -216,21 +215,11 @@ export class InboundCreator extends PluginCEBase {
async getContentToInsert() { async getContentToInsert() {
if (!this.currentNote || !this.targetNote) return ""; if (!this.currentNote || !this.targetNote) return "";
const forwardLink = this._addon.api.convert.note2link(this.currentNote, {}); return await this._addon.api.template.runQuickInsertTemplate(
const content = await this._addon.api.template.runTemplate( this.currentNote,
"[QuickInsertV2]", this.targetNote,
"link, linkText, subNoteItem, noteItem", { dryRun: true },
[
forwardLink,
this.currentNote.getNoteTitle().trim() || forwardLink,
this.currentNote,
this.targetNote,
],
{
dryRun: true,
},
); );
return content;
} }
getIndexToInsert() { getIndexToInsert() {

View File

@ -69,10 +69,10 @@ export class OutboundCreator extends PluginCEBase {
async accept(io: any) { async accept(io: any) {
if (!this.targetNotes) return; if (!this.targetNotes) return;
const content = await this.getContentToInsert();
this.notePicker.saveRecentNotes(); this.notePicker.saveRecentNotes();
io.targetNoteID = this.currentNote!.id; io.targetNoteID = this.currentNote!.id;
io.sourceNoteIDs = this.targetNotes.map((item) => item.id).filter(Boolean);
io.content = content; io.content = content;
io.lineIndex = this.getIndexToInsert(); io.lineIndex = this.getIndexToInsert();
} }
@ -240,19 +240,10 @@ export class OutboundCreator extends PluginCEBase {
if (!this.currentNote || !this.targetNotes?.length) return ""; if (!this.currentNote || !this.targetNotes?.length) return "";
let content = ""; let content = "";
for (const note of this.targetNotes) { for (const note of this.targetNotes) {
const forwardLink = this._addon.api.convert.note2link(note, {}); content += await this._addon.api.template.runQuickInsertTemplate(
content += await this._addon.api.template.runTemplate( note,
"[QuickInsertV2]", this.currentNote,
"link, linkText, subNoteItem, noteItem", { dryRun: true },
[
forwardLink,
note.getNoteTitle().trim() || forwardLink,
note,
this.currentNote,
],
{
dryRun: true,
},
); );
content += "\n"; content += "\n";
} }

View File

@ -41,10 +41,20 @@ async function openLinkCreator(
await io.deferred.promise; await io.deferred.promise;
const targetNote = Zotero.Items.get(io.targetNoteID); const targetNote = Zotero.Items.get(io.targetNoteID);
const content = io.content;
const lineIndex = io.lineIndex;
if (!targetNote || !content) return; if (!targetNote) return;
const sourceNotes = Zotero.Items.get(io.sourceNoteIDs as number[]);
let content = "";
for (const note of sourceNotes) {
content += await addon.api.template.runQuickInsertTemplate(
note,
targetNote,
{ dryRun: false },
);
content += "\n";
}
const lineIndex = io.lineIndex;
await addLineToNote(targetNote, content, lineIndex); await addLineToNote(targetNote, content, lineIndex);
} }