From 455ee7fcdea7baf1aeaed67603ec87004c1c8cce Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 15 Jan 2015 11:35:09 +0000 Subject: Modes; yet more teaks and fiddles. --- content_scripts/mode_find.coffee | 54 ++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'content_scripts/mode_find.coffee') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index e79bc0dd..f151b8cd 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -1,32 +1,33 @@ # NOTE(smblott). Ultimately, all of the FindMode-related code should be moved to this file. -# When we use find mode, the selection/focus can end up in a focusable/editable element. In this situation, +# When we use find mode, the selection/focus can land in a focusable/editable element. In this situation, # special considerations apply. We implement three special cases: -# 1. Prevent keyboard events from dropping us unintentionally into insert mode. -# 2. Prevent all printable keypress events on the active element from propagating beyond normal mode. See -# #1415. +# 1. Disable keyboard events in insert mode, because the user hasn't asked to enter insert mode. +# 2. Prevent printable keyboard events from propagating to the page; see #1415. # 3. If the very-next keystroke is Escape, then drop immediately into insert mode. # -class PostFindMode extends UIMode +class PostFindMode extends InputController constructor: (findModeAnchorNode) -> - # Locate the element we need to protect and focus it, if necessary. Usually, we can just rely on insert - # mode to have picked it up (when it received the focus). - element = InsertMode.permanentInstance.insertModeLock - unless element? - # For contentEditable elements, chrome does not leave them focused, so insert mode does not pick them - # up. We start at findModeAnchorNode and walk up the DOM, stopping at the last node encountered which is - # contentEditable. - element = findModeAnchorNode - element = element.parentElement while element?.parentElement?.isContentEditable - return unless element?.isContentEditable - # The element might be disabled (and therefore unable to receive focus), we use the approximate - # heuristic of checking that element is an ancestor of the active element. - return unless document.activeElement and DomUtils.isDOMDescendant document.activeElement, element - element.focus() + # Locate the element we need to protect. In most cases, it's just the active element. + element = + if document.activeElement and DomUtils.isEditable document.activeElement + document.activeElement + else + # For contentEditable elements, chrome does not focus them, although they are activated by keystrokes. + # We need to find the element ourselves. + element = findModeAnchorNode + element = element.parentElement while element.parentElement?.isContentEditable + if element.isContentEditable + if DomUtils.isDOMDescendant element, findModeAnchorNode + # TODO(smblott). We shouldn't really need to focus the element, here. Need to look into why this + # is necessary. + element.focus() + element + + return unless element super name: "post-find" - badge: "N" # Pretend to be normal mode (because we don't want the insert-mode badge). exitOnBlur: element exitOnClick: true keydown: (event) -> InsertMode.suppressEvent event # Truthy. @@ -35,7 +36,7 @@ class PostFindMode extends UIMode @alwaysContinueBubbling => if document.getSelection().type != "Range" # If the selection is no longer a range, then the user is interacting with the element, so get out - # of the way and stop suppressing insert mode. See discussion of Option 5c from #1415. + # of the way. See Option 5c from #1415. @exit() else InsertMode.suppressEvent event @@ -45,7 +46,7 @@ class PostFindMode extends UIMode @push _name: "mode-#{@id}/handle-escape" keydown: (event) -> - if document.activeElement == element and KeyboardUtils.isEscape event + if KeyboardUtils.isEscape event DomUtils.suppressKeyupAfterEscape handlerStack self.exit() false # Suppress event. @@ -53,7 +54,7 @@ class PostFindMode extends UIMode @remove() true # Continue bubbling. - # Prevent printable keyboard events from propagating to to the page; see #1415. + # Prevent printable keyboard events from propagating to the page; see #1415. do => handler = (event) => if event.srcElement == element and KeyboardUtils.isPrintable event @@ -69,10 +70,9 @@ class PostFindMode extends UIMode keypress: handler keyup: handler -# NOTE. There's a problem with this approach when a find/search lands in a contentEditable element. Chrome -# generates a focus event triggering insert mode (good), then immediately generates a "blur" event, disabling -# insert mode again. Nevertheless, unmapped keys *do* result in the element being focused again. -# So, asking insert mode whether it's active is giving us the wrong answer. + chooseBadge: (badge) -> + # If PostFindMode is active, then we don't want the "I" badge from insert mode. + InsertMode.suppressEvent badge root = exports ? window root.PostFindMode = PostFindMode -- cgit v1.2.3