diff --git a/src/modules/annotations.ts b/src/modules/annotations.ts index d9abc95c..64feb53f 100644 --- a/src/modules/annotations.ts +++ b/src/modules/annotations.ts @@ -4,15 +4,15 @@ import { sortByFixedTag2Length, groupBy, groupByResult, - getFixedTags, + memFixedTags, uniqueBy, - getFixedColor, + memFixedColor, sortByLength, } from "../utils/zzlb"; import { getPref } from "../utils/prefs"; import { groupByResultIncludeFixedTags } from "../utils/zzlb"; -import { getAllTagsDB } from "../utils/zzlb"; -import { getRelateTags } from "../utils/zzlb"; +import { memAllTagsDB } from "../utils/zzlb"; +import { memRelateTags } from "../utils/zzlb"; function register() { // if (!getPref("enable")) return; // ztoolkit.UI.basicOptions.log.disableZLog = true; @@ -163,9 +163,9 @@ export class AnnotationPopup { }>[] = []; if (getPref("showAllTags")) { - relateTags = await getAllTagsDB(); + relateTags = await memAllTagsDB(); } else { - relateTags = groupBy(getRelateTags(this.item), (t) => t.tag); + relateTags = groupBy(memRelateTags(this.item), (t) => t.tag); } if (getPref("show-relate-tags")) groupByResultIncludeFixedTags(relateTags); @@ -449,7 +449,7 @@ export class AnnotationPopup { async searchTagResult() { if (this.searchTag) { - const searchIn = await getAllTagsDB(); + const searchIn = await memAllTagsDB(); // getPref("showAllTags") // ? this.relateTags // : await getAllTagsDB(); @@ -471,8 +471,8 @@ export class AnnotationPopup { const allHave = this.isAllHave(tag); const noneHave = this.isNoneHave(tag); const someHave = this.strSomeHave(tag); - const bgColor = getFixedColor(tag, ""); - if (fixedTagsStyle && getFixedTags().includes(tag)) { + const bgColor = memFixedColor(tag, ""); + if (fixedTagsStyle && memFixedTags().includes(tag)) { return { tag: "span", namespace: "html", @@ -593,7 +593,7 @@ export class AnnotationPopup { ) { this.selectedTags.push({ tag, - color: color || getFixedColor(tag, undefined), + color: color || memFixedColor(tag, undefined), }); ztoolkit.UI.appendElement( { @@ -634,7 +634,7 @@ export class AnnotationPopup { if (this.selectedTags.length == 0 && this.searchTag) { this.selectedTags.push({ tag: this.searchTag, - color: getFixedColor(this.searchTag, undefined), + color: memFixedColor(this.searchTag, undefined), }); } if (this.delTags.length == 0 && this.selectedTags.length == 0) return; @@ -662,7 +662,7 @@ export class AnnotationPopup { } else { const color = this.selectedTags.map((a) => a.color).filter((f) => f)[0] || - getFixedColor(tagsRequire[0], undefined); + memFixedColor(tagsRequire[0], undefined); const tags = tagsRequire.map((a) => ({ name: a })); // 因为线程不一样,不能采用直接修改params.annotation的方式,所以直接采用新建的方式保存笔记 // 特意采用 Components.utils.cloneInto 方法 @@ -677,8 +677,8 @@ export class AnnotationPopup { this.reader?._primaryView._onSetSelectionPopup(null); } - getAllTagsDB.remove(); - getRelateTags.remove(this.item?.key); + memAllTagsDB.remove(); + memRelateTags.remove(this.item?.key); } } //测试预览 @@ -709,7 +709,7 @@ export class AnnotationPopup { (f) => f && !f.startsWith("#") && !f.includes("/"), ); const list: string[] = []; - const allTags = await getAllTagsDB(); + const allTags = await memAllTagsDB(); for (const t1 of filterArr) { for (const t2 of filterArr) { if (t1 != t2) { diff --git a/src/modules/annotationsToNote.ts b/src/modules/annotationsToNote.ts index 610b30b0..e6b335be 100644 --- a/src/modules/annotationsToNote.ts +++ b/src/modules/annotationsToNote.ts @@ -7,7 +7,7 @@ import { promiseAllWithProgress, sortByFixedTag2Length, getChildCollections, - getFixedColor, + memFixedColor, setProperty, } from "../utils/zzlb"; import { TagElementProps } from "zotero-plugin-toolkit/dist/tools/ui"; @@ -621,7 +621,7 @@ function exportTagsNote(tags: string[], items: Zotero.Item[]) { function getColorTags(tags: string[]) { return tags.map( (t) => - `${t}`, + `${t}`, ); } function getCiteAnnotationHtml(annotation: Zotero.Item, text = "") { diff --git a/src/modules/preferenceScript.ts b/src/modules/preferenceScript.ts index 329f4951..8070fff1 100644 --- a/src/modules/preferenceScript.ts +++ b/src/modules/preferenceScript.ts @@ -5,11 +5,11 @@ import { FixedColorDefault, FixedTagsDefault, getChildCollections, - getFixedColor, - getFixedColors, - getFixedTags, - getOptionalColor, - getRelateTags, + memFixedColor, + memFixedColors, + memFixedTags, + memOptionalColor, + memRelateTags, } from "../utils/zzlb"; import { AnnotationPopup } from "./annotations"; @@ -71,7 +71,7 @@ function replaceColorTagsElement(doc: Document) { justifyContent: "flex-start", flexWrap: "wrap", }, - children: getFixedTags().map((tag) => ({ + children: memFixedTags().map((tag) => ({ tag: "label", namespace: "html", properties: { @@ -79,7 +79,7 @@ function replaceColorTagsElement(doc: Document) { TextContent: tag, }, styles: { - background: getFixedColor(tag, undefined), + background: memFixedColor(tag, undefined), padding: "5px 1px", margin: "5px 1px", }, @@ -96,7 +96,7 @@ function initOptionalColorLabel(doc: Document) { `zotero-prefpane-${config.addonRef}-optional-color`, ); if (label) { - label.style.background = getOptionalColor(); + label.style.background = memOptionalColor(); } } async function updatePrefsUI() { @@ -125,9 +125,9 @@ function bindPrefEvents() { doc .querySelector(`#zotero-prefpane-${config.addonRef}-tags`) ?.addEventListener("keyup", (e) => { - getFixedTags.remove(); - getFixedColors.remove(); - getFixedColor.remove(); + memFixedTags.remove(); + memFixedColors.remove(); + memFixedColor.remove(); replaceColorTagsElement(doc); replaceTagsPreviewDiv(doc); }); @@ -135,8 +135,8 @@ function bindPrefEvents() { doc .querySelector(`#zotero-prefpane-${config.addonRef}-fixed-colors`) ?.addEventListener("keyup", (e) => { - getFixedColor.remove(); - getFixedColors.remove(); + memFixedColor.remove(); + memFixedColors.remove(); replaceColorTagsElement(doc); replaceTagsPreviewDiv(doc); }); @@ -144,8 +144,8 @@ function bindPrefEvents() { .querySelector(`#zotero-prefpane-${config.addonRef}-optional-color`) ?.addEventListener("keyup", (e) => { initOptionalColorLabel(doc); - getFixedColor.remove(); - getOptionalColor.remove(); + memFixedColor.remove(); + memOptionalColor.remove(); replaceTagsPreviewDiv(doc); }); @@ -157,19 +157,19 @@ function bindPrefEvents() { doc .querySelector(`#zotero-prefpane-${config.addonRef}-max-show`) ?.addEventListener("keyup", (e) => { - getOptionalColor.remove(); + memOptionalColor.remove(); replaceTagsPreviewDiv(doc); }); doc .querySelector(`#zotero-prefpane-${config.addonRef}-showAllTags`) ?.addEventListener("command", (e) => { - getOptionalColor.remove(); + memOptionalColor.remove(); replaceTagsPreviewDiv(doc); }); doc .querySelector(`#zotero-prefpane-${config.addonRef}-children-collection`) ?.addEventListener("command", (e) => { - getOptionalColor.remove(); + memOptionalColor.remove(); replaceTagsPreviewDiv(doc); }); doc @@ -180,15 +180,15 @@ function bindPrefEvents() { doc .querySelector(`#zotero-prefpane-${config.addonRef}-currentCollection`) ?.addEventListener("command", (e) => { - getOptionalColor.remove(); + memOptionalColor.remove(); replaceTagsPreviewDiv(doc); }); doc .querySelector(`#zotero-prefpane-${config.addonRef}-show-relate-tags`) ?.addEventListener("command", (e) => { - getOptionalColor.remove(); + memOptionalColor.remove(); replaceTagsPreviewDiv(doc); - getRelateTags.remove(); + memRelateTags.remove(); }); } async function replaceTagsPreviewDiv(doc?: Document) { diff --git a/src/utils/zzlb.ts b/src/utils/zzlb.ts index 5d27e945..9309d83c 100644 --- a/src/utils/zzlb.ts +++ b/src/utils/zzlb.ts @@ -66,7 +66,7 @@ export function sortByFixedTag2Length( a: groupByResult, b: groupByResult, ) { - const tags = getFixedTags(); + const tags = memFixedTags(); if (tags.includes(a.key) && tags.includes(b.key)) { return tags.indexOf(a.key) - tags.indexOf(b.key); } @@ -83,7 +83,7 @@ export function sortByFixedTag2TagName( a: groupByResult, b: groupByResult, ) { - const tags = getFixedTags(); + const tags = memFixedTags(); if (tags.includes(a.key) && tags.includes(b.key)) { return tags.indexOf(a.key) - tags.indexOf(b.key); } @@ -116,24 +116,24 @@ export const COLOR = { white: "#ffffff", }; -export const getOptionalColor = memoize( +export const memOptionalColor = memoize( () => (getPref("optional-color") as string)?.match(/#[0-9A-F]{6}/g)?.[0] || "#ffc0cb", ); -export const getFixedColor = memoize( +export const memFixedColor = memoize( (tag: string, optional: string | undefined = undefined): string => { - const tags = getFixedTags(); + const tags = memFixedTags(); if (tags.includes(tag)) { - return getFixedColors()[tags.indexOf(tag)]; + return memFixedColors()[tags.indexOf(tag)]; } - if (optional == undefined) return getOptionalColor() as string; + if (optional == undefined) return memOptionalColor() as string; return optional; }, (tag: string, optional: string | undefined = undefined) => tag + "_" + optional, ); -export const getFixedTags = memoize((): string[] => { +export const memFixedTags = memoize((): string[] => { const prefTags = ((getPref("tags") as string) || "") .split(",") .map((a) => a.trim()) @@ -141,14 +141,14 @@ export const getFixedTags = memoize((): string[] => { if (prefTags && prefTags.length > 0) return prefTags; return FixedTagsDefault.split(","); }); -export const getFixedColors = memoize((): string[] => { +export const memFixedColors = memoize((): string[] => { let fixedColor = (getPref("fixed-colors") as string) ?.match(/#[0-9A-F]{6}/g) ?.map((a) => a) || []; if (!fixedColor || fixedColor.length == 0) fixedColor = FixedColorDefault.split(","); - const tags = getFixedTags(); + const tags = memFixedTags(); return Array.from({ length: tags.length / fixedColor.length + 1, }).flatMap(() => fixedColor); @@ -174,7 +174,7 @@ export function setProperty>( } } export function groupByResultIncludeFixedTags(tagGroup: groupByResult[]) { - getFixedTags().forEach((tag) => { + memFixedTags().forEach((tag) => { if (tagGroup.findIndex((f) => f.key == tag) == -1) { tagGroup.push({ key: tag, values: [] }); } @@ -202,7 +202,7 @@ export function getCssTranslate(t1: HTMLElement) { return { x: 0, y: 0 }; } //使用条目、pdf、annotation、tag的关系进行读取 -const getAllTagsInLibraryAsync = memoize(async () => { +const memAllTagsInLibraryAsync = memoize(async () => { const allItems = await Zotero.Items.getAll(1, false, false, false); const items = allItems.filter((f) => !f.parentID && !f.isAttachment()); const pdfIds = items.flatMap((f) => f.getAttachments(false)); @@ -217,7 +217,7 @@ const getAllTagsInLibraryAsync = memoize(async () => { return groupBy([...tags, ...itemTags], (t) => t.tag); }); //使用查询优化性能 -export const getAllTagsDB = memoize(async () => { +export const memAllTagsDB = memoize(async () => { const rows = await Zotero.DB.queryAsync( "select name as tag,type from itemTags it join tags t on it.tagID=t.tagID", ); @@ -228,7 +228,7 @@ export const getAllTagsDB = memoize(async () => { ztoolkit.log(lines.length, lines); return groupBy(lines, (t) => t.tag); }); -export const getRelateTags = memoize( +export const memRelateTags = memoize( (item?: Zotero.Item) => { if (!getPref("show-relate-tags")) return []; return getTagsInCollections(getItemRelateCollections(item));