-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
186 lines (185 loc) · 5.48 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* MIT License
* Copyright © 2022 MarginNote
* Github: https://github.com/marginnoteapp/jump
* Welcom to contribute to this project!
*/
try {
const zh = {
confirm: "确定",
cancel: "取消",
no_metaData: "检测到您并未安装 Metadata 插件,无法设置页码偏移量",
offset: offset =>
offset
? `当前文档的页码偏移量为 ${offset}`
: "当前文档的页码偏移量为 0,可以前去 Metadata 插件中设置页码偏移量",
download: "下载 Metadata",
deleted: "该页已删除",
out_range: "超出本书页码范围",
gotopage: `转到指定页码`,
enter_integer: "请输入整数"
}
const en = {
confirm: "Confirm",
cancel: "Cancel",
deleted: "This page has been deleted",
download: "Download Metadata",
out_range: "Out of page range of this book",
no_metaData:
"You have not installed Metadata addon, so you can't set the page offset",
offset: offset =>
offset
? `This document's page offset is ${offset}`
: "This document's page offset is 0, you can go to Metadata addon to set the page offset",
gotopage: "Go To Page",
enter_integer: "Please enter an integer"
}
JSB.newAddon = () => {
const Addon = {
name: "Jump",
key: "jump"
}
const lang =
NSLocale.preferredLanguages().length &&
NSLocale.preferredLanguages()[0].startsWith("zh")
? zh
: en
const console = {
log(obj, suffix = "normal") {
JSB.log(`${Addon.key}-${suffix} %@`, obj)
}
}
function getPageOffset(md5) {
const data = NSUserDefaults.standardUserDefaults().objectForKey(
"metadata_profile_doc"
)
if (data === undefined) return undefined
return data[md5]?.addon?.pageOffset ?? 0
}
function popup(title, message, type, buttons = [lang.confirm]) {
return new Promise(resolve =>
UIAlertView.showWithTitleMessageStyleCancelButtonTitleOtherButtonTitlesTapBlock(
title,
message,
type,
lang.cancel,
buttons,
(alert, buttonIndex) => {
resolve(
type === 2
? {
content: alert.textFieldAtIndex(0).text.trim(),
option: buttonIndex - 1
}
: {
option: buttonIndex - 1
}
)
}
)
)
}
function showHUD(text, duration = 2) {
Application.sharedInstance().showHUD(text, self.window, duration)
}
function openUrl(url) {
Application.sharedInstance().openURL(NSURL.URLWithString(encodeURI(url)))
}
async function jump() {
let offset = getPageOffset(self.docmd5)
if (offset === undefined) {
const { option } = await popup("Jump", lang.no_metaData, 0, [
lang.download
])
if (option === 0) {
openUrl("https://github.com/marginnoteapp/metadata")
}
return
}
offset = Number(offset)
for (;;) {
const { option, content } = await popup("Jump", lang.offset(offset), 2)
if (option === -1) return
const page = Number(content)
if (Number.isInteger(page)) {
const realPage = page + offset
const index = self.documentController.indexFromPageNo(realPage)
if (
realPage > self.documentController.document.pageCount ||
realPage < 1
) {
showHUD(lang.out_range)
} else if (index === 0 && realPage !== 1) {
showHUD(lang.deleted)
} else {
self.documentController.setPageAtIndex(index)
return
}
} else {
showHUD(lang.enter_integer)
}
}
}
return JSB.defineClass(
Addon.name + ": JSExtension",
{
sceneWillConnect() {
self.status = false
self.app = Application.sharedInstance()
self.studyController = self.app.studyController(self.window)
},
documentDidOpen(docmd5) {
self.docmd5 = docmd5
self.documentController =
self.studyController.readerController.currentDocumentController
},
queryAddonCommandStatus() {
return self.studyController.studyMode !== 3
? {
image: "logo.png",
object: self,
selector: "onToggle:",
checked: self.status
}
: null
},
notebookWillOpen() {
NSNotificationCenter.defaultCenter().addObserverSelectorName(
self,
"onAddonBroadcast:",
"AddonBroadcast"
)
},
notebookWillClose() {
NSNotificationCenter.defaultCenter().removeObserverName(
self,
"AddonBroadcast"
)
},
async onAddonBroadcast(sender) {
if (
!Application.sharedInstance().checkNotifySenderInWindow(
sender,
self.window
)
)
return
const v = sender.userInfo.message
if (v === "jump") {
await jump()
}
},
async onToggle() {
self.status = true
self.studyController.refreshAddonCommands()
await jump()
self.status = false
self.studyController.refreshAddonCommands()
}
},
{}
)
}
} catch (err) {
JSB.log(`jump-error %@`, String(err))
}