add: annotation tag action
This commit is contained in:
parent
67ad622b33
commit
f97d66b2ba
|
|
@ -41,6 +41,7 @@ import {
|
|||
createNoteFromTemplate,
|
||||
createWorkspaceNote,
|
||||
} from "./modules/createNote";
|
||||
import { annotationTagAction } from "./modules/annotationTagAction";
|
||||
|
||||
async function onStartup() {
|
||||
await Promise.all([
|
||||
|
|
@ -56,7 +57,7 @@ async function onStartup() {
|
|||
|
||||
registerNoteLinkProxyHandler();
|
||||
|
||||
registerNotify(["tab", "item"]);
|
||||
registerNotify(["tab", "item", "item-tag"]);
|
||||
|
||||
registerEditorInstanceHook();
|
||||
|
||||
|
|
@ -126,6 +127,10 @@ function onNotify(
|
|||
if (annotationItems.length !== 0) {
|
||||
checkReaderAnnotationButton(annotationItems);
|
||||
}
|
||||
}
|
||||
// Insert annotation when assigning tag starts with @
|
||||
if (event === "add" && type === "item-tag") {
|
||||
annotationTagAction(ids as number[], extraData);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
import { addLineToNote, getNoteTreeFlattened } from "../utils/note";
|
||||
|
||||
export { annotationTagAction };
|
||||
|
||||
async function annotationTagAction(
|
||||
ids: Array<number | string>,
|
||||
extraData: Record<string, any>
|
||||
) {
|
||||
const workspaceNote = Zotero.Items.get(addon.data.workspace.mainId);
|
||||
if (!workspaceNote || !workspaceNote.isNote()) {
|
||||
return;
|
||||
}
|
||||
const nodes = getNoteTreeFlattened(workspaceNote);
|
||||
const headings: string[] = nodes.map((node) => node.model.name);
|
||||
|
||||
for (const tagId of ids.filter((t) =>
|
||||
(extraData[t].tag as string).startsWith("@")
|
||||
)) {
|
||||
const tagName = (extraData[tagId].tag as string).slice(1).trim();
|
||||
if (headings.includes(tagName) || tagName === "@") {
|
||||
let lineIndex: number;
|
||||
if (tagName === "@") {
|
||||
lineIndex = -1;
|
||||
} else {
|
||||
const targetNode = nodes.find((node) => node.model.name === tagName);
|
||||
lineIndex = targetNode?.model.endIndex;
|
||||
}
|
||||
|
||||
const annotationItem = Zotero.Items.get((tagId as string).split("-")[0]);
|
||||
if (!annotationItem.isAnnotation()) {
|
||||
continue;
|
||||
}
|
||||
await addLineToNote(
|
||||
workspaceNote,
|
||||
await addon.api.convert.annotations2html([annotationItem], {
|
||||
noteItem: workspaceNote,
|
||||
}),
|
||||
lineIndex
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue