resolve: #72 auto insert annotations to main note

This commit is contained in:
xiangyu 2022-07-08 15:55:21 +08:00
parent 596240220f
commit e686d180c6
7 changed files with 98 additions and 7 deletions

View File

@ -37,6 +37,7 @@
<command id="cmd_addheading_betternotes" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'addHeading'});" />
<command id="cmd_indent_betternotes" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'indentHeading'});" />
<command id="cmd_unindent_betternotes" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'unindentHeading'});" />
<command id="cmd_autoannotation_betternotes" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'updateAutoAnnotation'});" />
<command id="cmd_treeview_betternotes" oncommand="Zotero.Knowledge4Zotero.views.switchView(1);" />
<command id="cmd_mindmap_betternotes" oncommand="Zotero.Knowledge4Zotero.views.switchView(2);" />
<command id="cmd_bubblemap_betternotes" oncommand="Zotero.Knowledge4Zotero.views.switchView(3);" />
@ -79,6 +80,8 @@
<menuitem id="menu_addheading_betternotes" class="menu-type-betternotes" label="&zotero.__addonRef__.workspace.menu.addheading;" command="cmd_addheading_betternotes" />
<menuitem id="menu_indent_betternotes" class="menu-type-betternotes" label="&zotero.__addonRef__.workspace.menu.indent;" command="cmd_indent_betternotes" key="key_indent_betternotes" />
<menuitem id="menu_unindent_betternotes" class="menu-type-betternotes" label="&zotero.__addonRef__.workspace.menu.unindent;" command="cmd_unindent_betternotes" key="key_unindent_betternotes" />
<menuseparator />
<menuitem id="menu_autoannotation_betternotes" type="checkbox" label="&zotero.__addonRef__.workspace.menu.autoannotation;" command="cmd_autoannotation_betternotes" />
</menupopup>
<menupopup id="menu_viewPopup">

View File

@ -47,6 +47,7 @@
<command id="cmd_addheading" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'addHeading'});" />
<command id="cmd_indent_betternotes" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'indentHeading'});" />
<command id="cmd_unindent_betternotes" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'unindentHeading'});" />
<command id="cmd_autoannotation_betternotes" oncommand="Zotero.Knowledge4Zotero.events.onEditorEvent({type: 'updateAutoAnnotation', content: {event: event}});" />
<command id="cmd_treeview" oncommand="Zotero.Knowledge4Zotero.views.switchView(1);" />
<command id="cmd_mindmap" oncommand="Zotero.Knowledge4Zotero.views.switchView(2);" />
<command id="cmd_bubblemap" oncommand="Zotero.Knowledge4Zotero.views.switchView(3);" />
@ -86,6 +87,8 @@
<menuitem id="menu_addheading" label="&zotero.__addonRef__.workspace.menu.addheading;" command="cmd_addheading" />
<menuitem id="menu_indent" label="&zotero.__addonRef__.workspace.menu.indent;" command="cmd_indent_betternotes" key="key_indent" />
<menuitem id="menu_unindent" label="&zotero.__addonRef__.workspace.menu.unindent;" command="cmd_unindent_betternotes" key="key_unindent" />
<menuseparator />
<menuitem id="menu_autoannotation_betternotes" type="checkbox" label="&zotero.__addonRef__.workspace.menu.autoannotation;" command="cmd_autoannotation_betternotes" />
</menupopup>
</menu>

View File

@ -15,6 +15,7 @@
<!ENTITY zotero.__addonRef__.workspace.menu.addheading "Insert Heading...">
<!ENTITY zotero.__addonRef__.workspace.menu.indent "Decrease Heading Level">
<!ENTITY zotero.__addonRef__.workspace.menu.unindent "Increase Heading Level">
<!ENTITY zotero.__addonRef__.workspace.menu.autoannotation "Auto Insert New Annotations to Note">
<!ENTITY zotero.__addonRef__.workspace.menu.treeview "Outline: Tree View">
<!ENTITY zotero.__addonRef__.workspace.menu.mindmap "Outline: Mind Map">
<!ENTITY zotero.__addonRef__.workspace.menu.bubblemap "Outline: Bubble Map">

View File

@ -15,6 +15,7 @@
<!ENTITY zotero.__addonRef__.workspace.menu.addheading "插入标题...">
<!ENTITY zotero.__addonRef__.workspace.menu.indent "降低标题层级">
<!ENTITY zotero.__addonRef__.workspace.menu.unindent "提升标题层级">
<!ENTITY zotero.__addonRef__.workspace.menu.autoannotation "自动插入新注释到笔记">
<!ENTITY zotero.__addonRef__.workspace.menu.treeview "大纲: 树视图">
<!ENTITY zotero.__addonRef__.workspace.menu.mindmap "大纲: 思维导图">
<!ENTITY zotero.__addonRef__.workspace.menu.bubblemap "大纲: 气泡导图">

View File

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

View File

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

View File

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