From 4ee7eac30ca5f77affa315518bcbf1bf26b9293e Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Mon, 5 Dec 2022 09:35:56 +0200 Subject: [PATCH 1/7] Update readerhighlight.lua --- frontend/apps/reader/modules/readerhighlight.lua | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index 8f907f2bff68..cb8217354d64 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -1708,16 +1708,11 @@ function ReaderHighlight:onUnhighlight(bookmark_item) end end end - 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 + if idx then + logger.dbg("found highlight to delete on page", page, idx) + self:deleteHighlight(page, idx, bookmark_item) + return true end - logger.dbg("found highlight to delete on page", page, idx) - self:deleteHighlight(page, idx, bookmark_item) - return true end function ReaderHighlight:getHighlightBookmarkItem() @@ -1887,7 +1882,7 @@ end function ReaderHighlight:editHighlight(page, i, is_new_note, text) local item = self.view.highlight.saved[page][i] - self.ui.bookmark:renameBookmark({ + self.ui.bookmark:setBookmarkNote({ page = self.ui.document.info.has_pages and page or item.pos0, datetime = item.datetime, }, true, is_new_note, text) From 5e62128ddcd79d7255a006d6eae9a4678fb35d35 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Mon, 5 Dec 2022 09:36:55 +0200 Subject: [PATCH 2/7] readerbookmark: show current page --- .../apps/reader/modules/readerbookmark.lua | 468 ++++++++---------- 1 file changed, 206 insertions(+), 262 deletions(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index b6dd2e4b0107..b40f128d1516 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -29,6 +29,7 @@ local DISPLAY_PREFIX = { highlight = "\u{2592}\u{2002}", -- "medium shade" note = "\u{F040}\u{2002}", -- "pencil" bookmark = "\u{F097}\u{2002}", -- "empty bookmark" + curr_page = "\u{2605}\u{2002}", -- "star" } local ReaderBookmark = InputContainer:extend{ @@ -77,18 +78,22 @@ function ReaderBookmark:addToMainMenu(menu_items) } if not Device:isTouchDevice() then menu_items.toggle_bookmark = { - text_func = function() return self:isCurrentPageBookmarked() and _("Remove bookmark for current page") or _("Bookmark current page") end, + text_func = function() + return self:isCurrentPageBookmarked() and _("Remove bookmark for current page") or _("Bookmark current page") + end, callback = function() self:onToggleBookmark() end, } end - if self.ui.document.info.has_pages then + if self.ui.paging then menu_items.bookmark_browsing_mode = { text = _("Bookmark browsing mode"), - checked_func = function() return self.ui.paging.bookmark_flipping_mode end, + checked_func = function() + return self.ui.paging.bookmark_flipping_mode + end, callback = function(touchmenu_instance) - self:enableBookmarkBrowsingMode() + self:toggleBookmarkBrowsingMode() touchmenu_instance:closeMenu() end, } @@ -193,14 +198,10 @@ function ReaderBookmark:addToMainMenu(menu_items) } end -function ReaderBookmark:enableBookmarkBrowsingMode() +function ReaderBookmark:toggleBookmarkBrowsingMode() self.ui:handleEvent(Event:new("ToggleBookmarkFlipping")) end -function ReaderBookmark:isBookmarkInTimeOrder(a, b) - return a.datetime > b.datetime -end - function ReaderBookmark:isBookmarkInPositionOrder(a, b) if self.ui.paging then if a.page == b.page then -- both bookmarks in the same page @@ -258,19 +259,12 @@ function ReaderBookmark:isBookmarkInPositionOrder(a, b) end function ReaderBookmark:isBookmarkInPageOrder(a, b) - if self.ui.document.info.has_pages then - if a.page == b.page then -- have bookmarks before highlights - return a.highlighted - end - return a.page > b.page - else - local a_page = self.ui.document:getPageFromXPointer(a.page) - local b_page = self.ui.document:getPageFromXPointer(b.page) - if a_page == b_page then -- have bookmarks before highlights - return a.highlighted - end - return a_page > b_page + local a_page = self:getBookmarkPageNumber(a) + local b_page = self:getBookmarkPageNumber(b) + if a_page == b_page then -- have bookmarks before highlights + return a.highlighted end + return a_page > b_page end function ReaderBookmark:isBookmarkInReversePageOrder(a, b) @@ -278,29 +272,15 @@ function ReaderBookmark:isBookmarkInReversePageOrder(a, b) -- in reverse order), we want to skip highlights, but also the current -- page: so we do not do any "a.page == b.page" check (not even with -- a reverse logic than the one from above function). - if self.ui.document.info.has_pages then - return a.page < b.page - else - local a_page = self.ui.document:getPageFromXPointer(a.page) - local b_page = self.ui.document:getPageFromXPointer(b.page) - return a_page < b_page - end + return self:getBookmarkPageNumber(a) < self:getBookmarkPageNumber(b) end function ReaderBookmark:isBookmarkPageInPageOrder(a, b) - if self.ui.document.info.has_pages then - return a > b.page - else - return a > self.ui.document:getPageFromXPointer(b.page) - end + return a > self:getBookmarkPageNumber(b) end function ReaderBookmark:isBookmarkPageInReversePageOrder(a, b) - if self.ui.document.info.has_pages then - return a < b.page - else - return a < self.ui.document:getPageFromXPointer(b.page) - end + return a < self:getBookmarkPageNumber(b) end function ReaderBookmark:fixBookmarkSort(config) @@ -321,12 +301,12 @@ function ReaderBookmark:importSavedHighlight(config) if config:hasNot("highlights_imported") then for page, marks in pairs(textmarks) do for _, mark in ipairs(marks) do - page = self.ui.document.info.has_pages and page or mark.pos0 + mark_page = self.ui.paging and page or mark.pos0 -- highlights saved by some old versions don't have pos0 field -- we just ignore those highlights - if page then + if mark_page then self:addBookmark({ - page = page, + page = mark_page, datetime = mark.datetime, notes = mark.text, highlighted = true, @@ -337,6 +317,24 @@ function ReaderBookmark:importSavedHighlight(config) end end +function ReaderBookmark:updateHighlightsIfNeeded(config) + -- adds "chapter" property to highlights and bookmarks already saved in the document + local version = config:readSetting("bookmarks_version") or 0 + if version >= 20200615 then + return + end + for page, highlights in pairs(self.view.highlight.saved) do + for _, highlight in ipairs(highlights) do + local pn_or_xp = self.ui.paging and page or highlight.pos0 + highlight.chapter = self.ui.toc:getTocTitleByPage(pn_or_xp) + end + end + for _, bookmark in ipairs(self.bookmarks) do + local pn_or_xp = (self.ui.rolling and bookmark.pos0) and bookmark.pos0 or bookmark.page + bookmark.chapter = self.ui.toc:getTocTitleByPage(pn_or_xp) + end +end + function ReaderBookmark:onReadSettings(config) self.bookmarks = config:readSetting("bookmarks", {}) -- Bookmark formats in crengine and mupdf are incompatible. @@ -368,55 +366,38 @@ function ReaderBookmark:onReadSettings(config) self.ui:registerPostInitCallback(function() self:fixBookmarkSort(config) self:importSavedHighlight(config) + self:updateHighlightsIfNeeded(config) end) end function ReaderBookmark:onSaveSettings() self.ui.doc_settings:saveSetting("bookmarks", self.bookmarks) + self.ui.doc_settings:saveSetting("bookmarks_version", 20200615) self.ui.doc_settings:makeTrue("bookmarks_sorted") self.ui.doc_settings:makeTrue("bookmarks_sorted_20220106") self.ui.doc_settings:makeTrue("highlights_imported") end function ReaderBookmark:isCurrentPageBookmarked() - local pn_or_xp - if self.ui.document.info.has_pages then - pn_or_xp = self.view.state.page - else - pn_or_xp = self.ui.document:getXPointer() - end - return self:getDogearBookmarkIndex(pn_or_xp) and true or false + return self:getDogearBookmarkIndex(self:getCurrentPageNumber()) and true or false end function ReaderBookmark:onToggleBookmark() - local pn_or_xp - if self.ui.document.info.has_pages then - pn_or_xp = self.view.state.page - else - pn_or_xp = self.ui.document:getXPointer() - end - self:toggleBookmark(pn_or_xp) + self:toggleBookmark(self:getCurrentPageNumber()) self.view.footer:onUpdateFooter(self.view.footer_visible) - self.ui:handleEvent(Event:new("SetDogearVisibility", - not self.view.dogear_visible)) + self.ui:handleEvent(Event:new("SetDogearVisibility", not self.view.dogear_visible)) UIManager:setDirty(self.view.dialog, "ui") return true end function ReaderBookmark:setDogearVisibility(pn_or_xp) - if self:getDogearBookmarkIndex(pn_or_xp) then - self.ui:handleEvent(Event:new("SetDogearVisibility", true)) - else - self.ui:handleEvent(Event:new("SetDogearVisibility", false)) - end + local is_index = self:getDogearBookmarkIndex(pn_or_xp) and true or false + self.ui:handleEvent(Event:new("SetDogearVisibility", is_index)) end function ReaderBookmark:onPageUpdate(pageno) - if self.ui.document.info.has_pages then - self:setDogearVisibility(pageno) - else - self:setDogearVisibility(self.ui.document:getXPointer()) - end + local pn_or_xp = self.ui.paging and pageno or self.ui.document:getXPointer() + self:setDogearVisibility(pn_or_xp) end function ReaderBookmark:onPosUpdate(pos) @@ -425,52 +406,34 @@ end function ReaderBookmark:gotoBookmark(pn_or_xp, marker_xp) if pn_or_xp then - local event = self.ui.document.info.has_pages and "GotoPage" or "GotoXPointer" + local event = self.ui.paging and "GotoPage" or "GotoXPointer" self.ui:handleEvent(Event:new(event, pn_or_xp, marker_xp)) end end --- This function adds "chapter" property to highlights already saved in the document -function ReaderBookmark:updateHighlightsIfNeeded() - local version = self.ui.doc_settings:readSetting("bookmarks_version") or 0 - if version >= 20200615 then - return - end - - for page, highlights in pairs(self.view.highlight.saved) do - for _, highlight in pairs(highlights) do - local pg_or_xp = self.ui.document.info.has_pages and - page or highlight.pos0 - local chapter_name = self.ui.toc:getTocTitleByPage(pg_or_xp) - highlight.chapter = chapter_name - end - end - - for _, bookmark in ipairs(self.bookmarks) do - if bookmark.pos0 then - local pg_or_xp = self.ui.document.info.has_pages and - bookmark.page or bookmark.pos0 - local chapter_name = self.ui.toc:getTocTitleByPage(pg_or_xp) - bookmark.chapter = chapter_name - elseif bookmark.page then -- dogear bookmark - local chapter_name = self.ui.toc:getTocTitleByPage(bookmark.page) - bookmark.chapter = chapter_name - end - end - self.ui.doc_settings:saveSetting("bookmarks_version", 20200615) -end - function ReaderBookmark:onShowBookmark(match_table) self.select_mode = false self.filtered_mode = match_table and true or false - self:updateHighlightsIfNeeded() -- build up item_table local item_table = {} local is_reverse_sorting = G_reader_settings:nilOrTrue("bookmarks_items_reverse_sorting") - local curr_page = self.ui.rolling and self.ui.document:getXPointer() or self.ui.paging.current_page - curr_page = self:getBookmarkPageString(curr_page) + local curr_page_num = self:getCurrentPageNumber() + local curr_page_string = self:getBookmarkPageString(curr_page_num) + local curr_page_item = { + page = curr_page_num, + text = DISPLAY_PREFIX["curr_page"] .. _("Book current page"), + mandatory = curr_page_string, + bold = true, + } + local curr_page_index = self:getBookmarkInsertionIndexBinary(curr_page_item) + local curr_page_item_index local num = #self.bookmarks + 1 + curr_page_index = is_reverse_sorting and curr_page_index or num + 1 - curr_page_index for i = 1, #self.bookmarks do + if i == curr_page_index then + table.insert(item_table, curr_page_item) + curr_page_item_index = #item_table + end -- bookmarks are internally sorted by descending page numbers local v = self.bookmarks[is_reverse_sorting and i or num - i] if v.text == nil or v.text == "" then @@ -482,12 +445,16 @@ function ReaderBookmark:onShowBookmark(match_table) item.text_orig = item.text or item.notes item.text = DISPLAY_PREFIX[item.type] .. item.text_orig item.mandatory = self:getBookmarkPageString(item.page) - if item.mandatory == curr_page then + if item.mandatory == curr_page_string then item.bold = true end table.insert(item_table, item) end end + if not curr_page_item_index then -- curr_page_item after all bookmark items + table.insert(item_table, curr_page_item) + curr_page_item_index = #item_table + end local items_per_page = G_reader_settings:readSetting("bookmarks_items_per_page") local items_font_size = G_reader_settings:readSetting("bookmarks_items_font_size", Menu.getItemFontSize(items_per_page)) @@ -529,22 +496,27 @@ function ReaderBookmark:onShowBookmark(match_table) local bookmark = self function bm_menu:onMenuSelect(item) if self.select_mode then - if item.dim then - item.dim = nil - self.select_count = self.select_count - 1 - else - item.dim = true - self.select_count = self.select_count + 1 + if item.type then -- skip curr_page_item + if item.dim then + item.dim = nil + self.select_count = self.select_count - 1 + else + item.dim = true + self.select_count = self.select_count + 1 + end + bm_menu:updateItems() end - bm_menu:updateItems() else - bookmark.ui.link:addCurrentLocationToStack() - bookmark:gotoBookmark(item.page, item.pos0) + if item.type then + bookmark.ui.link:addCurrentLocationToStack() + bookmark:gotoBookmark(item.page, item.pos0) + end bm_menu.close_callback() end end function bm_menu:onMenuHold(item) + if not item.type then return true end -- curr_page_item local bm_view = T(_("Page: %1"), item.mandatory) .. " " .. T(_("Time: %1"), item.datetime) .. "\n\n" if item.type == "bookmark" then bm_view = bm_view .. item.text @@ -554,7 +526,8 @@ function ReaderBookmark:onShowBookmark(match_table) bm_view = bm_view .. "\n\n" .. item.text end end - self.textviewer = TextViewer:new{ + local textviewer + textviewer = TextViewer:new{ title = _("Bookmark details"), text = bm_view, justified = G_reader_settings:nilOrTrue("dict_justify"), @@ -577,7 +550,7 @@ function ReaderBookmark:onShowBookmark(match_table) end end bm_menu:switchItemTable(nil, item_table, -1) - UIManager:close(self.textviewer) + UIManager:close(textviewer) end, }) end, @@ -586,8 +559,8 @@ function ReaderBookmark:onShowBookmark(match_table) text = bookmark:getBookmarkNote(item) and _("Edit note") or _("Add note"), enabled = not self.select_mode, callback = function() - bookmark:renameBookmark(item) - UIManager:close(self.textviewer) + bookmark:setBookmarkNote(item) + UIManager:close(textviewer) end, }, }, @@ -596,23 +569,23 @@ function ReaderBookmark:onShowBookmark(match_table) text = _("Close"), is_enter_default = true, callback = function() - UIManager:close(self.textviewer) + UIManager:close(textviewer) end, }, { text = _("Go to bookmark"), enabled = not self.select_mode, callback = function() - UIManager:close(self.textviewer) - UIManager:close(bookmark.bookmark_menu) + UIManager:close(textviewer) bookmark.ui.link:addCurrentLocationToStack() bookmark:gotoBookmark(item.page, item.pos0) + bm_menu.close_callback() end, }, }, } } - UIManager:show(self.textviewer) + UIManager:show(textviewer) return true end @@ -638,7 +611,7 @@ function ReaderBookmark:onShowBookmark(match_table) local buttons = {} if self.select_mode then local actions_enabled = self.select_count > 0 - local more_selections_enabled = self.select_count < #item_table + local more_selections_enabled = self.select_count < #item_table - 1 if actions_enabled then dialog_title = T(N_("1 bookmark selected", "%1 bookmarks selected", self.select_count), self.select_count) else @@ -651,9 +624,11 @@ function ReaderBookmark:onShowBookmark(match_table) callback = function() UIManager:close(bm_dialog) for _, v in ipairs(item_table) do - v.dim = true + if v.type then -- skip curr_page_item + v.dim = true + end end - self.select_count = #item_table + self.select_count = #item_table - 1 bm_menu:updateItems() end, }, @@ -665,8 +640,9 @@ function ReaderBookmark:onShowBookmark(match_table) local item_first = (bm_menu.page - 1) * bm_menu.perpage + 1 local item_last = math.min(item_first + bm_menu.perpage - 1, #item_table) for i = item_first, item_last do - if item_table[i].dim == nil then - item_table[i].dim = true + local v = item_table[i] + if v.type and v.dim == nil then + v.dim = true self.select_count = self.select_count + 1 end end @@ -744,22 +720,18 @@ function ReaderBookmark:onShowBookmark(match_table) }, }) else - local actions_enabled = #item_table > 0 + local actions_enabled = #item_table > 1 local hl_count = 0 local nt_count = 0 local bm_count = 0 - local curr_page_bm_idx for i, v in ipairs(item_table) do if v.type == "highlight" then hl_count = hl_count + 1 elseif v.type == "note" then nt_count = nt_count + 1 - else + elseif v.type == "bookmark" then bm_count = bm_count + 1 end - if not curr_page_bm_idx and v.bold then - curr_page_bm_idx = i - end end dialog_title = T(DISPLAY_PREFIX["highlight"] .. "%1" .. " " .. DISPLAY_PREFIX["note"] .. "%2" .. " " .. @@ -804,10 +776,9 @@ function ReaderBookmark:onShowBookmark(match_table) table.insert(buttons, { { text = _("Book current page"), - enabled = curr_page_bm_idx ~= nil, callback = function() UIManager:close(bm_dialog) - bm_menu:switchItemTable(nil, item_table, curr_page_bm_idx) + bm_menu:switchItemTable(nil, item_table, curr_page_item_index) end, }, { @@ -862,15 +833,14 @@ function ReaderBookmark:onShowBookmark(match_table) bm_menu:updateItems() self:onSaveSettings() end - + + bm_menu:switchItemTable(nil, item_table, curr_page_item_index) -- show page with curr_page_item UIManager:show(self.bookmark_menu) return true end function ReaderBookmark:isBookmarkMatch(item, pn_or_xp) - -- this is not correct, but previous commit temporarily - -- reverted, see #2395 & #2394 - if self.ui.document.info.has_pages then + if self.ui.paging then return item.page == pn_or_xp else return self.ui.document:isXPointerInCurrentPage(item.page) @@ -895,7 +865,7 @@ end function ReaderBookmark:isBookmarkSame(item1, item2) if item1.notes ~= item2.notes then return false end - if self.ui.document.info.has_pages then + if self.ui.paging then return item1.pos0 and item1.pos1 and item2.pos0 and item2.pos1 and item1.pos0.page == item2.pos0.page and item1.pos0.x == item2.pos0.x and item1.pos0.y == item2.pos0.y @@ -906,24 +876,51 @@ function ReaderBookmark:isBookmarkSame(item1, item2) end end --- binary insert of sorted bookmarks -function ReaderBookmark:addBookmark(item) - local _start, _middle, _end, direction = 1, 1, #self.bookmarks, 0 +function ReaderBookmark:getBookmarkIndex(item) + for i, v in ipairs(self.bookmarks) do + if item.datetime == v.datetime and item.page == v.page then + return i + end + end +end + +function ReaderBookmark:getBookmarkIndexBinary(item) + local _start, _end, _middle = 1, #self.bookmarks while _start <= _end do - _middle = math.floor((_start + _end)/2) + _middle = bit.rshift(_start + _end, 1) + local v = self.bookmarks[_middle] + if item.datetime == v.datetime and item.page == v.page then + return _middle + elseif self:isBookmarkInPositionOrder(item, v) then + _end = _middle - 1 + else + _start = _middle + 1 + end + end +end + +function ReaderBookmark:getBookmarkInsertionIndexBinary(item) + local _start, _end, _middle, direction = 1, #self.bookmarks, 1, 0 + while _start <= _end do + _middle = bit.rshift(_start + _end, 1) if self:isBookmarkInPositionOrder(item, self.bookmarks[_middle]) then _end, direction = _middle - 1, 0 else _start, direction = _middle + 1, 1 end end - table.insert(self.bookmarks, _middle + direction, item) + return _middle + direction +end + +function ReaderBookmark:addBookmark(item) + local index = self:getBookmarkInsertionIndexBinary(item) + table.insert(self.bookmarks, index, item) self.ui:handleEvent(Event:new("BookmarkAdded", item)) self.view.footer:onUpdateFooter(self.view.footer_visible) end --- binary search of sorted bookmarks function ReaderBookmark:isBookmarkAdded(item) + -- binary search of sorted bookmarks (without check of datetime, for dictquicklookup) local _middle local _start, _end = 1, #self.bookmarks while _start <= _end do @@ -941,106 +938,63 @@ function ReaderBookmark:isBookmarkAdded(item) end function ReaderBookmark:removeHighlight(item) + self:removeBookmark(item) if item.pos0 then self.ui:handleEvent(Event:new("Unhighlight", item)) - else - self:removeBookmark(item) - -- Update dogear in case we removed a bookmark for current page - if self.ui.document.info.has_pages then - self:setDogearVisibility(self.view.state.page) - else - self:setDogearVisibility(self.ui.document:getXPointer()) - end + else -- dogear bookmark, update it in case we removed a bookmark for current page + self:setDogearVisibility(self:getCurrentPageNumber()) end end --- binary search to remove bookmark function ReaderBookmark:removeBookmark(item, reset_auto_text_only) - local _middle - local _start, _end = 1, #self.bookmarks - while _start <= _end do - _middle = math.floor((_start + _end)/2) - local v = self.bookmarks[_middle] - if item.datetime == v.datetime and item.page == v.page then - if reset_auto_text_only then - if self:isBookmarkAutoText(v) then - v.text = nil - end - else - self.ui:handleEvent(Event:new("BookmarkRemoved", v)) - table.remove(self.bookmarks, _middle) - self.view.footer:onUpdateFooter(self.view.footer_visible) - end - return - elseif self:isBookmarkInPositionOrder(item, v) then - _end = _middle - 1 - else - _start = _middle + 1 - end - end - -- If we haven't found item, it may be because there are multiple + -- If we haven't found item in binary search, it may be because there are multiple -- bookmarks on the same page, and the above binary search decided to -- not search on one side of one it found on page, where item could be. -- Fallback to do a full scan. - logger.dbg("removeBookmark: binary search didn't find bookmark, doing full scan") - for i=1, #self.bookmarks do - local v = self.bookmarks[i] - if item.datetime == v.datetime and item.page == v.page then - if reset_auto_text_only then - if self:isBookmarkAutoText(v) then - v.text = nil - end - else - self.ui:handleEvent(Event:new("BookmarkRemoved", v)) - table.remove(self.bookmarks, i) - self.view.footer:onUpdateFooter(self.view.footer_visible) - end - return + local index = self:getBookmarkIndexBinary(item) or self:getBookmarkIndex(item) + local v = self.bookmarks[index] + if reset_auto_text_only then + if self:isBookmarkAutoText(v) then + v.text = nil end + else + self.ui:handleEvent(Event:new("BookmarkRemoved", v)) + table.remove(self.bookmarks, index) + self.view.footer:onUpdateFooter(self.view.footer_visible) end - logger.warn("removeBookmark: full scan search didn't find bookmark") end function ReaderBookmark:updateBookmark(item) - for i=1, #self.bookmarks do - if item.datetime == self.bookmarks[i].datetime and item.page == self.bookmarks[i].page then - local bookmark_before = util.tableDeepCopy(self.bookmarks[i]) - local is_auto_text = self:isBookmarkAutoText(self.bookmarks[i]) - self.bookmarks[i].page = item.updated_highlight.pos0 - self.bookmarks[i].pos0 = item.updated_highlight.pos0 - self.bookmarks[i].pos1 = item.updated_highlight.pos1 - self.bookmarks[i].notes = item.updated_highlight.text - self.bookmarks[i].datetime = item.updated_highlight.datetime - self.bookmarks[i].chapter = item.updated_highlight.chapter - if is_auto_text then - self.bookmarks[i].text = self:getBookmarkAutoText(self.bookmarks[i]) - end - self.ui:handleEvent(Event:new("BookmarkUpdated", self.bookmarks[i], bookmark_before)) - self:onSaveSettings() - break - end + -- Called from Highlights when changing highlight boundaries (positions). + -- Binary search cannot be used. + local index = self:getBookmarkIndex(item) + local v = self.bookmarks[index] + local bookmark_before = util.tableDeepCopy(v) + local is_auto_text_before = self:isBookmarkAutoText(v) + v.page = item.updated_highlight.pos0 + v.pos0 = item.updated_highlight.pos0 + v.pos1 = item.updated_highlight.pos1 + v.notes = item.updated_highlight.text + v.datetime = item.updated_highlight.datetime + v.chapter = item.updated_highlight.chapter + if is_auto_text_before then + v.text = self:getBookmarkAutoText(v) end + self.ui:handleEvent(Event:new("BookmarkUpdated", v, bookmark_before)) + self:onSaveSettings() end -function ReaderBookmark:renameBookmark(item, from_highlight, is_new_note, new_text) +function ReaderBookmark:setBookmarkNote(item, from_highlight, is_new_note, new_text) local bookmark if from_highlight then - -- Called by ReaderHighlight:editHighlight, we need to find the bookmark - for __, bm in ipairs(self.bookmarks) do - if item.datetime == bm.datetime and item.page == bm.page then - if bm.text == nil or bm.text == "" then - bm.text = self:getBookmarkAutoText(bm) - end - bookmark = util.tableDeepCopy(bm) - bookmark.text_orig = bm.text or bm.notes - bookmark.mandatory = self:getBookmarkPageString(bm.page) - self.ui:handleEvent(Event:new("BookmarkEdited", bm)) - break - end - end - if not bookmark or bookmark.text_orig == nil then -- bookmark not found - return + local bm = self.bookmarks[self:getBookmarkIndex(item)] + if bm.text == nil or bm.text == "" then + bm.text = self:getBookmarkAutoText(bm) end + bookmark = util.tableDeepCopy(bm) + bookmark.text_orig = bm.text or bm.notes + bookmark.mandatory = self:getBookmarkPageString(bm.page) + self.ui:handleEvent(Event:new("BookmarkEdited", bm)) else bookmark = item end @@ -1067,12 +1021,8 @@ function ReaderBookmark:renameBookmark(item, from_highlight, is_new_note, new_te callback = function() UIManager:close(self.input) if is_new_note then -- "Add note" cancelled, remove saved highlight - for __, bm in ipairs(self.bookmarks) do - if bookmark.datetime == bm.datetime and bookmark.page == bm.page then - self:removeHighlight(bm) - break - end - end + local index = self:getBookmarkIndexBinary(bookmark) or self:getBookmarkIndex(bookmark) + self:removeHighlight(self.bookmarks[index]) end end, }, @@ -1091,14 +1041,11 @@ function ReaderBookmark:renameBookmark(item, from_highlight, is_new_note, new_te value = self:getBookmarkAutoText(bookmark) end bookmark.text = value or bookmark.notes - for __, bm in ipairs(self.bookmarks) do - if bookmark.datetime == bm.datetime and bookmark.page == bm.page then - bm.text = value - self.ui:handleEvent(Event:new("BookmarkEdited", bm)) - self.ui.highlight:writePdfAnnotation("content", bookmark.page, bookmark, bookmark.text) - break - end - end + local index = self:getBookmarkIndexBinary(bookmark) or self:getBookmarkIndex(bookmark) + local bm = self.bookmarks[index] + bm.text = value + self.ui:handleEvent(Event:new("BookmarkEdited", bm)) + self.ui.highlight:writePdfAnnotation("content", bookmark.page, bookmark, bookmark.text) UIManager:close(self.input) if from_highlight then if self.view.highlight.note_mark then @@ -1209,6 +1156,7 @@ function ReaderBookmark:onSearchBookmark(bm_menu) end function ReaderBookmark:doesBookmarkMatchTable(item, match_table) + if not item.type then return true end -- always show curr_page_item in search results if match_table[item.type] then if match_table.search_str == "" then return true @@ -1242,7 +1190,7 @@ function ReaderBookmark:toggleBookmark(pn_or_xp) page = pn_or_xp, datetime = os.date("%Y-%m-%d %H:%M:%S"), notes = notes, - chapter = chapter_name + chapter = chapter_name, }) end end @@ -1330,10 +1278,10 @@ end function ReaderBookmark:getLatestBookmark() local latest_bookmark, latest_bookmark_idx local latest_bookmark_datetime = "0" - for i = 1, #self.bookmarks do - if self.bookmarks[i].datetime > latest_bookmark_datetime then - latest_bookmark_datetime = self.bookmarks[i].datetime - latest_bookmark = self.bookmarks[i] + for i, v in ipairs(self.bookmarks) do + if v.datetime > latest_bookmark_datetime then + latest_bookmark_datetime = v.datetime + latest_bookmark = v latest_bookmark_idx = i end end @@ -1348,17 +1296,14 @@ function ReaderBookmark:getNumberOfBookmarks() return self.bookmarks and #self.bookmarks or 0 end -function ReaderBookmark:getNumberOfHighlightsAndNotes() - local highlights = 0 +function ReaderBookmark:getNumberOfHighlightsAndNotes() -- for Statistics plugin + local highlights = 0 -- Statistics show highlights+notes total amount local notes = 0 - for i = 1, #self.bookmarks do - if self.bookmarks[i].highlighted then + for _, v in ipairs(self.bookmarks) do + local bm_type = self:getBookmarkType(v) + if bm_type ~= "bookmark" then highlights = highlights + 1 - -- No real way currently to know which highlights - -- have been edited and became "notes". Editing them - -- adds this 'text' field, but just showing bookmarks - -- do that as well... - if self.bookmarks[i].text then + if bm_type == "note" then notes = notes + 1 end end @@ -1366,6 +1311,14 @@ function ReaderBookmark:getNumberOfHighlightsAndNotes() return highlights, notes end +function ReaderBookmark:getCurrentPageNumber() + return self.ui.paging and self.view.state.page or self.ui.document:getXPointer() +end + +function ReaderBookmark:getBookmarkPageNumber(bookmark) + return self.ui.paging and bookmark.page or self.ui.document:getPageFromXPointer(bookmark.page) +end + function ReaderBookmark:getBookmarkType(bookmark) if bookmark.highlighted then if self:isBookmarkAutoText(bookmark) then @@ -1399,12 +1352,7 @@ end function ReaderBookmark:getBookmarkedPages() local pages = {} for _, bm in ipairs(self.bookmarks) do - local page - if self.ui.rolling then - page = self.ui.document:getPageFromXPointer(bm.page) - else - page = bm.page - end + local page = self:getBookmarkPageNumber(bm) local btype = self:getBookmarkType(bm) if not pages[page] then pages[page] = {} @@ -1441,11 +1389,7 @@ function ReaderBookmark:getBookmarkNote(item) end function ReaderBookmark:getBookmarkForHighlight(item) - for i=1, #self.bookmarks do - if item.datetime == self.bookmarks[i].datetime and item.page == self.bookmarks[i].page then - return self.bookmarks[i] - end - end + return self.bookmarks[self:getBookmarkIndex(item)] end return ReaderBookmark From 5d9f953a986e1496f7cc85b23a33bbbf68db91a7 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Mon, 5 Dec 2022 09:53:47 +0200 Subject: [PATCH 3/7] Update readerbookmark.lua --- frontend/apps/reader/modules/readerbookmark.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index b40f128d1516..6b728cdff87c 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -301,7 +301,7 @@ function ReaderBookmark:importSavedHighlight(config) if config:hasNot("highlights_imported") then for page, marks in pairs(textmarks) do for _, mark in ipairs(marks) do - mark_page = self.ui.paging and page or mark.pos0 + local mark_page = self.ui.paging and page or mark.pos0 -- highlights saved by some old versions don't have pos0 field -- we just ignore those highlights if mark_page then @@ -833,7 +833,7 @@ function ReaderBookmark:onShowBookmark(match_table) bm_menu:updateItems() self:onSaveSettings() end - + bm_menu:switchItemTable(nil, item_table, curr_page_item_index) -- show page with curr_page_item UIManager:show(self.bookmark_menu) return true From 134dcbb17aa8952bd040e7395e613549e546eda3 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Thu, 8 Dec 2022 11:19:23 +0200 Subject: [PATCH 4/7] menu: mandatory dim --- frontend/ui/widget/menu.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frontend/ui/widget/menu.lua b/frontend/ui/widget/menu.lua index 017f1e685a2b..ffef40ac5f1f 100644 --- a/frontend/ui/widget/menu.lua +++ b/frontend/ui/widget/menu.lua @@ -196,7 +196,7 @@ function MenuItem:init() text = mandatory, face = self.info_face, bold = self.bold, - fgcolor = self.dim and Blitbuffer.COLOR_DARK_GRAY or nil, + fgcolor = self.mandatory_dim and Blitbuffer.COLOR_DARK_GRAY or nil, } local mandatory_w = mandatory_widget:getWidth() @@ -1060,6 +1060,7 @@ function Menu:updateItems(select_number) bidi_wrap_func = self.item_table[i].bidi_wrap_func, mandatory = self.item_table[i].mandatory, mandatory_func = self.item_table[i].mandatory_func, + mandatory_dim = self.item_table[i].mandatory_dim or self.item_table[i].dim, bold = self.item_table.current == i or self.item_table[i].bold == true, dim = self.item_table[i].dim, font = "smallinfofont", From cc3ff12b06fd50732149c3407728cfed99f0261b Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Thu, 8 Dec 2022 11:21:05 +0200 Subject: [PATCH 5/7] readerbookmark: mandatory dim --- .../apps/reader/modules/readerbookmark.lua | 77 ++++++++----------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index 6b728cdff87c..29397b6a4966 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -29,7 +29,6 @@ local DISPLAY_PREFIX = { highlight = "\u{2592}\u{2002}", -- "medium shade" note = "\u{F040}\u{2002}", -- "pencil" bookmark = "\u{F097}\u{2002}", -- "empty bookmark" - curr_page = "\u{2605}\u{2002}", -- "star" } local ReaderBookmark = InputContainer:extend{ @@ -419,21 +418,10 @@ function ReaderBookmark:onShowBookmark(match_table) local is_reverse_sorting = G_reader_settings:nilOrTrue("bookmarks_items_reverse_sorting") local curr_page_num = self:getCurrentPageNumber() local curr_page_string = self:getBookmarkPageString(curr_page_num) - local curr_page_item = { - page = curr_page_num, - text = DISPLAY_PREFIX["curr_page"] .. _("Book current page"), - mandatory = curr_page_string, - bold = true, - } - local curr_page_index = self:getBookmarkInsertionIndexBinary(curr_page_item) - local curr_page_item_index + local curr_page_index = self:getBookmarkInsertionIndexBinary({page = curr_page_num}) - 1 local num = #self.bookmarks + 1 - curr_page_index = is_reverse_sorting and curr_page_index or num + 1 - curr_page_index + curr_page_index = is_reverse_sorting and curr_page_index or num - curr_page_index for i = 1, #self.bookmarks do - if i == curr_page_index then - table.insert(item_table, curr_page_item) - curr_page_item_index = #item_table - end -- bookmarks are internally sorted by descending page numbers local v = self.bookmarks[is_reverse_sorting and i or num - i] if v.text == nil or v.text == "" then @@ -445,16 +433,18 @@ function ReaderBookmark:onShowBookmark(match_table) item.text_orig = item.text or item.notes item.text = DISPLAY_PREFIX[item.type] .. item.text_orig item.mandatory = self:getBookmarkPageString(item.page) + if (not is_reverse_sorting and i >= curr_page_index) or (is_reverse_sorting and i <= curr_page_index) then + item.after_curr_page = true + item.mandatory_dim = true + end if item.mandatory == curr_page_string then item.bold = true + item.after_curr_page = nil + item.mandatory_dim = nil end table.insert(item_table, item) end end - if not curr_page_item_index then -- curr_page_item after all bookmark items - table.insert(item_table, curr_page_item) - curr_page_item_index = #item_table - end local items_per_page = G_reader_settings:readSetting("bookmarks_items_per_page") local items_font_size = G_reader_settings:readSetting("bookmarks_items_font_size", Menu.getItemFontSize(items_per_page)) @@ -492,31 +482,29 @@ function ReaderBookmark:onShowBookmark(match_table) } table.insert(self.bookmark_menu, bm_menu) - -- buid up menu widget method as closure local bookmark = self + function bm_menu:onMenuSelect(item) if self.select_mode then - if item.type then -- skip curr_page_item - if item.dim then - item.dim = nil - self.select_count = self.select_count - 1 - else - item.dim = true - self.select_count = self.select_count + 1 + if item.dim then + item.dim = nil + if item.after_curr_page then + item.mandatory_dim = true end - bm_menu:updateItems() + self.select_count = self.select_count - 1 + else + item.dim = true + self.select_count = self.select_count + 1 end + bm_menu:updateItems() else - if item.type then - bookmark.ui.link:addCurrentLocationToStack() - bookmark:gotoBookmark(item.page, item.pos0) - end + bookmark.ui.link:addCurrentLocationToStack() + bookmark:gotoBookmark(item.page, item.pos0) bm_menu.close_callback() end end function bm_menu:onMenuHold(item) - if not item.type then return true end -- curr_page_item local bm_view = T(_("Page: %1"), item.mandatory) .. " " .. T(_("Time: %1"), item.datetime) .. "\n\n" if item.type == "bookmark" then bm_view = bm_view .. item.text @@ -599,9 +587,12 @@ function ReaderBookmark:onShowBookmark(match_table) if v.dim then v.dim = nil end + if v.after_curr_page then + v.mandatory_dim = true + end end bm_menu:switchItemTable(bookmark.filtered_mode and _("Bookmarks (search results)") - or _("Bookmarks"), item_table) + or _("Bookmarks"), item_table, curr_page_index) bm_menu:setTitleBarLeftIcon("appbar.menu") end end @@ -611,7 +602,7 @@ function ReaderBookmark:onShowBookmark(match_table) local buttons = {} if self.select_mode then local actions_enabled = self.select_count > 0 - local more_selections_enabled = self.select_count < #item_table - 1 + local more_selections_enabled = self.select_count < #item_table if actions_enabled then dialog_title = T(N_("1 bookmark selected", "%1 bookmarks selected", self.select_count), self.select_count) else @@ -624,11 +615,9 @@ function ReaderBookmark:onShowBookmark(match_table) callback = function() UIManager:close(bm_dialog) for _, v in ipairs(item_table) do - if v.type then -- skip curr_page_item - v.dim = true - end + v.dim = true end - self.select_count = #item_table - 1 + self.select_count = #item_table bm_menu:updateItems() end, }, @@ -641,7 +630,7 @@ function ReaderBookmark:onShowBookmark(match_table) local item_last = math.min(item_first + bm_menu.perpage - 1, #item_table) for i = item_first, item_last do local v = item_table[i] - if v.type and v.dim == nil then + if v.dim == nil then v.dim = true self.select_count = self.select_count + 1 end @@ -660,6 +649,9 @@ function ReaderBookmark:onShowBookmark(match_table) if v.dim then v.dim = nil end + if v.after_curr_page then + v.mandatory_dim = true + end end self.select_count = 0 bm_menu:updateItems() @@ -720,7 +712,7 @@ function ReaderBookmark:onShowBookmark(match_table) }, }) else - local actions_enabled = #item_table > 1 + local actions_enabled = #item_table > 0 local hl_count = 0 local nt_count = 0 local bm_count = 0 @@ -778,7 +770,7 @@ function ReaderBookmark:onShowBookmark(match_table) text = _("Book current page"), callback = function() UIManager:close(bm_dialog) - bm_menu:switchItemTable(nil, item_table, curr_page_item_index) + bm_menu:switchItemTable(nil, item_table, curr_page_index) end, }, { @@ -834,7 +826,7 @@ function ReaderBookmark:onShowBookmark(match_table) self:onSaveSettings() end - bm_menu:switchItemTable(nil, item_table, curr_page_item_index) -- show page with curr_page_item + bm_menu:switchItemTable(nil, item_table, curr_page_index) UIManager:show(self.bookmark_menu) return true end @@ -1156,7 +1148,6 @@ function ReaderBookmark:onSearchBookmark(bm_menu) end function ReaderBookmark:doesBookmarkMatchTable(item, match_table) - if not item.type then return true end -- always show curr_page_item in search results if match_table[item.type] then if match_table.search_str == "" then return true From 580fbb066109a1a5313ffc7e3cefa9ae390164da Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Fri, 9 Dec 2022 00:43:53 +0200 Subject: [PATCH 6/7] getBookmarkIndexBinarySearch, getBookmarkIndexFullScan and others --- .../apps/reader/modules/readerbookmark.lua | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index 29397b6a4966..cf5548e36f28 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -78,7 +78,7 @@ function ReaderBookmark:addToMainMenu(menu_items) if not Device:isTouchDevice() then menu_items.toggle_bookmark = { text_func = function() - return self:isCurrentPageBookmarked() and _("Remove bookmark for current page") or _("Bookmark current page") + return self:isPageBookmarked() and _("Remove bookmark for current page") or _("Bookmark current page") end, callback = function() self:onToggleBookmark() @@ -377,21 +377,21 @@ function ReaderBookmark:onSaveSettings() self.ui.doc_settings:makeTrue("highlights_imported") end -function ReaderBookmark:isCurrentPageBookmarked() - return self:getDogearBookmarkIndex(self:getCurrentPageNumber()) and true or false -end - function ReaderBookmark:onToggleBookmark() - self:toggleBookmark(self:getCurrentPageNumber()) + self:toggleBookmark() self.view.footer:onUpdateFooter(self.view.footer_visible) self.ui:handleEvent(Event:new("SetDogearVisibility", not self.view.dogear_visible)) UIManager:setDirty(self.view.dialog, "ui") return true end +function ReaderBookmark:isPageBookmarked(pn_or_xp) + local page = pn_or_xp or self:getCurrentPageNumber() + return self:getDogearBookmarkIndex(page) and true or false +end + function ReaderBookmark:setDogearVisibility(pn_or_xp) - local is_index = self:getDogearBookmarkIndex(pn_or_xp) and true or false - self.ui:handleEvent(Event:new("SetDogearVisibility", is_index)) + self.ui:handleEvent(Event:new("SetDogearVisibility", self:isPageBookmarked(pn_or_xp))) end function ReaderBookmark:onPageUpdate(pageno) @@ -421,6 +421,7 @@ function ReaderBookmark:onShowBookmark(match_table) local curr_page_index = self:getBookmarkInsertionIndexBinary({page = curr_page_num}) - 1 local num = #self.bookmarks + 1 curr_page_index = is_reverse_sorting and curr_page_index or num - curr_page_index + curr_page_index_filtered = curr_page_index for i = 1, #self.bookmarks do -- bookmarks are internally sorted by descending page numbers local v = self.bookmarks[is_reverse_sorting and i or num - i] @@ -443,6 +444,8 @@ function ReaderBookmark:onShowBookmark(match_table) item.mandatory_dim = nil end table.insert(item_table, item) + else + curr_page_index_filtered = curr_page_index_filtered - 1 end end @@ -592,7 +595,7 @@ function ReaderBookmark:onShowBookmark(match_table) end end bm_menu:switchItemTable(bookmark.filtered_mode and _("Bookmarks (search results)") - or _("Bookmarks"), item_table, curr_page_index) + or _("Bookmarks"), item_table, curr_page_index_filtered) bm_menu:setTitleBarLeftIcon("appbar.menu") end end @@ -767,10 +770,10 @@ function ReaderBookmark:onShowBookmark(match_table) table.insert(buttons, {}) table.insert(buttons, { { - text = _("Book current page"), + text = _("Current page"), callback = function() UIManager:close(bm_dialog) - bm_menu:switchItemTable(nil, item_table, curr_page_index) + bm_menu:switchItemTable(nil, item_table, curr_page_index_filtered) end, }, { @@ -826,7 +829,7 @@ function ReaderBookmark:onShowBookmark(match_table) self:onSaveSettings() end - bm_menu:switchItemTable(nil, item_table, curr_page_index) + bm_menu:switchItemTable(nil, item_table, curr_page_index_filtered) UIManager:show(self.bookmark_menu) return true end @@ -868,7 +871,7 @@ function ReaderBookmark:isBookmarkSame(item1, item2) end end -function ReaderBookmark:getBookmarkIndex(item) +function ReaderBookmark:getBookmarkIndexFullScan(item) for i, v in ipairs(self.bookmarks) do if item.datetime == v.datetime and item.page == v.page then return i @@ -876,7 +879,7 @@ function ReaderBookmark:getBookmarkIndex(item) end end -function ReaderBookmark:getBookmarkIndexBinary(item) +function ReaderBookmark:getBookmarkIndexBinarySearch(item) local _start, _end, _middle = 1, #self.bookmarks while _start <= _end do _middle = bit.rshift(_start + _end, 1) @@ -943,7 +946,7 @@ function ReaderBookmark:removeBookmark(item, reset_auto_text_only) -- bookmarks on the same page, and the above binary search decided to -- not search on one side of one it found on page, where item could be. -- Fallback to do a full scan. - local index = self:getBookmarkIndexBinary(item) or self:getBookmarkIndex(item) + local index = self:getBookmarkIndexBinarySearch(item) or self:getBookmarkIndexFullScan(item) local v = self.bookmarks[index] if reset_auto_text_only then if self:isBookmarkAutoText(v) then @@ -959,7 +962,7 @@ end function ReaderBookmark:updateBookmark(item) -- Called from Highlights when changing highlight boundaries (positions). -- Binary search cannot be used. - local index = self:getBookmarkIndex(item) + local index = self:getBookmarkIndexFullScan(item) local v = self.bookmarks[index] local bookmark_before = util.tableDeepCopy(v) local is_auto_text_before = self:isBookmarkAutoText(v) @@ -979,7 +982,7 @@ end function ReaderBookmark:setBookmarkNote(item, from_highlight, is_new_note, new_text) local bookmark if from_highlight then - local bm = self.bookmarks[self:getBookmarkIndex(item)] + local bm = self.bookmarks[self:getBookmarkIndexFullScan(item)] if bm.text == nil or bm.text == "" then bm.text = self:getBookmarkAutoText(bm) end @@ -1013,7 +1016,7 @@ function ReaderBookmark:setBookmarkNote(item, from_highlight, is_new_note, new_t callback = function() UIManager:close(self.input) if is_new_note then -- "Add note" cancelled, remove saved highlight - local index = self:getBookmarkIndexBinary(bookmark) or self:getBookmarkIndex(bookmark) + local index = self:getBookmarkIndexBinarySearch(bookmark) or self:getBookmarkIndexFullScan(bookmark) self:removeHighlight(self.bookmarks[index]) end end, @@ -1033,7 +1036,7 @@ function ReaderBookmark:setBookmarkNote(item, from_highlight, is_new_note, new_t value = self:getBookmarkAutoText(bookmark) end bookmark.text = value or bookmark.notes - local index = self:getBookmarkIndexBinary(bookmark) or self:getBookmarkIndex(bookmark) + local index = self:getBookmarkIndexBinarySearch(bookmark) or self:getBookmarkIndexFullScan(bookmark) local bm = self.bookmarks[index] bm.text = value self.ui:handleEvent(Event:new("BookmarkEdited", bm)) @@ -1164,7 +1167,8 @@ function ReaderBookmark:doesBookmarkMatchTable(item, match_table) end end -function ReaderBookmark:toggleBookmark(pn_or_xp) +function ReaderBookmark:toggleBookmark() + local pn_or_xp = self:getCurrentPageNumber() local index = self:getDogearBookmarkIndex(pn_or_xp) if index then self.ui:handleEvent(Event:new("BookmarkRemoved", self.bookmarks[index])) @@ -1380,7 +1384,7 @@ function ReaderBookmark:getBookmarkNote(item) end function ReaderBookmark:getBookmarkForHighlight(item) - return self.bookmarks[self:getBookmarkIndex(item)] + return self.bookmarks[self:getBookmarkIndexFullScan(item)] end return ReaderBookmark From 8c60319a2184366429efd093e360ce591447262f Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Fri, 9 Dec 2022 00:59:30 +0200 Subject: [PATCH 7/7] Update readerbookmark.lua --- frontend/apps/reader/modules/readerbookmark.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index cf5548e36f28..28d3c323a0e8 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -421,7 +421,7 @@ function ReaderBookmark:onShowBookmark(match_table) local curr_page_index = self:getBookmarkInsertionIndexBinary({page = curr_page_num}) - 1 local num = #self.bookmarks + 1 curr_page_index = is_reverse_sorting and curr_page_index or num - curr_page_index - curr_page_index_filtered = curr_page_index + local curr_page_index_filtered = curr_page_index for i = 1, #self.bookmarks do -- bookmarks are internally sorted by descending page numbers local v = self.bookmarks[is_reverse_sorting and i or num - i]