Skip to content

Commit

Permalink
Added menu to annotation in reader
Browse files Browse the repository at this point in the history
  • Loading branch information
frianasoa committed Dec 25, 2023
1 parent 3c91b71 commit 63e20be
Show file tree
Hide file tree
Showing 18 changed files with 506 additions and 265 deletions.
11 changes: 10 additions & 1 deletion bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var Filter;
var Image;
var Ai;
var CryptoJS;
var Actions;
var Annotations;
var Languages;

const ANNOTATION = 1;
const ANNOTATION_LABEL = "annotation";
Expand Down Expand Up @@ -172,7 +175,6 @@ function initPreferences(rootURI) {
src: rootURI + 'content/settings/preferences.xhtml',
scripts: [
rootURI + 'content/settings/zntable.js',
rootURI + 'content/settings/languages.js',
rootURI + 'content/settings/preferences.js',
],
image: rootURI+"/content/images/zenotes-settings.png"
Expand Down Expand Up @@ -223,6 +225,9 @@ async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })
Services.scriptloader.loadSubScript(rootURI + 'core/data.js');
Services.scriptloader.loadSubScript(rootURI + 'core/format.js');
Services.scriptloader.loadSubScript(rootURI + 'core/ai.js');
Services.scriptloader.loadSubScript(rootURI + 'content/notes/actions.js');
Services.scriptloader.loadSubScript(rootURI + 'core/annotations.js');
Services.scriptloader.loadSubScript(rootURI + 'core/languages.js');
Services.scriptloader.loadSubScript(rootURI + 'lib/CryptoJS 3.1.2/aes.js');

ZeNotes.init({ id, version, rootURI });
Expand All @@ -242,11 +247,15 @@ async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })
ZeNotes.Image = Image;
ZeNotes.Ai = Ai;
ZeNotes.CryptoJS = CryptoJS;
ZeNotes.Actions = Actions;
ZeNotes.Annotations = Annotations;
ZeNotes.Languages = Languages;

ZeNotes.Data = Data;


ZeNotes.Format = Format;
Annotations.initmenu();
await ZeNotes.main();
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions content/lib/fontawesome/6.1.1/svgs/b.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions content/lib/fontawesome/6.1.1/svgs/d.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions content/lib/fontawesome/6.1.1/svgs/g.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions content/lib/fontawesome/6.1.1/svgs/google.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions content/lib/fontawesome/6.1.1/svgs/language.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
333 changes: 333 additions & 0 deletions content/notes/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
Actions = {
editannotationcomment(annotation){
if(!annotation)
{
alert("Annotation not found!");
return;
}

var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}

var html = document.createElement("div");
html.style = "width:100%; padding: 0.5em;"
var value = currentcomment.split("\n").join("<br/>");
value = value.split("&").join("&amp;")
html.innerHTML = value;
html.contentEditable = true;

Dialog.open(html, function(){
let value = html.innerHTML.split("<br xmlns=\"http:https://www.w3.org/1999/xhtml\" />").join("\n");

value = value.split(" xmlns=\"http:https://www.w3.org/1999/xhtml\"").join("");
value = value.split("<div>").join("");
value = value.split("</div>").join("\n");
value = value.split("<br />").join("\n");
value = value.split("<br/>").join("\n");

value = value.split("&amp;").join("&");

annotation.annotationComment = value;
annotation.saveTx({skipSelect:true}).then(e=>{
Zotero.ZeNotes.Ui.reload();
});
}, "Edit annotation comment", "save");
},

showannotation(attachmentid, annotationpage, annotationkey){
var attachment = Zotero.Items.get(attachmentid);

if(!attachment)
{
alert("Attachment not found");
return;
}

if(!annotationkey)
{
alert("Annotation not found!");
return;
}
Zotero.OpenPDF.openToPage(attachment, annotationpage, annotationkey);
},

googletranslate(annotation, direct=false){
var tl = Zotero.ZeNotes.Prefs.get("target-language");
var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}

var mode = "api-key";
if(Zotero.ZeNotes.Prefs.getb("google-translate-key")=="")
{
mode="free-0";
}

Zotero.ZeNotes.Ai.Google.translate(annotation["annotationText"], tl, mode).then(r=>{
if(direct)
{
annotation.annotationComment = currentcomment+"\n\n<b>[Translation]</b>\n"+r[0]+"\n";
annotation.saveTx({skipSelect:true}).then(e=>{
});
return;
}

var table = AiUi.createdialog(annotation, currentcomment, r, "g-translate");
Dialog.open(table, function(){}, "Choose translation [Google]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
Dialog.open(html, function(){
});
});
},

deepltranslate(annotation, direct=false)
{
var tl = Zotero.ZeNotes.Prefs.get("target-language");
var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}

Zotero.ZeNotes.Ai.DeepL.translate(annotation["annotationText"], tl).then(r=>{
if(direct)
{
annotation.annotationComment = currentcomment+"\n\n<b>[Translation]</b>\n"+r[0]+"\n";
annotation.saveTx({skipSelect:true}).then(e=>{
});
return;
}

var table = AiUi.createdialog(annotation, currentcomment, r, "deepl-translate");
Dialog.open(table, function(){}, "Choose translation [DeepL]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
Dialog.open(html, function(){
});
});
},

bardcustomprompt(data, target, annotation)
{
if(Zotero.ZeNotes.Prefs.getb("bard-api-key")=="")
{
alert("Please set API key first.\nGo to ZeNotes > Settings > General Settings > AI API settings");
return;
}

var customprompt = Zotero.ZeNotes.Prefs.get(target+"-custom-prompt");
if(!customprompt)
{
customprompt = Zotero.ZeNotes.Ai.prompts[target];
}

var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}

Zotero.ZeNotes.Ai.Bard.customprompt(JSON.stringify(data), target).then(r=>{
var table = AiUi.createdialog(annotation, currentcomment, r, "bard");
var model = Zotero.ZeNotes.Prefs.get("bard-model");
var div = document.createElement("div");
div.innerHTML = "<h2>Custom prompt</h2> "+customprompt+"<hr/>"+this.displayjson(data);
try {
TabbedDialog.open(table, div, function(){}, "Edit and choose a candidate [Bard: "+model+"]", "close");
}
catch(e)
{
alert(e);
}
}).catch(r=>{
var div = document.createElement("div");
div.innerHTML = "<h2>Custom prompt</h2> "+customprompt+"<hr/>"+this.displayjson(data);
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
TabbedDialog.open(html, div, function(){
});
});
},

openaicustomprompt(data, target, annotation) {
if(Zotero.ZeNotes.Prefs.getb("openai-api-key")=="")
{
alert("Please set API key first.\nGo to ZeNotes > Settings > General Settings > AI API settings");
return;
}

var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}

var customprompt = Zotero.ZeNotes.Prefs.get(target+"-custom-prompt");
if(!customprompt)
{
customprompt = Zotero.ZeNotes.Ai.prompts[target];
}

Zotero.ZeNotes.Ai.OpenAi.customprompt(JSON.stringify(data), target).then(r=>{
var model = Zotero.ZeNotes.Prefs.get("openai-model");
var table = AiUi.createdialog(annotation, currentcomment, r, "openai");
var div = document.createElement("div");
div.innerHTML = "<h2>Custom prompt</h2> "+customprompt+"<hr/>"+this.displayjson(data);
try {
TabbedDialog.open(table, div, function(){}, "Edit and choose a candidate [OpenAi: "+model+"]", "close");
}
catch(e)
{
alert(e);
}
}).catch(r=>{
var div = document.createElement("div");
div.innerHTML = "<h2>Custom prompt</h2> "+customprompt+"<hr/>"+this.displayjson(data);
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
TabbedDialog.open(html, div, function(){
});
});
},

bardparaphrase(annotation, direct=false)
{
if(Zotero.ZeNotes.Prefs.getb("bard-api-key")=="")
{
alert("Please set API key first.\nGo to ZeNotes > Settings > General Settings > AI API settings");
return;
}

var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}

Zotero.ZeNotes.Ai.Bard.paraphrase(annotation["annotationText"]).then(r=>{
if(direct)
{
annotation.annotationComment = currentcomment+"\n\n<b>[Paraphrase]</b>\n"+r[0]+"\n";
annotation.saveTx({skipSelect:true}).then(e=>{
});
return;
}

var table = AiUi.createdialog(annotation, currentcomment, r, "bard");
var model = Zotero.ZeNotes.Prefs.get("bard-model");
Dialog.open(table, function(){}, "Edit and choose a paraphrase [Bard: "+model+"]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
if(direct)
{
alert(html);
return;
}
Dialog.open(html, function(){
});
});
},

openaiparaphrase(annotation)
{
if(Zotero.ZeNotes.Prefs.getb("openai-api-key")=="")
{
alert("Please set API key first.\nGo to ZeNotes > Settings > General Settings > AI API settings");
return;
}

var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}

Zotero.ZeNotes.Ai.OpenAi.paraphrase(annotation["annotationText"]).then(r=>{
var table = AiUi.createdialog(annotation, currentcomment, r, "gpt");
var model = Zotero.ZeNotes.Prefs.get("openai-model");
Dialog.open(table, function(){}, "Edit and choose a paraphrase [OpenAi: "+model+"]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
Dialog.open(html, function(){
});
});
},

displayjson(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
json = json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'json-number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'json-key';
} else {
cls = 'json-string';
}
} else if (/true|false/.test(match)) {
cls = 'json-boolean';
} else if (/null/.test(match)) {
cls = 'json-null';
}
return '<span class="' + cls + '">' + match + '</span>';
});

json = json.split("\n").join("<br/>").split(" ").join("&#160;&#160;");
return json;
}

}
Loading

0 comments on commit 63e20be

Please sign in to comment.