Skip to content

Commit

Permalink
Added google translate to annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
frianasoa committed Dec 12, 2023
1 parent e40a82b commit 5a17a56
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 60 deletions.
23 changes: 12 additions & 11 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,17 @@ async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })
// setDefaultPrefs(rootURI);
}

Services.scriptloader.loadSubScript(rootURI + 'core/zenotes.js');
Services.scriptloader.loadSubScript(rootURI + 'core/prefs.js');
Services.scriptloader.loadSubScript(rootURI + 'core/database.js');
Services.scriptloader.loadSubScript(rootURI + 'core/image.js');
Services.scriptloader.loadSubScript(rootURI + 'core/filter.js');
Services.scriptloader.loadSubScript(rootURI + 'core/utils.js');
Services.scriptloader.loadSubScript(rootURI + 'core/zenotes.js');
Services.scriptloader.loadSubScript(rootURI + 'core/ui.js');
Services.scriptloader.loadSubScript(rootURI + 'core/menu.js');

Services.scriptloader.loadSubScript(rootURI + 'core/data.js');
Services.scriptloader.loadSubScript(rootURI + 'core/database.js');
Services.scriptloader.loadSubScript(rootURI + 'core/prefs.js');
Services.scriptloader.loadSubScript(rootURI + 'core/format.js');
Services.scriptloader.loadSubScript(rootURI + 'core/ai.js');

Services.scriptloader.loadSubScript(rootURI + 'lib/CryptoJS 3.1.2/aes.js');

ZeNotes.init({ id, version, rootURI });
Expand All @@ -232,6 +230,10 @@ async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })

Zotero_Tabs = Zotero.getMainWindow().Zotero_Tabs;
Menu.addToAllWindows();
ZeNotes.Prefs = Prefs;
ZeNotes.Database = Database;
await Database.init();

ZeNotes.Ui = Ui;
ZeNotes.Menu = Menu;
ZeNotes.Utils = Utils;
Expand All @@ -241,11 +243,9 @@ async function startup({ id, version, resourceURI, rootURI = resourceURI.spec })
ZeNotes.CryptoJS = CryptoJS;

ZeNotes.Data = Data;
ZeNotes.Database = Database;
ZeNotes.Prefs = Prefs;


ZeNotes.Format = Format;
Database.create();
Database.z6transition(Prefs.get("tag-lists"));
await ZeNotes.main();
}

Expand All @@ -259,15 +259,16 @@ function onMainWindowUnload({ window }) {

function shutdown() {
log("Shutting down 1.2");

ZeNotes.Database.close();

if (Zotero.platformMajorVersion < 102) {
removeMainWindowListener();
}
else
{
chromeHandle.destruct();
}

ZeNotes.removeFromAllWindows();
ZeNotes = undefined;
}
Expand Down
67 changes: 56 additions & 11 deletions content/notes/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ Menus = {
}

var items_ai = {}


if(Zotero.ZeNotes.Prefs.getb("bard-api-key")!="google-translate-key")
{
items_ai["translate-google-en"] = {name: "Translate to English (Google)", icon: "fa-language"};
items_ai["sep-ai-02"] = "---------";
}
if(Zotero.ZeNotes.Prefs.getb("bard-api-key")!="")
{
items_ai["paraphrase-bard"] = {name: "Paraphrase annotation (Bard)", icon: "fa-language"},
items_ai["paraphrase-bard"] = {name: "Paraphrase annotation (Bard)", icon: "fa-language"};
items_ai["sep-ai-01"] = "---------";
}


var items1 = {
"showfile": {name: "Show attached files", icon: "fa-file-pdf"},
"showentry": {name: "Show entry", icon: "fa-file-lines"},
Expand Down Expand Up @@ -170,16 +176,17 @@ Menus = {
Zotero.OpenPDF.openToPage(attachment, annotationpage, annotationkey);
},

actions (key, options)
actions(key, options)
{
var tohide = "column";
if(Zotero.ZeNotes.Prefs.get("vertical-table")=="true" || Zotero.ZeNotes.Prefs.get("vertical-table")==true)
var isvertical = Zotero.ZeNotes.Prefs.get("vertical-table");
if( isvertical=="true" || isvertical==true);
{
tohide = "row";
}

var td = options.$trigger.get(0);
var column = td.dataset.column;
var column = td.dataset.column;
var itemid = td.dataset.itemid;
var itemkey = td.dataset.itemkey;
var attachmentid = td.dataset.attachmentid;
Expand All @@ -192,7 +199,7 @@ Menus = {

var filekey = td.dataset.filekey;
var notekey = td.dataset.notekey;

try
{
var filenames = JSON.parse(td.dataset.filenames);
Expand All @@ -204,7 +211,6 @@ Menus = {

if(key=="showentry")
{

let win = Zotero.getMainWindow();
if (win) {
win.ZoteroPane.selectItems([itemid]);
Expand Down Expand Up @@ -234,9 +240,47 @@ Menus = {
// New page to prevent page jump back. Will default to original openToPage when Zotero 7 is stable.
// Zotero.OpenPDF.openToPage(attachment, annotationpagelabel, annotationkey);
this.openToPage(parseInt(attachmentid), parseInt(annotationpage), annotationkey)


}


else if(key=="translate-google-en")
{
if(!annotationkey)
{
alert("Annotation not found!");
return;
}

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

var annotation = Zotero.Items.get(annotationid);
var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}
Zotero.ZeNotes.Ai.Google.translate(annotation["annotationText"], "en").then(r=>{
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(){
});
});
}

else if(key=="paraphrase-bard")
{
if(!annotationkey)
Expand All @@ -258,8 +302,8 @@ Menus = {
currentcomment = "";
}
Zotero.ZeNotes.Ai.Bard.paraphrase(annotation["annotationText"]).then(r=>{
var table = AiUi.createdialog(r);
Dialog.open(table, function(){}, "Choose paraprahse", "close");
var table = AiUi.createdialog(annotation, currentcomment, r, "bard");
Dialog.open(table, function(){}, "Choose paraprahse [Bard]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
Expand All @@ -274,6 +318,7 @@ Menus = {
});
});
}

else if(key=="showfile")
{
this.choosefile(filenames);
Expand Down
4 changes: 2 additions & 2 deletions content/notes/notes.css
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ body {
cursor: pointer;
}

#zn-bard-table {
#zn-ai-table {

}

#zn-bard-table td{
#zn-ai-table td{
border-bottom: solid 1px;
padding: 0.6em;
}
Expand Down
1 change: 1 addition & 0 deletions content/settings/ai.xhtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div xmlns="http://www.w3.org/1999/xhtml">
<table id="ai">
<tr><td>Bard API key</td><td colspan="2"><input id="zn-bard-api-key" type="password" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'bard-api-key', 'encrypt');" style="width:100%;" placeholder="Enter your Bard API key"/></td><td></td></tr>
<tr><td>Google translate API key</td><td colspan="2"><input id="zn-google-translate-key" type="password" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'google-translate-key', 'encrypt');" style="width:100%;" placeholder="Enter your google translate API key"/></td><td></td></tr>
</table>
</div>
3 changes: 3 additions & 0 deletions content/settings/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ Zotero_Preferences.ZeNotes = {
Zotero_Preferences.ZeNotes.loadpreference("column-width", "zn-column-width");

Zotero_Preferences.ZeNotes.loadpreference("bard-api-key", "zn-bard-api-key", "encrypt");

Zotero_Preferences.ZeNotes.loadpreference("google-translate-project-id", "zn-google-translate-project-id", "encrypt");
Zotero_Preferences.ZeNotes.loadpreference("google-translate-key", "zn-google-translate-key", "encrypt");
})
.catch(error => {
console.log('Error loading content: ' + error);
Expand Down
29 changes: 21 additions & 8 deletions content/ui/ai-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ AiUi = {
c.contentEditable = "false";
c.style.backgroundColor = "";
},
acceptcandidate(id, currentcomment, annotation)

acceptcandidate(id, currentcomment, annotation, mode="bard")
{
var c = document.getElementById(id);
var comment = c.innerText;
annotation.annotationComment = currentcomment+"\n\n<b>[Paraphrase]</b>\n"+comment+"\n";

if(mode=="g-translate")
{
annotation.annotationComment = currentcomment+"\n\n<b>[Translation]</b>\n"+comment+"\n";
}

annotation.saveTx({skipSelect:true}).then(e=>{
Zotero.ZeNotes.Ui.reload();
});
},
createdialog(candidates)

createdialog(annotation, currentcomment, candidates, mode="bard")
{
var confirm_message = "Do you want to use this paraprase?";
if(mode=="g-translate")
{
confirm_message = "Do you want to use this translation?"
}
var table = document.createElement("table");
table.id = "zn-bard-table";
table.id = "zn-ai-table";
var th = document.createElement("tr");
table.appendChild(th);
for(h of ["Candidate", "Content", ""])
Expand All @@ -46,7 +59,7 @@ AiUi = {

let candidate = document.createElement("td");

candidate.id = "bard-paraphrase-"+i;
candidate.id = "ai-candidate-"+i;
candidate.addEventListener("blur", function(e){
AiUi.savecandidate(e.target.id);
})
Expand All @@ -59,7 +72,7 @@ AiUi = {

let edit = document.createElement("i");
edit.className = "fas fa-pen zn-button";
edit.dataset.candidateid = "bard-paraphrase-"+i;
edit.dataset.candidateid = "ai-candidate-"+i;
edit.addEventListener("click", function(e){
AiUi.editcandidate(e.target.dataset.candidateid);
});
Expand All @@ -68,14 +81,14 @@ AiUi = {

let accept = document.createElement("i");
accept.className = "fas fa-check zn-button";
accept.dataset.candidateid = "bard-paraphrase-"+i;
accept.dataset.candidateid = "ai-candidate-"+i;
accept.title = "Use this candidate."
accept.addEventListener("click", function(e){
if(!confirm("Do you want to use this paraprase?"))
if(!confirm(confirm_message))
{
return;
}
AiUi.acceptcandidate(e.target.dataset.candidateid, currentcomment, annotation);
AiUi.acceptcandidate(e.target.dataset.candidateid, currentcomment, annotation, mode);
});
buttons.appendChild(accept);
i+=1;
Expand Down
47 changes: 40 additions & 7 deletions core/ai.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
var fetch = Zotero.getMainWindow().fetch;
Ai={
request(url, options){
request(url, options, mode="bard"){
return fetch(url, options).
then(res => {
return res.json()
})
.then(data => {
try {
return Promise.resolve(data.candidates.map(function(v){return v.output}));
if(mode=="bard")
{
try {
return Promise.resolve(data.candidates.map(function(v){return v.output}));
}
catch(e) {
return Promise.resolve([data.error.message]);
}
}
catch(e) {
return Promise.resolve(["Error: "+e]);
else if(mode=="g-translate")
{
try {
// Google translate
return Promise.resolve(data.data.translations.map(function(e){return e.translatedText}));
}
catch(e)
{
return Promise.resolve(["Error: "+e]);
}
}

}).catch(e=>{
return Promise.reject(["Error: "+e]);
});
Expand All @@ -28,7 +43,6 @@ Ai.Bard = {
async summarize(sentence, ratio=1/4)
{
var prompts = "Summarize the following in about "+Math.round(sentence.split(" ").length*ratio)+" words:"
alert(prompts);
return this.sendprompt(sentence, prompts);
},

Expand Down Expand Up @@ -60,6 +74,25 @@ Ai.Bard = {
},
body: JSON.stringify(payload),
}
return Ai.request(url, options);
return Ai.request(url, options, "bard");
},
}

Ai.Google = {
translate(sentence, language){
var apikey = Zotero.ZeNotes.Prefs.getb("google-translate-key");
var url = "https://translation.googleapis.com/language/translate/v2?key="+apikey+"&target="+language

var payload = {q: sentence};

var options = {
method: 'POST',
headers: {
"Content-Type": "application/json; charset=utf-8",
'Accept': 'application/json',
},
body: JSON.stringify(payload),
}
return Ai.request(url, options, "g-translate");
},
}
Loading

0 comments on commit 5a17a56

Please sign in to comment.