Skip to content

Commit

Permalink
theme
Browse files Browse the repository at this point in the history
  • Loading branch information
MuiseDestiny committed Mar 17, 2023
1 parent c3c600e commit 9d7dbbe
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 33 deletions.
3 changes: 2 additions & 1 deletion addon/chrome/content/dist/assets/index.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion addon/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ pref("extensions.zotero.__addonRef__.enable", true);
pref("extensions.zotero.__addonRef__.function.graphView.enable", true);
pref("extensions.zotero.__addonRef__.graphView.show", false);
pref("extensions.zotero.__addonRef__.graphView.height", "400px");
pref("extensions.zotero.__addonRef__.graphView.theme", "light");


pref("extensions.zotero.__addonRef__.function.tagsColumn.enable", true);
pref("extensions.zotero.__addonRef__.tagsColumn.margin", "0.35");
pref("extensions.zotero.__addonRef__.tagsColumn.align", "left");

pref("extensions.zotero.__addonRef__.function.textTagsColumn.enable", true);
pref("extensions.zotero.__addonRef__.textTagsColumn.prefix", "#");
pref("extensions.zotero.__addonRef__.textTagsColumn.match", "");
pref("extensions.zotero.__addonRef__.textTagsColumn.match", "#");
pref("extensions.zotero.__addonRef__.textTagsColumn.opacity", "0.5");
pref("extensions.zotero.__addonRef__.textTagsColumn.backgroundColor", "#fadec9");
pref("extensions.zotero.__addonRef__.textTagsColumn.textColor", "black");
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.7",
"version": "2.4.8",
"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 @@ -95,6 +95,7 @@ function onShutdown(): void {
ztoolkit.unregisterAll()
ztoolkit.UI.unregisterAll()
ztoolkit.ItemTree.unregisterAll()

// 移除创建的按钮
document.querySelector("#zotero-style-show-hide-graph-view")?.remove();
// 恢复嵌套标签
Expand Down
1 change: 1 addition & 0 deletions src/modules/bubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class Bubble {
window.clearTimeout(Number(closeTimer))
}
// 透明度
ele.style.display = ""
ele.style.opacity = "1"
} else {
ele = ztoolkit.UI.createElement(document, "div", {
Expand Down
200 changes: 174 additions & 26 deletions src/modules/graphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const d3 = require("./d3")
export default class GraphView {
private renderer: any;
private container!: HTMLDivElement;
private resizer!: HTMLDivElement;
private urls: any = {
Zotero: "https://www.zotero.org/",
Style: "https://github.com/MuiseDestiny/zotero-style",
Expand All @@ -15,6 +16,10 @@ export default class GraphView {
Issue: "https://github.com/MuiseDestiny/zotero-style/issues/new/choose",
Plugins: "https://zotero-chinese.gitee.io/zotero-plugins/#/"
}
private functions: any = {
light: () => {this.setTheme("light")},
dark: () => { this.setTheme("dark") },
}
private cache: any ={};
private mode = "default"
private modeFunction: {[mode: string]: Function}
Expand All @@ -24,7 +29,6 @@ export default class GraphView {
related: this.getGraphByRelatedLink.bind(this),
author: this.getGraphByAuthorLink.bind(this),
tag: this.getGraphByTagLink.bind(this),
publication: this.getGraphByPublicationLink.bind(this),
};
}

Expand All @@ -33,13 +37,131 @@ export default class GraphView {
this.registerButton()
}

public setTheme(theme?: "light" | "dark" | undefined) {
ztoolkit.log("set theme", theme)
if (!theme) {
theme = Zotero.Prefs.get(`${config.addonRef}.graphView.theme`) as ("light" | "dark")
if (["light", "dark"].indexOf(theme) == -1) {
theme = "light"
}
}
const fillTag = 7583672
const themeColor = {
light: {
backgroundColor: "#ffffff",
resizerColor: "#cccccc",
colors: {
"fill": {
"a": 1,
"rgb": 5921370
},
"fillFocused": {
"a": 1,
"rgb": 9137391
},
"fillTag": {
"a": .5,
"rgb": fillTag
},
"fillUnresolved": {
"a": 0.5,
"rgb": 11250603
},
"fillAttachment": {
"a": 1,
"rgb": 14724096
},
"arrow": {
"a": 0.5,
"rgb": 2236962
},
"circle": {
"a": 1,
"rgb": 9137391
},
"line": {
"a": 1,
"rgb": 13948116
},
"text": {
"a": 1,
"rgb": 2236962
},
"fillHighlight": {
"a": 1,
"rgb": 9730288
},
"lineHighlight": {
"a": 1,
"rgb": 9730288
}
}
},
dark: {
backgroundColor: "#2e3441",
resizerColor: "#3b4252",
colors: {
"fill": {
"a": 1,
"rgb": 12237498
},
"fillFocused": {
"a": 1,
"rgb": 9137391
},
"fillTag": {
"a": .5,
"rgb": fillTag
},
"fillUnresolved": {
"a": 0.5,
"rgb": 6710886
},
"fillAttachment": {
"a": 1,
"rgb": 14737009
},
"arrow": {
"a": 0.5,
"rgb": 14342874
},
"circle": {
"a": 1,
"rgb": 9137391
},
"line": {
"a": .23,
"rgb": 13948116
},
"text": {
"a": 1,
"rgb": 14342874
},
"fillHighlight": {
"a": 1,
"rgb": 8215533
},
"lineHighlight": {
"a": 1,
"rgb": 8215533
}
}
}
}
this.renderer.colors = themeColor[theme].colors
this.renderer.testCSS();
this.container.style.backgroundColor = themeColor[theme].backgroundColor;
this.resizer.style.backgroundColor = themeColor[theme].resizerColor;
this.renderer.containerEl.style.backgroundColor = themeColor[theme].backgroundColor;
}

private registerButton() {
console.log("registerButton is called")
const node = document.querySelector("#zotero-tb-advanced-search")
console.log(node)
let newNode = node?.cloneNode(true) as XUL.ToolBarButton
newNode.setAttribute("id", "zotero-style-show-hide-graph-view")
newNode.setAttribute("tooltiptext", "Hi, I am your Style.")
newNode.setAttribute("tooltiptext", "show/hide graph view")
newNode.setAttribute("command", "")
newNode.setAttribute("oncommand", "")
newNode.addEventListener("click", async () => {
Expand Down Expand Up @@ -86,16 +208,27 @@ export default class GraphView {
private getGraphByDefaultLink() {
return {
nodes: {
// url
Zotero: { links: { Style: true }, type: "url"},
Style: { links: { Zotero: true }, type: "url" },
Help: { links: { Style: true }, type: "url" },
Share: { links: { Style: true }, type: "url" },
Issue: { links: { Style: true }, type: "url" },
Plugins: {links: {Zotero: true, Style: true}, type: "url"}
Plugins: { links: { Zotero: true, Style: true }, type: "url" },
// function
Theme: { links: { Style: true }, type: "tag" },
light: { links: { Theme: true }, type: "function" },
dark: { links: { Theme: true }, type: "function" }

}
}
}

/**
* 计划分析日,周,月阅读的文献
* @param items
* @returns
*/
private async _getGraphByDefaultLink(items: Zotero.Item[]) {
const recentDay = "Recent Day"
const recentWeek = "Recent Week"
Expand Down Expand Up @@ -165,21 +298,42 @@ export default class GraphView {
return graph
}

private getGraphByItemArrLink(items: Zotero.Item[], getArr: Function) {
let nodes: { [key: string]: any } = {}
let graph: { [key: string]: any } = { nodes }
private async getGraphByItemArrLink(items: Zotero.Item[], getArr: Function) {
const nodes: { [key: string]: any } = {}
const graph: { [key: string]: any } = { nodes }
const sharedValues: { [key: string]: { count: number, items: Set<Zotero.Item> } } = {}

// 找出所有共享值
items.forEach((item) => {
let id = item.id
nodes[id] = { links: {}, type: "item" }
const values = getArr(item)
const otherItems = items.filter(i => i != item)
otherItems.forEach(otherItem => {
const hasCommonValue = getArr(otherItem).find((value: string) => {
return values.indexOf(value) != -1
})
if (hasCommonValue) {
nodes[id].links[otherItem.id] = true
if (values.length == 0) {
nodes[item.id] = { links: {}, type: "item" }
}
values.forEach((value: string) => {
if (!sharedValues.hasOwnProperty(value)) {
sharedValues[value] = { count: 0, items: new Set() }
}
sharedValues[value].count += 1
sharedValues[value].items.add(item)
})
})
// 根据分位点,计算临界值
const pct = 0.95
const countArr = Object.values(sharedValues).map(i => i.count).filter(i=>i>1).sort()
const limit = countArr[parseInt((countArr.length * pct).toFixed(0))]
console.log(limit)
// 创建节点对象
Object.keys(sharedValues).forEach((value: string) => {
const items = [...sharedValues[value].items]
items.forEach(item => {
nodes[item.id] ??= { links: {}, type: "item" }
if (items.length > limit) {
nodes[item.id].links[value] = true;
(nodes[value] ??= {links: {}, type: "tag"}).links[item.id] = true
}
items.filter(i => i != item).forEach(_item => {
nodes[item.id].links[_item.id] = true
})
})
})
return graph
Expand All @@ -199,21 +353,14 @@ export default class GraphView {
const allTags = item.getTags().map(tag => tag.tag).filter(i=>!i.startsWith("/"))
let tags: string[] = []
allTags.forEach(tag => {
tags.push(tag)
tag.split("/").forEach(i => tags.push(i))
})
return tags
}
return this.getGraphByItemArrLink(items, getTags)
}

private getGraphByPublicationLink(items: Zotero.Item[]) {
let getPublicationTitle = (item: Zotero.Item) => {
return [item.getField("publicationTitle")]
}
return this.getGraphByItemArrLink(items, getPublicationTitle)
}


private async createContainer() {
document.querySelectorAll("#graph").forEach(e => e.remove());
document.querySelectorAll(".resizer").forEach(e => e.remove())
Expand Down Expand Up @@ -328,7 +475,7 @@ export default class GraphView {
await this.initIFrame(frame)

// 调节高度
const resizer = ztoolkit.UI.createElement(document, "div", {
const resizer = this.resizer = ztoolkit.UI.createElement(document, "div", {
styles: {
height: `1px`,
width: "100%",
Expand Down Expand Up @@ -369,10 +516,9 @@ export default class GraphView {
document.removeEventListener('mouseup', mouseUpHandler);
};
resizer.addEventListener('mousedown', mouseDownHandler);

this.setTheme()
}


private async initIFrame(frame: HTMLIFrameElement) {
// 等待js执行结束
// @ts-ignore
Expand Down Expand Up @@ -417,6 +563,8 @@ export default class GraphView {
ZoteroPane.itemsView.selectItem(Number(id))
} else if (type == "url") {
Zotero.launchURL(this.urls[id]);
} else if (type == "function"){
this.functions[id]()
}
}
/**
Expand Down
7 changes: 6 additions & 1 deletion src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ const utils = {
// })
window.setTimeout(async () => {
this.requests.cache = {}
const response = await this.requests.get(
let response = await this.requests.get(
`https://easyscholar.cc/homeController/getQueryTable.ajax?sourceName=${escape(publicationTitle)}`,
)
response = response || await this.requests.get(
`https://easyscholar.cc/homeController/getQueryTable.ajax?sourceName=${publicationTitle}`,
)
console.log(
response)
if (response && response.data) {
let data = response.data[0]
if (data) {
Expand Down
1 change: 1 addition & 0 deletions src/modules/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,7 @@ export default class Views {
public async createGraphView() {
if (!Zotero.Prefs.get(`${config.addonRef}.function.graphView.enable`) as boolean) { return }
await (new GraphView()).init();

}

/**
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.7",
"version": "2.4.8",
"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.7",
"version": "2.4.8",
"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.7</em:version>
<em:version>2.4.8</em:version>
<em:targetApplication>
<rdf:Description>
<em:id>[email protected]</em:id>
Expand Down

0 comments on commit 9d7dbbe

Please sign in to comment.