From 8891624add1493840207a422a597d36fdbab13f7 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 11 Dec 2016 15:14:04 +0100 Subject: [PATCH] Improve hint marker position for multiline text --- extension/lib/markable-elements.coffee | 16 +++++++++------- extension/lib/marker.coffee | 9 ++++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/extension/lib/markable-elements.coffee b/extension/lib/markable-elements.coffee index 9948870..b0ea096 100644 --- a/extension/lib/markable-elements.coffee +++ b/extension/lib/markable-elements.coffee @@ -127,14 +127,16 @@ getRects = (element, viewport) -> # - `area`: The area of the part of the element that is inside the viewport. # - `width`: The width of the visible rect at `nonCoveredPoint`. # - `textOffset`: The distance between the left edge of the element and the left -# edge of its text vertically near `nonCoveredPoint`. If the element is a -# block and has several lines of text, this is set to `null` so that the -# marker is placed at the edge of the block. That’s the case for “cards” with -# an image to the left and a title as well as some text to the right (where -# the entire “card” is a link). +# edge of its text vertically near `nonCoveredPoint`. Might be `null`. The +# calculation might stop early if `isBlock`. +# - `isBlock`: `true` if the element is a block and has several lines of text +# (which is the case for “cards” with an image to the left and a title as well +# as some text to the right (where the entire “card” is a link)). This is used +# to place the the marker at the edge of the block. getElementShape = (elementData, tryRight, rects = null) -> {viewport, element} = elementData - result = {nonCoveredPoint: null, area: 0, width: 0, textOffset: null} + result = + {nonCoveredPoint: null, area: 0, width: 0, textOffset: null, isBlock: false} rects ?= getRects(element, viewport) totalArea = 0 @@ -198,7 +200,7 @@ getElementShape = (elementData, tryRight, rects = null) -> # the `textOffset` (see the description of `textOffset` at the # beginning of the function). if bounds.top > smallestBottom - lefts = [] + result.isBlock = true return true if bounds.bottom < smallestBottom diff --git a/extension/lib/marker.coffee b/extension/lib/marker.coffee index 1f03d69..3da23ab 100644 --- a/extension/lib/marker.coffee +++ b/extension/lib/marker.coffee @@ -58,7 +58,7 @@ class Marker # into the DOM, and thus gotten a width and height. setPosition: -> { - textOffset + textOffset, isBlock width: elementWidth nonCoveredPoint: {x: left, y: top, offset} } = @elementShape @@ -78,8 +78,11 @@ class Marker top *= @zoom if textOffset? - # Move the marker just to the left of the text of its element. - left += textOffset * @zoom - rect.width + # Move the marker just to the left of the text of its element, unless the + # element is a “block” (see `getElementShape` in markable-elements.coffee) + # and the marker wouldn’t cover text. + unless isBlock and rect.width < textOffset + left += textOffset * @zoom - rect.width else # Otherwise make sure that it doesn’t flow outside the right side of its # element. This is to avoid the following situation (where `+` is a small -- 2.39.3