Skip to content

Commit

Permalink
优化进度列渲染,优化卡顿
Browse files Browse the repository at this point in the history
  • Loading branch information
MuiseDestiny committed Mar 9, 2023
1 parent a167058 commit 382044e
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 32 deletions.
1 change: 1 addition & 0 deletions addon/chrome/content/preferences.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<checkbox label="Tags" preference="extensions.zotero.__addonRef__.function.tagsColumn.enable" />
<checkbox label="#Tags" preference="extensions.zotero.__addonRef__.function.textTagsColumn.enable" />
<checkbox label="Rating" preference="extensions.zotero.__addonRef__.function.ratingColumn.enable" />
<checkbox label="Creator" preference="extensions.zotero.__addonRef__.function.creatorColumn.enable" />
<checkbox label="Progress" preference="extensions.zotero.__addonRef__.function.progressColumn.enable" />
<checkbox label="Graph View" preference="extensions.zotero.__addonRef__.function.graphView.enable" />
<checkbox label="Nested Tags" preference="extensions.zotero.__addonRef__.function.Tags.enable" />
Expand Down
7 changes: 7 additions & 0 deletions addon/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ pref("extensions.zotero.__addonRef__.function.itemTypeFilter.enable", true);
pref("extensions.zotero.__addonRef__.function.addNumberToCollectionTree.enable", true);
pref("extensions.zotero.__addonRef__.function.Tags.enable", true);

pref("extensions.zotero.__addonRef__.function.creatorColumn.enable", true);
pref("extensions.zotero.__addonRef__.creatorColumn.format", "${firstCreator} et al.");
pref("extensions.zotero.__addonRef__.creatorColumn.slices", "0:1");
pref("extensions.zotero.__addonRef__.creatorColumn.join", ", ");



pref("extensions.zotero.__addonRef__.nestedTags.sortord", "0");

pref("extensions.zotero.__addonRef__.GPT.secretKey", "");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zotero-style",
"version": "2.4.1",
"version": "2.4.2",
"description": "让你的Zotero看起来更有趣",
"config": {
"addonName": "Zotero Style",
Expand Down
1 change: 1 addition & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async function onStartup() {
views.createRatingColumn(),
views.initItemSelectListener(),
views.addNumberToCollectionTree(),
views.renderCreatorColumn(),
]
try {
await Promise.all(tasks);
Expand Down
132 changes: 104 additions & 28 deletions src/modules/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,63 @@ export default class Views {
)
}

public async renderCreatorColumn() {
if (!Zotero.Prefs.get(`${config.addonRef}.function.creatorColumn.enable`) as boolean) { return }
const key = "firstCreator"
await ztoolkit.ItemTree.addRenderCellHook(
key,
(index: number, data: string, column: any, original: Function) => {
const cellSpan = original(index, data, column) as HTMLSpanElement;
const item = ZoteroPane.getSortedItems()[index]
const creators = item.getCreators() as any
const firstCreator = item.firstCreator as string;
const format = Zotero.Prefs.get(`${config.addonRef}.creatorColumn.format`) as string
const join = Zotero.Prefs.get(`${config.addonRef}.creatorColumn.join`) as string
let slices = Zotero.Prefs.get(`${config.addonRef}.creatorColumn.slices`) as string
let newCreators: any = []
try {
slices.split(/,\s*/).forEach((slice: string) => {
newCreators = newCreators.concat(
creators.slice(...slice.split(":").filter(i => i.trim().length).map(i => Number(i)))
)
})
} catch { return cellSpan}
let textArray = []
for (let i = 0; i < newCreators.length; i++) {
textArray.push(
format
.replace(/\$\{firstName\}/g, newCreators[i].firstName)
.replace(/\$\{lastName\}/g, newCreators[i].lastName)
.replace(/\$\{firstCreator\}/g, firstCreator)
)
}
cellSpan.innerText = textArray.join(join)
return cellSpan
}
)

this.patchSetting(
key,
[
{
prefKey: "creatorColumn.format",
name: "Format",
type: "input"
},
{
prefKey: "creatorColumn.slices",
name: "Slices",
type: "input",
},
{
prefKey: "creatorColumn.join",
name: "Join",
type: "input",
},
]
)
}

private replaceCellIcon(item: Zotero.Item, cellSpan: HTMLSpanElement) {
const iconSpan = cellSpan.querySelector(".cell-icon") as HTMLSpanElement
let res = item.attachmentPath?.match(/\.(\w+)$/)
Expand Down Expand Up @@ -831,6 +888,39 @@ export default class Views {
*/
public async createProgressColumn() {
if (!Zotero.Prefs.get(`${config.addonRef}.function.progressColumn.enable`) as boolean) { return }
let update = async (item: Zotero.Item) => {
const cacheKey = `${item.key}-annoRecord`
if (item.isRegularItem()) {
let pdfItem
try {
pdfItem = await item.getBestAttachment()
} catch {
return
}
if (!pdfItem) {
return
}
let page: number = 0;
try {
page = Number(this.addonItem.get(item, "readingTime").page)
} catch { }
let annoRecord: any = { page, data: {} }
const annoArray = pdfItem.getAnnotations()
annoArray.forEach((anno: any) => {
const charNum = (anno.annotationText || anno.annotationComment || "").length
try {
let pageIndex = Number(JSON.parse(anno.annotationPosition).pageIndex)
const _page = pageIndex + 1
page = _page > page ? _page : page
annoRecord.data[pageIndex] ??= []
annoRecord.data[pageIndex].push({ value: charNum, color: anno.annotationColor })
} catch { }
})
annoRecord.page = page
this.cache[cacheKey] = annoRecord
return annoRecord
}
}
const key = "Progress"
await ztoolkit.ItemTree.register(
key,
Expand All @@ -842,10 +932,9 @@ export default class Views {
item: Zotero.Item
) => {
window.setTimeout(async () => {
const cacheKey = `${item.key}-getBestAttachment`
this.cache[cacheKey] = item.isRegularItem() && await item.getBestAttachment()
await update(item)
})
return this.addonItem.get(item, "readingTime")?.page
return ""
},
{
renderCellHook: (index: any, data: any, column: any) => {
Expand All @@ -856,30 +945,18 @@ export default class Views {
height: "20px"
}
}) as HTMLSpanElement

let item = ZoteroPane.itemsView.getRow(index).ref
let page: number = 0;
try {
page = Number(this.addonItem.get(item, "readingTime").page)
} catch { }
let annoRecord: any = { page, data: {} }
let pdfItem
const cacheKey = `${item.key}-getBestAttachment`
pdfItem = this.cache[cacheKey]
if (!pdfItem) { return span }
const annoArray = pdfItem.getAnnotations()
if (annoArray.length == 0) { return span }
annoArray.forEach((anno: any) => {
const charNum = (anno.annotationText || anno.annotationComment || "").length
try {
let pageIndex = Number(JSON.parse(anno.annotationPosition).pageIndex)
const _page = pageIndex + 1
page = _page > page ? _page : page
annoRecord.data[pageIndex] ??= []
annoRecord.data[pageIndex].push({ value: charNum, color: anno.annotationColor})
} catch { }
})
annoRecord.page = page
let selectedItems = ZoteroPane.getSelectedItems()
let item = ZoteroPane.getSortedItems()[index]
const cacheKey = `${item.key}-annoRecord`
let annoRecord = this.cache[cacheKey]
if (!annoRecord) { return span}
if (selectedItems.length) {
if (selectedItems[0].key == item.key) {
window.setTimeout(async () => {
await update(item)
})
}
}
const style = Zotero.Prefs.get(
`${config.addonRef}.progressColumn.style`
) as string || "bar"
Expand All @@ -895,7 +972,6 @@ export default class Views {
for (let i = 0; i < annoRecord.page; i++) {
values.push(annoRecord.data[i] ? annoRecord.data[i].map((i: any)=>i.value).reduce((a: any, b: any) => a + b) : 0)
}
console.log(annoRecord, values)

if (style != "stack") {
// 不是stack,需要精简数据
Expand Down
4 changes: 2 additions & 2 deletions update.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"[email protected]": {
"updates": [
{
"version": "2.4.1",
"version": "2.4.2",
"update_link": "https://github.com/muisedestiny/zotero-style/releases/latest/download/zotero-style.xpi",
"applications": {
"gecko": {
Expand All @@ -12,7 +12,7 @@
}
},
{
"version": "2.4.1",
"version": "2.4.2",
"update_link": "https://github.com/muisedestiny/zotero-style/releases/latest/download/zotero-style.xpi",
"applications": {
"zotero": {
Expand Down
2 changes: 1 addition & 1 deletion update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<rdf:Seq>
<rdf:li>
<rdf:Description>
<em:version>2.4.1</em:version>
<em:version>2.4.2</em:version>
<em:targetApplication>
<rdf:Description>
<em:id>[email protected]</em:id>
Expand Down

0 comments on commit 382044e

Please sign in to comment.