-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
94 lines (93 loc) · 3.92 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
JSB.newAddon = function(mainPath){
var newAddonClass = JSB.defineClass('AutoTitle : 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,'onProcessExcerptText:','ProcessNewExcerpt');
NSNotificationCenter.defaultCenter().addObserverSelectorName(self,'onProcessExcerptText:','ChangeExcerptRange');
self.autotitle = NSUserDefaults.standardUserDefaults().objectForKey('marginnote_autotitle');
},
notebookWillClose: function(notebookid) {
NSNotificationCenter.defaultCenter().removeObserverName(self,'ProcessNewExcerpt');
NSNotificationCenter.defaultCenter().removeObserverName(self,'ChangeExcerptRange');
},
documentDidOpen: function(docmd5) {
},
documentWillClose: function(docmd5) {
},
controllerWillLayoutSubviews: function(controller) {
},
queryAddonCommandStatus: function() {
if(Application.sharedInstance().studyController(self.window).studyMode < 3)
return {image:'title.png',object:self,selector:'toggleAutoTitle:',checked:(self.autotitle?true:false)};
return null;
},
//Clicking note
onProcessExcerptText: function(sender){
if(!Application.sharedInstance().checkNotifySenderInWindow(sender,self.window))return;//Don't process message from other window
if(!self.autotitle)return;
var noteid = sender.userInfo.noteid;
var note = Database.sharedInstance().getNoteById(noteid);
if(note && note.excerptText && note.excerptText.length > 0 && note.excerptText.length <= 250 && !note.groupNoteId){
var timerCount = 0;
NSTimer.scheduledTimerWithTimeInterval(1,true,function(timer){
var text = note.excerptText.split("**").join("");
if(text && text.length){
UndoManager.sharedInstance().undoGrouping('AutoTitle',note.notebookId,function(){
note.noteTitle = text;
note.excerptText = '';
Database.sharedInstance().setNotebookSyncDirty(note.notebookId);
});
NSNotificationCenter.defaultCenter().postNotificationNameObjectUserInfo('RefreshAfterDBChange',self,{topicid:note.notebookId});
}
timerCount++;
if(timerCount >= 4){
timer.invalidate();
}
});
}
},
toggleAutoTitle: function(sender) {
var lan = NSLocale.preferredLanguages().length?NSLocale.preferredLanguages()[0].substring(0,2):'en';
if(self.autotitle){
self.autotitle = false;
if(lan == 'zh')
Application.sharedInstance().showHUD('自动设置标题已关闭',self.window,2);
else
Application.sharedInstance().showHUD('Auto title is turned off',self.window,2);
}
else{
self.autotitle = true;
if(lan == 'zh')
Application.sharedInstance().showHUD('创建摘录后,摘录内容将自动被设置为笔记标题',self.window,2);
else
Application.sharedInstance().showHUD('After creating an excerpt, the excerpt will be automatically set as the note title',self.window,2);
}
NSUserDefaults.standardUserDefaults().setObjectForKey(self.autotitle,'marginnote_autotitle');
Application.sharedInstance().studyController(self.window).refreshAddonCommands();
},
}, /*Class members*/{
addonDidConnect: function() {
},
addonWillDisconnect: function() {
},
applicationWillEnterForeground: function() {
},
applicationDidEnterBackground: function() {
},
applicationDidReceiveLocalNotification: function(notify) {
},
});
return newAddonClass;
};