generated from MuiseDestiny/zotero-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 103
/
item.ts
153 lines (146 loc) · 3.68 KB
/
item.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const log = console.log
export default class AddonItem {
public item!: _ZoteroItem;
public title = "Addon Item"
public prefKey = "Zotero.AddonItem.key";
public cache: {[key: string]: any} = {};
constructor() {
}
/**
* 初始化插件所依赖条目
* @returns
*/
public async init() {
log("******\n\n")
log("AddonItem init is called")
let item: _ZoteroItem;
let addonItemKey = Zotero.Prefs.get(this.prefKey)
if (addonItemKey) {
item = await Zotero.Items.getByLibraryAndKeyAsync(1, addonItemKey)
if (item) {
this.item = item
log("From prefKey")
log("\n\n******")
this.hiddenNotes()
return
}
}
let s = new Zotero.Search();
s.addCondition("title", "contains", this.title);
var ids = await s.search();
let items = await Zotero.Items.getAsync(ids);
console.log(items)
if (ids.length) {
// exist
item = items[0]
log("From local")
} else {
// @ts-ignore
item = new Zotero.Item('computerProgram');
item.setField('title', this.title);
await item.saveTx()
log("From new")
}
Zotero.Prefs.set(this.prefKey, item.key)
this.item = item
this.hiddenNotes()
log("\n\n******")
}
/**
* @param item 哪个item的数据
* @param key 数据key
* @param data 数据
*/
public async set(item: _ZoteroItem, key: string, data: object | string) {
let noteItem = this.getNoteItem(item) || await this.createNoteItem()
let noteData = this.getNoteData(noteItem)
noteData[key] = data
noteItem.setNote(`${item.key}\n${JSON.stringify(noteData)}`)
await noteItem.saveTx()
}
/**
* @param item
* @param key
*/
public get(item: _ZoteroItem, key: string) {
if (!item) { return }
// 搜索本地
let noteItem
try {
noteItem = this.getNoteItem(item)
} catch { }
if (noteItem) {
return (this.getNoteData(noteItem))[key];
}
}
/**
* 获取笔记记录的数据
* @param noteItem
* @returns
*/
public getNoteData(noteItem: _ZoteroItem) {
try {
return JSON.parse(noteItem.note.replace(/<.+?>/g, "").replace(/[^\n\{]+/, ""))
} catch {
return {}
}
}
/**
* 创建一个空白笔记
* @returns
*/
public async createNoteItem() {
//@ts-ignore
let noteItem = new Zotero.Item('note')
noteItem.parentID = this.item.id;
await noteItem.saveTx()
return noteItem
}
/**
* item对应的笔记,根据item.key == noteItem._displayTitle寻找
* @param item
* @returns
*/
public getNoteItem(item: _ZoteroItem) {
if (!item) { return }
const key = item.key
const cacheKey = `getNoteItem-${key}`
if (this.cache[cacheKey]) { this.cache[cacheKey] }
const ids = this.item.getNotes()
let noteItem
for (let id of ids) {
let idInfo = Zotero.Items.getLibraryAndKeyFromID(id)
let _noteItem = Zotero.Items.getByLibraryAndKey(idInfo.libraryID, idInfo.key)
if (_noteItem._displayTitle.includes(key)) {
noteItem = _noteItem
this.cache[cacheKey] = noteItem
break
}
}
return noteItem
}
public hiddenNotes() {
const excludeKey = this.item.key
const search = Zotero.Search.prototype.search;
const itemTitle = this.title
Zotero.Search.prototype.search = async function () {
let ids = await search.apply(this, arguments);
// 只有在搜索结果是笔记时才过滤
if (
Zotero.Items.get(ids[0]).itemTypeID == 26 &&
Zotero.Items.get(ids.slice(-1)[0]).itemTypeID == 26
) {
log("hook ids", ids.length)
return ids.filter((id: number) => {
const parentID = Zotero.Items.get(id).parentID
if (!parentID) { return true }
const parentItem = Zotero.Items.get(parentID)
if (!parentItem) { return true }
return parentItem.key != excludeKey && parentItem.getField("title") != itemTitle
})
} else {
return ids
}
}
}
}