Skip to content

Commit

Permalink
Change duplicate buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenglongMa committed Apr 25, 2024
1 parent a6be28d commit 2f4d44f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
10 changes: 5 additions & 5 deletions addon/chrome/content/zoplicate.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.merge-button {
.duplicate-box-button {
-moz-appearance: none;
border-radius: 12px;
border: 1px solid rgba(0, 0, 0, .65);
Expand All @@ -11,20 +11,20 @@
min-height: 22px;
}

.merge-button[disabled="false"]:-moz-focusring {
.duplicate-box-button[disabled="false"]:-moz-focusring {
box-shadow: 0 0 1px -moz-mac-focusring inset, 0 0 4px 1px -moz-mac-focusring, 0 0 2px 1px -moz-mac-focusring;
}

.merge-button[disabled="false"]:hover{
.duplicate-box-button[disabled="false"]:hover{
background: white;
cursor: pointer;
}

.merge-button[disabled="false"]:hover:active {
.duplicate-box-button[disabled="false"]:hover:active {
background: -moz-linear-gradient(rgba(83, 128, 232, .9), rgba(91, 118, 255, .9));
box-shadow: inset 0 0 3px rgba(0, 0, 0, .2), inset 0 1px 7px rgba(0, 0, 0, .4), 0 1px 0 rgba(255, 255, 255, .1);
}

.merge-button[disabled="true"] {
.duplicate-box-button[disabled="true"] {
filter: grayscale(100%);
}
9 changes: 5 additions & 4 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { createZToolkit } from "./utils/ztoolkit";
import { Notifier } from "./modules/notifier";
import { registerStyleSheet } from "./utils/window";
import { BulkDuplicates } from "./modules/bulkDuplicates";
import { fetchDuplicates } from "./modules/duplicates";
import { fetchDuplicates, registerButtonsInDuplicatePane } from "./modules/duplicates";
import menus from "./modules/menus";
// import "./modules/zduplicates.js";
import { DB } from "./modules/db";
import { NonDuplicates } from "./modules/nonDuplicates";
import { NonDuplicates, registerNonDuplicatesSection } from "./modules/nonDuplicates";
import { patchGetSearchObject, patchItemSaveData } from "./modules/patcher";
import { containsRegularItem, isInDuplicatesPane, refreshItemTree } from "./utils/zotero";
import { registerDuplicateStats } from "./modules/duplicateStats";
Expand All @@ -28,11 +28,11 @@ async function onStartup() {
}

async function onMainWindowLoad(win: Window): Promise<void> {
// Create ztoolkit for every window
addon.data.ztoolkit = createZToolkit();
const db = DB.getInstance();
await db.init();
NonDuplicates.getInstance().init(db);
registerNonDuplicatesSection();
registerStyleSheet();
registerPrefs();
Notifier.registerNotifier();
Expand All @@ -41,6 +41,7 @@ async function onMainWindowLoad(win: Window): Promise<void> {
patchGetSearchObject();
patchItemSaveData();
await registerDuplicateStats();
registerButtonsInDuplicatePane(win);
}

async function onMainWindowUnload(win: Window): Promise<void> {
Expand Down Expand Up @@ -99,7 +100,7 @@ async function onNotify(event: string, type: string, ids: number[] | string[], e
libraryIDs = ids as number[];
}
const libraryID = libraryIDs[0]; // normally only one libraryID
const { duplicatesObj } = await fetchDuplicates(libraryID, true);
const { duplicatesObj } = await fetchDuplicates({ libraryID, refresh: true });
if (type == "item" && event == "add") {
await Notifier.whenItemsAdded(duplicatesObj, ids as number[]);
}
Expand Down
21 changes: 9 additions & 12 deletions src/modules/bulkDuplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class BulkDuplicates {

private constructor() {}

private bulkMergeButtonID = "zoplicate-bulk-merge-button";
private innerButtonID = this.bulkMergeButtonID + "-inner";
private externalButtonID = this.bulkMergeButtonID + "-external";
public static readonly bulkMergeButtonID = "zoplicate-bulk-merge-button";
public static readonly innerButtonID = this.bulkMergeButtonID + "-inner";
public static readonly externalButtonID = this.bulkMergeButtonID + "-external";
private win: Window | undefined;
private static instance: BulkDuplicates;
private _isRunning = false;
Expand All @@ -45,18 +45,18 @@ export class BulkDuplicates {
}

private getBulkMergeButtons(win: Window) {
return [win.document.getElementById(this.innerButtonID), win.document.getElementById(this.externalButtonID)];
return [win.document.getElementById(BulkDuplicates.innerButtonID), win.document.getElementById(BulkDuplicates.externalButtonID)];
}

private createBulkMergeButton(win: Window, id: string): TagElementProps {
public createBulkMergeButton(win: Window, id: string): TagElementProps {
return {
tag: "button",
id: id,
attributes: {
label: getString("bulk-merge-title"),
image: `chrome://${config.addonRef}/content/icons/merge.svg`,
},
classList: ["merge-button"],
classList: ["duplicate-box-button"],
namespace: "xul",
listeners: [
{
Expand Down Expand Up @@ -187,20 +187,17 @@ export class BulkDuplicates {

ZoteroPane.collectionsView &&
ZoteroPane.collectionsView.onSelect.addListener(async () => {
const mergeButton = win.document.getElementById("zotero-duplicates-merge-button") as Element;
const groupBox = win.document.getElementById("zotero-item-pane-groupbox") as Element;
if (isInDuplicatesPane()) {
ztoolkit.UI.appendElement(msgVBox, groupBox);
ztoolkit.UI.insertElementBefore(this.createBulkMergeButton(win, this.innerButtonID), mergeButton);
ztoolkit.UI.appendElement(this.createBulkMergeButton(win, this.externalButtonID), groupBox);
ztoolkit.UI.appendElement(this.createBulkMergeButton(win, BulkDuplicates.externalButtonID), groupBox);
if (this._isRunning && ZoteroPane.itemsView) {
await ZoteroPane.itemsView.waitForLoad();
ZoteroPane.itemsView.selection.clearSelection();
}
} else {
const externalButton = win.document.getElementById(this.externalButtonID);
const externalButton = win.document.getElementById(BulkDuplicates.externalButtonID);
if (externalButton) {
mergeButton.parentNode?.removeChild(win.document.getElementById(this.innerButtonID)!);
groupBox.removeChild(win.document.getElementById(msgID)!);
groupBox.removeChild(externalButton);
}
Expand All @@ -212,7 +209,7 @@ export class BulkDuplicates {
ztoolkit.log("refresh");
if (isInDuplicatesPane() && ZoteroPane.itemsView) {
const disabled = ZoteroPane.itemsView.rowCount <= 0;
updateButtonDisabled(win!, disabled, this.innerButtonID, this.externalButtonID);
updateButtonDisabled(win!, disabled, BulkDuplicates.innerButtonID, BulkDuplicates.externalButtonID);
if (this._isRunning) {
ZoteroPane.itemsView.selection.clearSelection();
}
Expand Down
19 changes: 16 additions & 3 deletions src/modules/duplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ import { getPref, setPref, Action, MasterItem } from "../utils/prefs";
import { merge } from "./merger";
import { goToDuplicatesPane } from "../utils/zotero";
import { DuplicateItems } from "./duplicateItems";
import { createNonDuplicateButton } from "./nonDuplicates";
import { BulkDuplicates } from "./bulkDuplicates";

export function registerButtonsInDuplicatePane(win: Window) {
const mergeButton = win.document.getElementById("zotero-duplicates-merge-button") as Element;
if (mergeButton) {
ztoolkit.UI.insertElementBefore(createNonDuplicateButton(), mergeButton);
ztoolkit.UI.insertElementBefore(
BulkDuplicates.getInstance().createBulkMergeButton(win, BulkDuplicates.innerButtonID),
mergeButton,
);
}
}

export async function fetchAllDuplicates(refresh = false) {
const libraries = Zotero.Libraries.getAll();
for (const library of libraries) {
await fetchDuplicates(library.libraryID, refresh);
await fetchDuplicates({ libraryID: library.libraryID, refresh });
}
}

Expand All @@ -18,10 +31,10 @@ export async function fetchAllDuplicates(refresh = false) {
* @param libraryID Library ID
* @param refresh Whether to refresh the search, default is false
*/
export async function fetchDuplicates(
export async function fetchDuplicates({
libraryID = ZoteroPane.getSelectedLibraryID(),
refresh = false,
): Promise<{
} = {}): Promise<{
libraryID: number;
duplicatesObj: { getSetItemsByItemID(itemID: number): number[] };
duplicateItems: number[];
Expand Down
15 changes: 4 additions & 11 deletions src/modules/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { showingDuplicateStats } from "../utils/prefs";
import { fetchAllDuplicates, fetchDuplicates } from "./duplicates";
import { MenuManager } from "zotero-plugin-toolkit/dist/managers/menu";
import { DB } from "./db";
import { isInDuplicatesPane, refreshItemTree } from "../utils/zotero";
import { isInDuplicatesPane } from "../utils/zotero";
import MenuPopup = XUL.MenuPopup;
import { toggleNonDuplicates } from "./nonDuplicates";

function registerMenus(win: Window) {
const menuManager = new ztoolkit.Menu();
Expand All @@ -32,11 +33,7 @@ function registerItemsViewMenu(menuManager: MenuManager, win: Window) {
label: isDuplicateMenuTitle,
id: `${config.addonRef}-menuitem-is-duplicate`,
commandListener: async (ev) => {
const selectedItems = Zotero.getActiveZoteroPane().getSelectedItems();
await DB.getInstance().deleteNonDuplicates(selectedItems.map((item) => item.id));
if (isInDuplicatesPane()) {
refreshItemTree();
}
await toggleNonDuplicates("unmark");
},
getVisibility: (elem, ev) => {
return showingIsDuplicate;
Expand All @@ -49,11 +46,7 @@ function registerItemsViewMenu(menuManager: MenuManager, win: Window) {
id: `${config.addonRef}-menuitem-not-duplicate`,
icon: `chrome://${config.addonRef}/content/icons/menu/non-duplicate.svg`,
commandListener: async (ev) => {
const selectedItems = Zotero.getActiveZoteroPane().getSelectedItems();
await DB.getInstance().insertNonDuplicates(selectedItems.map((item) => item.id));
if (isInDuplicatesPane()) {
refreshItemTree();
}
await toggleNonDuplicates("mark");
},
getVisibility: (elem, ev) => {
return showingNotDuplicate;
Expand Down

0 comments on commit 2f4d44f

Please sign in to comment.