-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
103 lines (102 loc) · 4.56 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
JSB.newAddon = function(mainPath){
JSB.require('WebViewController');
var newAddonClass = JSB.defineClass('DeepLAddon : JSExtension', /*Instance members*/{
//Window initialize
sceneWillConnect: function() {
self.webController = WebViewController.new();
},
//Window disconnect
sceneDidDisconnect: function() {
},
//Window resign active
sceneWillResignActive: function() {
},
//Window become active
sceneDidBecomeActive: function() {
},
notebookWillOpen: function(notebookid) {
NSNotificationCenter.defaultCenter().addObserverSelectorName(self,'onPopupMenuOnNote:','PopupMenuOnNote');
NSNotificationCenter.defaultCenter().addObserverSelectorName(self,'onPopupMenuOnSelection:','PopupMenuOnSelection');
NSTimer.scheduledTimerWithTimeInterval(0.2,false,function(){
var deepl_on = NSUserDefaults.standardUserDefaults().objectForKey('marginnote_deel_on');
if(Application.sharedInstance().studyController(self.window).studyMode < 3 && deepl_on == true){// Not support in card deck mode
Application.sharedInstance().studyController(self.window).view.addSubview(self.webController.view);
self.layoutWebController();
Application.sharedInstance().studyController(self.window).refreshAddonCommands();
NSTimer.scheduledTimerWithTimeInterval(0.2,false,function(){
Application.sharedInstance().studyController(self.window).becomeFirstResponder(); //For dismiss keyboard on iOS
});
}
});
},
notebookWillClose: function(notebookid) {
self.webController.view.removeFromSuperview();
NSNotificationCenter.defaultCenter().removeObserverName(self,'PopupMenuOnNote');
NSNotificationCenter.defaultCenter().removeObserverName(self,'PopupMenuOnSelection');
},
documentDidOpen: function(docmd5) {
},
documentWillClose: function(docmd5) {
},
controllerWillLayoutSubviews: function(controller) {
if(controller == Application.sharedInstance().studyController(self.window)){
self.layoutWebController();
}
},
queryAddonCommandStatus: function() {
if(Application.sharedInstance().studyController(self.window).studyMode < 3)
return {image:'deepl.png',object:self,selector:'toggleTranslate:',checked:(self.webController.view.window?true:false)};
return null;
},
//Clicking note
onPopupMenuOnNote: function(sender){
if(!Application.sharedInstance().checkNotifySenderInWindow(sender,self.window))return;//Don't process message from other window
if(!self.webController.view.window)return;
var text = sender.userInfo.note.allNoteText();
if(text && text.length){
self.webController.translateText(text);
}
},
//Selecting text on pdf or epub
onPopupMenuOnSelection: function(sender){
if(!Application.sharedInstance().checkNotifySenderInWindow(sender,self.window))return;//Don't process message from other window
if(!self.webController.view.window)return;
var text = sender.userInfo.documentController.selectionText;
if(text && text.length){
self.webController.translateText(text);
}
},
toggleTranslate: function(sender) {
if(self.webController.view.window){
self.webController.view.removeFromSuperview();
NSUserDefaults.standardUserDefaults().setObjectForKey(false,'marginnote_deel_on');
}
else{
Application.sharedInstance().studyController(self.window).view.addSubview(self.webController.view);
self.layoutWebController();
NSUserDefaults.standardUserDefaults().setObjectForKey(true,'marginnote_deel_on');
NSTimer.scheduledTimerWithTimeInterval(0.2,false,function(){
Application.sharedInstance().studyController(self.window).becomeFirstResponder(); //For dismiss keyboard on iOS
});
}
Application.sharedInstance().studyController(self.window).refreshAddonCommands();
},
}, /*Class members*/{
addonDidConnect: function() {
},
addonWillDisconnect: function() {
},
applicationWillEnterForeground: function() {
},
applicationDidEnterBackground: function() {
},
applicationDidReceiveLocalNotification: function(notify) {
},
});
newAddonClass.prototype.layoutWebController = function(){
var frame = Application.sharedInstance().studyController(this.window).view.bounds;
var width = frame.width > 300?(300 + (frame.width - 300)/2):300;
this.webController.view.frame = {x:(frame.width-width)/2,y:frame.height - 300,width:width,height:280};
};
return newAddonClass;
};