From e686d180c62067fa26997b46bb7e99e7a148e2eb Mon Sep 17 00:00:00 2001
From: xiangyu <3170102889@zju.edu.cn>
Date: Fri, 8 Jul 2022 15:55:21 +0800
Subject: [PATCH] resolve: #72 auto insert annotations to main note
---
addon/chrome/content/overlay.xul | 3 ++
addon/chrome/content/workspace.xul | 3 ++
addon/chrome/locale/en-US/overlay.dtd | 1 +
addon/chrome/locale/zh-CN/overlay.dtd | 1 +
src/events.ts | 53 +++++++++++++++++++++++++++
src/knowledge.ts | 11 +++---
src/views.ts | 33 ++++++++++++++++-
7 files changed, 98 insertions(+), 7 deletions(-)
diff --git a/addon/chrome/content/overlay.xul b/addon/chrome/content/overlay.xul
index dd0b07a..39014c0 100644
--- a/addon/chrome/content/overlay.xul
+++ b/addon/chrome/content/overlay.xul
@@ -37,6 +37,7 @@
+
@@ -79,6 +80,8 @@
+
+
diff --git a/addon/chrome/locale/en-US/overlay.dtd b/addon/chrome/locale/en-US/overlay.dtd
index 51c3ef0..1ead6c0 100644
--- a/addon/chrome/locale/en-US/overlay.dtd
+++ b/addon/chrome/locale/en-US/overlay.dtd
@@ -15,6 +15,7 @@
+
diff --git a/addon/chrome/locale/zh-CN/overlay.dtd b/addon/chrome/locale/zh-CN/overlay.dtd
index 71c4a00..269732b 100644
--- a/addon/chrome/locale/zh-CN/overlay.dtd
+++ b/addon/chrome/locale/zh-CN/overlay.dtd
@@ -15,6 +15,7 @@
+
diff --git a/src/events.ts b/src/events.ts
index 53a9fc6..bd2954b 100644
--- a/src/events.ts
+++ b/src/events.ts
@@ -88,6 +88,24 @@ class AddonEvents extends AddonBase {
this._Addon.views.switchKey(true);
}
}
+ if (
+ Zotero.Prefs.get("Knowledge4Zotero.autoAnnotation") &&
+ event === "add" &&
+ type === "item" &&
+ Zotero.Items.get(ids).filter((item) => {
+ return item.isAnnotation();
+ }).length > 0
+ ) {
+ Zotero.debug("Knowledge4Zotero: autoAnnotation");
+ const annotations = Zotero.Items.get(ids).filter((item) => {
+ return item.isAnnotation();
+ });
+ this.onEditorEvent(
+ new EditorMessage("addAnnotationToNote", {
+ params: { annotations: annotations },
+ })
+ );
+ }
},
};
}
@@ -865,6 +883,30 @@ class AddonEvents extends AddonBase {
lineIndex,
message.content.editorInstance._item.id
);
+ } else if (message.type === "addAnnotationToNote") {
+ /*
+ message.content = {
+ params: {annotations}
+ }
+ */
+ const annotations = message.content.params.annotations;
+ await this._Addon.knowledge.addAnnotationsToNote(
+ undefined,
+ annotations,
+ -1
+ );
+ this._Addon.views.showProgressWindow(
+ "Better Notes",
+ `[Auto] Insert Annotation to ${
+ this._Addon.knowledge.currentLine >= 0
+ ? `line ${this._Addon.knowledge.currentLine} in`
+ : "end of"
+ } main note`
+ );
+ // Move cursor foward
+ if (this._Addon.knowledge.currentLine >= 0) {
+ this._Addon.knowledge.currentLine += annotations.length;
+ }
} else if (message.type === "jumpNode") {
/*
message.content = {
@@ -1098,6 +1140,16 @@ class AddonEvents extends AddonBase {
-1,
node.model.lineIndex
);
+ } else if (message.type === "updateAutoAnnotation") {
+ /*
+ message.content = {
+ editorInstance
+ }
+ */
+ let autoAnnotation = Zotero.Prefs.get("Knowledge4Zotero.autoAnnotation");
+ autoAnnotation = !autoAnnotation;
+ Zotero.Prefs.set("Knowledge4Zotero.autoAnnotation", autoAnnotation);
+ this._Addon.views.updateAutoInsertAnnotationsMenu();
} else if (message.type === "insertNotes") {
/*
message.content = {}
@@ -1602,6 +1654,7 @@ class AddonEvents extends AddonBase {
) {
this._Addon.syncList.changeSyncPeriod(10);
}
+ this._Addon.views.updateAutoInsertAnnotationsMenu();
}
}
diff --git a/src/knowledge.ts b/src/knowledge.ts
index af5e4cc..c3c78cf 100644
--- a/src/knowledge.ts
+++ b/src/knowledge.ts
@@ -67,6 +67,7 @@ class Knowledge extends AddonBase {
this._Addon.views.initKnowledgeWindow(win);
this._Addon.views.switchView(OutlineType.treeView);
this._Addon.views.updateOutline();
+ this._Addon.views.updateAutoInsertAnnotationsMenu();
} else {
Zotero.debug("openWorkspaceWindow: as tab");
this._Addon.views._initIframe = Zotero.Promise.defer();
@@ -282,7 +283,7 @@ class Knowledge extends AddonBase {
await this.scrollWithRefresh(lineIndex);
}
- private _dataURLtoBlob(dataurl: string) {
+ _dataURLtoBlob(dataurl: string) {
let parts = dataurl.split(",");
let mime = parts[0].match(/:(.*?);/)[1];
if (parts[0].indexOf("base64") !== -1) {
@@ -298,7 +299,7 @@ class Knowledge extends AddonBase {
return null;
}
- private async _importImage(note: ZoteroItem, src, download = false) {
+ async _importImage(note: ZoteroItem, src, download = false) {
let blob;
if (src.startsWith("data:")) {
blob = this._dataURLtoBlob(src);
@@ -338,8 +339,8 @@ class Knowledge extends AddonBase {
async addAnnotationsToNote(
note: ZoteroItem,
- lineIndex: number,
- annotations: ZoteroItem[]
+ annotations: ZoteroItem[],
+ lineIndex: number
) {
note = note || this.getWorkspaceNote();
if (!note) {
@@ -350,7 +351,7 @@ class Knowledge extends AddonBase {
const annotJson = await this._Addon.parse.parseAnnotation(annot);
annotationJSONList.push(annotJson);
}
- await this.importImagesToNote(note, annotations);
+ await this.importImagesToNote(note, annotationJSONList);
const html =
Zotero.EditorInstanceUtilities.serializeAnnotations(
annotationJSONList
diff --git a/src/views.ts b/src/views.ts
index 4aca61c..f275571 100644
--- a/src/views.ts
+++ b/src/views.ts
@@ -408,8 +408,7 @@ class AddonViews extends AddonBase {
if (!params.ignore) {
const newLink =
link + (link.includes("?") ? "&ignore=1" : "?ignore=1");
- const linkIndex =
- this._Addon.parse.parseLinkIndexInText(oldLine);
+ const linkIndex = this._Addon.parse.parseLinkIndexInText(oldLine);
Zotero.debug(linkIndex);
return `${oldLine.slice(
0,
@@ -874,6 +873,36 @@ class AddonViews extends AddonBase {
);
}
+ updateAutoInsertAnnotationsMenu(
+ _window: Window = undefined,
+ tryStandalone: boolean = true
+ ) {
+ _window = _window || window;
+
+ Zotero.debug("updateAutoInsertAnnotationsMenu");
+
+ let autoAnnotation = Zotero.Prefs.get("Knowledge4Zotero.autoAnnotation");
+ if (typeof autoAnnotation === "undefined") {
+ autoAnnotation = false;
+ Zotero.Prefs.set("Knowledge4Zotero.autoAnnotation", autoAnnotation);
+ }
+
+ const menuitem: XUL.Element = _window.document.getElementById(
+ "menu_autoannotation_betternotes"
+ );
+ if (autoAnnotation) {
+ menuitem.setAttribute("checked", true);
+ } else {
+ menuitem.removeAttribute("checked");
+ }
+ if (tryStandalone) {
+ _window = this._Addon.knowledge.getWorkspaceWindow();
+ if (_window) {
+ this.updateAutoInsertAnnotationsMenu(_window, false);
+ }
+ }
+ }
+
showProgressWindow(
header: string,
context: string,