Skip to content

Commit

Permalink
修改一下缓存方法
Browse files Browse the repository at this point in the history
  • Loading branch information
zzlb0224 committed Mar 20, 2024
1 parent efd77dc commit 9c586ee
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 deletions.
30 changes: 15 additions & 15 deletions src/modules/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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",
Expand Down Expand Up @@ -593,7 +593,7 @@ export class AnnotationPopup {
) {
this.selectedTags.push({
tag,
color: color || getFixedColor(tag, undefined),
color: color || memFixedColor(tag, undefined),
});
ztoolkit.UI.appendElement(
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 方法
Expand All @@ -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);
}
}
//测试预览
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/annotationsToNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
promiseAllWithProgress,
sortByFixedTag2Length,
getChildCollections,
getFixedColor,
memFixedColor,
setProperty,
} from "../utils/zzlb";
import { TagElementProps } from "zotero-plugin-toolkit/dist/tools/ui";
Expand Down Expand Up @@ -621,7 +621,7 @@ function exportTagsNote(tags: string[], items: Zotero.Item[]) {
function getColorTags(tags: string[]) {
return tags.map(
(t) =>
`<span style="background-color:${getFixedColor(t, undefined)};box-shadow: ${getFixedColor(t, undefined)} 0px 0px 5px 4px;">${t}</span>`,
`<span style="background-color:${memFixedColor(t, undefined)};box-shadow: ${memFixedColor(t, undefined)} 0px 0px 5px 4px;">${t}</span>`,
);
}
function getCiteAnnotationHtml(annotation: Zotero.Item, text = "") {
Expand Down
42 changes: 21 additions & 21 deletions src/modules/preferenceScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -71,15 +71,15 @@ function replaceColorTagsElement(doc: Document) {
justifyContent: "flex-start",
flexWrap: "wrap",
},
children: getFixedTags().map((tag) => ({
children: memFixedTags().map((tag) => ({
tag: "label",
namespace: "html",
properties: {
innerText: tag,
TextContent: tag,
},
styles: {
background: getFixedColor(tag, undefined),
background: memFixedColor(tag, undefined),
padding: "5px 1px",
margin: "5px 1px",
},
Expand All @@ -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() {
Expand Down Expand Up @@ -125,27 +125,27 @@ 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);
});

doc
.querySelector(`#zotero-prefpane-${config.addonRef}-fixed-colors`)
?.addEventListener("keyup", (e) => {
getFixedColor.remove();
getFixedColors.remove();
memFixedColor.remove();
memFixedColors.remove();
replaceColorTagsElement(doc);
replaceTagsPreviewDiv(doc);
});
doc
.querySelector(`#zotero-prefpane-${config.addonRef}-optional-color`)
?.addEventListener("keyup", (e) => {
initOptionalColorLabel(doc);
getFixedColor.remove();
getOptionalColor.remove();
memFixedColor.remove();
memOptionalColor.remove();
replaceTagsPreviewDiv(doc);
});

Expand All @@ -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
Expand All @@ -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) {
Expand Down
28 changes: 14 additions & 14 deletions src/utils/zzlb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function sortByFixedTag2Length<T>(
a: groupByResult<T>,
b: groupByResult<T>,
) {
const tags = getFixedTags();
const tags = memFixedTags();
if (tags.includes(a.key) && tags.includes(b.key)) {
return tags.indexOf(a.key) - tags.indexOf(b.key);
}
Expand All @@ -83,7 +83,7 @@ export function sortByFixedTag2TagName<T>(
a: groupByResult<T>,
b: groupByResult<T>,
) {
const tags = getFixedTags();
const tags = memFixedTags();
if (tags.includes(a.key) && tags.includes(b.key)) {
return tags.indexOf(a.key) - tags.indexOf(b.key);
}
Expand Down Expand Up @@ -116,39 +116,39 @@ 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())
.filter((f) => f);
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);
Expand All @@ -174,7 +174,7 @@ export function setProperty<T, K extends keyof NonNullable<T>>(
}
}
export function groupByResultIncludeFixedTags<T>(tagGroup: groupByResult<T>[]) {
getFixedTags().forEach((tag) => {
memFixedTags().forEach((tag) => {
if (tagGroup.findIndex((f) => f.key == tag) == -1) {
tagGroup.push({ key: tag, values: [] });
}
Expand Down Expand Up @@ -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));
Expand All @@ -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",
);
Expand All @@ -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));
Expand Down

0 comments on commit 9c586ee

Please sign in to comment.