From e9b9e7ef463931dfdd7a774041518fd48c1eb741 Mon Sep 17 00:00:00 2001 From: shenyutao Date: Fri, 25 Aug 2023 15:43:17 +0800 Subject: [PATCH] ipv code & language --- addon/locale/en-US/addon.ftl | 5 ++++- addon/locale/zh-CN/addon.ftl | 5 ++++- src/hooks.ts | 20 +++++++++----------- src/modules/Common.ts | 15 ++++++--------- src/modules/dataStorage.ts | 36 +++++++++++++++++++++--------------- src/modules/tldrFetcher.ts | 10 +++------- 6 files changed, 47 insertions(+), 44 deletions(-) diff --git a/addon/locale/en-US/addon.ftl b/addon/locale/en-US/addon.ftl index 04c1f65..7bff027 100644 --- a/addon/locale/en-US/addon.ftl +++ b/addon/locale/en-US/addon.ftl @@ -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 \ No newline at end of file +tldr-itemnotfound = Item Not Found in Semantic scholar +popWindow-succeed = Succeed +popWindow-failed = Failed +popWindow-waiting = Waiting \ No newline at end of file diff --git a/addon/locale/zh-CN/addon.ftl b/addon/locale/zh-CN/addon.ftl index 02467b4..4ed130e 100644 --- a/addon/locale/zh-CN/addon.ftl +++ b/addon/locale/zh-CN/addon.ftl @@ -2,4 +2,7 @@ menuitem-updatetldrlabel = 更新TLDR menucollection-updatetldrlabel = 批量更新TLDR itembox-tldrlabel = TLDR tldr-unrelated = 未关联TLDR -tldr-itemnotfound = 未搜索到此条目 \ No newline at end of file +tldr-itemnotfound = 未搜索到此条目 +popWindow-succeed = 成功 +popWindow-failed = 失败 +popWindow-waiting = 等待 \ No newline at end of file diff --git a/src/hooks.ts b/src/hooks.ts index fc770f7..36603c0 100644 --- a/src/hooks.ts +++ b/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); })(); diff --git a/src/modules/Common.ts b/src/modules/Common.ts index fa9c684..0473ac4 100644 --- a/src/modules/Common.ts +++ b/src/modules/Common.ts @@ -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; }); diff --git a/src/modules/dataStorage.ts b/src/modules/dataStorage.ts index 6906bc6..fe38219 100644 --- a/src/modules/dataStorage.ts +++ b/src/modules/dataStorage.ts @@ -1,12 +1,13 @@ import { config } from "../../package.json"; -export class Data { +export class Data { + [x: string]: any; private filePath: string; - private inited = false; - private _data: any; + private _data: Record; constructor(filePath: string) { this.filePath = filePath; + this._data = {} as Record; } async getAsync() { @@ -18,9 +19,9 @@ export class Data { return this.data; } - async modify(action: (data: any) => Promise) { + async modify(action: (data: Record) => Record | Promise>) { 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; 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) { 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; } } } @@ -70,14 +70,14 @@ export class DataStorage { "extensions", config.addonName, ); - private dataMap: { [key: string]: Data } = {}; + private dataMap: { [key: string]: Data } = {}; private static shared = new DataStorage(); - static instance(dataType: string) { + static instance(dataType: string): Data { const path = PathUtils.join(this.shared.dataDir, dataType); if (this.shared.dataMap[dataType] === undefined) { - const data = new Data(path); + const data = new Data(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('TLDR.json'); \ No newline at end of file diff --git a/src/modules/tldrFetcher.ts b/src/modules/tldrFetcher.ts index 90b00bd..fac592d 100644 --- a/src/modules/tldrFetcher.ts +++ b/src/modules/tldrFetcher.ts @@ -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; });