add: template stages

This commit is contained in:
xiangyu 2022-08-12 13:49:35 +08:00
parent bcf7c63d3e
commit aa2ce4c4ef
2 changed files with 119 additions and 16 deletions

View File

@ -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("<p> </p>");
}
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("<p> </p>");
}
}
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("<p> </p>");
}
await this._Addon.knowledge.addLineToNote(
undefined,
newLines.join("\n"),
@ -1378,10 +1408,32 @@ class AddonEvents extends AddonBase {
const newLines = [];
newLines.push("<p> </p>");
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("<p> </p>");
}
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
}</a></p>`;
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("<p> </p>");
}
}
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("<p> </p>");
}
await this._Addon.knowledge.addLineToNote(
undefined,
newLines.join("\n"),

View File

@ -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(