From 27e67c39c6bb3f8bead68284612fc53ed6bd2db7 Mon Sep 17 00:00:00 2001 From: windingwind Date: Tue, 14 Feb 2023 12:18:17 +0800 Subject: [PATCH] fix: sync bug when item id re-assigned --- package.json | 2 +- src/editor/editorController.ts | 2 +- src/editor/editorViews.ts | 4 ++-- src/note/noteExportController.ts | 4 ++-- src/note/noteImportController.ts | 2 +- src/note/noteUtils.ts | 12 +++++++----- src/sync/syncController.ts | 8 +++++--- src/sync/syncUtils.ts | 10 ++++++++-- src/workspace/workspaceWindow.ts | 4 ++-- src/zotero/events.ts | 2 +- 10 files changed, 30 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 77a910c..094b736 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,6 @@ "cross-env": "^7.0.3", "esbuild": "^0.14.34", "release-it": "^14.14.0", - "zotero-types": "^0.1.2" + "zotero-types": "^1.0.9" } } diff --git a/src/editor/editorController.ts b/src/editor/editorController.ts index da40f9f..e8940e5 100644 --- a/src/editor/editorController.ts +++ b/src/editor/editorController.ts @@ -10,7 +10,7 @@ class EditorController extends AddonBase { instance: Zotero.EditorInstance; time: number; }>; - editorPromise: _ZoteroPromiseObject; + editorPromise: _ZoteroTypes.PromiseObject; activeEditor: Zotero.EditorInstance; constructor(parent: BetterNotes) { diff --git a/src/editor/editorViews.ts b/src/editor/editorViews.ts index 8a88874..74d54f4 100644 --- a/src/editor/editorViews.ts +++ b/src/editor/editorViews.ts @@ -262,7 +262,7 @@ class EditorViews extends AddonBase { { id: `knowledge-addcitation-popup-${topItem.id}`, rank: 0, - text: topItem.getField("title"), + text: topItem.getField("title") as string, eventType: "insertCitation", }, ], @@ -699,7 +699,7 @@ class EditorViews extends AddonBase { ); newLines.push(templateText); const newLineString = newLines.join("\n"); - const notifyFlag: _ZoteroPromiseObject = Zotero.Promise.defer(); + const notifyFlag: _ZoteroTypes.PromiseObject = Zotero.Promise.defer(); const notifierName = "insertLinkWait"; this._Addon.ZoteroNotifies.registerNotifyListener( notifierName, diff --git a/src/note/noteExportController.ts b/src/note/noteExportController.ts index 2e332cc..ecf74b2 100644 --- a/src/note/noteExportController.ts +++ b/src/note/noteExportController.ts @@ -13,8 +13,8 @@ class NoteExport extends AddonBase { note: Zotero.Item; filename: string; }>; - _pdfPrintPromise: _ZoteroPromiseObject; - _docxPromise: _ZoteroPromiseObject; + _pdfPrintPromise: _ZoteroTypes.PromiseObject; + _docxPromise: _ZoteroTypes.PromiseObject; _docxBlob: Blob; constructor(parent: BetterNotes) { diff --git a/src/note/noteImportController.ts b/src/note/noteImportController.ts index 360e410..4f2c8fb 100644 --- a/src/note/noteImportController.ts +++ b/src/note/noteImportController.ts @@ -41,7 +41,7 @@ class NoteImport extends AddonBase { } catch (e) { this._Addon.toolkit.Tool.log(`Import: ${String(e)}`); } - if (!options.ignoreVersion && mdStatus.meta?.version < noteItem?._version) { + if (!options.ignoreVersion && mdStatus.meta?.version < noteItem?.version) { if ( !confirm( `The target note seems to be newer than the file ${file}. Are you sure you want to import it anyway?` diff --git a/src/note/noteUtils.ts b/src/note/noteUtils.ts index 04319a1..e00c989 100644 --- a/src/note/noteUtils.ts +++ b/src/note/noteUtils.ts @@ -306,7 +306,7 @@ class NoteUtils extends AddonBase { } = { ignore: false, withLine: false } ) { let libraryID = note.libraryID; - let library = Zotero.Libraries.get(libraryID); + let library = Zotero.Libraries.get(libraryID) as Zotero.Library; let groupID: string; if (library.libraryType === "user") { groupID = "u"; @@ -346,7 +346,7 @@ class NoteUtils extends AddonBase { const attachment = annotation.parentItem; let libraryID = attachment.libraryID; - let library = Zotero.Libraries.get(libraryID); + let library = Zotero.Libraries.get(libraryID) as Zotero.Library; if (library.libraryType === "user") { openURI = `zotero://open-pdf/library/items/${attachment.key}`; } else if (library.libraryType === "group") { @@ -691,10 +691,10 @@ class NoteUtils extends AddonBase { }; } this._Addon.toolkit.Tool.log(params); - let item: Zotero.Item = await Zotero.Items.getByLibraryAndKeyAsync( + let item: Zotero.Item = (await Zotero.Items.getByLibraryAndKeyAsync( params.libraryID, params.noteKey - ); + )) as Zotero.Item; if (!item || !item.isNote()) { return { item: undefined, @@ -775,7 +775,9 @@ class NoteUtils extends AddonBase { } } } - this._Addon.toolkit.Tool.log(`line ${currentLineIndex} of item ${itemID} selected.`); + this._Addon.toolkit.Tool.log( + `line ${currentLineIndex} of item ${itemID} selected.` + ); // Don't use the instance._item.id, as it might not be updated. this.currentLine[itemID] = currentLineIndex; if (realElement.tagName === "A") { diff --git a/src/sync/syncController.ts b/src/sync/syncController.ts index 83e61a7..83fa55a 100644 --- a/src/sync/syncController.ts +++ b/src/sync/syncController.ts @@ -16,7 +16,9 @@ class SyncController extends AddonBase { getSyncNoteIds(): number[] { const ids = Zotero.Prefs.get("Knowledge4Zotero.syncNoteIds") as string; - return ids.split(",").map((id: string) => Number(id)); + return Zotero.Items.get(ids.split(",").map((id: string) => Number(id))) + .filter((item) => item.isNote()) + .map((item) => item.id); } isSyncNote(note: Zotero.Item): boolean { @@ -95,7 +97,7 @@ class SyncController extends AddonBase { } // Note version doesn't match (note side change) // This might be unreliable when Zotero account is not login - if (Number(MDStatus.meta.version) !== noteItem._version) { + if (Number(MDStatus.meta.version) !== noteItem.version) { noteAhead = true; } if (noteAhead && MDAhead) { @@ -139,7 +141,7 @@ class SyncController extends AddonBase { let progress; // Wrap the code in try...catch so that the lock can be released anyway try { - this._Addon.toolkit.Tool.log("sync start") + this._Addon.toolkit.Tool.log("sync start"); this.sycnLock = true; if (!items || !items.length) { items = Zotero.Items.get(this.getSyncNoteIds()); diff --git a/src/sync/syncUtils.ts b/src/sync/syncUtils.ts index 9655b70..c9c59c1 100644 --- a/src/sync/syncUtils.ts +++ b/src/sync/syncUtils.ts @@ -46,7 +46,7 @@ class SyncUtils extends AddonBase { } async _getDataURL(item: Zotero.Item) { - let path = await item.getFilePathAsync(); + let path = (await item.getFilePathAsync()) as string; let buf = new Uint8Array((await OS.File.read(path, {})) as Uint8Array) .buffer; return ( @@ -890,7 +890,13 @@ class SyncUtils extends AddonBase { imgKey ); this._Addon.toolkit.Tool.log(attachmentItem); - this._Addon.toolkit.Tool.log("image", libraryID, imgKey, attachmentItem, node); + this._Addon.toolkit.Tool.log( + "image", + libraryID, + imgKey, + attachmentItem, + node + ); if (!attachmentItem) { continue; } diff --git a/src/workspace/workspaceWindow.ts b/src/workspace/workspaceWindow.ts index b66ec73..d01c1d8 100644 --- a/src/workspace/workspaceWindow.ts +++ b/src/workspace/workspaceWindow.ts @@ -7,13 +7,13 @@ import { EditorMessage, OutlineType } from "../utils"; import AddonBase from "../module"; class WorkspaceWindow extends AddonBase { - private _initIframe: _ZoteroPromiseObject; + private _initIframe: _ZoteroTypes.PromiseObject; public workspaceWindow: Window; public workspaceTabId: string; public workspaceNoteEditor: Zotero.EditorInstance | undefined; public previewItemID: number; private _firstInit: boolean; - public _workspacePromise: _ZoteroPromiseObject; + public _workspacePromise: _ZoteroTypes.PromiseObject; private _DOMParser: any; constructor(parent: BetterNotes) { diff --git a/src/zotero/events.ts b/src/zotero/events.ts index 6ce7076..882e067 100644 --- a/src/zotero/events.ts +++ b/src/zotero/events.ts @@ -119,7 +119,7 @@ class ZoteroEvents extends AddonBase { }); } - private async onEditorInstanceCreated(instance: _ZoteroEditorInstance) { + private async onEditorInstanceCreated(instance: Zotero.EditorInstance) { await instance._initPromise; instance._knowledgeUIInitialized = false;