Skip to content

Commit

Permalink
期刊标签修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
MuiseDestiny committed Apr 18, 2023
1 parent 128cae0 commit 751a688
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 332 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zotero-style",
"version": "2.6.1",
"version": "2.6.2",
"description": "A Zotero plugin for creating a personal Zotero Style.",
"config": {
"addonName": "Zotero Style",
Expand Down Expand Up @@ -33,6 +33,7 @@
"homepage": "https://github.com/muisedestiny/zotero-style#readme",
"dependencies": {
"3d-force-graph": "^1.71.1",
"color-rna": "^1.0.1",
"d3": "^7.8.2",
"math": "^0.0.3",
"pako": "^2.1.0",
Expand Down
9 changes: 2 additions & 7 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import Events from "./modules/events";
import AddonItem from "./modules/item";
import { registerPrefsScripts, registerPrefs } from "./modules/prefs";
import LocalStorage from "./modules/localStorage";
import GraphView from "./modules/graphView";

async function onStartup() {
await Zotero.Promise.delay(1000)
registerPrefs();
// Register the callback in Zotero as an item observer
const notifierID = Zotero.Notifier.registerObserver(
Expand All @@ -34,10 +32,6 @@ async function onStartup() {
Zotero.uiReadyPromise,
]);
initLocale();

// // 不争不抢先加载
await Zotero.Promise.delay(1000)

// 初始化储存位置
let storage
const storageIn = Zotero.Prefs.get(`${config.addonRef}.storage.in`) as string
Expand All @@ -60,7 +54,8 @@ async function onStartup() {
events.onInit()

const views = new Views(storage)

// 为用户默认打开显示所有标签,若要显示分类下,需要手动关闭
Zotero.Prefs.set("tagSelector.displayAllTags", true)
const tasks = [
views.createGraphView(),
views.renderTitleColumn(),
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ if (!basicTool.getGlobal("Zotero")[config.addonInstance]) {
_globalThis.ztoolkit = addon.data.ztoolkit;
ztoolkit.basicOptions.log.prefix = `[${config.addonName}]`;
ztoolkit.basicOptions.log.disableConsole = addon.data.env === "production";
ztoolkit.UI.basicOptions.ui.enableElementJSONLog =
addon.data.env === "development";
ztoolkit.UI.basicOptions.ui.enableElementJSONLog = false
// addon.data.env === "development";
ztoolkit.basicOptions.log.disableConsole = true;
ztoolkit.UI.basicOptions.ui.enableElementJSONLog = true;
ztoolkit.UI.basicOptions.ui.enableElementJSONLog = false;
Zotero.ZoteroStyle = addon;
// Trigger addon hook for initialization
addon.hooks.onStartup();
Expand Down
15 changes: 15 additions & 0 deletions src/modules/easyscholar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@ const field2Info: any = {
rank = 1
}
return { rank, key, value }
},
CPU(s: string) {
let key = "CPU", value = s, rank
if (s == "一流") {
rank = 1
} else if (s == "权威") {
rank = 2
} else if (s == "学科顶尖") {
rank = 3
} else if (s == "学科一流") {
rank = 4
} else if (s == "学科重要") {
rank = 5
}
return { rank, key, value }
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/modules/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ class LocalStorage {
this.lock.resolve()
}

get(item: Zotero.Item, key: string) {
if (this.cache == undefined) { return }
get(item: Zotero.Item | {key: string}, key: string) {
if (this.cache == undefined) {
console.log("cache is undefined")
return
}
return (this.cache[item.key] ??= {})[key]
}

async set(item: Zotero.Item, key: string, value: any) {
async set(item: Zotero.Item | {key: string}, key: string, value: any) {
await this.lock.promise;
(this.cache[item.key] ??= {})[key] = value
window.setTimeout(async () => {
Expand Down
28 changes: 28 additions & 0 deletions src/modules/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,33 @@ export default class Progress {
return sColor;
}

static shade(col: any, amt: any) {
var usePound = false;

if (col[0] == "#") {
col = col.slice(1);
usePound = true;
}

var num = parseInt(col, 16);

var r = (num >> 16) + amt;

if (r > 255) r = 255;
else if (r < 0) r = 0;

var b = ((num >> 8) & 0x00FF) + amt;

if (b > 255) b = 255;
else if (b < 0) b = 0;

var g = (num & 0x0000FF) + amt;

if (g > 255) g = 255;
else if (g < 0) g = 0;

return (usePound ? "#" : "") + (g | (b << 8) | (r << 16)).toString(16);
}

}

235 changes: 152 additions & 83 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,160 @@
import Requests from "E:/Github/zotero-reference/src/modules/requests";

import localStorage from "./localStorage";
import { config } from "../../package.json";
import LocalStorage from "./localStorage";

let lock: undefined | _ZoteroPromiseObject = undefined
const utils = {
requests: new Requests(),
localStorage: new localStorage(config.addonRef),
wait(item: Zotero.Item, key: string, local: boolean=true) {
switch (key) {
case "publication":
const publicationTitle = this.getPublicationTitle(item)
if (publicationTitle == "") { return }
let data
if (local) {
data = this.localStorage.get(item, key)
}
if (data !== undefined) { return data }
// 开启一个异步更新影响因子
let secretKey = Zotero.Prefs.get(`${config.addonRef}.easyscholar.secretKey`) as string
window.setTimeout(async () => {
this.requests.cache = {}
if (secretKey) {
await lock;
lock = Zotero.Promise.defer()
let response
try {
response = await this.requests.get(
`https://easyscholar.cc/open/getPublicationRank?secretKey=${secretKey}&publicationName=${encodeURIComponent(publicationTitle)}`,
)
} catch { console.log(response) }
// 延迟
await Zotero.Promise.delay(1000)
lock!.resolve()
if (response && response.data) {
// 自定义数据集+官方数据集合并
let officialAllData = response.data.officialRank.all
if (!officialAllData) {
if (!local) {
new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true })
.createLine({ text: "Not Found", type: "default" }).show()
}
return await this.localStorage.set(item, key, "")
}
let customRankInfo = response.data.customRank.rankInfo
response.data.customRank.rank.forEach((rankString: string) => {
try {
// 1613160542602600448&&&2
let uuid: string, rank: string;
[uuid, rank] = rankString.split("&&&")
rank = {
"1": "oneRankText",
"2": "twoRankText",
"3": "threeRankText",
"4": "fourRankText",
"5": "fiveRankText"
}[rank] as string
let info = customRankInfo.find((i:any) => i.uuid == uuid)
officialAllData[info.abbName] = info[rank]
} catch {}
})
if (officialAllData) {
await this.localStorage.set(item, key, officialAllData)
// 显示它支持的所有字段
let popupWin = new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true }).show()
popupWin.createLine({text: publicationTitle, type: "default"})
Object.keys(officialAllData).forEach(k => {
popupWin.createLine({ text: `${k}: ${officialAllData[k]}`, type: "success" })
})
ztoolkit.ItemTree.refresh()
}
}
} else {
if (!local) {
new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true })
.createLine({ text: "No easyScholar secret key configured", type: "fail" }).show()
}
}
})
return
default:
break
const requests = new Requests()

export function getPublicationTitle(item: Zotero.Item) {
return [item.getField("publicationTitle"), item.getField("proceedingsTitle")].find(i => i.trim().length > 0)
}

/**
* @param localStorage
* @param item
* @param tip
* @returns -1 失败 0 数据库无信息 1 成功
*/
export async function updatePublicationTags(localStorage: LocalStorage, item: Zotero.Item, tip: boolean = false) {
const publicationTitle = getPublicationTitle(item)
let secretKey = Zotero.Prefs.get(`${config.addonRef}.easyscholar.secretKey`) as string
if (!secretKey) { return -1}
let response
try {
response = await requests.get(
`https://easyscholar.cc/open/getPublicationRank?secretKey=${secretKey}&publicationName=${encodeURIComponent(publicationTitle)}`,
)
} catch { console.log(response) }
if (response && response.data) {
// 自定义数据集 + 官方数据集合并
let officialAllData = response.data.officialRank.all
if (!officialAllData) {
if (tip) {
new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true })
.createLine({ text: "Not Found", type: "default" }).show()
}
await localStorage.set(item, "publication", "")
return 0
}
let customRankInfo = response.data.customRank.rankInfo
response.data.customRank.rank.forEach((rankString: string) => {
try {
// 1613160542602600448&&&2
let uuid: string, rank: string;
[uuid, rank] = rankString.split("&&&")
rank = {
"1": "oneRankText",
"2": "twoRankText",
"3": "threeRankText",
"4": "fourRankText",
"5": "fiveRankText"
}[rank] as string
let info = customRankInfo.find((i: any) => i.uuid == uuid)
officialAllData[info.abbName] = info[rank]
} catch { }
})
if (officialAllData) {
await localStorage.set(item, "publication", officialAllData)
// 显示它支持的所有字段
if (tip) {
let popupWin = new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true }).show()
popupWin.createLine({ text: publicationTitle, type: "default" })
Object.keys(officialAllData).forEach(k => {
popupWin.createLine({ text: `${k}: ${officialAllData[k]}`, type: "success" })
})
ztoolkit.ItemTree.refresh()
}
return 1
}
},
getPublicationTitle(item: Zotero.Item) {
return [item.getField("publicationTitle"), item.getField("proceedingsTitle")].find(i=>i.trim().length > 0)
}
}

export default utils
// let lock: undefined | _ZoteroPromiseObject = undefined
// const utils = {
// requests: new Requests(),
// localStorage: new localStorage(config.addonRef),
// wait(item: Zotero.Item, key: string, local: boolean=true) {
// switch (key) {
// case "publication":
// const publicationTitle = this.getPublicationTitle(item)
// if (publicationTitle == "") { return }
// let data
// if (local) {
// try {
// data = this.localStorage.get(item, key)
// } catch {}
// }
// if (data !== undefined) { return data }
// // 开启一个异步更新影响因子
// let secretKey = Zotero.Prefs.get(`${config.addonRef}.easyscholar.secretKey`) as string
// window.setTimeout(async () => {
// this.requests.cache = {}
// if (secretKey) {
// await lock;
// lock = Zotero.Promise.defer()
// let response
// try {
// response = await this.requests.get(
// `https://easyscholar.cc/open/getPublicationRank?secretKey=${secretKey}&publicationName=${encodeURIComponent(publicationTitle)}`,
// )
// } catch { console.log(response) }
// // 延迟
// await Zotero.Promise.delay(1000)
// lock!.resolve()
// if (response && response.data) {
// // 自定义数据集+官方数据集合并
// let officialAllData = response.data.officialRank.all
// if (!officialAllData) {
// if (!local) {
// new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true })
// .createLine({ text: "Not Found", type: "default" }).show()
// }
// return await this.localStorage.set(item, key, "")
// }
// let customRankInfo = response.data.customRank.rankInfo
// response.data.customRank.rank.forEach((rankString: string) => {
// try {
// // 1613160542602600448&&&2
// let uuid: string, rank: string;
// [uuid, rank] = rankString.split("&&&")
// rank = {
// "1": "oneRankText",
// "2": "twoRankText",
// "3": "threeRankText",
// "4": "fourRankText",
// "5": "fiveRankText"
// }[rank] as string
// let info = customRankInfo.find((i:any) => i.uuid == uuid)
// officialAllData[info.abbName] = info[rank]
// } catch {}
// })
// if (officialAllData) {
// await this.localStorage.set(item, key, officialAllData)
// console.log(item.key, officialAllData)
// // 显示它支持的所有字段
// let popupWin = new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true }).show()
// popupWin.createLine({text: publicationTitle, type: "default"})
// Object.keys(officialAllData).forEach(k => {
// popupWin.createLine({ text: `${k}: ${officialAllData[k]}`, type: "success" })
// })
// // ztoolkit.ItemTree.refresh()
// }
// }
// } else {
// if (!local) {
// new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true })
// .createLine({ text: "No easyScholar secret key configured", type: "fail" }).show()
// }
// }
// })
// return
// default:
// break
// }
// },
// getPublicationTitle(item: Zotero.Item) {
// return [item.getField("publicationTitle"), item.getField("proceedingsTitle")].find(i=>i.trim().length > 0)
// }
// }

// export default utils
Loading

0 comments on commit 751a688

Please sign in to comment.