forked from Ajatt-Tools/anki.koplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
201 lines (185 loc) · 7.4 KB
/
main.lua
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
local ButtonDialog = require("ui/widget/buttondialog")
local CustomContextMenu = require("customcontextmenu")
local DataStorage = require("datastorage")
local DictQuickLookup = require("ui/widget/dictquicklookup")
local MenuBuilder = require("menubuilder")
local lfs = require("libs/libkoreader-lfs")
local Widget = require("ui/widget/widget")
local UIManager = require("ui/uimanager")
local util = require("util")
local _ = require("gettext")
local AnkiWidget = Widget:extend {
-- this contains all the user configurable options
-- to access them: conf.xxx:get_value()
user_config = require("configwrapper")
}
function AnkiWidget:show_config_widget()
local note_count = #self.anki_connect.local_notes
local with_custom_tags_cb = function()
self.current_note:add_tags(self.user_config.custom_tags:get_value())
self.anki_connect:add_note(self.current_note)
self.config_widget:onClose()
end
self.config_widget = ButtonDialog:new {
buttons = {
{{
text = "Add with custom context",
id = "custom_context",
enabled = self.current_note.contextual_lookup,
callback = function()self:show_custom_context_widget() end
}},
{{ text = ("Sync (%d) offline note(s)"):format(note_count), id = "sync", enabled = note_count > 0, callback = function() self.anki_connect:sync_offline_notes() end }},
{{ text = "Add with custom tags", id = "custom_tags", callback = with_custom_tags_cb }},
{{
text = "Delete latest note",
id = "note_delete",
enabled = self.anki_connect.latest_synced_note ~= nil,
callback = function()
self.anki_connect:delete_latest_note()
self.config_widget:onClose()
end
}},
},
}
UIManager:show(self.config_widget)
end
function AnkiWidget:show_custom_context_widget()
local function on_save_cb()
local m = self.context_menu
self.anki_connect:add_note(self.current_note)
self.context_menu:onClose()
if self.user_config.close_dictionary:get_value() then
self.ui.dictionary.dict_window:onClose()
if self.config_widget then
self.config_widget:onClose()
end
end
end
self.context_menu = CustomContextMenu:new{
note = self.current_note, -- to extract context out of
on_save_cb = on_save_cb, -- called when saving note with updated context
}
UIManager:show(self.context_menu)
end
-- [[
-- This function name is not chosen at random. There are 2 places where this function is called:
-- - frontend/apps/filemanager/filemanagermenu.lua
-- - frontend/apps/reader/modules/readermenu.lua
-- These call the function `pcall(widget.addToMainMenu, widget, self.menu_items)` which lets other widgets add
-- items to the dictionary menu
-- ]]
function AnkiWidget:addToMainMenu(menu_items)
local builder = MenuBuilder:new{
user_config = self.user_config,
extensions = self.extensions,
ui = self.ui
}
menu_items.anki_settings = { text = "Anki Settings", sub_item_table = builder:build() }
end
function AnkiWidget:load_extensions()
self.extensions = {} -- contains filenames by numeric index, loaded modules by value
local ext_directory = DataStorage:getFullDataDir() .. "/plugins/anki.koplugin/extensions/"
for file in lfs.dir(ext_directory) do
if file:match("EXT_.*%.lua") then
table.insert(self.extensions, file)
local ext_module = assert(loadfile(ext_directory .. file))()
self.extensions[file] = ext_module
end
end
table.sort(self.extensions)
end
-- This function is called automatically for all tables extending from Widget
function AnkiWidget:init()
-- this contains all the logic for creating anki cards
self.anki_connect = require("ankiconnect"):new {
conf = self.user_config,
btn = self.add_to_anki_btn,
ui = self.ui, -- AnkiConnect helper class has no access to the UI by default, so add it here
}
self:load_extensions()
self.anki_note = require("ankinote"):extend{
ext_modules = self.extensions,
conf = self.user_config,
ui = self.ui
}
-- this holds the latest note created by the user!
self.current_note = nil
self.ui.menu:registerToMainMenu(self)
self:handle_events()
-- Insert new button in the popup dictionary to allow adding anki cards
-- TODO disable button if lookup was not contextual
DictQuickLookup.tweak_buttons_func = function(popup_dict, buttons)
local btn_text = "Add to Anki"
if self.user_config.always_open_custom_context:get_value() then
btn_text = "Add to Anki (select context)"
end
self.add_to_anki_btn = {
id = "add_to_anki",
text = _(btn_text),
font_bold = true,
callback = function()
self.current_note = self.anki_note:new(popup_dict)
if self.user_config.always_open_custom_context:get_value() then
self:show_custom_context_widget()
else
self.anki_connect:add_note(self.current_note)
end
end,
hold_callback = function()
self.current_note = self.anki_note:new(popup_dict)
self:show_config_widget()
end,
}
table.insert(buttons, 1, { self.add_to_anki_btn })
end
end
function AnkiWidget:extend_doc_settings()
local doc = self.ui.document
local _, file = util.splitFilePathName(doc.file)
local file_pattern = "^%[([^%]]-)%]_(.-)_%[([^%]]-)%]%.[^%.]+"
local f_author, f_title, f_extra = file:match(file_pattern)
local file_properties = {
title = f_title,
author = f_author,
description = f_extra,
}
local document_properties = doc:getProps()
local get_prop = function(property)
local d_p, f_p = document_properties[property], file_properties[property]
local d_len, f_len = d_p and #d_p or 0, f_p and #f_p or 0
-- if our custom f_p match is more exact, pick that one
-- e.g. for PDF the title is usually the full filename
local f_p_more_precise = d_len == 0 or d_len > f_len and f_len ~= 0
return f_p_more_precise and f_p or d_p
end
local metadata = {
title = get_prop('title'),
author = get_prop('author') or get_prop('authors'),
description = get_prop('description'),
current_page = function() return self.ui.view.state.page end,
pages = doc.info.number_of_pages or "N/A"
}
local metadata_mt = {
__index = function(t, k) return rawget(t, k) or "N/A" end
}
self.ui.document._anki_metadata = setmetatable(metadata, metadata_mt)
end
function AnkiWidget:handle_events()
-- these all return false so that the event goes up the chain, other widgets might wanna react to these events
self.onCloseWidget = function()
self.user_config:save()
end
self.onSuspend = function()
self.user_config:save()
end
self.onNetworkConnected = function()
self.anki_connect.wifi_connected = true
end
self.onNetworkDisconnected = function()
self.anki_connect.wifi_connected = false
end
self.onReaderReady = function()
self:extend_doc_settings()
end
end
return AnkiWidget