diff --git a/src/events.ts b/src/events.ts index cf5da24..404cba4 100644 --- a/src/events.ts +++ b/src/events.ts @@ -1311,28 +1311,58 @@ class AddonEvents extends AddonBase { const editor = await this._Addon.knowledge.getWorkspaceEditorInstance(); + const sharedObj = {}; + + let renderredTemplate = await this._Addon.template.renderTemplateAsync( + message.content.params.templateName, + "items, copyNoteImage, editor, sharedObj", + [items, copyNoteImage, editor, sharedObj], + true, + "beforeloop" + ); + + if (renderredTemplate) { + newLines.push(renderredTemplate); + newLines.push("
"); + } + for (const topItem of items) { /* Available variables: - topItem, itemNotes, copyNoteImage + topItem, itemNotes, copyNoteImage, editor */ const itemNotes: ZoteroItem[] = topItem .getNotes() .map((e) => Zotero.Items.get(e)); - const renderredTemplate = - await this._Addon.template.renderTemplateAsync( - message.content.params.templateName, - "topItem, itemNotes, copyNoteImage, editor", - [topItem, itemNotes, copyNoteImage, editor] - ); + renderredTemplate = await this._Addon.template.renderTemplateAsync( + message.content.params.templateName, + "topItem, itemNotes, copyNoteImage, editor, sharedObj", + [topItem, itemNotes, copyNoteImage, editor, sharedObj], + true, + "default" + ); if (renderredTemplate) { newLines.push(renderredTemplate); newLines.push("
"); } } + + renderredTemplate = await this._Addon.template.renderTemplateAsync( + message.content.params.templateName, + "items, copyNoteImage, editor, sharedObj", + [items, copyNoteImage, editor, sharedObj], + true, + "afterloop" + ); + + if (renderredTemplate) { + newLines.push(renderredTemplate); + newLines.push("
"); + } + await this._Addon.knowledge.addLineToNote( undefined, newLines.join("\n"), @@ -1378,10 +1408,32 @@ class AddonEvents extends AddonBase { const newLines = []; newLines.push("
"); + const toCopyImage = []; + + const copyNoteImage = (noteItem: ZoteroItem) => { + toCopyImage.push(noteItem); + }; + + const editor = await this._Addon.knowledge.getWorkspaceEditorInstance(); + const sharedObj = {}; + + let renderredTemplate = await this._Addon.template.renderTemplateAsync( + message.content.params.templateName, + "items, copyNoteImage, editor, sharedObj", + [notes, copyNoteImage, editor, sharedObj], + true, + "beforeloop" + ); + + if (renderredTemplate) { + newLines.push(renderredTemplate); + newLines.push("
"); + } + for (const noteItem of notes) { /* Available variables: - noteItem, topItem, link + noteItem, topItem, link, copyNoteImage, editor */ let topItem = noteItem.parentItem; while (topItem && !topItem.isRegularItem()) { @@ -1393,18 +1445,33 @@ class AddonEvents extends AddonBase { linkText ? linkText : linkURL }`; - const renderredTemplate = - await this._Addon.template.renderTemplateAsync( - message.content.params.templateName, - "noteItem, topItem, link", - [noteItem, topItem, link] - ); + renderredTemplate = await this._Addon.template.renderTemplateAsync( + message.content.params.templateName, + "noteItem, topItem, link, copyNoteImage, editor, sharedObj", + [noteItem, topItem, link, copyNoteImage, editor, sharedObj], + true, + "default" + ); if (renderredTemplate) { newLines.push(renderredTemplate); newLines.push("
"); } } + + renderredTemplate = await this._Addon.template.renderTemplateAsync( + message.content.params.templateName, + "notes, copyNoteImage, editor, sharedObj", + [notes, copyNoteImage, editor, sharedObj], + true, + "afterloop" + ); + + if (renderredTemplate) { + newLines.push(renderredTemplate); + newLines.push("
"); + } + await this._Addon.knowledge.addLineToNote( undefined, newLines.join("\n"), diff --git a/src/template.ts b/src/template.ts index 86cfa78..d7892e1 100644 --- a/src/template.ts +++ b/src/template.ts @@ -313,7 +313,8 @@ class AddonTemplate extends AddonBase { key: string, argString: string = "", argList: any[] = [], - useDefault: boolean = true + useDefault: boolean = true, + stage: string = "default" ) { Zotero.debug(`renderTemplate: ${key}`); let templateText = this.getTemplateText(key); @@ -324,6 +325,23 @@ class AddonTemplate extends AddonBase { } } + const templateLines = templateText.split("\n"); + let startIndex = templateLines.indexOf(`// @${stage}-begin`), + endIndex = templateLines.indexOf(`// @${stage}-end`); + if (startIndex < 0 && endIndex < 0 && stage !== "default") { + // Skip this stage + return ""; + } + if (startIndex < 0) { + // We skip the flag line later + startIndex = -1; + } + if (endIndex < 0) { + endIndex = templateLines.length; + } + // Skip the flag lines + templateText = templateLines.slice(startIndex + 1, endIndex).join("\n"); + let _newLine: string = ""; try { _newLine = new Function(argString, "return `" + templateText + "`")( @@ -340,7 +358,8 @@ class AddonTemplate extends AddonBase { key: string, argString: string = "", argList: any[] = [], - useDefault: boolean = true + useDefault: boolean = true, + stage: string = "default" ) { Zotero.debug(`renderTemplateAsync: ${key}`); let templateText = this.getTemplateText(key); @@ -351,6 +370,23 @@ class AddonTemplate extends AddonBase { } } + const templateLines = templateText.split("\n"); + let startIndex = templateLines.indexOf(`// @${stage}-begin`), + endIndex = templateLines.indexOf(`// @${stage}-end`); + if (startIndex < 0 && endIndex < 0 && stage !== "default") { + // Skip this stage + return ""; + } + if (startIndex < 0) { + // We skip the flag line later + startIndex = -1; + } + if (endIndex < 0) { + endIndex = templateLines.length; + } + // Skip the flag lines + templateText = templateLines.slice(startIndex + 1, endIndex).join("\n"); + let _newLine: string = ""; try { const AsyncFunction = Object.getPrototypeOf(