Skip to content

Commit

Permalink
Improve message header clicking behavior (#598)
Browse files Browse the repository at this point in the history
* Avoid calling `preventDefault` when a subitem gets clicked.
  This was breaking the Replace File button.
* Ignore clicks that were part of a text selection drag
  • Loading branch information
edemaine committed Apr 21, 2022
1 parent b751580 commit e9d20bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ To see every change with descriptions aimed at developers, see
As a continuously updated web app, Coauthor uses dates
instead of version numbers.

## 2022-04-21

* Fix "Replace File" button and improve behavior of clicking on message header
to highlight corresponding item in Table of Contents.
[[#598](https://github.com/edemaine/coauthor/issues/598)]

## 2022-04-11

* Hovering over a message highlights the corresponding item
Expand Down
21 changes: 18 additions & 3 deletions client/message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,19 @@ Submessage.displayName = 'Submessage'

messageInView = new ReactiveDict

## Maintain whether selection changed nontrivially recently, which causes
## the following click to be ignored by showTOC.
skipClick = false
document.addEventListener 'selectionchange', ->
selection = document.getSelection()
for i in [0...selection.rangeCount]
range = selection.getRangeAt i
unless range.startContainer == range.endContainer and
range.startOffset == range.endOffset
skipClick = true
return
window.addEventListener 'click', -> skipClick = false

submessageCount = 0
export WrappedSubmessage = React.memo ({message, read}) ->
here = useTracker ->
Expand Down Expand Up @@ -2400,7 +2413,6 @@ export WrappedSubmessage = React.memo ({message, read}) ->
## Scroll the table of contents (if visible) to align with this message (if
## possible), and pulse the table of contents item (for when not possible).
showTOC = (e) ->
e.preventDefault()
## Find corresponding table of contents entry
toc = document.querySelector 'nav.contents'
return unless toc? # not in a view with table of contents
Expand All @@ -2416,8 +2428,11 @@ export WrappedSubmessage = React.memo ({message, read}) ->
item.classList.remove 'hover'
tocHoverIndicator()
when 'click'
## Ignore propagated click events e.g. from Action dropdown button.
return unless e.target.className.startsWith 'panel-heading'
return if skipClick # ignore selecting clicks
## Ignore propagated click events e.g. from Action dropdown button
## or from Replace File hidden <input>.
return unless /panel-heading|panel-title/.test e.target.className
e.preventDefault()
## Scroll to align TOC item with message header
toc.scroll
top: itemTop - msgTop - 9 # fudge factor
Expand Down

0 comments on commit e9d20bc

Please sign in to comment.