From 9aa73361723619ce9047af17e599c432beb3d89f Mon Sep 17 00:00:00 2001 From: MuiseDestiny Date: Tue, 25 Jun 2024 23:00:35 +0800 Subject: [PATCH] fix: #120 --- package.json | 2 +- src/modules/menu.ts | 96 ++++++++++++++++++++++++++------------------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index 47eca57..812c1d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zotero-attanger", - "version": "1.2.3", + "version": "1.2.4", "description": "Attanger for Zotero", "config": { "addonName": "Zotero Attanger", diff --git a/src/modules/menu.ts b/src/modules/menu.ts index 250d80a..3e6b6df 100644 --- a/src/modules/menu.ts +++ b/src/modules/menu.ts @@ -601,50 +601,64 @@ export async function moveFile(attItem: Zotero.Item) { const filename = PathUtils.filename(sourcePath); let destPath = PathUtils.joinRelative(destDir, filename); if (sourcePath == destPath) return; - // window.alert(destPath) - if ( - (await IOUtils.exists(destPath)) && - file2md5(sourcePath) != file2md5(destPath) - ) { - await Zotero.Promise.delay(1000); - // Click to enter a specified suffix. - const popupWin = new ztoolkit.ProgressWindow("Attanger", { - closeTime: -1, - closeOtherProgressWindows: true, - }) - .createLine({ - text: "The target file already exists; a numeric suffix will be automatically added to the filename.", - icon: addon.data.icons.moveFile, + if ((await IOUtils.exists(destPath))) { + ztoolkit.log("目标目录存在", file2md5(sourcePath), file2md5(destPath)) + window.alert(1) + if (file2md5(sourcePath) != file2md5(destPath)) { + ztoolkit.log("不是同一个文件") + await Zotero.Promise.delay(1000); + // Click to enter a specified suffix. + const popupWin = new ztoolkit.ProgressWindow("Attanger", { + closeTime: -1, + closeOtherProgressWindows: true, }) - .show(); - // Zotero.ProgressWindowSet.remove(popupWin) - popupWin.addDescription( - `Click to enter a specified suffix.`, - ); - await waitUtilAsync(() => - // @ts-ignore oriate - Boolean(popupWin.lines && popupWin.lines[0]._itemText), - ); - const lock = Zotero.Promise.defer(); - const timer = window.setTimeout(() => { - popupWin.close(); - lock.resolve(); - }, 3e3); - // @ts-ignore private - popupWin.lines[0]._hbox.ownerDocument - .querySelector("label[href]") - .addEventListener("click", async (ev: MouseEvent) => { - ev.stopPropagation(); - ev.preventDefault(); - window.clearTimeout(timer); + .createLine({ + text: "The target file already exists; a numeric suffix will be automatically added to the filename.", + icon: addon.data.icons.moveFile, + }) + .show(); + // Zotero.ProgressWindowSet.remove(popupWin) + popupWin.addDescription( + `Click to enter a specified suffix.`, + ); + await waitUtilAsync(() => + // @ts-ignore oriate + Boolean(popupWin.lines && popupWin.lines[0]._itemText), + ); + const lock = Zotero.Promise.defer(); + const timer = window.setTimeout(() => { popupWin.close(); - const suffix = window.prompt("Suffix") as string; - destPath = await addSuffixToFilename(destPath, suffix); lock.resolve(); - }); + }, 3e3); + // @ts-ignore private + popupWin.lines[0]._hbox.ownerDocument + .querySelector("label[href]") + .addEventListener("click", async (ev: MouseEvent) => { + ev.stopPropagation(); + ev.preventDefault(); + window.clearTimeout(timer); + popupWin.close(); + const suffix = window.prompt("Suffix") as string; + destPath = await addSuffixToFilename(destPath, suffix); + lock.resolve(); + }); - await lock.promise; - destPath = await addSuffixToFilename(destPath); + await lock.promise; + destPath = await addSuffixToFilename(destPath); + } else { + ztoolkit.log("是同一个文件") + new ztoolkit.ProgressWindow("Attanger", { + closeTime: 3000, + closeOtherProgressWindows: true, + }) + .createLine({ + text: "The target file already exists", + icon: addon.data.icons.moveFile, + }) + .show(); + await attItem.eraseTx() + return attItem + } } // 创建中间路径 if (!(await IOUtils.exists(destDir))) { @@ -738,7 +752,7 @@ function removeFile(file: any, force = false) { } function file2md5(filepath: string) { - Zotero.Utilities.Internal.md5(Zotero.File.pathToFile(filepath)); + return Zotero.Utilities.Internal.md5(Zotero.File.pathToFile(filepath)); } /** * 获取Item的分类路径