From c21aa9ab03d5537d575d3c46d3affce7f3ece5e5 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 13 Jun 2015 12:55:01 +0100 Subject: Refactor findInPlace. This code belongs together, so we put it together. --- content_scripts/hud.coffee | 4 +--- content_scripts/mode_find.coffee | 5 ++++- content_scripts/mode_visual_edit.coffee | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index cd34efe8..2f4e008c 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -33,9 +33,7 @@ HUD = @tween.fade 1.0, 150 search: (data) -> - window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport - FindMode.updateQuery data.query - @findMode.findInPlace() + @findMode.findInPlace data.query # Show the number of matches in the HUD UI. matchCount = if FindMode.query.parsedQuery.length > 0 then FindMode.query.matchCount else 0 diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index ff9506e7..21918be2 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -84,7 +84,10 @@ class FindMode extends Mode selection.removeAllRanges() selection.addRange range - findInPlace: -> + findInPlace: (query) -> + # If requested, restore the scroll position (so that failed searches leave the scroll position unchanged). + window.scrollTo @scrollX, @scrollY if @options.returnToViewport + FindMode.updateQuery query # Restore the selection. That way, we're always searching forward from the same place, so we find the right # match as the user adds matching characters, or removes previously-matched characters. See #1434. @restoreSelection() diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index eaaf94d4..170071ac 100644 --- a/content_scripts/mode_visual_edit.coffee +++ b/content_scripts/mode_visual_edit.coffee @@ -353,7 +353,7 @@ class Movement extends CountPrefix do => doFind = (count, backwards) => initialRange = @selection.getRangeAt(0).cloneRange() - for [0...count] by 1 + for [0...count] unless FindMode.execute null, {colorSelection: false, backwards} @setSelectionRange initialRange HUD.showForDuration("No matches for '#{FindMode.query.rawQuery}'", 1000) -- cgit v1.2.3 From f34f842417bdac363fac5a3e47366b3ee94c2454 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 13 Jun 2015 12:59:17 +0100 Subject: Fix returnToViewport. The super-class's constructor sets @options, so we can't set it here; instead, we pass the options along. --- content_scripts/mode_find.coffee | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 21918be2..297c4158 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -60,14 +60,15 @@ class FindMode extends Mode matchCount: 0 hasResults: false - constructor: (@options = {}) -> + constructor: (options = {}) -> + console.log "constructor", @options # Save the selection, so findInPlace can restore it. @initialRange = getCurrentRange() FindMode.query = rawQuery: "" - if @options.returnToViewport + if options.returnToViewport @scrollX = window.scrollX @scrollY = window.scrollY - super + super extend options, name: "find" indicator: false exitOnClick: true -- cgit v1.2.3 From 4f426affde34c895cf342dac256895ffe98ee4f3 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 13 Jun 2015 13:03:10 +0100 Subject: Refactor duplicate code. --- content_scripts/hud.coffee | 2 +- content_scripts/mode_find.coffee | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 2f4e008c..bfad71b7 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -57,7 +57,7 @@ HUD = @tween.fade 0, 150, => @hide true, updateIndicator hideFindMode: (data) -> - window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport + @findMode.checkReturnToViewPort() # An element element won't receive a focus event if the search landed on it while we were in the HUD # iframe. To end up with the correct modes active, we create a focus/blur event manually after refocusing diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 297c4158..bba457b6 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -87,7 +87,7 @@ class FindMode extends Mode findInPlace: (query) -> # If requested, restore the scroll position (so that failed searches leave the scroll position unchanged). - window.scrollTo @scrollX, @scrollY if @options.returnToViewport + @checkReturnToViewPort() FindMode.updateQuery query # Restore the selection. That way, we're always searching forward from the same place, so we find the right # match as the user adds matching characters, or removes previously-matched characters. See #1434. @@ -196,6 +196,9 @@ class FindMode extends Mode @restoreDefaultSelectionHighlight: -> document.body.classList.remove("vimiumFindMode") + checkReturnToViewPort: -> + window.scrollTo @scrollX, @scrollY if @options.returnToViewport + getCurrentRange = -> selection = getSelection() if selection.type == "None" -- cgit v1.2.3