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

Imported Items

"); 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("

Imported Notes

"); 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: '

💡 Meta Data

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n

Title

\n
\n ${topItem.getField(\'title\')}\n
\n

Journal

\n
\n ${topItem.getField(\'publicationTitle\')}\n
\n

1st Author

\n
\n ${topItem.getField(\'firstCreator\')}\n
\n

Authors

\n
\n ${topItem.getCreators().map((v)=>v.firstName+" "+v.lastName).join("; ")}\n
\n

Pub. date

\n
\n ${topItem.getField(\'date\')}\n
\n

DOI

\n
\n ${topItem.getField(\'DOI\')}\n
\n

Archive

\n
\n ${topItem.getField(\'archive\')}\n
\n

Archive Location

\n
\n ${topItem.getField(\'archiveLocation\')}\n
\n

Call No.

\n
\n ${topItem.getField(\'callNumber\')}\n
\n${itemNotes.map((noteItem)=>{\nconst noteLine = `

📜 Note: ${noteItem.key}

\n
\n ${noteItem.getNote()}\n

Merge Date: ${new Date().toISOString().substr(0,10)+" "+ new Date().toTimeString()}

\n
\n

📝 Comments

\n
\n

Make your comments

\n

\n
`;\ncopyNoteImage(noteItem);\nreturn noteLine;\n}).join("\\n")}\n', + disabled: false, + }, + { + name: "[Note] with metadata", + text: "

Note: ${link}

\n${topItem?`

Title: ${topItem.getField('title')}

\\n

Author: ${topItem.getField('firstCreator')}

\\n

Date: ${topItem.getField('date')}

`:''}", + disabled: false, + }, + { + name: "[Text] today", + text: "

TODO: ${new Date().toLocaleDateString()}

\n

Tasks

\n\n
\n

Insert more items with meta-data in workspace window->Edit

\n
\n

\n

Done Tasks

\n

\n

Todo Tomorrow

\n

\n", + disabled: false, + }, + ]) + ); + } } }