add: keep tags of note from annotation in sync with the original annotation

resolve: #1095
This commit is contained in:
windingwind 2024-09-08 17:21:44 +02:00
parent 887d71487e
commit ec96373413
9 changed files with 99 additions and 5 deletions

View File

@ -87,6 +87,14 @@
></button>
</hbox>
</groupbox>
<groupbox>
<label><html:h2 data-l10n-id="annotationNote-title"></html:h2></label>
<checkbox
data-l10n-id="annotationNote-enableTagSync"
native="true"
preference="__prefsPrefix__.annotationNote.enableTagSync"
/>
</groupbox>
<groupbox>
<label><html:h2 data-l10n-id="about-title"></html:h2></label>

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -23,6 +23,10 @@ annotation-title = PDF批注
annotation-autoAnnotation =
.label = 自动添加新批注到工作区笔记
annotationNote-title = 从注释生成笔记
annotationNote-enableTagSync =
.label = 保持注释生成的笔记的标签与原始注释同步
about-title = 关于
help =
.value = { $name } 版本 { $version } 构建 { $time }

View File

@ -25,3 +25,5 @@ pref("__prefsPrefix__.editor.noteLinkPreview", true);
pref("__prefsPrefix__.openNote.takeover", true);
pref("__prefsPrefix__.openNote.defaultAsWindow", false);
pref("__prefsPrefix__.annotationNote.enableTagSync", true);

View File

@ -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],
);
}
}
}

View File

@ -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();
}