57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
/**
|
|
* Open Zotero's built-in Select Items dialog and return chosen library items.
|
|
*/
|
|
export async function pickLibraryItems(options?: {
|
|
multiSelect?: boolean;
|
|
}): Promise<Zotero.Item[]> {
|
|
const multiSelect = options?.multiSelect !== false;
|
|
const mainWin = Zotero.getMainWindow?.() as
|
|
| (Window & { openDialog?: (...args: any[]) => Window | null })
|
|
| undefined;
|
|
if (!mainWin?.openDialog) {
|
|
throw new Error("无法打开文献选择窗口");
|
|
}
|
|
|
|
const io: {
|
|
multiSelect: boolean;
|
|
onlyRegularItems: boolean;
|
|
dataOut: number[] | null;
|
|
itemTreeID: string;
|
|
} = {
|
|
multiSelect,
|
|
onlyRegularItems: true,
|
|
dataOut: null,
|
|
itemTreeID: "chatpapers-select-items",
|
|
};
|
|
|
|
const urls = [
|
|
"chrome://zotero/content/selectItemsDialog.xhtml",
|
|
"chrome://zotero/content/selectItemsDialog.xul",
|
|
];
|
|
|
|
let opened = false;
|
|
for (const url of urls) {
|
|
try {
|
|
mainWin.openDialog(
|
|
url,
|
|
"chatpapers-select-items-dialog",
|
|
"chrome,modal,centerscreen,resizable=yes",
|
|
io,
|
|
);
|
|
opened = true;
|
|
break;
|
|
} catch (e) {
|
|
ztoolkit.log("pickLibraryItems openDialog failed", url, e);
|
|
}
|
|
}
|
|
|
|
if (!opened) {
|
|
throw new Error("当前 Zotero 版本无法打开文献选择对话框");
|
|
}
|
|
|
|
const ids = Array.isArray(io.dataOut) ? io.dataOut : [];
|
|
if (!ids.length) return [];
|
|
|
|
return (Zotero.Items.get(ids).filter(Boolean) as Zotero.Item[]) || [];
|
|
}
|