diff --git a/frontend/apps/reader/modules/readerannotation.lua b/frontend/apps/reader/modules/readerannotation.lua index b88446580..43aed436c 100644 --- a/frontend/apps/reader/modules/readerannotation.lua +++ b/frontend/apps/reader/modules/readerannotation.lua @@ -16,7 +16,7 @@ function ReaderAnnotation:buildAnnotation(bm, highlights, init) note = nil end local chapter = bm.chapter - local hl, pageno = self:getHighlightByDatetime(highlights, bm.datetime) + local hl, pageno = self:getHighlightForBookmark(highlights, bm) if init then if note and self.ui.bookmark:isBookmarkAutoText(bm) then note = nil @@ -31,7 +31,7 @@ function ReaderAnnotation:buildAnnotation(bm, highlights, init) bm.pos0.page = bm.page bm.pos1.page = bm.page end - if not hl then -- page bookmark or orphaned bookmark + if hl == nil then -- page bookmark or orphaned bookmark hl = {} if bm.highlighted then -- orphaned bookmark hl.drawer = self.view.highlight.saved_drawer @@ -62,10 +62,12 @@ function ReaderAnnotation:buildAnnotation(bm, highlights, init) } end -function ReaderAnnotation:getHighlightByDatetime(highlights, datetime) +function ReaderAnnotation:getHighlightForBookmark(highlights, bookmark) + if not bookmark.highlighted then return end -- page bookmark + local doesMatch = self:getMatchFunc() for pageno, page_highlights in pairs(highlights) do for _, highlight in ipairs(page_highlights) do - if highlight.datetime == datetime then + if doesMatch(highlight, bookmark) then return highlight, pageno end end @@ -113,10 +115,10 @@ function ReaderAnnotation:onReadSettings(config) if needs_update or needs_sort then if self.ui.rolling then self.ui:registerPostInitCallback(function() - self:updatedAnnotations(needs_update, needs_sort) + self:updateAnnotations(needs_update, needs_sort) end) else - self:updatedAnnotations(needs_update, needs_sort) + self:updateAnnotations(needs_update, needs_sort) end config:delSetting("annotations_externally_modified") end @@ -242,7 +244,7 @@ function ReaderAnnotation:sortItems(items) end end -function ReaderAnnotation:updatedAnnotations(needs_update, needs_sort) +function ReaderAnnotation:updateAnnotations(needs_update, needs_sort) if needs_update then self.needs_update = true self:updatePageNumbers() @@ -270,24 +272,24 @@ function ReaderAnnotation:isItemInPositionOrderRolling(a, b) local a_page = self.document:getPageFromXPointer(a.page) local b_page = self.document:getPageFromXPointer(b.page) if a_page == b_page then -- both items in the same page - if a.drawer and b.drawer then -- both items are highlights, compare positions - local compare_xp = self.document:compareXPointers(a.page, b.page) - if compare_xp then - if compare_xp == 0 then -- both highlights with the same start, compare ends - compare_xp = self.document:compareXPointers(a.pos1, b.pos1) - if compare_xp then - return compare_xp > 0 - end - logger.warn("Invalid xpointer in highlight:", a.pos1, b.pos1) - return true - end - return compare_xp > 0 - end - -- if compare_xp is nil, some xpointer is invalid and "a" will be sorted first to page 1 - logger.warn("Invalid xpointer in highlight:", a.page, b.page) - return true + if (not a.drawer) ~= (not b.drawer) then -- comparing a page bookmark and a highlight + return not a.drawer -- have page bookmarks before highlights end - return not a.drawer -- have page bookmarks before highlights + local compare_xp = self.document:compareXPointers(a.page, b.page) + if compare_xp then + if a.drawer and compare_xp == 0 then -- both highlights with the same start, compare ends + compare_xp = self.document:compareXPointers(a.pos1, b.pos1) + if compare_xp then + return compare_xp > 0 + end + logger.warn("Invalid xpointer in highlight:", a.pos1, b.pos1) + return true + end + return compare_xp > 0 + end + -- if compare_xp is nil, some xpointer is invalid and "a" will be sorted first to page 1 + logger.warn("Invalid xpointer in highlight:", a.page, b.page) + return true end return a_page < b_page end @@ -324,31 +326,36 @@ function ReaderAnnotation:isItemInPositionOrderPaging(a, b) return a.page < b.page end -function ReaderAnnotation:getItemIndex(item, no_binary) +function ReaderAnnotation:getMatchFunc() local doesMatch - if item.datetime then + if self.ui.rolling then doesMatch = function(a, b) - return a.datetime == b.datetime + if a.datetime ~= nil and b.datetime ~= nil and a.datetime ~= b.datetime then + return false + end + if a.text ~= b.text or a.pos0 ~= b.pos0 or a.pos1 ~= b.pos1 then + return false + end + return true end else - if self.ui.rolling then - doesMatch = function(a, b) - if a.text ~= b.text or a.pos0 ~= b.pos0 or a.pos1 ~= b.pos1 then - return false - end - return true + doesMatch = function(a, b) + if a.datetime ~= nil and b.datetime ~= nil and a.datetime ~= b.datetime then + return false end - else - doesMatch = function(a, b) - if a.text ~= b.text or a.pos0.page ~= b.pos0.page - or a.pos0.x ~= b.pos0.x or a.pos1.x ~= b.pos1.x - or a.pos0.y ~= b.pos0.y or a.pos1.y ~= b.pos1.y then - return false - end - return true + if a.text ~= b.text or a.pos0.page ~= b.pos0.page + or a.pos0.x ~= b.pos0.x or a.pos1.x ~= b.pos1.x + or a.pos0.y ~= b.pos0.y or a.pos1.y ~= b.pos1.y then + return false end + return true end end + return doesMatch +end + +function ReaderAnnotation:getItemIndex(item, no_binary) + local doesMatch = self:getMatchFunc() if not no_binary then local isInOrder = self.ui.rolling and self.isItemInPositionOrderRolling or self.isItemInPositionOrderPaging