From ec963734130c28b8bb82dd6380a598055920f6fa Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Sun, 8 Sep 2024 17:21:44 +0200 Subject: [PATCH] add: keep tags of note from annotation in sync with the original annotation resolve: #1095 --- addon/chrome/content/preferences.xhtml | 8 +++ addon/locale/en-US/preferences.ftl | 4 ++ addon/locale/it-IT/preferences.ftl | 4 ++ addon/locale/ru-RU/preferences.ftl | 4 ++ addon/locale/tr-TR/preferences.ftl | 4 ++ addon/locale/zh-CN/preferences.ftl | 4 ++ addon/prefs.js | 2 + src/hooks.ts | 16 +++++- src/modules/{reader.ts => annotationNote.ts} | 58 +++++++++++++++++++- 9 files changed, 99 insertions(+), 5 deletions(-) rename src/modules/{reader.ts => annotationNote.ts} (73%) diff --git a/addon/chrome/content/preferences.xhtml b/addon/chrome/content/preferences.xhtml index 5b0bd95..e6e5cb5 100644 --- a/addon/chrome/content/preferences.xhtml +++ b/addon/chrome/content/preferences.xhtml @@ -87,6 +87,14 @@ > + + + + diff --git a/addon/locale/en-US/preferences.ftl b/addon/locale/en-US/preferences.ftl index 61a4680..e1496d5 100644 --- a/addon/locale/en-US/preferences.ftl +++ b/addon/locale/en-US/preferences.ftl @@ -23,6 +23,10 @@ annotation-title = PDF Annotation annotation-autoAnnotation = .label = Automatically add new annotations to workspace note +annotationNote-title = Note from Annotation +annotationNote-enableTagSync = + .label = Keep tags of note from annotation in sync with the original annotation + about-title = About help = .value = { $name } VERSION { $version } Build { $time } \ No newline at end of file diff --git a/addon/locale/it-IT/preferences.ftl b/addon/locale/it-IT/preferences.ftl index 2b8ca36..0eee91b 100644 --- a/addon/locale/it-IT/preferences.ftl +++ b/addon/locale/it-IT/preferences.ftl @@ -23,6 +23,10 @@ annotation-title = Annotazione PDF annotation-autoAnnotation = .label = Aggiungi automaticamente le nuove annotazioni alla nota di lavoro +annotationNote-title = Note from Annotation +annotationNote-enableTagSync = + .label = Keep tags of note from annotation in sync with the original annotation + about-title = Informazioni su Better Notes help = .value = { $name } VERSION { $version } Build { $time } diff --git a/addon/locale/ru-RU/preferences.ftl b/addon/locale/ru-RU/preferences.ftl index b5f803c..2cd6567 100644 --- a/addon/locale/ru-RU/preferences.ftl +++ b/addon/locale/ru-RU/preferences.ftl @@ -23,6 +23,10 @@ annotation-title = PDF Аннотация annotation-autoAnnotation = .label = Автодобавлять новые аннотации к заметкам раб.пространства +annotationNote-title = Note from Annotation +annotationNote-enableTagSync = + .label = Keep tags of note from annotation in sync with the original annotation + about-title = About help = .value = { $name } VERSION { $version } Build { $time } \ No newline at end of file diff --git a/addon/locale/tr-TR/preferences.ftl b/addon/locale/tr-TR/preferences.ftl index a6ba749..e69823a 100644 --- a/addon/locale/tr-TR/preferences.ftl +++ b/addon/locale/tr-TR/preferences.ftl @@ -23,6 +23,10 @@ annotation-title = PDF Ek Açıklamaları annotation-autoAnnotation = .label = Çalışma alanı notuna otomatik olarak yeni ek açıklamaları ekle +annotationNote-title = Note from Annotation +annotationNote-enableTagSync = + .label = Keep tags of note from annotation in sync with the original annotation + about-title = Hakkında help = .value = { $name } Sürüm { $version } Yapı Numarası { $time } diff --git a/addon/locale/zh-CN/preferences.ftl b/addon/locale/zh-CN/preferences.ftl index 08f17e5..8a4ad1f 100644 --- a/addon/locale/zh-CN/preferences.ftl +++ b/addon/locale/zh-CN/preferences.ftl @@ -23,6 +23,10 @@ annotation-title = PDF批注 annotation-autoAnnotation = .label = 自动添加新批注到工作区笔记 +annotationNote-title = 从注释生成笔记 +annotationNote-enableTagSync = + .label = 保持注释生成的笔记的标签与原始注释同步 + about-title = 关于 help = .value = { $name } 版本 { $version } 构建 { $time } \ No newline at end of file diff --git a/addon/prefs.js b/addon/prefs.js index 407478a..305deae 100644 --- a/addon/prefs.js +++ b/addon/prefs.js @@ -25,3 +25,5 @@ pref("__prefsPrefix__.editor.noteLinkPreview", true); pref("__prefsPrefix__.openNote.takeover", true); pref("__prefsPrefix__.openNote.defaultAsWindow", false); + +pref("__prefsPrefix__.annotationNote.enableTagSync", true); diff --git a/src/hooks.ts b/src/hooks.ts index 22dc7a9..c1c3f83 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -18,7 +18,10 @@ import { import { openWorkspaceWindow } from "./modules/workspace/window"; import { openNotePreview } from "./modules/workspace/preview"; import { registerNotify } from "./modules/notify"; -import { registerReaderAnnotationButton } from "./modules/reader"; +import { + registerReaderAnnotationButton, + syncAnnotationNoteTags, +} from "./modules/annotationNote"; import { setSyncing, callSyncing } from "./modules/sync/hooks"; import { showTemplatePicker, @@ -140,8 +143,15 @@ async function onNotify( } onUpdateNoteTabsTitle(modifiedNotes); } - } else { - return; + } + if (type === "item-tag") { + for (const itemTagID of ids) { + await syncAnnotationNoteTags( + Number((itemTagID as string).split("-")[0]), + event as "add" | "remove", + extraData[itemTagID], + ); + } } } diff --git a/src/modules/reader.ts b/src/modules/annotationNote.ts similarity index 73% rename from src/modules/reader.ts rename to src/modules/annotationNote.ts index e970558..0cf4835 100644 --- a/src/modules/reader.ts +++ b/src/modules/annotationNote.ts @@ -1,9 +1,12 @@ import { config } from "../../package.json"; import { ICONS } from "../utils/config"; -import { getNoteLink, getNoteLinkParams } from "../utils/link"; +import { getNoteLinkParams } from "../utils/link"; import { addLineToNote } from "../utils/note"; +import { getPref } from "../utils/prefs"; -export function registerReaderAnnotationButton() { +export { registerReaderAnnotationButton, syncAnnotationNoteTags }; + +function registerReaderAnnotationButton() { Zotero.Reader.registerEventListener( "renderSidebarAnnotationHeader", (event) => { @@ -135,3 +138,54 @@ async function createNoteFromAnnotation( addon.hooks.onOpenNote(note.id, "builtin", {}); } + +async function syncAnnotationNoteTags( + itemID: number, + action: "add" | "remove", + tagData: { tag: string; type: number }, +) { + if (!getPref("annotationNote.enableTagSync")) { + return; + } + const item = Zotero.Items.get(itemID); + if (!item || (!item.isAnnotation() && !item.isNote())) { + return; + } + let targetItem: Zotero.Item; + if (item.isAnnotation()) { + const annotationModel = await addon.api.relation.getLinkTargetByAnnotation( + item.libraryID, + item.key, + ); + if (!annotationModel) { + return; + } + targetItem = Zotero.Items.getByLibraryAndKey( + annotationModel.toLibID, + annotationModel.toKey, + ) as Zotero.Item; + } else { + const annotationModel = await addon.api.relation.getAnnotationByLinkTarget( + item.libraryID, + item.key, + ); + if (!annotationModel) { + return; + } + targetItem = Zotero.Items.getByLibraryAndKey( + annotationModel.fromLibID, + annotationModel.fromKey, + ) as Zotero.Item; + } + if (!targetItem) { + return; + } + + if (action === "add") { + targetItem.addTag(tagData.tag, tagData.type); + } else { + targetItem.removeTag(tagData.tag); + } + + await targetItem.saveTx(); +}