aboutsummaryrefslogtreecommitdiffstats
path: root/content_scripts/mode_find.coffee
diff options
context:
space:
mode:
authorStephen Blott2017-10-29 14:08:00 +0000
committerGitHub2017-10-29 14:08:00 +0000
commit52bd2cdbf662e3d40c0f5273485379be77ef8c19 (patch)
treef54999999cf42b320d6a8cd929065f54d5630c3f /content_scripts/mode_find.coffee
parent69c3e8a4b8aef152a2c994c5da922a37ac3f5cf6 (diff)
parent24148c709806d1631f35cd8efd3fdec4f2376f92 (diff)
downloadvimium-52bd2cdbf662e3d40c0f5273485379be77ef8c19.tar.bz2
Merge pull request #2748 from mrmr1993/frontend-split
Move normal mode and its commands out of vimium_frontend.coffee
Diffstat (limited to 'content_scripts/mode_find.coffee')
-rw-r--r--content_scripts/mode_find.coffee52
1 files changed, 51 insertions, 1 deletions
diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee
index 5238ab34..b6c80cec 100644
--- a/content_scripts/mode_find.coffee
+++ b/content_scripts/mode_find.coffee
@@ -79,7 +79,7 @@ class FindMode extends Mode
exit: (event) ->
super()
- handleEscapeForFindMode() if event
+ FindMode.handleEscape() if event
restoreSelection: ->
range = @initialRange
@@ -201,6 +201,34 @@ class FindMode extends Mode
@restoreDefaultSelectionHighlight: forTrusted -> document.body.classList.remove("vimiumFindMode")
+ # The user has found what they're looking for and is finished searching. We enter insert mode, if possible.
+ @handleEscape: ->
+ document.body.classList.remove("vimiumFindMode")
+ # Removing the class does not re-color existing selections. we recreate the current selection so it reverts
+ # back to the default color.
+ selection = window.getSelection()
+ unless selection.isCollapsed
+ range = window.getSelection().getRangeAt(0)
+ window.getSelection().removeAllRanges()
+ window.getSelection().addRange(range)
+ focusFoundLink() || selectFoundInputElement()
+
+ # Save the query so the user can do further searches with it.
+ @handleEnter: ->
+ focusFoundLink()
+ document.body.classList.add("vimiumFindMode")
+ FindMode.saveQuery()
+
+ @findNext: (backwards) ->
+ Marks.setPreviousPosition()
+ FindMode.query.hasResults = FindMode.execute null, {backwards}
+
+ if FindMode.query.hasResults
+ focusFoundLink()
+ new PostFindMode()
+ else
+ HUD.showForDuration("No matches for '#{FindMode.query.rawQuery}'", 1000)
+
checkReturnToViewPort: ->
window.scrollTo @scrollX, @scrollY if @options.returnToViewport
@@ -215,6 +243,28 @@ getCurrentRange = ->
selection.collapseToStart() if selection.type == "Range"
selection.getRangeAt 0
+getLinkFromSelection = ->
+ node = window.getSelection().anchorNode
+ while (node && node != document.body)
+ return node if (node.nodeName.toLowerCase() == "a")
+ node = node.parentNode
+ null
+
+focusFoundLink = ->
+ if (FindMode.query.hasResults)
+ link = getLinkFromSelection()
+ link.focus() if link
+
+selectFoundInputElement = ->
+ # Since the last focused element might not be the one currently pointed to by find (e.g. the current one
+ # might be disabled and therefore unable to receive focus), we use the approximate heuristic of checking
+ # that the last anchor node is an ancestor of our element.
+ findModeAnchorNode = document.getSelection().anchorNode
+ if (FindMode.query.hasResults && document.activeElement &&
+ DomUtils.isSelectable(document.activeElement) &&
+ DomUtils.isDOMDescendant(findModeAnchorNode, document.activeElement))
+ DomUtils.simulateSelect(document.activeElement)
+
root = exports ? (window.root ?= {})
root.PostFindMode = PostFindMode
root.FindMode = FindMode