2019-12-06 21:55:39 +00:00
|
|
|
local BD = require("ui/bidi")
|
2017-07-28 20:39:54 +00:00
|
|
|
local ButtonDialog = require("ui/widget/buttondialog")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2013-10-22 15:11:31 +00:00
|
|
|
local Event = require("ui/event")
|
2018-03-05 15:38:04 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2017-07-28 20:39:54 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2019-10-25 15:25:26 +00:00
|
|
|
local MultiConfirmBox = require("ui/widget/multiconfirmbox")
|
|
|
|
local Notification = require("ui/widget/notification")
|
2017-09-10 18:35:27 +00:00
|
|
|
local TimeVal = require("ui/timeval")
|
2018-12-17 13:15:13 +00:00
|
|
|
local Translator = require("ui/translator")
|
2013-10-22 15:11:31 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2020-06-08 18:47:31 +00:00
|
|
|
local util = require("util")
|
2020-11-08 12:07:51 +00:00
|
|
|
local ffiUtil = require("ffi/util")
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
2019-08-24 11:45:07 +00:00
|
|
|
local C_ = _.pgettext
|
2018-10-08 16:58:43 +00:00
|
|
|
local T = require("ffi/util").template
|
2019-02-03 09:01:58 +00:00
|
|
|
local Screen = Device.screen
|
2013-04-23 22:59:52 +00:00
|
|
|
|
2020-11-08 12:07:51 +00:00
|
|
|
local ReaderHighlight = InputContainer:new{
|
|
|
|
}
|
|
|
|
|
|
|
|
local function inside_box(pos, box)
|
|
|
|
if pos then
|
|
|
|
local x, y = pos.x, pos.y
|
|
|
|
if box.x <= x and box.y <= y
|
|
|
|
and box.x + box.w >= x
|
|
|
|
and box.y + box.h >= y then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function cleanupSelectedText(text)
|
|
|
|
-- Trim spaces and new lines at start and end
|
|
|
|
text = text:gsub("^[\n%s]*", "")
|
|
|
|
text = text:gsub("[\n%s]*$", "")
|
|
|
|
-- Trim spaces around newlines
|
|
|
|
text = text:gsub("%s*\n%s*", "\n")
|
|
|
|
-- Trim consecutive spaces (that would probably have collapsed
|
|
|
|
-- in rendered CreDocuments)
|
|
|
|
text = text:gsub("%s%s+", " ")
|
|
|
|
return text
|
|
|
|
end
|
2013-04-23 22:59:52 +00:00
|
|
|
|
|
|
|
function ReaderHighlight:init()
|
2020-11-08 12:07:51 +00:00
|
|
|
self._highlight_buttons = {
|
|
|
|
-- highlight and add_note are for the document itself,
|
|
|
|
-- so we put them first.
|
|
|
|
["01_highlight"] = function(_self)
|
|
|
|
return {
|
|
|
|
text = _("Highlight"),
|
|
|
|
callback = function()
|
|
|
|
_self:saveHighlight()
|
|
|
|
_self:onClose()
|
|
|
|
end,
|
|
|
|
enabled = _self.hold_pos ~= nil,
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
["02_add_note"] = function(_self)
|
|
|
|
return {
|
|
|
|
text = _("Add Note"),
|
|
|
|
callback = function()
|
|
|
|
_self:addNote()
|
|
|
|
_self:onClose()
|
|
|
|
end,
|
|
|
|
enabled = _self.hold_pos ~= nil,
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
-- copy and search are internal functions that don't depend on anything,
|
|
|
|
-- hence the second line.
|
|
|
|
["03_copy"] = function(_self)
|
|
|
|
return {
|
|
|
|
text = C_("Text", "Copy"),
|
|
|
|
enabled = Device:hasClipboard(),
|
|
|
|
callback = function()
|
|
|
|
Device.input.setClipboardText(cleanupSelectedText(_self.selected_text.text))
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
["04_search"] = function(_self)
|
|
|
|
return {
|
|
|
|
text = _("Search"),
|
|
|
|
callback = function()
|
|
|
|
_self:onHighlightSearch()
|
|
|
|
_self:onClose()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
-- then information lookup functions, putting on the left those that
|
|
|
|
-- depend on an internet connection.
|
|
|
|
["05_wikipedia"] = function(_self)
|
|
|
|
return {
|
|
|
|
text = _("Wikipedia"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:scheduleIn(0.1, function()
|
|
|
|
_self:lookupWikipedia()
|
|
|
|
-- We don't call _self:onClose(), we need the highlight
|
|
|
|
-- to still be there, as we may Highlight it from the
|
|
|
|
-- dict lookup widget
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
["06_dictionary"] = function(_self)
|
|
|
|
return {
|
|
|
|
text = _("Dictionary"),
|
|
|
|
callback = function()
|
|
|
|
_self:onHighlightDictLookup()
|
|
|
|
-- We don't call _self:onClose(), same reason as above
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
["07_translate"] = function(_self)
|
|
|
|
return {
|
|
|
|
text = _("Translate"),
|
|
|
|
callback = function()
|
|
|
|
_self:translate(_self.selected_text)
|
|
|
|
-- We don't call _self:onClose(), so one can still see
|
|
|
|
-- the highlighted text when moving the translated
|
|
|
|
-- text window, and also if NetworkMgr:promptWifiOn()
|
|
|
|
-- is needed, so the user can just tap again on this
|
|
|
|
-- button and does not need to select the text again.
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Text export functions if applicable.
|
|
|
|
if not self.ui.document.info.has_pages then
|
|
|
|
self:addToHighlightDialog("08_view_html", function(_self)
|
|
|
|
return {
|
|
|
|
text = _("View HTML"),
|
|
|
|
callback = function()
|
|
|
|
_self:viewSelectionHTML()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
if Device:canShareText() then
|
|
|
|
self:addToHighlightDialog("09_share_text", function(_self)
|
|
|
|
return {
|
|
|
|
text = _("Share Text"),
|
|
|
|
callback = function()
|
|
|
|
local text = cleanupSelectedText(_self.selected_text.text)
|
|
|
|
-- call self:onClose() before calling the android framework
|
|
|
|
_self:onClose()
|
|
|
|
Device.doShareText(text)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Links
|
|
|
|
if self.selected_link ~= nil then
|
|
|
|
self:addToHighlightDialog("10_follow_link", function(_self)
|
|
|
|
return {
|
|
|
|
text = _("Follow Link"),
|
|
|
|
callback = function()
|
|
|
|
local link = _self.selected_link.link or _self.selected_link
|
|
|
|
_self.ui.link:onGotoLink(link)
|
|
|
|
_self:onClose()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2014-10-09 09:41:23 +00:00
|
|
|
self.ui:registerPostInitCallback(function()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end)
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2017-01-23 14:54:14 +00:00
|
|
|
function ReaderHighlight:setupTouchZones()
|
|
|
|
-- deligate gesture listener to readerui
|
|
|
|
self.ges_events = {}
|
|
|
|
self.onGesture = nil
|
|
|
|
|
|
|
|
if not Device:isTouchDevice() then return end
|
2020-08-05 22:13:26 +00:00
|
|
|
local hold_pan_rate = G_reader_settings:readSetting("hold_pan_rate")
|
|
|
|
if not hold_pan_rate then
|
|
|
|
hold_pan_rate = Screen.low_pan_rate and 5.0 or 30.0
|
|
|
|
end
|
2017-01-23 14:54:14 +00:00
|
|
|
self.ui:registerTouchZones({
|
|
|
|
{
|
|
|
|
id = "readerhighlight_tap",
|
|
|
|
ges = "tap",
|
|
|
|
screen_zone = {
|
|
|
|
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
|
|
|
|
},
|
2019-03-30 20:05:44 +00:00
|
|
|
overrides = {
|
2020-01-24 19:05:21 +00:00
|
|
|
-- Tap on existing highlights have priority over
|
|
|
|
-- everything but tap on links (as links can be
|
|
|
|
-- part of some highlighted text)
|
2019-03-30 20:05:44 +00:00
|
|
|
"tap_forward",
|
|
|
|
"tap_backward",
|
|
|
|
"readermenu_tap",
|
|
|
|
"readerconfigmenu_tap",
|
2020-01-24 19:05:21 +00:00
|
|
|
"readerfooter_tap",
|
|
|
|
"tap_top_left_corner",
|
|
|
|
"tap_top_right_corner",
|
|
|
|
"tap_left_bottom_corner",
|
|
|
|
"tap_right_bottom_corner",
|
2019-03-30 20:05:44 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
handler = function(ges) return self:onTap(nil, ges) end
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
{
|
|
|
|
id = "readerhighlight_hold",
|
|
|
|
ges = "hold",
|
|
|
|
screen_zone = {
|
|
|
|
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
|
|
|
|
},
|
2020-01-24 19:05:21 +00:00
|
|
|
overrides = {
|
|
|
|
"readerfooter_hold",
|
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
handler = function(ges) return self:onHold(nil, ges) end
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
{
|
|
|
|
id = "readerhighlight_hold_release",
|
|
|
|
ges = "hold_release",
|
|
|
|
screen_zone = {
|
|
|
|
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
|
|
|
|
},
|
|
|
|
handler = function() return self:onHoldRelease() end
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
{
|
|
|
|
id = "readerhighlight_hold_pan",
|
|
|
|
ges = "hold_pan",
|
2020-08-05 22:13:26 +00:00
|
|
|
rate = hold_pan_rate,
|
2017-01-23 14:54:14 +00:00
|
|
|
screen_zone = {
|
|
|
|
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
|
|
|
|
},
|
|
|
|
handler = function(ges) return self:onHoldPan(nil, ges) end
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2017-01-23 14:54:14 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onReaderReady()
|
|
|
|
self:setupTouchZones()
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2017-03-04 13:46:38 +00:00
|
|
|
function ReaderHighlight:addToMainMenu(menu_items)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- insert table to main reader menu
|
2017-03-04 13:46:38 +00:00
|
|
|
menu_items.highlight_options = {
|
2017-09-13 10:44:37 +00:00
|
|
|
text = _("Highlighting"),
|
2014-03-13 13:52:43 +00:00
|
|
|
sub_item_table = self:genHighlightDrawerMenu(),
|
2017-02-28 21:46:32 +00:00
|
|
|
}
|
2020-09-24 13:17:37 +00:00
|
|
|
if self.document.info.has_pages then
|
|
|
|
menu_items.panel_zoom_options = {
|
|
|
|
text = _("Panel zoom (manga/comic)"),
|
|
|
|
sub_item_table = self:genPanelZoomMenu(),
|
|
|
|
}
|
|
|
|
end
|
2018-12-17 13:15:13 +00:00
|
|
|
menu_items.translation_settings = Translator:genSettingsMenu()
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2014-11-06 03:07:48 +00:00
|
|
|
local highlight_style = {
|
|
|
|
lighten = _("Lighten"),
|
2017-04-09 20:16:56 +00:00
|
|
|
underscore = _("Underline"),
|
2014-11-06 03:07:48 +00:00
|
|
|
invert = _("Invert"),
|
|
|
|
}
|
|
|
|
|
2020-09-24 13:17:37 +00:00
|
|
|
local function getPanelZoomSupportedExt()
|
|
|
|
local default_supported_ext = {
|
|
|
|
cbz = true,
|
|
|
|
cbt = true,
|
|
|
|
}
|
|
|
|
return G_reader_settings:readSetting("panel_zoom_ext") or default_supported_ext
|
|
|
|
end
|
|
|
|
|
|
|
|
local function isPanelZoomSupported(file)
|
|
|
|
local filetype = util.getFileNameSuffix(file)
|
|
|
|
local supported_filetypes = getPanelZoomSupportedExt()
|
|
|
|
return supported_filetypes[filetype]
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:genPanelZoomMenu()
|
|
|
|
return {
|
|
|
|
{
|
|
|
|
text = _("Allow panel zoom"),
|
|
|
|
checked_func = function()
|
|
|
|
return self.panel_zoom_enabled
|
|
|
|
end,
|
|
|
|
callback = function()
|
|
|
|
self:onTogglePanelZoomSetting()
|
|
|
|
end,
|
|
|
|
hold_callback = function()
|
|
|
|
local ext = util.getFileNameSuffix(self.ui.document.file)
|
|
|
|
local supported_ext = getPanelZoomSupportedExt()
|
|
|
|
supported_ext[ext] = not supported_ext[ext]
|
|
|
|
G_reader_settings:saveSetting("panel_zoom_ext", supported_ext)
|
|
|
|
end,
|
|
|
|
separator = true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-01-17 19:05:17 +00:00
|
|
|
function ReaderHighlight:genHighlightDrawerMenu()
|
2014-11-06 03:07:48 +00:00
|
|
|
local get_highlight_style = function(style)
|
|
|
|
return {
|
|
|
|
text = highlight_style[style],
|
2014-06-08 06:02:22 +00:00
|
|
|
checked_func = function()
|
2014-11-06 03:07:48 +00:00
|
|
|
return self.view.highlight.saved_drawer == style
|
2014-05-28 11:45:01 +00:00
|
|
|
end,
|
2014-11-06 03:07:48 +00:00
|
|
|
enabled_func = function()
|
|
|
|
return not self.view.highlight.disabled
|
2014-05-28 11:45:01 +00:00
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
callback = function()
|
2014-11-06 03:07:48 +00:00
|
|
|
self.view.highlight.saved_drawer = style
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-11-06 03:07:48 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
return {
|
2014-03-13 13:52:43 +00:00
|
|
|
{
|
2019-02-03 10:17:27 +00:00
|
|
|
text = _("Allow highlighting"),
|
|
|
|
checked_func = function()
|
|
|
|
return not self.view.highlight.disabled
|
2014-05-28 11:45:01 +00:00
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
callback = function()
|
2014-11-06 03:07:48 +00:00
|
|
|
self.view.highlight.disabled = not self.view.highlight.disabled
|
2016-10-23 14:36:50 +00:00
|
|
|
end,
|
2018-09-04 21:55:58 +00:00
|
|
|
hold_callback = function(touchmenu_instance)
|
2019-10-25 15:25:26 +00:00
|
|
|
self:toggleDefault()
|
2018-09-04 21:55:58 +00:00
|
|
|
end,
|
2019-02-03 10:17:27 +00:00
|
|
|
separator = true,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2014-11-06 03:07:48 +00:00
|
|
|
get_highlight_style("lighten"),
|
|
|
|
get_highlight_style("underscore"),
|
|
|
|
get_highlight_style("invert"),
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2017-09-20 15:35:30 +00:00
|
|
|
-- Returns a unique id, that can be provided on delayed call to :clear(id)
|
|
|
|
-- to ensure current highlight has not already been cleared, and that we
|
|
|
|
-- are not going to clear a new highlight
|
|
|
|
function ReaderHighlight:getClearId()
|
|
|
|
self.clear_id = TimeVal.now() -- can act as a unique id
|
|
|
|
return self.clear_id
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:clear(clear_id)
|
|
|
|
if clear_id then -- should be provided by delayed call to clear()
|
|
|
|
if clear_id ~= self.clear_id then
|
|
|
|
-- if clear_id is no more valid, highlight has already been
|
|
|
|
-- cleared since this clear_id was given
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.clear_id = nil -- invalidate id
|
2014-11-05 04:28:11 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
self.view.highlight.temp = {}
|
|
|
|
else
|
|
|
|
self.ui.document:clearSelection()
|
|
|
|
end
|
2019-02-03 09:01:58 +00:00
|
|
|
if self.restore_page_mode_func then
|
|
|
|
self.restore_page_mode_func()
|
|
|
|
self.restore_page_mode_func = nil
|
|
|
|
end
|
|
|
|
self.selected_text_start_xpointer = nil
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.hold_pos then
|
|
|
|
self.hold_pos = nil
|
2014-07-02 09:46:17 +00:00
|
|
|
self.selected_text = nil
|
2017-06-18 16:08:57 +00:00
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
2014-08-26 12:29:53 +00:00
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
function ReaderHighlight:onClearHighlight()
|
|
|
|
self:clear()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-03-28 00:18:25 +00:00
|
|
|
function ReaderHighlight:onTap(_, ges)
|
2020-11-08 01:18:50 +00:00
|
|
|
-- We only actually need to clear if we have something to clear in the first place.
|
|
|
|
-- (We mainly want to avoid CRe's clearSelection,
|
|
|
|
-- which may incur a redraw as it invalidates the cache, c.f., #6854)
|
|
|
|
-- ReaderHighlight:clear can only return true if self.hold_pos was set anyway.
|
|
|
|
local cleared = self.hold_pos and self:clear()
|
|
|
|
-- We only care about potential taps on existing highlights, not on taps that closed a highlight menu.
|
|
|
|
if not cleared and ges then
|
2014-08-26 12:29:53 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
return self:onTapPageSavedHighlight(ges)
|
|
|
|
else
|
|
|
|
return self:onTapXPointerSavedHighlight(ges)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onTapPageSavedHighlight(ges)
|
2014-03-13 13:52:43 +00:00
|
|
|
local pages = self.view:getCurrentPageList()
|
|
|
|
local pos = self.view:screenToPageTransform(ges.pos)
|
|
|
|
for key, page in pairs(pages) do
|
|
|
|
local items = self.view.highlight.saved[page]
|
2017-01-23 14:54:14 +00:00
|
|
|
if items then
|
|
|
|
for i = 1, #items do
|
|
|
|
local pos0, pos1 = items[i].pos0, items[i].pos1
|
|
|
|
local boxes = self.ui.document:getPageBoxesFromPositions(page, pos0, pos1)
|
|
|
|
if boxes then
|
|
|
|
for index, box in pairs(boxes) do
|
|
|
|
if inside_box(pos, box) then
|
2017-11-20 20:58:58 +00:00
|
|
|
logger.dbg("Tap on highlight")
|
2017-01-23 14:54:14 +00:00
|
|
|
return self:onShowHighlightDialog(page, i)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onTapXPointerSavedHighlight(ges)
|
2018-10-08 16:57:59 +00:00
|
|
|
-- Getting screen boxes is done for each tap on screen (changing pages,
|
|
|
|
-- showing menu...). We might want to cache these boxes per page (and
|
|
|
|
-- clear that cache when page layout change or highlights are added
|
|
|
|
-- or removed).
|
2019-03-13 12:05:50 +00:00
|
|
|
local cur_view_top, cur_view_bottom
|
2014-03-13 13:52:43 +00:00
|
|
|
local pos = self.view:screenToPageTransform(ges.pos)
|
2020-11-08 01:18:50 +00:00
|
|
|
-- NOTE: By now, pos.page is set, but if a highlight spans across multiple pages,
|
|
|
|
-- it's stored under the hash of its *starting* point,
|
|
|
|
-- so we can't just check the current page, hence the giant hashwalk of death...
|
|
|
|
-- We can't even limit the walk to page <= pos.page,
|
|
|
|
-- because pos.page isn't super accurate in continuous mode
|
|
|
|
-- (it's the page number for what's it the topleft corner of the screen,
|
|
|
|
-- i.e., often a bit earlier)...
|
|
|
|
for page, items in pairs(self.view.highlight.saved) do
|
2017-01-23 14:54:14 +00:00
|
|
|
if items then
|
|
|
|
for i = 1, #items do
|
2020-11-08 01:18:50 +00:00
|
|
|
local item = items[i]
|
|
|
|
local pos0, pos1 = item.pos0, item.pos1
|
2017-11-20 20:58:58 +00:00
|
|
|
-- document:getScreenBoxesFromPositions() is expensive, so we
|
|
|
|
-- first check this item is on current page
|
2019-03-13 12:05:50 +00:00
|
|
|
if not cur_view_top then
|
|
|
|
-- Even in page mode, it's safer to use pos and ui.dimen.h
|
|
|
|
-- than pages' xpointers pos, even if ui.dimen.h is a bit
|
|
|
|
-- larger than pages' heights
|
|
|
|
cur_view_top = self.ui.document:getCurrentPos()
|
|
|
|
if self.view.view_mode == "page" and self.ui.document:getVisiblePageCount() > 1 then
|
|
|
|
cur_view_bottom = cur_view_top + 2 * self.ui.dimen.h
|
|
|
|
else
|
|
|
|
cur_view_bottom = cur_view_top + self.ui.dimen.h
|
2019-02-26 06:16:43 +00:00
|
|
|
end
|
|
|
|
end
|
2019-03-13 12:05:50 +00:00
|
|
|
local spos0 = self.ui.document:getPosFromXPointer(pos0)
|
|
|
|
local spos1 = self.ui.document:getPosFromXPointer(pos1)
|
|
|
|
local start_pos = math.min(spos0, spos1)
|
|
|
|
local end_pos = math.max(spos0, spos1)
|
|
|
|
if start_pos <= cur_view_bottom and end_pos >= cur_view_top then
|
2018-10-10 16:50:24 +00:00
|
|
|
local boxes = self.ui.document:getScreenBoxesFromPositions(pos0, pos1, true) -- get_segments=true
|
2017-11-20 20:58:58 +00:00
|
|
|
if boxes then
|
|
|
|
for index, box in pairs(boxes) do
|
|
|
|
if inside_box(pos, box) then
|
|
|
|
logger.dbg("Tap on highlight")
|
|
|
|
return self:onShowHighlightDialog(page, i)
|
|
|
|
end
|
2017-01-23 14:54:14 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2019-02-15 23:42:27 +00:00
|
|
|
function ReaderHighlight:updateHighlight(page, index, side, direction, move_by_char)
|
|
|
|
if self.ui.document.info.has_pages then -- we do this only if it's epub file
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local highlight = self.view.highlight.saved[page][index]
|
|
|
|
local highlight_time = highlight.datetime
|
|
|
|
local highlight_beginning = highlight.pos0
|
|
|
|
local highlight_end = highlight.pos1
|
|
|
|
if side == 0 then -- we move pos0
|
2019-02-17 15:15:32 +00:00
|
|
|
local updated_highlight_beginning
|
2019-02-15 23:42:27 +00:00
|
|
|
if direction == 1 then -- move highlight to the right
|
|
|
|
if move_by_char then
|
|
|
|
updated_highlight_beginning = self.ui.document:getNextVisibleChar(highlight_beginning)
|
|
|
|
else
|
|
|
|
updated_highlight_beginning = self.ui.document:getNextVisibleWordStart(highlight_beginning)
|
|
|
|
end
|
|
|
|
else -- move highlight to the left
|
|
|
|
if move_by_char then
|
|
|
|
updated_highlight_beginning = self.ui.document:getPrevVisibleChar(highlight_beginning)
|
|
|
|
else
|
|
|
|
updated_highlight_beginning = self.ui.document:getPrevVisibleWordStart(highlight_beginning)
|
|
|
|
end
|
2019-02-17 15:15:32 +00:00
|
|
|
end
|
|
|
|
if updated_highlight_beginning then
|
|
|
|
local order = self.ui.document:compareXPointers(updated_highlight_beginning, highlight_end)
|
|
|
|
if order and order > 0 then -- only if beginning did not go past end
|
2019-02-15 23:42:27 +00:00
|
|
|
self.view.highlight.saved[page][index].pos0 = updated_highlight_beginning
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else -- we move pos1
|
2019-02-17 15:15:32 +00:00
|
|
|
local updated_highlight_end
|
2019-02-15 23:42:27 +00:00
|
|
|
if direction == 1 then -- move highlight to the right
|
|
|
|
if move_by_char then
|
|
|
|
updated_highlight_end = self.ui.document:getNextVisibleChar(highlight_end)
|
|
|
|
else
|
|
|
|
updated_highlight_end = self.ui.document:getNextVisibleWordEnd(highlight_end)
|
|
|
|
end
|
|
|
|
else -- move highlight to the left
|
|
|
|
if move_by_char then
|
|
|
|
updated_highlight_end = self.ui.document:getPrevVisibleChar(highlight_end)
|
|
|
|
else
|
|
|
|
updated_highlight_end = self.ui.document:getPrevVisibleWordEnd(highlight_end)
|
|
|
|
end
|
2019-02-17 15:15:32 +00:00
|
|
|
end
|
|
|
|
if updated_highlight_end then
|
|
|
|
local order = self.ui.document:compareXPointers(highlight_beginning, updated_highlight_end)
|
|
|
|
if order and order > 0 then -- only if end did not go back past beginning
|
2019-02-15 23:42:27 +00:00
|
|
|
self.view.highlight.saved[page][index].pos1 = updated_highlight_end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local new_beginning = self.view.highlight.saved[page][index].pos0
|
|
|
|
local new_end = self.view.highlight.saved[page][index].pos1
|
|
|
|
local new_text = self.ui.document:getTextFromXPointers(new_beginning, new_end)
|
2020-06-17 21:51:02 +00:00
|
|
|
local new_chapter = self.ui.toc:getTocTitleByPage(new_beginning)
|
2020-07-21 22:14:57 +00:00
|
|
|
self.view.highlight.saved[page][index].text = cleanupSelectedText(new_text)
|
2020-06-17 21:51:02 +00:00
|
|
|
self.view.highlight.saved[page][index].chapter = new_chapter
|
2019-02-15 23:42:27 +00:00
|
|
|
local new_highlight = self.view.highlight.saved[page][index]
|
|
|
|
self.ui.bookmark:updateBookmark({
|
|
|
|
page = highlight_beginning,
|
|
|
|
datetime = highlight_time,
|
|
|
|
updated_highlight = new_highlight
|
|
|
|
}, true)
|
2020-03-20 20:58:58 +00:00
|
|
|
if side == 0 then
|
|
|
|
-- Ensure we show the page with the new beginning of highlight
|
|
|
|
if not self.ui.document:isXPointerInCurrentPage(new_beginning) then
|
|
|
|
self.ui:handleEvent(Event:new("GotoXPointer", new_beginning))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Ensure we show the page with the new end of highlight
|
|
|
|
if not self.ui.document:isXPointerInCurrentPage(new_end) then
|
|
|
|
if self.view.view_mode == "page" then
|
|
|
|
self.ui:handleEvent(Event:new("GotoXPointer", new_end))
|
|
|
|
else
|
|
|
|
-- Not easy to get the y that would show the whole line
|
|
|
|
-- containing new_end. So, we scroll so that new_end
|
|
|
|
-- is at 2/3 of the screen.
|
|
|
|
local end_y = self.ui.document:getPosFromXPointer(new_end)
|
|
|
|
local top_y = end_y - math.floor(Screen:getHeight() * 2/3)
|
|
|
|
self.ui.rolling:_gotoPos(top_y)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 23:42:27 +00:00
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
|
|
|
end
|
|
|
|
|
2014-01-17 19:05:17 +00:00
|
|
|
function ReaderHighlight:onShowHighlightDialog(page, index)
|
2019-02-15 23:42:27 +00:00
|
|
|
local buttons = {
|
|
|
|
{
|
2014-03-13 13:52:43 +00:00
|
|
|
{
|
2019-02-15 23:42:27 +00:00
|
|
|
text = _("Delete"),
|
|
|
|
callback = function()
|
|
|
|
self:deleteHighlight(page, index)
|
|
|
|
-- other part outside of the dialog may be dirty
|
|
|
|
UIManager:close(self.edit_highlight_dialog, "ui")
|
|
|
|
end,
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2019-02-15 23:42:27 +00:00
|
|
|
{
|
|
|
|
text = _("Edit"),
|
|
|
|
callback = function()
|
|
|
|
self:editHighlight(page, index)
|
|
|
|
UIManager:close(self.edit_highlight_dialog)
|
|
|
|
end,
|
|
|
|
},
|
2019-10-06 21:47:53 +00:00
|
|
|
{
|
|
|
|
text = _("…"),
|
|
|
|
callback = function()
|
|
|
|
self.selected_text = self.view.highlight.saved[page][index]
|
|
|
|
self:onShowHighlightMenu()
|
|
|
|
UIManager:close(self.edit_highlight_dialog)
|
|
|
|
end,
|
|
|
|
},
|
2019-02-15 23:42:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if not self.ui.document.info.has_pages then
|
2019-12-06 21:55:39 +00:00
|
|
|
local start_prev = "◁⇱"
|
|
|
|
local start_next = "⇱▷"
|
|
|
|
local end_prev = "◁⇲"
|
|
|
|
local end_next = "⇲▷"
|
|
|
|
if BD.mirroredUILayout() then
|
|
|
|
-- Sadly, there's only north west & south east arrow to corner,
|
|
|
|
-- north east and south west do not exist in Unicode.
|
|
|
|
start_prev, start_next = BD.ltr(start_next), BD.ltr(start_prev)
|
|
|
|
end_prev, end_next = BD.ltr(end_next), BD.ltr(end_prev)
|
|
|
|
end
|
2019-02-15 23:42:27 +00:00
|
|
|
table.insert(buttons, {
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = start_prev,
|
2019-02-15 23:42:27 +00:00
|
|
|
callback = function()
|
|
|
|
self:updateHighlight(page, index, 0, -1, false)
|
|
|
|
end,
|
|
|
|
hold_callback = function()
|
|
|
|
self:updateHighlight(page, index, 0, -1, true)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = start_next,
|
2019-02-15 23:42:27 +00:00
|
|
|
callback = function()
|
|
|
|
self:updateHighlight(page, index, 0, 1, false)
|
|
|
|
end,
|
|
|
|
hold_callback = function()
|
|
|
|
self:updateHighlight(page, index, 0, 1, true)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = end_prev,
|
2019-02-15 23:42:27 +00:00
|
|
|
callback = function()
|
|
|
|
self:updateHighlight(page, index, 1, -1, false)
|
|
|
|
end,
|
|
|
|
hold_callback = function()
|
|
|
|
self:updateHighlight(page, index, 1, -1, true)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
2019-12-06 21:55:39 +00:00
|
|
|
text = end_next,
|
2019-02-15 23:42:27 +00:00
|
|
|
callback = function()
|
|
|
|
self:updateHighlight(page, index, 1, 1, false)
|
|
|
|
end,
|
|
|
|
hold_callback = function()
|
|
|
|
self:updateHighlight(page, index, 1, 1, true)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
self.edit_highlight_dialog = ButtonDialog:new{
|
|
|
|
buttons = buttons
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
UIManager:show(self.edit_highlight_dialog)
|
|
|
|
return true
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2020-11-08 12:07:51 +00:00
|
|
|
function ReaderHighlight:addToHighlightDialog(idx, fn_button)
|
|
|
|
-- fn_button is a function that takes the ReaderHighlight instance
|
|
|
|
-- as argument, and returns a table describing the button to be added.
|
|
|
|
self._highlight_buttons[idx] = fn_button
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:removeFromHighlightDialog(idx)
|
|
|
|
local button = self._highlight_buttons[idx]
|
|
|
|
self._highlight_buttons[idx] = nil
|
|
|
|
return button
|
|
|
|
end
|
|
|
|
|
2019-10-06 21:47:53 +00:00
|
|
|
function ReaderHighlight:onShowHighlightMenu()
|
2020-11-08 12:07:51 +00:00
|
|
|
local highlight_buttons = {{}}
|
|
|
|
|
|
|
|
local columns = 2
|
|
|
|
for idx, fn_button in ffiUtil.orderedPairs(self._highlight_buttons) do
|
|
|
|
if #highlight_buttons[#highlight_buttons] >= columns then
|
|
|
|
table.insert(highlight_buttons, {})
|
|
|
|
end
|
|
|
|
table.insert(highlight_buttons[#highlight_buttons], fn_button(self))
|
|
|
|
logger.dbg("ReaderHighlight", idx..": line "..#highlight_buttons..", col "..#highlight_buttons[#highlight_buttons])
|
2020-01-05 11:56:01 +00:00
|
|
|
end
|
|
|
|
|
2019-10-06 21:47:53 +00:00
|
|
|
self.highlight_dialog = ButtonDialog:new{
|
|
|
|
buttons = highlight_buttons,
|
|
|
|
tap_close_callback = function() self:handleEvent(Event:new("Tap")) end,
|
|
|
|
}
|
|
|
|
UIManager:show(self.highlight_dialog)
|
|
|
|
end
|
|
|
|
|
2020-05-05 16:56:30 +00:00
|
|
|
function ReaderHighlight:_resetHoldTimer(clear)
|
|
|
|
if clear then
|
|
|
|
self.hold_last_tv = nil
|
|
|
|
else
|
|
|
|
self.hold_last_tv = TimeVal.now()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-24 13:17:37 +00:00
|
|
|
function ReaderHighlight:onTogglePanelZoomSetting(arg, ges)
|
|
|
|
if not self.document.info.has_pages then return end
|
|
|
|
self.panel_zoom_enabled = not self.panel_zoom_enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onPanelZoom(arg, ges)
|
|
|
|
self:clear()
|
|
|
|
local hold_pos = self.view:screenToPageTransform(ges.pos)
|
|
|
|
if not hold_pos then return false end -- outside page boundary
|
|
|
|
local rect = self.ui.document:getPanelFromPage(hold_pos.page, hold_pos)
|
|
|
|
if not rect then return false end -- panel not found, return
|
|
|
|
local image = self.ui.document:getPagePart(hold_pos.page, rect, 0)
|
|
|
|
|
|
|
|
if image then
|
|
|
|
local ImageViewer = require("ui/widget/imageviewer")
|
|
|
|
local imgviewer = ImageViewer:new{
|
|
|
|
image = image,
|
|
|
|
with_title_bar = false,
|
|
|
|
fullscreen = true,
|
|
|
|
}
|
|
|
|
UIManager:show(imgviewer)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2017-01-15 20:51:43 +00:00
|
|
|
function ReaderHighlight:onHold(arg, ges)
|
2020-09-24 13:17:37 +00:00
|
|
|
if self.document.info.has_pages and self.panel_zoom_enabled then
|
|
|
|
return self:onPanelZoom(arg, ges)
|
|
|
|
end
|
|
|
|
|
2014-11-06 03:07:48 +00:00
|
|
|
-- disable hold gesture if highlighting is disabled
|
2020-01-24 19:05:21 +00:00
|
|
|
if self.view.highlight.disabled then return false end
|
2017-09-20 15:35:30 +00:00
|
|
|
self:clear() -- clear previous highlight (delayed clear may not have done it yet)
|
2018-02-26 16:25:34 +00:00
|
|
|
self.hold_ges_pos = ges.pos -- remember hold original gesture position
|
2014-03-13 13:52:43 +00:00
|
|
|
self.hold_pos = self.view:screenToPageTransform(ges.pos)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("hold position in page", self.hold_pos)
|
2014-03-13 13:52:43 +00:00
|
|
|
if not self.hold_pos then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("not inside page area")
|
2020-01-24 19:05:21 +00:00
|
|
|
return false
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-10-12 15:07:13 +00:00
|
|
|
|
2017-01-15 20:51:43 +00:00
|
|
|
-- check if we were holding on an image
|
2018-04-21 20:03:04 +00:00
|
|
|
-- we provide want_frames=true, so we get a list of images for
|
|
|
|
-- animated GIFs (supported by ImageViewer)
|
|
|
|
local image = self.ui.document:getImageFromPosition(self.hold_pos, true)
|
2017-01-15 20:51:43 +00:00
|
|
|
if image then
|
|
|
|
logger.dbg("hold on image")
|
|
|
|
local ImageViewer = require("ui/widget/imageviewer")
|
|
|
|
local imgviewer = ImageViewer:new{
|
|
|
|
image = image,
|
|
|
|
-- title_text = _("Document embedded image"),
|
|
|
|
-- No title, more room for image
|
|
|
|
with_title_bar = false,
|
|
|
|
fullscreen = true,
|
|
|
|
}
|
|
|
|
UIManager:show(imgviewer)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- otherwise, we must be holding on text
|
2014-03-13 13:52:43 +00:00
|
|
|
local ok, word = pcall(self.ui.document.getWordFromPosition, self.ui.document, self.hold_pos)
|
|
|
|
if ok and word then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("selected word:", word)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.selected_word = word
|
2017-09-10 12:26:49 +00:00
|
|
|
local link = self.ui.link:getLinkFromGes(ges)
|
2017-09-09 16:30:00 +00:00
|
|
|
self.selected_link = nil
|
|
|
|
if link then
|
|
|
|
logger.dbg("link:", link)
|
|
|
|
self.selected_link = link
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
local boxes = {}
|
|
|
|
table.insert(boxes, self.selected_word.sbox)
|
|
|
|
self.view.highlight.temp[self.hold_pos.page] = boxes
|
|
|
|
end
|
2017-06-18 16:08:57 +00:00
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @todo only mark word?
|
2014-12-01 14:39:41 +00:00
|
|
|
-- Unfortunately, CREngine does not return good coordinates
|
2014-11-30 00:12:00 +00:00
|
|
|
-- UIManager:setDirty(self.dialog, "partial", self.selected_word.sbox)
|
2020-05-05 16:56:30 +00:00
|
|
|
self:_resetHoldTimer()
|
2019-03-13 12:10:32 +00:00
|
|
|
if word.pos0 then
|
|
|
|
-- Remember original highlight start position, so we can show
|
|
|
|
-- a marker when back from across-pages text selection, which
|
|
|
|
-- is handled in onHoldPan()
|
|
|
|
self.selected_text_start_xpointer = word.pos0
|
|
|
|
end
|
2020-01-24 19:05:21 +00:00
|
|
|
return true
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2020-01-24 19:05:21 +00:00
|
|
|
return false
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2016-03-28 00:18:25 +00:00
|
|
|
function ReaderHighlight:onHoldPan(_, ges)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.hold_pos == nil then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("no previous hold position")
|
2020-05-05 16:56:30 +00:00
|
|
|
self:_resetHoldTimer(true)
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
2014-07-02 08:38:09 +00:00
|
|
|
local page_area = self.view:getScreenPageArea(self.hold_pos.page)
|
|
|
|
if ges.pos:notIntersectWith(page_area) then
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("not inside page area", ges, page_area)
|
2020-05-05 16:56:30 +00:00
|
|
|
self:_resetHoldTimer()
|
2014-07-02 08:38:09 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
self.holdpan_pos = self.view:screenToPageTransform(ges.pos)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("holdpan position in page", self.holdpan_pos)
|
2019-02-03 09:01:58 +00:00
|
|
|
|
|
|
|
if not self.ui.document.info.has_pages and self.selected_text_start_xpointer then
|
|
|
|
-- With CreDocuments, allow text selection across multiple pages
|
|
|
|
-- by (temporarily) switching to scroll mode when panning to the
|
|
|
|
-- top left or bottom right corners.
|
2020-01-06 20:09:00 +00:00
|
|
|
local mirrored_reading = BD.mirroredUILayout()
|
|
|
|
if self.ui.rolling and self.ui.rolling.inverse_reading_order then
|
|
|
|
mirrored_reading = not mirrored_reading
|
|
|
|
end
|
|
|
|
local is_in_prev_page_corner, is_in_next_page_corner
|
|
|
|
if mirrored_reading then
|
2019-12-06 21:55:39 +00:00
|
|
|
-- Note: this might not be really usable, as crengine native selection
|
|
|
|
-- is not adapted to RTL text
|
2020-01-06 20:09:00 +00:00
|
|
|
-- top right corner
|
|
|
|
is_in_prev_page_corner = self.holdpan_pos.y < 1/8*Screen:getHeight()
|
2019-12-06 21:55:39 +00:00
|
|
|
and self.holdpan_pos.x > 7/8*Screen:getWidth()
|
2020-01-06 20:09:00 +00:00
|
|
|
-- bottom left corner
|
|
|
|
is_in_next_page_corner = self.holdpan_pos.y > 7/8*Screen:getHeight()
|
2019-12-06 21:55:39 +00:00
|
|
|
and self.holdpan_pos.x < 1/8*Screen:getWidth()
|
2020-01-06 20:09:00 +00:00
|
|
|
else -- default in LTR UI with no inverse_reading_order
|
|
|
|
-- top left corner
|
|
|
|
is_in_prev_page_corner = self.holdpan_pos.y < 1/8*Screen:getHeight()
|
|
|
|
and self.holdpan_pos.x < 1/8*Screen:getWidth()
|
|
|
|
-- bottom right corner
|
|
|
|
is_in_next_page_corner = self.holdpan_pos.y > 7/8*Screen:getHeight()
|
|
|
|
and self.holdpan_pos.x > 7/8*Screen:getWidth()
|
2019-12-06 21:55:39 +00:00
|
|
|
end
|
2020-01-06 20:09:00 +00:00
|
|
|
if is_in_prev_page_corner or is_in_next_page_corner then
|
2020-05-05 16:56:30 +00:00
|
|
|
self:_resetHoldTimer()
|
2019-02-03 09:01:58 +00:00
|
|
|
if self.was_in_some_corner then
|
|
|
|
-- Do nothing, wait for the user to move his finger out of that corner
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
self.was_in_some_corner = true
|
2019-03-13 12:10:32 +00:00
|
|
|
if self.ui.document:getVisiblePageCount() == 1 then -- single page mode
|
|
|
|
-- We'll adjust hold_pos.y after the mode switch and the scroll
|
|
|
|
-- so it's accurate in the new screen coordinates
|
|
|
|
local orig_y = self.ui.document:getScreenPositionFromXPointer(self.selected_text_start_xpointer)
|
|
|
|
if self.view.view_mode ~= "scroll" then
|
|
|
|
-- Switch from page mode to scroll mode
|
|
|
|
local restore_page_mode_xpointer = self.ui.document:getXPointer() -- top of current page
|
|
|
|
self.restore_page_mode_func = function()
|
|
|
|
self.ui:handleEvent(Event:new("SetViewMode", "page"))
|
|
|
|
self.ui.rolling:onGotoXPointer(restore_page_mode_xpointer, self.selected_text_start_xpointer)
|
|
|
|
end
|
|
|
|
self.ui:handleEvent(Event:new("SetViewMode", "scroll"))
|
|
|
|
end
|
|
|
|
-- (using rolling:onGotoViewRel(1/3) has some strange side effects)
|
|
|
|
local scroll_distance = math.floor(Screen:getHeight() * 1/3)
|
2020-01-06 20:09:00 +00:00
|
|
|
local move_y = is_in_next_page_corner and scroll_distance or -scroll_distance
|
2019-03-13 12:10:32 +00:00
|
|
|
self.ui.rolling:_gotoPos(self.ui.document:getCurrentPos() + move_y)
|
|
|
|
local new_y = self.ui.document:getScreenPositionFromXPointer(self.selected_text_start_xpointer)
|
|
|
|
self.hold_pos.y = self.hold_pos.y - orig_y + new_y
|
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
|
|
|
return true
|
|
|
|
else -- two pages mode
|
|
|
|
-- We don't switch to scroll mode: we just turn 1 page to
|
|
|
|
-- allow continuing the selection.
|
|
|
|
-- Unlike in 1-page mode, we have a limitation here: we can't adjust
|
|
|
|
-- the selection to further than current page and prev/next one.
|
|
|
|
-- So don't handle another corner if we already handled one:
|
2020-01-06 20:09:00 +00:00
|
|
|
-- Note that this feature won't work well with the RTL UI or
|
|
|
|
-- if inverse_reading_order as crengine currently always displays
|
|
|
|
-- the first page on the left and the second on the right in
|
|
|
|
-- dual page mode.
|
2019-03-13 12:10:32 +00:00
|
|
|
if self.restore_page_mode_func then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
-- Also, we are not able to move hold_pos.x out of screen,
|
|
|
|
-- so if we started on the right page, ignore top left corner,
|
|
|
|
-- and if we started on the left page, ignore bottom right corner.
|
2020-06-14 00:21:41 +00:00
|
|
|
local screen_half_width = math.floor(Screen:getWidth() * 0.5)
|
2020-01-06 20:09:00 +00:00
|
|
|
if self.hold_pos.x >= screen_half_width and is_in_prev_page_corner then
|
2019-03-13 12:10:32 +00:00
|
|
|
return true
|
2020-01-06 20:09:00 +00:00
|
|
|
elseif self.hold_pos.x <= screen_half_width and is_in_next_page_corner then
|
2019-03-13 12:10:32 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
local cur_page = self.ui.document:getCurrentPage()
|
2019-02-03 09:01:58 +00:00
|
|
|
local restore_page_mode_xpointer = self.ui.document:getXPointer() -- top of current page
|
|
|
|
self.restore_page_mode_func = function()
|
|
|
|
self.ui.rolling:onGotoXPointer(restore_page_mode_xpointer, self.selected_text_start_xpointer)
|
|
|
|
end
|
2020-01-06 20:09:00 +00:00
|
|
|
if is_in_next_page_corner then -- bottom right corner in LTR UI
|
2019-03-13 12:10:32 +00:00
|
|
|
self.ui.rolling:_gotoPage(cur_page + 1, true) -- no odd left page enforcement
|
|
|
|
self.hold_pos.x = self.hold_pos.x - screen_half_width
|
2020-01-06 20:09:00 +00:00
|
|
|
else -- top left corner in RTL UI
|
2019-03-13 12:10:32 +00:00
|
|
|
self.ui.rolling:_gotoPage(cur_page - 1, true) -- no odd left page enforcement
|
|
|
|
self.hold_pos.x = self.hold_pos.x + screen_half_width
|
|
|
|
end
|
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
|
|
|
return true
|
2019-02-03 09:01:58 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
self.was_in_some_corner = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-30 00:12:00 +00:00
|
|
|
local old_text = self.selected_text and self.selected_text.text
|
2014-03-13 13:52:43 +00:00
|
|
|
self.selected_text = self.ui.document:getTextFromPositions(self.hold_pos, self.holdpan_pos)
|
2019-02-03 09:01:58 +00:00
|
|
|
|
|
|
|
if self.selected_text and self.selected_text.pos0 then
|
|
|
|
if not self.selected_text_start_xpointer then
|
2019-03-13 12:10:32 +00:00
|
|
|
-- This should have been set in onHold(), where we would get
|
|
|
|
-- a precise pos0 on the first word selected.
|
|
|
|
-- Do it here too in case onHold() missed it, but it could be
|
|
|
|
-- less precise (getTextFromPositions() does order pos0 and pos1,
|
|
|
|
-- so it's not certain pos0 is where we started from; we get
|
|
|
|
-- the ones from the first pan, and if it is not small enough
|
|
|
|
-- and spans quite some height, the marker could point away
|
|
|
|
-- from the start position)
|
2019-02-03 09:01:58 +00:00
|
|
|
self.selected_text_start_xpointer = self.selected_text.pos0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-30 00:12:00 +00:00
|
|
|
if self.selected_text and old_text and old_text == self.selected_text.text then
|
|
|
|
-- no modification
|
|
|
|
return
|
|
|
|
end
|
2020-05-05 16:56:30 +00:00
|
|
|
self:_resetHoldTimer() -- selection updated
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("selected text:", self.selected_text)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.selected_text then
|
|
|
|
self.view.highlight.temp[self.hold_pos.page] = self.selected_text.sboxes
|
|
|
|
-- remove selected word if hold moves out of word box
|
|
|
|
if not self.selected_text.sboxes or #self.selected_text.sboxes == 0 then
|
|
|
|
self.selected_word = nil
|
|
|
|
elseif self.selected_word and not self.selected_word.sbox:contains(self.selected_text.sboxes[1]) or
|
|
|
|
#self.selected_text.sboxes > 1 then
|
|
|
|
self.selected_word = nil
|
|
|
|
end
|
|
|
|
end
|
2014-12-03 06:06:43 +00:00
|
|
|
UIManager:setDirty(self.dialog, "ui")
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2019-08-22 15:11:47 +00:00
|
|
|
local info_message_ocr_text = _([[
|
|
|
|
No OCR results or no language data.
|
2018-03-05 15:38:04 +00:00
|
|
|
|
|
|
|
KOReader has a build-in OCR engine for recognizing words in scanned PDF and DjVu documents. In order to use OCR in scanned pages, you need to install tesseract trained data for your document language.
|
|
|
|
|
2020-07-30 20:15:02 +00:00
|
|
|
You can download language data files for version 3.04 from https://tesseract-ocr.github.io/tessdoc/Data-Files
|
2018-03-05 15:38:04 +00:00
|
|
|
|
2018-03-17 17:13:04 +00:00
|
|
|
Copy the language data files for Tesseract 3.04 (e.g., eng.traineddata for English and spa.traineddata for Spanish) into koreader/data/tessdata]])
|
2018-03-05 15:38:04 +00:00
|
|
|
|
2017-09-09 16:30:00 +00:00
|
|
|
function ReaderHighlight:lookup(selected_word, selected_link)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- if we extracted text directly
|
|
|
|
if selected_word.word then
|
|
|
|
local word_box = self.view:pageToScreenTransform(self.hold_pos.page, selected_word.sbox)
|
2017-09-09 16:30:00 +00:00
|
|
|
self.ui:handleEvent(Event:new("LookupWord", selected_word.word, word_box, self, selected_link))
|
2014-03-13 13:52:43 +00:00
|
|
|
-- or we will do OCR
|
|
|
|
elseif selected_word.sbox and self.hold_pos then
|
|
|
|
local word = self.ui.document:getOCRWord(self.hold_pos.page, selected_word)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("OCRed word:", word)
|
2018-03-05 15:38:04 +00:00
|
|
|
if word and word ~= "" then
|
|
|
|
local word_box = self.view:pageToScreenTransform(self.hold_pos.page, selected_word.sbox)
|
|
|
|
self.ui:handleEvent(Event:new("LookupWord", word, word_box, self, selected_link))
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = info_message_ocr_text,
|
|
|
|
})
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-07-19 18:49:03 +00:00
|
|
|
end
|
|
|
|
|
2018-10-08 16:58:43 +00:00
|
|
|
function ReaderHighlight:viewSelectionHTML(debug_view)
|
|
|
|
if self.ui.document.info.has_pages then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if self.selected_text and self.selected_text.pos0 and self.selected_text.pos1 then
|
|
|
|
-- For available flags, see the "#define WRITENODEEX_*" in crengine/src/lvtinydom.cpp
|
2020-01-14 00:14:15 +00:00
|
|
|
-- Start with valid and classic displayed HTML (with only block nodes indented),
|
2020-06-05 08:06:37 +00:00
|
|
|
-- including styles found in <HEAD>, linked CSS files content, and misc info.
|
|
|
|
local html_flags = 0x6830
|
2019-08-17 17:10:54 +00:00
|
|
|
if not debug_view then
|
|
|
|
debug_view = 0
|
|
|
|
end
|
|
|
|
if debug_view == 1 then
|
2018-10-08 16:58:43 +00:00
|
|
|
-- Each node on a line, with markers and numbers of skipped chars and siblings shown,
|
|
|
|
-- with possibly invalid HTML (text nodes not escaped)
|
2020-06-05 08:06:37 +00:00
|
|
|
html_flags = 0x6B5A
|
2019-08-17 17:10:54 +00:00
|
|
|
elseif debug_view == 2 then
|
2020-01-31 10:50:13 +00:00
|
|
|
-- Additionally see rendering methods of each node
|
2020-06-05 08:06:37 +00:00
|
|
|
html_flags = 0x6F5A
|
2020-01-31 10:50:13 +00:00
|
|
|
elseif debug_view == 3 then
|
|
|
|
-- Or additionally see unicode codepoint of each char
|
2020-06-05 08:06:37 +00:00
|
|
|
html_flags = 0x6B5E
|
2018-10-08 16:58:43 +00:00
|
|
|
end
|
|
|
|
local html, css_files = self.ui.document:getHTMLFromXPointers(self.selected_text.pos0,
|
|
|
|
self.selected_text.pos1, html_flags, true)
|
|
|
|
if html then
|
|
|
|
-- Make some invisible chars visible
|
2019-08-17 17:10:54 +00:00
|
|
|
if debug_view >= 1 then
|
2018-10-08 16:58:43 +00:00
|
|
|
html = html:gsub("\xC2\xA0", "␣") -- no break space: open box
|
|
|
|
html = html:gsub("\xC2\xAD", "⋅") -- soft hyphen: dot operator (smaller than middle dot ·)
|
2019-07-14 20:18:33 +00:00
|
|
|
-- Prettify inlined CSS (from <HEAD>, put in an internal
|
|
|
|
-- <body><stylesheet> element by crengine (the opening tag may
|
|
|
|
-- include some href=, or end with " ~X>" with some html_flags)
|
|
|
|
-- (We do that in debug_view mode only: as this may increase
|
|
|
|
-- the height of this section, we don't want to have to scroll
|
|
|
|
-- many pages to get to the HTML content on the initial view.)
|
|
|
|
html = html:gsub("(<stylesheet[^>]*>)%s*(.-)%s*(</stylesheet>)", function(pre, css_text, post)
|
2020-06-08 18:47:31 +00:00
|
|
|
return pre .. "\n" .. util.prettifyCSS(css_text) .. post
|
2019-07-14 20:18:33 +00:00
|
|
|
end)
|
2018-10-08 16:58:43 +00:00
|
|
|
end
|
|
|
|
local TextViewer = require("ui/widget/textviewer")
|
|
|
|
local Font = require("ui/font")
|
|
|
|
local textviewer
|
|
|
|
local buttons_table = {}
|
|
|
|
if css_files then
|
|
|
|
for i=1, #css_files do
|
|
|
|
local button = {
|
2020-01-04 00:18:51 +00:00
|
|
|
text = T(_("View %1"), BD.filepath(css_files[i])),
|
2018-10-08 16:58:43 +00:00
|
|
|
callback = function()
|
|
|
|
local css_text = self.ui.document:getDocumentFileContent(css_files[i])
|
2018-10-22 16:47:01 +00:00
|
|
|
local cssviewer
|
|
|
|
cssviewer = TextViewer:new{
|
2018-10-08 16:58:43 +00:00
|
|
|
title = css_files[i],
|
|
|
|
text = css_text or _("Failed getting CSS content"),
|
|
|
|
text_face = Font:getFace("smallinfont"),
|
|
|
|
justified = false,
|
2019-12-06 21:55:35 +00:00
|
|
|
para_direction_rtl = false,
|
|
|
|
auto_para_direction = false,
|
2018-10-22 16:47:01 +00:00
|
|
|
buttons_table = {
|
|
|
|
{{
|
|
|
|
text = _("Prettify"),
|
|
|
|
enabled = css_text and true or false,
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(cssviewer)
|
|
|
|
UIManager:show(TextViewer:new{
|
|
|
|
title = css_files[i],
|
2020-06-08 18:47:31 +00:00
|
|
|
text = util.prettifyCSS(css_text),
|
2018-10-22 16:47:01 +00:00
|
|
|
text_face = Font:getFace("smallinfont"),
|
|
|
|
justified = false,
|
2019-12-06 21:55:35 +00:00
|
|
|
para_direction_rtl = false,
|
|
|
|
auto_para_direction = false,
|
2018-10-22 16:47:01 +00:00
|
|
|
})
|
|
|
|
end,
|
|
|
|
}},
|
|
|
|
{{
|
|
|
|
text = _("Close"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(cssviewer)
|
|
|
|
end,
|
|
|
|
}},
|
|
|
|
}
|
2018-10-08 16:58:43 +00:00
|
|
|
}
|
|
|
|
UIManager:show(cssviewer)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
-- One button per row, too make room for the possibly long css filename
|
|
|
|
table.insert(buttons_table, {button})
|
|
|
|
end
|
|
|
|
end
|
2019-08-17 17:10:54 +00:00
|
|
|
local next_debug_text
|
|
|
|
local next_debug_view = debug_view + 1
|
|
|
|
if next_debug_view == 1 then
|
|
|
|
next_debug_text = _("Switch to debug view")
|
|
|
|
elseif next_debug_view == 2 then
|
2020-01-31 10:50:13 +00:00
|
|
|
next_debug_text = _("Switch to rendering debug view")
|
|
|
|
elseif next_debug_view == 3 then
|
|
|
|
next_debug_text = _("Switch to unicode debug view")
|
2019-08-17 17:10:54 +00:00
|
|
|
else
|
|
|
|
next_debug_view = 0
|
|
|
|
next_debug_text = _("Switch to standard view")
|
|
|
|
end
|
2018-10-08 16:58:43 +00:00
|
|
|
table.insert(buttons_table, {{
|
2019-08-17 17:10:54 +00:00
|
|
|
text = next_debug_text,
|
2018-10-08 16:58:43 +00:00
|
|
|
callback = function()
|
|
|
|
UIManager:close(textviewer)
|
2019-08-17 17:10:54 +00:00
|
|
|
self:viewSelectionHTML(next_debug_view)
|
2018-10-08 16:58:43 +00:00
|
|
|
end,
|
|
|
|
}})
|
|
|
|
table.insert(buttons_table, {{
|
|
|
|
text = _("Close"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(textviewer)
|
|
|
|
end,
|
|
|
|
}})
|
|
|
|
textviewer = TextViewer:new{
|
|
|
|
title = _("Selection HTML"),
|
|
|
|
text = html,
|
|
|
|
text_face = Font:getFace("smallinfont"),
|
|
|
|
justified = false,
|
2019-12-06 21:55:35 +00:00
|
|
|
para_direction_rtl = false,
|
|
|
|
auto_para_direction = false,
|
2018-10-08 16:58:43 +00:00
|
|
|
buttons_table = buttons_table,
|
|
|
|
}
|
|
|
|
UIManager:show(textviewer)
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Failed getting HTML for selection"),
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-19 18:49:03 +00:00
|
|
|
function ReaderHighlight:translate(selected_text)
|
2014-03-13 13:52:43 +00:00
|
|
|
if selected_text.text ~= "" then
|
2018-12-16 17:02:38 +00:00
|
|
|
self:onTranslateText(selected_text.text)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- or we will do OCR
|
|
|
|
else
|
|
|
|
local text = self.ui.document:getOCRText(self.hold_pos.page, selected_text)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("OCRed text:", text)
|
2018-03-05 15:38:04 +00:00
|
|
|
if text and text ~= "" then
|
2018-12-16 17:02:38 +00:00
|
|
|
self:onTranslateText(text)
|
2018-03-05 15:38:04 +00:00
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = info_message_ocr_text,
|
|
|
|
})
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-07-19 18:49:03 +00:00
|
|
|
end
|
|
|
|
|
2018-12-16 17:02:38 +00:00
|
|
|
function ReaderHighlight:onTranslateText(text)
|
|
|
|
Translator:showTranslation(text)
|
|
|
|
end
|
|
|
|
|
2014-08-11 13:49:42 +00:00
|
|
|
function ReaderHighlight:onHoldRelease()
|
2020-05-05 16:56:30 +00:00
|
|
|
local long_final_hold = false
|
|
|
|
if self.hold_last_tv then
|
|
|
|
local hold_duration = TimeVal.now() - self.hold_last_tv
|
2017-09-10 18:35:27 +00:00
|
|
|
hold_duration = hold_duration.sec + hold_duration.usec/1000000
|
2020-05-05 16:56:30 +00:00
|
|
|
if hold_duration > 3.0 then
|
|
|
|
-- We stayed 3 seconds before release without updating selection
|
|
|
|
long_final_hold = true
|
|
|
|
end
|
|
|
|
self.hold_last_tv = nil
|
|
|
|
end
|
|
|
|
if self.selected_word then -- single-word selection
|
|
|
|
if long_final_hold or G_reader_settings:isTrue("highlight_action_on_single_word") then
|
|
|
|
-- Force a 0-distance pan to have a self.selected_text with this word,
|
|
|
|
-- which will enable the highlight menu or action instead of dict lookup
|
2018-02-26 16:25:34 +00:00
|
|
|
self:onHoldPan(nil, {pos=self.hold_ges_pos})
|
2017-09-10 18:35:27 +00:00
|
|
|
end
|
|
|
|
end
|
2019-01-17 21:12:38 +00:00
|
|
|
|
2018-02-26 16:25:34 +00:00
|
|
|
if self.selected_text then
|
2019-01-17 21:12:38 +00:00
|
|
|
local default_highlight_action = G_reader_settings:readSetting("default_highlight_action")
|
2020-05-05 16:56:30 +00:00
|
|
|
if long_final_hold or not default_highlight_action then
|
|
|
|
-- bypass default action and show popup if long final hold
|
2019-10-06 21:47:53 +00:00
|
|
|
self:onShowHighlightMenu()
|
2019-01-17 21:12:38 +00:00
|
|
|
elseif default_highlight_action == "highlight" then
|
|
|
|
self:saveHighlight()
|
|
|
|
self:onClose()
|
|
|
|
elseif default_highlight_action == "translate" then
|
|
|
|
self:translate(self.selected_text)
|
|
|
|
self:onClose()
|
|
|
|
elseif default_highlight_action == "wikipedia" then
|
|
|
|
self:lookupWikipedia()
|
|
|
|
self:onClose()
|
2020-05-05 16:56:30 +00:00
|
|
|
elseif default_highlight_action == "dictionary" then
|
|
|
|
self:onHighlightDictLookup()
|
|
|
|
self:onClose()
|
|
|
|
elseif default_highlight_action == "search" then
|
|
|
|
self:onHighlightSearch()
|
|
|
|
-- No self:onClose() to not remove the selected text
|
|
|
|
-- which will have been the first search result
|
2018-12-16 17:02:38 +00:00
|
|
|
end
|
2018-02-26 16:25:34 +00:00
|
|
|
elseif self.selected_word then
|
|
|
|
self:lookup(self.selected_word, self.selected_link)
|
|
|
|
self.selected_word = nil
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
return true
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
2019-03-14 14:33:04 +00:00
|
|
|
function ReaderHighlight:onCycleHighlightAction()
|
|
|
|
local next_actions = {
|
|
|
|
highlight = "translate",
|
|
|
|
translate = "wikipedia",
|
2020-05-05 16:56:30 +00:00
|
|
|
wikipedia = "dictionary",
|
|
|
|
dictionary = "search",
|
|
|
|
search = nil,
|
2019-03-14 14:33:04 +00:00
|
|
|
}
|
|
|
|
local current_action = G_reader_settings:readSetting("default_highlight_action")
|
|
|
|
if not current_action then
|
|
|
|
G_reader_settings:saveSetting("default_highlight_action", "highlight")
|
|
|
|
UIManager:show(Notification:new{
|
|
|
|
text = _("Default highlight action changed to 'highlight'."),
|
|
|
|
timeout = 1,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
local next_action = next_actions[current_action]
|
|
|
|
G_reader_settings:saveSetting("default_highlight_action", next_action)
|
|
|
|
UIManager:show(Notification:new{
|
|
|
|
text = T(_("Default highlight action changed to '%1'."), (next_action or "default")),
|
|
|
|
timeout = 1,
|
|
|
|
})
|
|
|
|
end
|
2019-04-18 10:12:38 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:onCycleHighlightStyle()
|
|
|
|
local next_actions = {
|
|
|
|
lighten = "underscore",
|
|
|
|
underscore = "invert",
|
|
|
|
invert = "lighten"
|
|
|
|
}
|
|
|
|
self.view.highlight.saved_drawer = next_actions[self.view.highlight.saved_drawer]
|
|
|
|
self.ui.doc_settings:saveSetting("highlight_drawer", self.view.highlight.saved_drawer)
|
|
|
|
UIManager:show(Notification:new{
|
|
|
|
text = T(_("Default highlight style changed to '%1'."), self.view.highlight.saved_drawer),
|
|
|
|
timeout = 1,
|
|
|
|
})
|
|
|
|
return true
|
2019-03-14 14:33:04 +00:00
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
function ReaderHighlight:highlightFromHoldPos()
|
2014-08-11 13:49:42 +00:00
|
|
|
if self.hold_pos then
|
|
|
|
if not self.selected_text then
|
|
|
|
self.selected_text = self.ui.document:getTextFromPositions(self.hold_pos, self.hold_pos)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("selected text:", self.selected_text)
|
2014-08-11 13:49:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
function ReaderHighlight:onHighlight()
|
|
|
|
self:saveHighlight()
|
|
|
|
end
|
|
|
|
|
2017-10-18 15:19:06 +00:00
|
|
|
function ReaderHighlight:onUnhighlight(bookmark_item)
|
2017-07-28 20:39:54 +00:00
|
|
|
local page
|
|
|
|
local sel_text
|
|
|
|
local sel_pos0
|
2017-10-18 15:19:06 +00:00
|
|
|
local datetime
|
2017-07-28 20:39:54 +00:00
|
|
|
local idx
|
2017-10-18 15:19:06 +00:00
|
|
|
if bookmark_item then -- called from Bookmarks menu onHold
|
|
|
|
page = bookmark_item.page
|
|
|
|
sel_text = bookmark_item.notes
|
|
|
|
sel_pos0 = bookmark_item.pos0
|
|
|
|
datetime = bookmark_item.datetime
|
|
|
|
else -- called from DictQuickLookup Unhighlight button
|
2017-07-28 20:39:54 +00:00
|
|
|
page = self.hold_pos.page
|
2020-07-21 22:14:57 +00:00
|
|
|
sel_text = cleanupSelectedText(self.selected_text.text)
|
2017-07-28 20:39:54 +00:00
|
|
|
sel_pos0 = self.selected_text.pos0
|
|
|
|
end
|
2017-10-18 15:19:06 +00:00
|
|
|
if self.ui.document.info.has_pages then -- We can safely use page
|
|
|
|
for index = 1, #self.view.highlight.saved[page] do
|
|
|
|
local highlight = self.view.highlight.saved[page][index]
|
|
|
|
-- pos0 are tables and can't be compared directly, except when from
|
|
|
|
-- DictQuickLookup where these are the same object.
|
|
|
|
-- If bookmark_item provided, just check datetime
|
|
|
|
if highlight.text == sel_text and (
|
|
|
|
(datetime == nil and highlight.pos0 == sel_pos0) or
|
|
|
|
(datetime ~= nil and highlight.datetime == datetime)) then
|
|
|
|
idx = index
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else -- page is a xpointer
|
|
|
|
-- The original page could be found in bookmark_item.text, but
|
|
|
|
-- no more if it has been renamed: we need to loop through all
|
|
|
|
-- highlights on all page slots
|
|
|
|
for p, highlights in pairs(self.view.highlight.saved) do
|
|
|
|
for index = 1, #highlights do
|
|
|
|
local highlight = highlights[index]
|
|
|
|
-- pos0 are strings and can be compared directly
|
|
|
|
if highlight.text == sel_text and (
|
|
|
|
(datetime == nil and highlight.pos0 == sel_pos0) or
|
|
|
|
(datetime ~= nil and highlight.datetime == datetime)) then
|
|
|
|
page = p -- this is the original page slot
|
|
|
|
idx = index
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if idx then
|
|
|
|
break
|
|
|
|
end
|
2017-07-28 20:39:54 +00:00
|
|
|
end
|
|
|
|
end
|
2017-10-18 15:19:06 +00:00
|
|
|
if bookmark_item and not idx then
|
|
|
|
logger.warn("unhighlight: bookmark_item not found among highlights", bookmark_item)
|
|
|
|
-- Remove it from bookmarks anyway, so we're not stuck with an
|
|
|
|
-- unremovable bookmark
|
|
|
|
self.ui.bookmark:removeBookmark(bookmark_item)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
logger.dbg("found highlight to delete on page", page, idx)
|
|
|
|
self:deleteHighlight(page, idx, bookmark_item)
|
2017-07-28 20:39:54 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2015-03-12 10:50:57 +00:00
|
|
|
function ReaderHighlight:getHighlightBookmarkItem()
|
|
|
|
if self.hold_pos and not self.selected_text then
|
|
|
|
self:highlightFromHoldPos()
|
|
|
|
end
|
|
|
|
if self.selected_text and self.selected_text.pos0 and self.selected_text.pos1 then
|
|
|
|
local datetime = os.date("%Y-%m-%d %H:%M:%S")
|
|
|
|
local page = self.ui.document.info.has_pages and
|
|
|
|
self.hold_pos.page or self.selected_text.pos0
|
2020-06-17 21:51:02 +00:00
|
|
|
local chapter_name = self.ui.toc:getTocTitleByPage(page)
|
2015-03-12 10:50:57 +00:00
|
|
|
return {
|
|
|
|
page = page,
|
|
|
|
pos0 = self.selected_text.pos0,
|
|
|
|
pos1 = self.selected_text.pos1,
|
|
|
|
datetime = datetime,
|
2020-07-21 22:14:57 +00:00
|
|
|
notes = cleanupSelectedText(self.selected_text.text),
|
2015-03-12 10:50:57 +00:00
|
|
|
highlighted = true,
|
2020-06-17 21:51:02 +00:00
|
|
|
chapter = chapter_name,
|
2015-03-12 10:50:57 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-15 15:13:19 +00:00
|
|
|
function ReaderHighlight:saveHighlight()
|
2017-07-27 17:00:37 +00:00
|
|
|
self.ui:handleEvent(Event:new("AddHighlight"))
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("save highlight")
|
2014-03-13 13:52:43 +00:00
|
|
|
local page = self.hold_pos.page
|
2015-02-01 16:09:07 +00:00
|
|
|
if self.hold_pos and self.selected_text and self.selected_text.pos0
|
|
|
|
and self.selected_text.pos1 then
|
2014-03-13 13:52:43 +00:00
|
|
|
if not self.view.highlight.saved[page] then
|
|
|
|
self.view.highlight.saved[page] = {}
|
|
|
|
end
|
2014-11-27 13:59:27 +00:00
|
|
|
local datetime = os.date("%Y-%m-%d %H:%M:%S")
|
2020-06-17 21:51:02 +00:00
|
|
|
local pg_or_xp = self.ui.document.info.has_pages and
|
|
|
|
self.hold_pos.page or self.selected_text.pos0
|
|
|
|
local chapter_name = self.ui.toc:getTocTitleByPage(pg_or_xp)
|
2014-11-27 13:59:27 +00:00
|
|
|
local hl_item = {
|
|
|
|
datetime = datetime,
|
2020-07-21 22:14:57 +00:00
|
|
|
text = cleanupSelectedText(self.selected_text.text),
|
2014-11-27 13:59:27 +00:00
|
|
|
pos0 = self.selected_text.pos0,
|
|
|
|
pos1 = self.selected_text.pos1,
|
|
|
|
pboxes = self.selected_text.pboxes,
|
|
|
|
drawer = self.view.highlight.saved_drawer,
|
2020-06-17 21:51:02 +00:00
|
|
|
chapter = chapter_name
|
2014-11-27 13:59:27 +00:00
|
|
|
}
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(self.view.highlight.saved[page], hl_item)
|
2015-03-12 10:50:57 +00:00
|
|
|
local bookmark_item = self:getHighlightBookmarkItem()
|
|
|
|
if bookmark_item then
|
|
|
|
self.ui.bookmark:addBookmark(bookmark_item)
|
|
|
|
end
|
2014-11-21 10:32:43 +00:00
|
|
|
--[[
|
2017-11-20 20:58:58 +00:00
|
|
|
-- disable exporting highlights to My Clippings
|
2014-11-21 10:32:43 +00:00
|
|
|
-- since it's not portable and there is a better Evernote plugin
|
|
|
|
-- to do the same thing
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.selected_text.text ~= "" then
|
2014-11-21 10:32:43 +00:00
|
|
|
self:exportToClippings(page, hl_item)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-11-21 10:32:43 +00:00
|
|
|
--]]
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.selected_text.pboxes then
|
|
|
|
self:exportToDocument(page, hl_item)
|
|
|
|
end
|
2019-03-12 19:14:34 +00:00
|
|
|
return page, #self.view.highlight.saved[page]
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2014-11-21 10:32:43 +00:00
|
|
|
--[[
|
2013-06-16 11:00:15 +00:00
|
|
|
function ReaderHighlight:exportToClippings(page, item)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("export highlight to clippings", item)
|
2014-03-13 13:52:43 +00:00
|
|
|
local clippings = io.open("/mnt/us/documents/My Clippings.txt", "a+")
|
|
|
|
if clippings and item.text then
|
|
|
|
local current_locale = os.setlocale()
|
|
|
|
os.setlocale("C")
|
|
|
|
clippings:write(self.document.file:gsub("(.*/)(.*)", "%2").."\n")
|
2016-03-28 01:50:23 +00:00
|
|
|
clippings:write("- KOReader Highlight Page "..page.." ")
|
2014-03-13 13:52:43 +00:00
|
|
|
clippings:write("| Added on "..os.date("%A, %b %d, %Y %I:%M:%S %p\n\n"))
|
2014-05-12 12:32:17 +00:00
|
|
|
-- My Clippings only holds one line of highlight
|
|
|
|
clippings:write(item["text"]:gsub("\n", " ").."\n")
|
2014-03-13 13:52:43 +00:00
|
|
|
clippings:write("==========\n")
|
|
|
|
clippings:close()
|
|
|
|
os.setlocale(current_locale)
|
|
|
|
end
|
2013-06-16 11:00:15 +00:00
|
|
|
end
|
2014-11-21 10:32:43 +00:00
|
|
|
--]]
|
2013-06-16 11:00:15 +00:00
|
|
|
|
2014-02-01 16:16:51 +00:00
|
|
|
function ReaderHighlight:exportToDocument(page, item)
|
2020-02-25 20:26:19 +00:00
|
|
|
local setting = G_reader_settings:readSetting("save_document")
|
|
|
|
if setting == "disable" then return end
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("export highlight to document", item)
|
2020-02-25 20:26:19 +00:00
|
|
|
local can_write = self.ui.document:saveHighlight(page, item)
|
|
|
|
if can_write == false and not self.warned_once then
|
|
|
|
self.warned_once = true
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _([[
|
|
|
|
Highlights in this document will be saved in the settings file, but they won't be written in the document itself because the file is in a read-only location.
|
|
|
|
|
|
|
|
If you wish your highlights to be saved in the document, just move it to a writable directory first.]]),
|
|
|
|
timeout = 5,
|
|
|
|
})
|
|
|
|
end
|
2014-02-01 16:16:51 +00:00
|
|
|
end
|
|
|
|
|
2013-06-15 15:13:19 +00:00
|
|
|
function ReaderHighlight:addNote()
|
2019-03-12 19:14:34 +00:00
|
|
|
local page, index = self:saveHighlight()
|
|
|
|
self:editHighlight(page, index)
|
|
|
|
UIManager:close(self.edit_highlight_dialog)
|
|
|
|
self.ui:handleEvent(Event:new("AddNote"))
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2014-08-20 06:41:45 +00:00
|
|
|
function ReaderHighlight:lookupWikipedia()
|
|
|
|
if self.selected_text then
|
2020-07-21 22:14:57 +00:00
|
|
|
self.ui:handleEvent(Event:new("LookupWikipedia", cleanupSelectedText(self.selected_text.text)))
|
2014-08-20 06:41:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
function ReaderHighlight:onHighlightSearch()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("search highlight")
|
2014-11-05 04:28:11 +00:00
|
|
|
self:highlightFromHoldPos()
|
|
|
|
if self.selected_text then
|
2020-07-21 22:14:57 +00:00
|
|
|
local text = util.stripPunctuation(cleanupSelectedText(self.selected_text.text))
|
2015-03-12 09:41:20 +00:00
|
|
|
self.ui:handleEvent(Event:new("ShowSearchDialog", text))
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-15 10:11:42 +00:00
|
|
|
function ReaderHighlight:onHighlightDictLookup()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("dictionary lookup highlight")
|
2015-06-15 10:11:42 +00:00
|
|
|
self:highlightFromHoldPos()
|
|
|
|
if self.selected_text then
|
2020-07-21 22:14:57 +00:00
|
|
|
self.ui:handleEvent(Event:new("LookupWord", cleanupSelectedText(self.selected_text.text)))
|
2015-06-15 10:11:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-15 15:13:19 +00:00
|
|
|
function ReaderHighlight:shareHighlight()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.info("share highlight")
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderHighlight:moreAction()
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.info("more action")
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2017-10-18 15:19:06 +00:00
|
|
|
function ReaderHighlight:deleteHighlight(page, i, bookmark_item)
|
2017-07-27 17:00:37 +00:00
|
|
|
self.ui:handleEvent(Event:new("DelHighlight"))
|
2017-10-18 15:19:06 +00:00
|
|
|
logger.dbg("delete highlight", page, i)
|
2020-11-08 01:18:50 +00:00
|
|
|
-- The per-page table is a pure array
|
2014-11-27 13:59:27 +00:00
|
|
|
local removed = table.remove(self.view.highlight.saved[page], i)
|
2020-11-08 01:18:50 +00:00
|
|
|
-- But the main, outer table is a hash, so clear the table for this page if there are no longer any highlights on it
|
|
|
|
if #self.view.highlight.saved[page] == 0 then
|
|
|
|
self.view.highlight.saved[page] = nil
|
|
|
|
end
|
2017-10-18 15:19:06 +00:00
|
|
|
if bookmark_item then
|
|
|
|
self.ui.bookmark:removeBookmark(bookmark_item)
|
|
|
|
else
|
|
|
|
self.ui.bookmark:removeBookmark({
|
|
|
|
page = self.ui.document.info.has_pages and page or removed.pos0,
|
|
|
|
datetime = removed.datetime,
|
|
|
|
})
|
|
|
|
end
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
|
|
|
|
2017-10-18 15:19:06 +00:00
|
|
|
function ReaderHighlight:editHighlight(page, i)
|
|
|
|
local item = self.view.highlight.saved[page][i]
|
|
|
|
self.ui.bookmark:renameBookmark({
|
|
|
|
page = self.ui.document.info.has_pages and page or item.pos0,
|
|
|
|
datetime = item.datetime,
|
|
|
|
}, true)
|
2013-06-15 15:13:19 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2014-01-17 19:05:17 +00:00
|
|
|
function ReaderHighlight:onReadSettings(config)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.view.highlight.saved_drawer = config:readSetting("highlight_drawer") or self.view.highlight.saved_drawer
|
2016-10-23 14:36:50 +00:00
|
|
|
local disable_highlight = config:readSetting("highlight_disabled")
|
|
|
|
if disable_highlight == nil then
|
|
|
|
disable_highlight = G_reader_settings:readSetting("highlight_disabled") or false
|
|
|
|
end
|
|
|
|
self.view.highlight.disabled = disable_highlight
|
2020-09-24 13:17:37 +00:00
|
|
|
|
|
|
|
-- panel zoom settings isn't supported in EPUB
|
|
|
|
if self.document.info.has_pages then
|
|
|
|
self.panel_zoom_enabled = config:readSetting("panel_zoom_enabled")
|
|
|
|
if self.panel_zoom_enabled == nil then
|
|
|
|
self.panel_zoom_enabled = isPanelZoomSupported(self.ui.document.file)
|
|
|
|
end
|
|
|
|
end
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2020-08-05 22:13:26 +00:00
|
|
|
function ReaderHighlight:onUpdateHoldPanRate()
|
|
|
|
self:setupTouchZones()
|
|
|
|
end
|
|
|
|
|
2014-01-17 19:05:17 +00:00
|
|
|
function ReaderHighlight:onSaveSettings()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.doc_settings:saveSetting("highlight_drawer", self.view.highlight.saved_drawer)
|
2014-11-06 03:07:48 +00:00
|
|
|
self.ui.doc_settings:saveSetting("highlight_disabled", self.view.highlight.disabled)
|
2020-09-24 13:17:37 +00:00
|
|
|
self.ui.doc_settings:saveSetting("panel_zoom_enabled", self.panel_zoom_enabled)
|
2014-01-17 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
2014-08-20 06:41:45 +00:00
|
|
|
function ReaderHighlight:onClose()
|
|
|
|
UIManager:close(self.highlight_dialog)
|
|
|
|
-- clear highlighted text
|
2014-08-26 12:29:53 +00:00
|
|
|
self:clear()
|
2014-08-20 06:41:45 +00:00
|
|
|
end
|
|
|
|
|
2019-10-25 15:25:26 +00:00
|
|
|
function ReaderHighlight:toggleDefault()
|
|
|
|
local highlight_disabled = G_reader_settings:isTrue("highlight_disabled")
|
|
|
|
UIManager:show(MultiConfirmBox:new{
|
|
|
|
text = highlight_disabled and _("Would you like to enable or disable highlighting by default?\n\nThe current default (★) is disabled.")
|
|
|
|
or _("Would you like to enable or disable highlighting by default?\n\nThe current default (★) is enabled."),
|
|
|
|
choice1_text_func = function()
|
|
|
|
return highlight_disabled and _("Disable (★)") or _("Disable")
|
|
|
|
end,
|
|
|
|
choice1_callback = function()
|
|
|
|
G_reader_settings:saveSetting("highlight_disabled", true)
|
|
|
|
end,
|
|
|
|
choice2_text_func = function()
|
|
|
|
return highlight_disabled and _("Enable") or _("Enable (★)")
|
|
|
|
end,
|
|
|
|
choice2_callback = function()
|
|
|
|
G_reader_settings:saveSetting("highlight_disabled", false)
|
2016-10-23 14:36:50 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return ReaderHighlight
|