Skip to content

Commit

Permalink
Finish item section
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenglongMa committed Apr 28, 2024
1 parent 126e70c commit d56ba20
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 53 deletions.
4 changes: 4 additions & 0 deletions addon/locale/en-US/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ refresh-duplicates-done = Duplicate items has been refreshed.
menuitem-not-duplicate = They are NOT duplicates
menuitem-is-duplicate = They are duplicates
# Add non-duplicate
add-not-duplicates-alert-error-duplicates = The selected items are not identified as Duplicates.
add-not-duplicates-alert-error-diff-library = Cannot add items from different libraries.
2 changes: 0 additions & 2 deletions addon/locale/en-US/itemSection.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ section-non-duplicate-sidenav =
non-duplicate-add =
.tooltiptext = Add to Non Duplicates
# Add non-duplicate
add-not-duplicates-alert-error = The selected items are not identified as Duplicates.
4 changes: 4 additions & 0 deletions addon/locale/zh-CN/addon.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ refresh-duplicates-done = 重复条目已刷新
menuitem-not-duplicate = 标记为非重复条目
menuitem-is-duplicate = 标记为重复条目
# Add non-duplicate
add-not-duplicates-alert-error-duplicates = 所选条目没有被识别为重复条目。
add-not-duplicates-alert-error-diff-library = 无法添加不同库中的条目。
3 changes: 0 additions & 3 deletions addon/locale/zh-CN/itemSection.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ section-non-duplicate-sidenav =
non-duplicate-add =
.tooltiptext = 添加到非重复条目
# Add non-duplicate
add-not-duplicates-alert-error = 所选条目没有被识别为重复条目。
7 changes: 7 additions & 0 deletions src/modules/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ class SQLiteDB implements IDatabase {
}

async insertNonDuplicatePair(itemID: number, itemID2: number, libraryID?: number) {
if (itemID === itemID2) {
return;
}
libraryID = libraryID ?? Zotero.Items.get(itemID).libraryID;
const row = this.buildRow(itemID, itemID2, libraryID);
await this._db.queryAsync(
Expand All @@ -221,6 +224,10 @@ class SQLiteDB implements IDatabase {
}

async insertNonDuplicatePairs(rows: { itemID: number; itemID2: number }[], libraryID?: number) {
rows = rows.filter(row => row.itemID !== row.itemID2);
if (rows.length === 0) {
return;
}
libraryID = libraryID ?? Zotero.Items.get(rows[0].itemID).libraryID;
const placeholders = rows.map(() => "(?, ?, ?)").join(",");
const values = rows.flatMap(({ itemID, itemID2 }) => this.buildRow(itemID, itemID2, libraryID));
Expand Down
81 changes: 35 additions & 46 deletions src/modules/nonDuplicates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import database, {IDatabase} from "./db";
import database, { IDatabase } from "./db";
import { patchFindDuplicates } from "./patcher";
import { config } from "../../package.json";
import { isInDuplicatesPane, refreshItemTree } from "../utils/zotero";
Expand Down Expand Up @@ -55,40 +55,26 @@ export function registerNonDuplicatesSection(db: IDatabase) {
return;
}
const itemIDs = [...io.dataOut, item.id];
if (!(await areDuplicates(itemIDs))) {
Zotero.alert(
body.ownerDocument.defaultView!,
config.addonName,
getString("add-not-duplicates-alert-error"),
);

if(new Set(itemIDs).size < 2) {
return;
}

// TODO: exist?
await toggleNonDuplicates("mark", itemIDs);
let message: string = "";
const libraryIDs = new Set(itemIDs.map((item) => Zotero.Items.get(item).libraryID));

// let relItems = await Zotero.Items.getAsync(io.dataOut);
// if (!relItems.length) {
// return;
// }
// if (relItems[0].libraryID != item.libraryID) {
// Zotero.alert(body.ownerDocument.defaultView!, "", "You cannot relate items in different libraries.");
// return;
// }
// await Zotero.DB.executeTransaction(async () => {
// for (let relItem of relItems) {
// if (this._item.addRelatedItem(relItem)) {
// await this._item.save({
// skipDateModifiedUpdate: true
// });
// }
// if (relItem.addRelatedItem(this._item)) {
// await relItem.save({
// skipDateModifiedUpdate: true
// });
// }
// }
// });
if (libraryIDs.size > 1) {
message = "add-not-duplicates-alert-error-diff-library";
} else if (!(await areDuplicates(itemIDs))) {
message = "add-not-duplicates-alert-error-duplicates";
}

if (message !== "") {
Zotero.alert(body.ownerDocument.defaultView!, config.addonName, getString(message));
return;
}

await toggleNonDuplicates("mark", itemIDs);
// End of OnClick
},
},
Expand All @@ -111,19 +97,17 @@ export function registerNonDuplicatesSection(db: IDatabase) {
const notifierKey = Zotero.Notifier.registerObserver(
{
notify: (event, type, ids, extraData) => {
// const item = Zotero.Items.get(body.dataset.itemID || "");
const itemID = body.dataset.itemID;
const item = itemID && Zotero.Items.get(itemID);
ztoolkit.log(`non duplicate notify ${type}`, ids, item);
if (
item &&
// @ts-ignore
event === "updateNonDuplicates" &&
event === "refreshNonDuplicate" &&
type === "item" &&
(ids as number[]).includes(item.id)
) {
ztoolkit.log(
`non duplicate notify update ${type}`,
ids,
item.id,
);
ztoolkit.log(`non duplicate notify [removeNonDuplicate] ${type}`, ids, item.id);
refresh();
}
},
Expand All @@ -142,7 +126,8 @@ export function registerNonDuplicatesSection(db: IDatabase) {
}
},
onItemChange: ({ body, item, setEnabled }) => {
ztoolkit.log("onItemChange non duplicates");
// ztoolkit.log("debug flag onItemChange non duplicates", item.getDisplayTitle(), body);
body.dataset.itemID = String(item.id);
// if (body.closest("bn-workspace") as HTMLElement | undefined) {
// setEnabled(true);
// body.dataset.itemID = String(item.id);
Expand All @@ -152,8 +137,6 @@ export function registerNonDuplicatesSection(db: IDatabase) {
},
onRender: () => {},
onAsyncRender: async ({ body, item, editable }) => {
// await renderGraph(body, item);

ztoolkit.log("onAsyncRender non duplicates", body);

body.replaceChildren();
Expand Down Expand Up @@ -188,7 +171,8 @@ export function registerNonDuplicatesSection(db: IDatabase) {
// @ts-ignore
let remove = document.createXULElement("toolbarbutton");
remove.addEventListener("command", () => {
toggleNonDuplicates("unmark", [item.id, otherItemID]);
const itemIDs = [item.id, otherItemID];
toggleNonDuplicates("unmark", itemIDs);
});
remove.className = "zotero-clicky zotero-clicky-minus";
remove.setAttribute("data-l10n-id", "section-button-remove");
Expand All @@ -201,10 +185,7 @@ export function registerNonDuplicatesSection(db: IDatabase) {
});
}

export async function toggleNonDuplicates(
action: "mark" | "unmark",
items?: number[] | Zotero.Item[],
) {
export async function toggleNonDuplicates(action: "mark" | "unmark", items?: number[] | Zotero.Item[]) {
const selectedItems = items && items.length ? items : Zotero.getActiveZoteroPane().getSelectedItems();
const itemIDs = selectedItems.map((item) => (typeof item === "number" ? item : item.id));
if (action === "mark") {
Expand All @@ -216,6 +197,14 @@ export async function toggleNonDuplicates(
if (isInDuplicatesPane()) {
refreshItemTree();
}
await Zotero.Notifier.trigger(
// @ts-ignore
"refreshNonDuplicate",
"item",
itemIDs,
{},
true,
);
}

export function createNonDuplicateButton(): TagElementProps {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getString(localString: string): string;
function getString(localString: string, branch: string): string;
function getString(
localeString: string,
options: { branch?: string | undefined; args?: Record<string, unknown> },
options: { branch?: string; args?: Record<string, unknown> },
): string;
function getString(...inputs: any[]) {
if (inputs.length === 1) {
Expand All @@ -61,7 +61,7 @@ function getString(...inputs: any[]) {

function _getString(
localeString: string,
options: { branch?: string | undefined; args?: Record<string, unknown> } = {},
options: { branch?: string; args?: Record<string, unknown> } = {},
): string {
const localStringWithPrefix = `${config.addonRef}-${localeString}`;
const { branch, args } = options;
Expand Down

0 comments on commit d56ba20

Please sign in to comment.