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

View File

@ -69,10 +69,10 @@ export class OutboundCreator extends PluginCEBase {
async accept(io: any) {
if (!this.targetNotes) return;
const content = await this.getContentToInsert();
this.notePicker.saveRecentNotes();
io.targetNoteID = this.currentNote!.id;
io.sourceNoteIDs = this.targetNotes.map((item) => item.id).filter(Boolean);
io.content = content;
io.lineIndex = this.getIndexToInsert();
}
@ -240,19 +240,10 @@ export class OutboundCreator extends PluginCEBase {
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 += await this._addon.api.template.runQuickInsertTemplate(
note,
this.currentNote,
{ dryRun: true },
);
content += "\n";
}

View File

@ -41,10 +41,20 @@ async function openLinkCreator(
await io.deferred.promise;
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);
}