Skip to content

Commit

Permalink
chore: move link creator content generation outside window scope
Browse files Browse the repository at this point in the history
fix: content generation abort after window closed

fix: #1225
  • Loading branch information
windingwind committed Dec 13, 2024
1 parent a3b5ac8 commit 6c8b8dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
21 changes: 5 additions & 16 deletions src/elements/linkCreator/inboundCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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() {
Expand Down
19 changes: 5 additions & 14 deletions src/elements/linkCreator/outboundCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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";
}
Expand Down
16 changes: 13 additions & 3 deletions src/utils/linkCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 6c8b8dc

Please sign in to comment.