Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
zzlb0224 committed Jul 25, 2024
1 parent 35b4545 commit 5b6098c
Show file tree
Hide file tree
Showing 4 changed files with 1,041 additions and 1,053 deletions.
9 changes: 8 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { getString, initLocale } from "./utils/locale";
import { registerPrefsScripts } from "./modules/preferenceScript";
import { createZToolkit } from "./utils/ztoolkit";
import Annotations from "./modules/annotations";
import AnnotationsToNote, { annotationToNoteTags as annotationToNoteTags, annotationToNoteType } from "./modules/menu";
import AnnotationsToNote, { getSelectedItems } from "./modules/menu";
import RelationHeader from "./modules/RelationHeader";
import highlightWords from "./modules/highlightWords";
import toolLink from "./modules/referenceMark";
import { actionTranAnnotations } from "./action/action-tran-annotations";
import { memFixedColor, stopPropagation } from "./utils/zzlb";
import { exportNoteByType, exportSingleNote, getAllAnnotations } from "./modules/AnnotationsToNote";
import { groupBy } from "./utils/groupBy";
import { sortFixedTags10ValuesLength, sortValuesLength } from "./utils/sort";
import { TagElementProps } from "zotero-plugin-toolkit/dist/tools/ui";
import { annotationToNoteTags, annotationToNoteType } from "./hooksMenuEvent";

async function onStartup() {
await Promise.all([Zotero.initializationPromise, Zotero.unlockPromise, Zotero.uiReadyPromise]);
Expand Down Expand Up @@ -167,6 +173,7 @@ async function onMenuEvent(type: "annotationToNoteTags" | "annotationToNoteType"
return;
}
}

function onDialogEvents(type: string) {
switch (type) {
case "dialogExample":
Expand Down
139 changes: 139 additions & 0 deletions src/hooksMenuEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { TagElementProps } from "zotero-plugin-toolkit/dist/tools/ui";
import { config } from "../package.json";
import { getAllAnnotations, exportNoteByType, exportSingleNote } from "./modules/AnnotationsToNote";
import { getSelectedItems } from "./modules/menu";
import { groupBy } from "./utils/groupBy";
import { sortValuesLength, sortFixedTags10ValuesLength } from "./utils/sort";
import { memFixedColor, stopPropagation } from "./utils/zzlb";

const iconBaseUrl = `chrome://${config.addonRef}/content/icons/`;

export async function annotationToNoteType(win: Window, collectionOrItem: "collection" | "item" = "collection") {
//用于弹出菜单
const doc = win.document;
const popup = doc.querySelector(`#${config.addonRef}-create-note-type-popup-${collectionOrItem}`) as XUL.MenuPopup;
// Remove all children in popup
while (popup?.firstChild) {
popup.removeChild(popup.firstChild);
}
// const id = getParentAttr(popup, "id");
// const isc = id?.includes("collection");
// ztoolkit.log("id", id);

const ans = getAllAnnotations(await getSelectedItems(collectionOrItem)); //.flatMap((a) => a.tags.map((t2) => Object.assign({}, a, { tag: t2 })));
const tags = groupBy(ans, (an) => an.type)
.sort(sortValuesLength)
.slice(0, 20);
const maxLen = Math.max(...tags.map((a) => a.values.length));

// Add new children
let elemProp: TagElementProps;
// const tags =memFixedTags()
if (tags.length === 0) {
elemProp = {
tag: "menuitem",
properties: {
label: "没有标签",
},
attributes: {
disabled: true,
},
};
} else {
elemProp = {
tag: "fragment",
children: tags.map((tag) => {
const color = memFixedColor(tag.key);
//取对数可以保留差异比较大的值
const pre = (100 - (Math.log(tag.values.length) / Math.log(maxLen)) * 100).toFixed();
return {
tag: "menuitem",
icon: iconBaseUrl + "favicon.png",
styles: {
background: `linear-gradient(to left, ${color}, #fff ${pre}%, ${color} ${pre}%)`,
},
properties: {
label: `${tag.key}[${tag.values.length}]`,
},
// children:[{tag:"div",styles:{height:"2px",background:memFixedColor(tag.key),width:`${tag.values.length/maxLen*100}%`}}],
listeners: [
{
type: "command",
listener: (event: any) => {
stopPropagation(event);
exportNoteByType(tag.key as _ZoteroTypes.Annotations.AnnotationType, collectionOrItem);
},
},
],
};
}),
};
}
ztoolkit.UI.appendElement(elemProp, popup);
}

export async function annotationToNoteTags(win: Window, collectionOrItem: "collection" | "item" = "collection") {
//用于弹出菜单
const doc = win.document;
const popup = doc.querySelector(`#${config.addonRef}-create-note-tag-popup-${collectionOrItem}`) as XUL.MenuPopup;
// Remove all children in popup
while (popup?.firstChild) {
popup.removeChild(popup.firstChild);
}
// const id = getParentAttr(popup, "id");
// const isc = id?.includes("collection");
// ztoolkit.log("id", id);

const ans = getAllAnnotations(await getSelectedItems(collectionOrItem)).flatMap((a) =>
a.tags.map((t2) => Object.assign({}, a, { tag: t2 })),
);
const tags = groupBy(ans, (an) => an.tag.tag)
.sort(sortFixedTags10ValuesLength)
.slice(0, 20);
const maxLen = Math.max(...tags.map((a) => a.values.length));

// Add new children
let elemProp: TagElementProps;
// const tags =memFixedTags()
if (tags.length === 0) {
elemProp = {
tag: "menuitem",
properties: {
label: "没有标签",
},
attributes: {
disabled: true,
},
};
} else {
elemProp = {
tag: "fragment",
children: tags.map((tag) => {
const color = memFixedColor(tag.key);
//取对数可以保留差异比较大的值
const pre = (100 - (Math.log(tag.values.length) / Math.log(maxLen)) * 100).toFixed();
return {
tag: "menuitem",
icon: iconBaseUrl + "favicon.png",
styles: {
background: `linear-gradient(to left, ${color}, #fff ${pre}%, ${color} ${pre}%)`,
},
properties: {
label: `${tag.key}[${tag.values.length}]`,
},
// children:[{tag:"div",styles:{height:"2px",background:memFixedColor(tag.key),width:`${tag.values.length/maxLen*100}%`}}],
listeners: [
{
type: "command",
listener: (event: any) => {
stopPropagation(event);
exportSingleNote(tag.key, collectionOrItem);
},
},
],
};
}),
};
}
ztoolkit.UI.appendElement(elemProp, popup);
}
Loading

0 comments on commit 5b6098c

Please sign in to comment.