add: async template rendering
This commit is contained in:
parent
24a6e44446
commit
8ecd9b7034
1
build.js
1
build.js
|
|
@ -101,6 +101,7 @@ esbuild
|
|||
// Entry should be the same as addon/chrome/content/overlay.xul
|
||||
outfile: path.join(buildDir, "addon/chrome/content/scripts/index.js"),
|
||||
// minify: true,
|
||||
target: ['firefox60']
|
||||
})
|
||||
.catch(() => process.exit(1));
|
||||
|
||||
|
|
|
|||
|
|
@ -1045,7 +1045,7 @@ class AddonEvents extends AddonBase {
|
|||
*/
|
||||
const newLines = [];
|
||||
|
||||
const renderredTemplate = this._Addon.template.renderTemplate(
|
||||
const renderredTemplate = await this._Addon.template.renderTemplateAsync(
|
||||
message.content.params.templateName
|
||||
);
|
||||
|
||||
|
|
@ -1094,6 +1094,8 @@ class AddonEvents extends AddonBase {
|
|||
toCopyImage.push(noteItem);
|
||||
};
|
||||
|
||||
const editor = await this._Addon.knowledge.getWorkspaceEditorInstance();
|
||||
|
||||
for (const topItem of items) {
|
||||
/*
|
||||
Available variables:
|
||||
|
|
@ -1104,11 +1106,12 @@ class AddonEvents extends AddonBase {
|
|||
.getNotes()
|
||||
.map((e) => Zotero.Items.get(e));
|
||||
|
||||
const renderredTemplate = this._Addon.template.renderTemplate(
|
||||
message.content.params.templateName,
|
||||
"topItem, itemNotes, copyNoteImage",
|
||||
[topItem, itemNotes, copyNoteImage]
|
||||
);
|
||||
const renderredTemplate =
|
||||
await this._Addon.template.renderTemplateAsync(
|
||||
message.content.params.templateName,
|
||||
"topItem, itemNotes, copyNoteImage, editor",
|
||||
[topItem, itemNotes, copyNoteImage, editor]
|
||||
);
|
||||
|
||||
if (renderredTemplate) {
|
||||
newLines.push(renderredTemplate);
|
||||
|
|
@ -1170,11 +1173,12 @@ class AddonEvents extends AddonBase {
|
|||
linkText ? linkText : linkURL
|
||||
}</a></p>`;
|
||||
|
||||
const renderredTemplate = this._Addon.template.renderTemplate(
|
||||
message.content.params.templateName,
|
||||
"noteItem, topItem, link",
|
||||
[noteItem, topItem, link]
|
||||
);
|
||||
const renderredTemplate =
|
||||
await this._Addon.template.renderTemplateAsync(
|
||||
message.content.params.templateName,
|
||||
"noteItem, topItem, link",
|
||||
[noteItem, topItem, link]
|
||||
);
|
||||
|
||||
if (renderredTemplate) {
|
||||
newLines.push(renderredTemplate);
|
||||
|
|
@ -1346,7 +1350,7 @@ class AddonEvents extends AddonBase {
|
|||
annotations[0].attachmentItemID
|
||||
).parentID;
|
||||
|
||||
const renderredTemplate = this._Addon.template.renderTemplate(
|
||||
const renderredTemplate = await this._Addon.template.renderTemplateAsync(
|
||||
"[QuickNote]",
|
||||
"annotationItem, topItem",
|
||||
[annotationItem, annotationItem.parentItem.parentItem]
|
||||
|
|
|
|||
|
|
@ -1042,7 +1042,7 @@ class Knowledge extends AddonBase {
|
|||
);
|
||||
const subNoteLines = convertResult.lines;
|
||||
|
||||
const templateText = this._Addon.template.renderTemplate(
|
||||
const templateText = await this._Addon.template.renderTemplateAsync(
|
||||
"[QuickImport]",
|
||||
"subNoteLines, subNoteItem, noteItem",
|
||||
[subNoteLines, subNote, currentNote]
|
||||
|
|
|
|||
|
|
@ -296,6 +296,37 @@ class AddonTemplate extends AddonBase {
|
|||
return _newLine;
|
||||
}
|
||||
|
||||
async renderTemplateAsync(
|
||||
key: string,
|
||||
argString: string = "",
|
||||
argList: any[] = [],
|
||||
useDefault: boolean = true
|
||||
) {
|
||||
Zotero.debug(`renderTemplateAsync: ${key}`);
|
||||
let templateText = this.getTemplateText(key);
|
||||
if (useDefault && !templateText) {
|
||||
templateText = this._defaultTemplates.find((t) => t.name === key).text;
|
||||
if (!templateText) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
let _newLine: string = "";
|
||||
try {
|
||||
const AsyncFunction = Object.getPrototypeOf(
|
||||
async function () {}
|
||||
).constructor;
|
||||
const _ = new AsyncFunction(argString, "return `" + templateText + "`");
|
||||
console.log(_);
|
||||
_newLine = await _(...argList);
|
||||
} catch (e) {
|
||||
// alert(`Template ${key} Error: ${e}`);
|
||||
console.log(e);
|
||||
return "";
|
||||
}
|
||||
return _newLine;
|
||||
}
|
||||
|
||||
getTemplateKeys(): NoteTemplate[] {
|
||||
let templateKeys: string = Zotero.Prefs.get(
|
||||
"Knowledge4Zotero.templateKeys"
|
||||
|
|
|
|||
|
|
@ -406,7 +406,7 @@ class AddonViews extends AddonBase {
|
|||
false
|
||||
);
|
||||
const subNoteLines = convertResult.lines;
|
||||
const templateText = this._Addon.template.renderTemplate(
|
||||
const templateText = await this._Addon.template.renderTemplateAsync(
|
||||
"[QuickImport]",
|
||||
"subNoteLines, subNoteItem, noteItem",
|
||||
[subNoteLines, note, this._Addon.knowledge.getWorkspaceNote()]
|
||||
|
|
|
|||
Loading…
Reference in New Issue