From d358bc28400cb4c093da0229b1facf82ff2e1675 Mon Sep 17 00:00:00 2001 From: xiangyu <3170102889@zju.edu.cn> Date: Mon, 23 May 2022 00:11:44 +0800 Subject: [PATCH] feat: default note template feat: item template support more variables --- src/events.ts | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/src/events.ts b/src/events.ts index 01fa127..7a4ea3c 100644 --- a/src/events.ts +++ b/src/events.ts @@ -1,5 +1,5 @@ import { link } from "fs"; -import { AddonBase, EditorMessage } from "./base"; +import { AddonBase, EditorMessage, NoteTemplate } from "./base"; class AddonEvents extends AddonBase { notifierCallback: object; @@ -791,19 +791,28 @@ class AddonEvents extends AddonBase { } const newLines = []; - newLines.push("
"); const templateText = this._Addon.template.getTemplateByName( message.content.params.templateName ).text; + const toCopyImage = []; + + const copyNoteImage = (noteItem: ZoteroItem) => { + toCopyImage.push(noteItem); + }; + for (const topItem of items) { /* Available variables: - topItem + topItem, itemNotes, copyNoteImage */ + const itemNotes: ZoteroItem[] = topItem + .getNotes() + .map((e) => Zotero.Items.get(e)); + let _newLine: string = ""; try { eval("_newLine = `" + templateText + "`"); @@ -815,6 +824,12 @@ class AddonEvents extends AddonBase { newLines.push("
"); } await this._Addon.knowledge.addLinesToNote(undefined, newLines, -1); + const mainNote = this._Addon.knowledge.getWorkspaceNote(); + await Zotero.DB.executeTransaction(async () => { + for (const subNote of toCopyImage) { + await Zotero.Notes.copyEmbeddedImages(subNote, mainNote); + } + }); } else if (message.type === "insertNoteUsingTemplate") { /* message.content = { @@ -846,7 +861,6 @@ class AddonEvents extends AddonBase { } const newLines = []; - newLines.push("
"); const templateText = this._Addon.template.getTemplateByName( @@ -1047,6 +1061,31 @@ class AddonEvents extends AddonBase { private resetState(): void { // Reset preferrence state. + let templatesRaw: string = Zotero.Prefs.get( + "Knowledge4Zotero.noteTemplate" + ); + if (!templatesRaw) { + Zotero.Prefs.set( + "Knowledge4Zotero.noteTemplate", + JSON.stringify([ + { + name: "[Item] item-notes with metadata", + text: '
| \n Title \n | \n \n ${topItem.getField(\'title\')}\n | \n
|---|---|
| \n Journal \n | \n \n ${topItem.getField(\'publicationTitle\')}\n | \n
| \n 1st Author \n | \n \n ${topItem.getField(\'firstCreator\')}\n | \n
| \n Authors \n | \n \n ${topItem.getCreators().map((v)=>v.firstName+" "+v.lastName).join("; ")}\n | \n
| \n Pub. date \n | \n \n ${topItem.getField(\'date\')}\n | \n
| \n DOI \n | \n \n ${topItem.getField(\'DOI\')}\n | \n
| \n Archive \n | \n \n ${topItem.getField(\'archive\')}\n | \n
| \n Archive Location \n | \n \n ${topItem.getField(\'archiveLocation\')}\n | \n
| \n Call No. \n | \n \n ${topItem.getField(\'callNumber\')}\n | \n
\n ${noteItem.getNote()}\n\nMerge Date: ${new Date().toISOString().substr(0,10)+" "+ new Date().toTimeString()}
\n
📝 Comments
\n\n`;\ncopyNoteImage(noteItem);\nreturn noteLine;\n}).join("\\n")}\n', + disabled: false, + }, + { + name: "[Note] with metadata", + text: "Make your comments
\n \n
Note: ${link}
\n${topItem?`Title: ${topItem.getField('title')}
\\nAuthor: ${topItem.getField('firstCreator')}
\\nDate: ${topItem.getField('date')}
`:''}", + disabled: false, + }, + { + name: "[Text] today", + text: "\n\n\nInsert more items with meta-data in workspace window->Edit
\n