ipv code & language
This commit is contained in:
parent
f1d5275e6f
commit
e9b9e7ef46
|
|
@ -2,4 +2,7 @@ menuitem-updatetldrlabel = update TLDR
|
|||
menucollection-updatetldrlabel = update TLDR
|
||||
itembox-tldrlabel = TLDR
|
||||
tldr-unrelated = TLDR Unrelated in Semantic scholar
|
||||
tldr-itemnotfound = Item Not Found in Semantic scholar
|
||||
tldr-itemnotfound = Item Not Found in Semantic scholar
|
||||
popWindow-succeed = Succeed
|
||||
popWindow-failed = Failed
|
||||
popWindow-waiting = Waiting
|
||||
|
|
@ -2,4 +2,7 @@ menuitem-updatetldrlabel = 更新TLDR
|
|||
menucollection-updatetldrlabel = 批量更新TLDR
|
||||
itembox-tldrlabel = TLDR
|
||||
tldr-unrelated = 未关联TLDR
|
||||
tldr-itemnotfound = 未搜索到此条目
|
||||
tldr-itemnotfound = 未搜索到此条目
|
||||
popWindow-succeed = 成功
|
||||
popWindow-failed = 失败
|
||||
popWindow-waiting = 等待
|
||||
20
src/hooks.ts
20
src/hooks.ts
|
|
@ -3,8 +3,8 @@ import { config } from "../package.json";
|
|||
import { getString, initLocale } from "./utils/locale";
|
||||
import { registerPrefsScripts } from "./modules/preferenceScript";
|
||||
import { createZToolkit } from "./utils/ztoolkit";
|
||||
import { TLDRFetcher, TLDRFieldKey } from "./modules/tldrFetcher";
|
||||
import { Data, DataStorage } from "./modules/dataStorage";
|
||||
import { tldrs } from "./modules/dataStorage";
|
||||
import { TLDRFetcher } from "./modules/tldrFetcher";
|
||||
|
||||
async function onStartup() {
|
||||
await Promise.all([
|
||||
|
|
@ -14,7 +14,7 @@ async function onStartup() {
|
|||
]);
|
||||
initLocale();
|
||||
|
||||
await DataStorage.instance(TLDRFieldKey).getAsync(); // 加载TLDR数据
|
||||
await tldrs.getAsync();
|
||||
|
||||
RegisterFactory.registerPrefs();
|
||||
|
||||
|
|
@ -100,7 +100,7 @@ function onLoad() {
|
|||
}
|
||||
|
||||
function noNotifyDeleteItem(ids: (string | number)[]) {
|
||||
DataStorage.instance(TLDRFieldKey).modify((data: any) => {
|
||||
tldrs.modify(data => {
|
||||
ids.forEach((id) => {
|
||||
delete data[id];
|
||||
});
|
||||
|
|
@ -128,18 +128,18 @@ function onUpdateItems(items: Zotero.Item[], forceFetch: boolean = false) {
|
|||
return false;
|
||||
}
|
||||
if (!forceFetch) {
|
||||
return DataStorage.instance(TLDRFieldKey).get()[item.id] === undefined;
|
||||
return tldrs.get()[item.id] === undefined
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (items.length <= 0) {
|
||||
return;
|
||||
}
|
||||
const newPopWin = (closeOnClick = false) => {
|
||||
const newPopWin = (closeOnClick = true) => {
|
||||
return new ztoolkit.ProgressWindow(config.addonName, {
|
||||
closeOnClick: closeOnClick,
|
||||
}).createLine({
|
||||
text: `Waiting: ${items.length}, succeed: 0, failed: 0`,
|
||||
text: `${getString("popWindow-waiting")}: ${items.length}; ${getString("popWindow-succeed")}: 0; ${getString("popWindow-failed")}: 0`,
|
||||
type: "default",
|
||||
progress: 0,
|
||||
});
|
||||
|
|
@ -158,9 +158,7 @@ function onUpdateItems(items: Zotero.Item[], forceFetch: boolean = false) {
|
|||
ztoolkit.ItemBox.refresh();
|
||||
popupWin.changeLine({
|
||||
progress: (index * 100) / count,
|
||||
text: `Waiting: ${count - index - 1}, succeed: ${
|
||||
succeedItems.length
|
||||
}, failed: ${failedItems.length}`,
|
||||
text: `${getString("popWindow-waiting")}: ${count - index - 1}; ${getString("popWindow-succeed")}: ${succeedItems.length}; ${getString("popWindow-failed")}: ${failedItems.length}`,
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
|
@ -169,7 +167,7 @@ function onUpdateItems(items: Zotero.Item[], forceFetch: boolean = false) {
|
|||
popupWin.changeLine({
|
||||
type: "success",
|
||||
progress: 100,
|
||||
text: `Success: ${succeedItems.length}\nFailed: ${failedItems.length}`,
|
||||
text: `${getString("popWindow-succeed")}: ${succeedItems.length}; ${getString("popWindow-failed")}: ${failedItems.length}`,
|
||||
});
|
||||
popupWin.startCloseTimer(3000);
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import { config } from "../../package.json";
|
||||
import { getString } from "../utils/locale";
|
||||
import { DataStorage } from "./dataStorage";
|
||||
import { TLDRFieldKey, TLDRItemNotFound, TLDRUnrelated } from "./tldrFetcher";
|
||||
import { tldrs, TLDRItemNotFound, TLDRUnrelated } from "./dataStorage";
|
||||
|
||||
export class RegisterFactory {
|
||||
// 注册zotero的通知
|
||||
|
|
@ -88,16 +87,14 @@ export class UIFactory {
|
|||
// tldr行
|
||||
static async registerTLDRItemBoxRow() {
|
||||
await ztoolkit.ItemBox.register(
|
||||
TLDRFieldKey,
|
||||
"TLDR",
|
||||
getString("itembox-tldrlabel"),
|
||||
(field, unformatted, includeBaseMapped, item, original) => {
|
||||
const tldrInfo = DataStorage.instance(TLDRFieldKey).get()[item.id];
|
||||
const tldrInfo = tldrs.get()[item.id];
|
||||
if (tldrInfo === TLDRUnrelated) {
|
||||
return "";
|
||||
// return getString(TLDRUnrelated);
|
||||
return getString(TLDRUnrelated);
|
||||
} else if (tldrInfo === TLDRItemNotFound) {
|
||||
return "";
|
||||
// return getString(TLDRItemNotFound);
|
||||
return getString(TLDRItemNotFound);
|
||||
} else if (tldrInfo) {
|
||||
return tldrInfo;
|
||||
} else {
|
||||
|
|
@ -108,7 +105,7 @@ export class UIFactory {
|
|||
editable: true,
|
||||
setFieldHook: (field, value, loadIn, item, original) => {
|
||||
(async () => {
|
||||
await DataStorage.instance(TLDRFieldKey).modify((data: any) => {
|
||||
await tldrs.modify((data: any) => {
|
||||
data[item.id] = value;
|
||||
return data;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
import { config } from "../../package.json";
|
||||
|
||||
export class Data {
|
||||
export class Data<K extends string | number | symbol, V> {
|
||||
[x: string]: any;
|
||||
private filePath: string;
|
||||
private inited = false;
|
||||
private _data: any;
|
||||
private _data: Record<K, V>;
|
||||
|
||||
constructor(filePath: string) {
|
||||
this.filePath = filePath;
|
||||
this._data = {} as Record<K, V>;
|
||||
}
|
||||
|
||||
async getAsync() {
|
||||
|
|
@ -18,9 +19,9 @@ export class Data {
|
|||
return this.data;
|
||||
}
|
||||
|
||||
async modify(action: (data: any) => Promise<any>) {
|
||||
async modify(action: (data: Record<K, V>) => Record<K, V> | Promise<Record<K, V>>) {
|
||||
await this.initDataIfNeed();
|
||||
const data = await this.data;
|
||||
const data = this.data;
|
||||
const newData = await action(data);
|
||||
try {
|
||||
await IOUtils.writeJSON(this.filePath, newData, {
|
||||
|
|
@ -37,7 +38,7 @@ export class Data {
|
|||
async delete() {
|
||||
try {
|
||||
await IOUtils.remove(this.filePath);
|
||||
this.data = {};
|
||||
this.data = {} as Record<K, V>;
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
|
|
@ -48,18 +49,17 @@ export class Data {
|
|||
return this._data;
|
||||
}
|
||||
|
||||
private set data(value: any) {
|
||||
private set data(value: Record<K, V>) {
|
||||
this._data = value;
|
||||
}
|
||||
|
||||
private async initDataIfNeed() {
|
||||
if (this.inited) {
|
||||
return;
|
||||
}
|
||||
if (this.inited) { return; }
|
||||
this.inited = true;
|
||||
try {
|
||||
this.data = await IOUtils.readJSON(this.filePath, { decompress: false });
|
||||
} catch (error) {
|
||||
this.data = {};
|
||||
this.data = {} as Record<K, V>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -70,14 +70,14 @@ export class DataStorage {
|
|||
"extensions",
|
||||
config.addonName,
|
||||
);
|
||||
private dataMap: { [key: string]: Data } = {};
|
||||
private dataMap: { [key: string]: Data<any, any> } = {};
|
||||
|
||||
private static shared = new DataStorage();
|
||||
|
||||
static instance(dataType: string) {
|
||||
static instance<K extends string | number | symbol, V>(dataType: string): Data<K, V> {
|
||||
const path = PathUtils.join(this.shared.dataDir, dataType);
|
||||
if (this.shared.dataMap[dataType] === undefined) {
|
||||
const data = new Data(path);
|
||||
const data = new Data<K, V>(path);
|
||||
this.shared.dataMap[dataType] = data;
|
||||
return data;
|
||||
} else {
|
||||
|
|
@ -85,5 +85,11 @@ export class DataStorage {
|
|||
}
|
||||
}
|
||||
|
||||
private constructor() {}
|
||||
private constructor() {
|
||||
IOUtils.makeDirectory(this.dataDir, { createAncestors: true, ignoreExisting: true });
|
||||
}
|
||||
}
|
||||
|
||||
export const TLDRUnrelated = "tldr-unrelated"; // semantic scholar 找到了该item,但是该item没有tldr
|
||||
export const TLDRItemNotFound = "tldr-itemnotfound"; // semantic scholar 找不到该item
|
||||
export const tldrs = DataStorage.instance<string | number, string>('TLDR.json');
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { DataStorage } from "./dataStorage";
|
||||
import { tldrs, TLDRUnrelated, TLDRItemNotFound } from "./dataStorage";
|
||||
|
||||
type SemanticScholarItemInfo = {
|
||||
title?: string;
|
||||
|
|
@ -6,10 +6,6 @@ type SemanticScholarItemInfo = {
|
|||
tldr?: string;
|
||||
};
|
||||
|
||||
export const TLDRFieldKey = "TLDR";
|
||||
export const TLDRUnrelated = "tldr-unrelated"; // semantic scholar 找到了该item,但是该item没有tldr
|
||||
export const TLDRItemNotFound = "tldr-itemnotfound"; // semantic scholar 找不到该item,
|
||||
|
||||
export class TLDRFetcher {
|
||||
private readonly zoteroItem: Zotero.Item;
|
||||
private readonly title?: string;
|
||||
|
|
@ -42,14 +38,14 @@ export class TLDRFetcher {
|
|||
}
|
||||
if (match) {
|
||||
const result = info.tldr ?? TLDRUnrelated;
|
||||
DataStorage.instance(TLDRFieldKey).modify((data: any) => {
|
||||
tldrs.modify((data: any) => {
|
||||
data[this.zoteroItem.id] = result;
|
||||
return data;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
DataStorage.instance(TLDRFieldKey).modify((data: any) => {
|
||||
tldrs.modify((data: any) => {
|
||||
data[this.zoteroItem.id] = TLDRItemNotFound;
|
||||
return data;
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue