-
Notifications
You must be signed in to change notification settings - Fork 11
/
NewCommonNote
81 lines (69 loc) · 2.47 KB
/
NewCommonNote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// New Common Note
// @author windingwind, wklimowicz
// @Modify by [jackhanyuan](https://github.com/jackhanyuan)
// @link https://github.com/windingwind/zotero-tag/discussions/109
//
// [New Common Note]
// Name : New Common Note
// Event : None
// Operation : Script
// Data : JS code below
// Shortcut : None
// Menu Label: New Common Note
const Zotero = require("Zotero");
const window = require("window");
if (!Zotero.BetterNotes?.api.note?.insert) {
return "[Action:Auto-Template] Better Notes for Zotero > 1.1.4-21 is not installed or disabled.";
}
if (!item) {
return "[Action:Auto-Template] Target item is empty";
}
// Check if any note exists for the item
const autoTemplateTag = "auto-template";
const notes = Zotero.Items.get(item.getNotes());
if (notes.length > 0) {
return "A note exists for this item";
}
const prefsKey = "extensions.actionsTags.customAction.autoRunBNCommonTemplate";
// Get template name
let templateName = Zotero.Prefs.get(prefsKey, true);
let templateContent = Zotero.BetterNotes.api.template.getTemplateText(templateName);
if (!templateContent) {
// Template name is not defined or invalid. Get from input
const templateNames = Zotero.BetterNotes.api.template.getTemplateKeys();
function selectItems(itemList) {
var io = { dataIn: itemList, dataOut: null };
window.openDialog("chrome://zotero/content/ingester/selectitems.xhtml",
"_blank", "chrome,modal,centerscreen,resizable=yes", io);
return io.dataOut;
}
templateName = Object.values(selectItems(templateNames))[0];
templateContent = Zotero.BetterNotes.api.template.getTemplateText(templateName);
if (!templateContent) {
return "[Action:Auto-Template] Template is invalid";
}
Zotero.Prefs.set(prefsKey, templateName, true);
}
const parentItem = Zotero.Items.getTopLevel([item])[0];
const noteItem = new Zotero.Item("note");
noteItem.libraryID = parentItem.libraryID;
noteItem.parentID = parentItem.id;
noteItem.addTag(autoTemplateTag, 0);
await noteItem.saveTx();
let html = "";
if (templateName.toLowerCase().startsWith("[item]")) {
html = await Zotero.BetterNotes.api.template.runItemTemplate(templateName, {
itemIds: [parentItem.id],
targetNoteId: noteItem.id,
});
} else {
html = await Zotero.BetterNotes.api.template.runTextTemplate(templateName, {
targetNoteId: noteItem.id,
});
}
await Zotero.BetterNotes.api.note.insert(
noteItem,
html,
-1,
);
return `[Action:Auto-Template] Note ${noteItem.getNoteTitle()} is created on item ${item.getField("title")} from template ${templateName}`;