diff --git a/addon/chrome/content/syncList.xul b/addon/chrome/content/syncList.xul index 64e725d..783b63e 100644 --- a/addon/chrome/content/syncList.xul +++ b/addon/chrome/content/syncList.xul @@ -40,6 +40,7 @@ + diff --git a/addon/chrome/locale/en-US/overlay.dtd b/addon/chrome/locale/en-US/overlay.dtd index 2dcd49f..51c3ef0 100644 --- a/addon/chrome/locale/en-US/overlay.dtd +++ b/addon/chrome/locale/en-US/overlay.dtd @@ -54,6 +54,7 @@ + diff --git a/addon/chrome/locale/zh-CN/overlay.dtd b/addon/chrome/locale/zh-CN/overlay.dtd index 9958c77..71c4a00 100644 --- a/addon/chrome/locale/zh-CN/overlay.dtd +++ b/addon/chrome/locale/zh-CN/overlay.dtd @@ -54,6 +54,7 @@ + diff --git a/src/events.ts b/src/events.ts index 7446c94..ed335ee 100644 --- a/src/events.ts +++ b/src/events.ts @@ -1586,6 +1586,13 @@ class AddonEvents extends AddonBase { this._Addon.template.getCitationStyle(); // Initialize sync notes this._Addon.sync.getSyncNoteIds(); + // Initialize sync period + // Default sync period is 10s + if ( + typeof Zotero.Prefs.get("Knowledge4Zotero.syncPeriod") === "undefined" + ) { + this._Addon.syncList.changeSyncPeriod(10); + } } } diff --git a/src/sync.ts b/src/sync.ts index c082b3a..839e853 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -193,7 +193,7 @@ class AddonSync extends AddonBase { noteItem.removeTag(sycnTag.tag); } noteItem.addTag( - `sync://note/?version=${noteItem._version}&path=${ + `sync://note/?version=${noteItem._version + 1}&path=${ path ? encodeURIComponent(path) : syncInfo["path"] }&filename=${ filename ? encodeURIComponent(filename) : syncInfo["filename"] @@ -206,11 +206,14 @@ class AddonSync extends AddonBase { setSync() { const _t = new Date().getTime(); this.triggerTime = _t; - setTimeout(() => { - if (this.triggerTime === _t) { - this.doSync(); - } - }, 10000); + const syncPeriod = Number(Zotero.Prefs.get("Knowledge4Zotero.syncPeriod")); + if (syncPeriod > 0) { + setTimeout(() => { + if (this.triggerTime === _t) { + this.doSync(); + } + }, syncPeriod); + } } async doSync( @@ -229,7 +232,7 @@ class AddonSync extends AddonBase { const filepath = decodeURIComponent(syncInfo.path); const filename = decodeURIComponent(syncInfo.filename); if ( - Number(syncInfo.version) < item._version - 1 || + Number(syncInfo.version) < item._version || !(await OS.File.exists(`${filepath}/${filename}`)) || forceNoteIds.includes(item.id) ) { diff --git a/src/syncList.ts b/src/syncList.ts index b8fa2d8..c2e90e0 100644 --- a/src/syncList.ts +++ b/src/syncList.ts @@ -14,7 +14,7 @@ class AddonSyncList extends AddonBase { window.open( "chrome://Knowledge4Zotero/content/syncList.xul", "", - "chrome,centerscreen,resizable,status,width=500,height=400" + "chrome,centerscreen,resizable,status,width=600,height=400" ); } } @@ -25,6 +25,9 @@ class AddonSyncList extends AddonBase { } doUpdate() { + if (!this._window || this._window.closed) { + return; + } const notes = Zotero.Items.get(this._Addon.sync.getSyncNoteIds()); const listbox = this._window.document.getElementById("sync-list"); let e, @@ -77,6 +80,19 @@ class AddonSyncList extends AddonBase { }); listbox.append(listitem); } + + const periodButton = this._window.document.getElementById( + "changesyncperiod" + ) as XUL.Button; + const period = + Number(Zotero.Prefs.get("Knowledge4Zotero.syncPeriod")) / 1000; + periodButton.setAttribute( + "label", + periodButton.getAttribute("label").split(":")[0] + + ":" + + (period > 0 ? period + "s" : "disabled") + ); + this._window.focus(); } getSelectedItems(): ZoteroItem[] { @@ -112,6 +128,22 @@ class AddonSyncList extends AddonBase { this.doUpdate(); } + changeSyncPeriod(period: number = -1) { + if (period < 0) { + const inputPeriod = prompt("Enter synchronization period in seconds:"); + if (inputPeriod) { + period = Number(inputPeriod); + } else { + return; + } + } + if (period < 0) { + period = 0; + } + Zotero.Prefs.set("Knowledge4Zotero.syncPeriod", Math.round(period) * 1000); + this.doUpdate(); + } + async removeSync() { let selectedItems = this.getSelectedItems(); if (selectedItems.length === 0) {