From 85aa76ec68167ea0ac08cc627b3f12e1077b1b1f Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Thu, 28 May 2015 22:12:08 +0100 Subject: Decide find mode text in the HUD iframe, not in frontend --- content_scripts/hud.coffee | 8 ++++++++ content_scripts/vimium_frontend.coffee | 17 ++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 84b8abeb..3e749da5 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -26,6 +26,14 @@ HUD = @hudUI.show {name: "show", text} @tween.fade 1.0, 150 + showFindMode: (text = "") -> + return unless @enabled() + @hudUI.show {name: "showFindMode", text} + @tween.fade 1.0, 150 + + updateMatchesCount: (matchCount, showMatchText = true) -> + @hudUI.postMessage {name: "updateMatchesCount", matchCount, showMatchText} + # Hide the HUD. # If :immediate is falsy, then the HUD is faded out smoothly (otherwise it is hidden immediately). # If :updateIndicator is truthy, then we also refresh the mode indicator. The only time we don't update the diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 8c28b4e6..46af3847 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -686,9 +686,6 @@ updateFindModeQuery = -> # default to 'smartcase' mode, unless noIgnoreCase is explicitly specified findModeQuery.ignoreCase = !hasNoIgnoreCaseFlag && !Utils.hasUpperCase(findModeQuery.parsedQuery) - # Don't count matches in the HUD. - HUD.hide(true) - # if we are dealing with a regex, grep for all matches in the text, and then call window.find() on them # sequentially so the browser handles the scrolling / text selection. if findModeQuery.isRegex @@ -993,13 +990,11 @@ window.goNext = -> findAndFollowRel("next") || findAndFollowLink(nextStrings) showFindModeHUDForQuery = -> - if findModeQuery.rawQuery and (findModeQueryHasResults || findModeQuery.parsedQuery.length == 0) - plural = if findModeQuery.matchCount == 1 then "" else "es" - HUD.show("/" + findModeQuery.rawQuery + " (" + findModeQuery.matchCount + " Match#{plural})") - else if findModeQuery.rawQuery - HUD.show("/" + findModeQuery.rawQuery + " (No Matches)") - else - HUD.show("/") + matchCount = if findModeQuery.parsedQuery.length > 0 then findModeQuery.matchCount else 0 + showCount = findModeQuery.rawQuery.length > 0 + + HUD.showFindMode findModeQuery.rawQuery + HUD.updateMatchesCount matchCount, showCount getCurrentRange = -> selection = getSelection() @@ -1027,7 +1022,7 @@ window.enterFindMode = (options = {}) -> findModeSaveSelection() findModeQuery = rawQuery: "" findMode = new FindMode options - HUD.show "/" + HUD.showFindMode() findMode window.showHelpDialog = (html, fid) -> -- cgit v1.2.3 From 91077b021e24f5ed6a8505e4e2afb97cdc2cbbb0 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Thu, 28 May 2015 22:22:40 +0100 Subject: Make all find mode updates go via the HUD in preparation to use an input --- content_scripts/hud.coffee | 13 +++++++++++++ content_scripts/vimium.css | 5 +++++ content_scripts/vimium_frontend.coffee | 25 +++++++++---------------- 3 files changed, 27 insertions(+), 16 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 3e749da5..64ebd8e3 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -28,12 +28,25 @@ HUD = showFindMode: (text = "") -> return unless @enabled() + # NOTE(mrmr1993): We set findModeQuery.rawQuery here rather in search while we still handle keys in the + # main frame. When key handling is moved to the HUD iframe, this line should be deleted, and the + # equivalent in search should be uncommented. + findModeQuery.rawQuery = text @hudUI.show {name: "showFindMode", text} @tween.fade 1.0, 150 updateMatchesCount: (matchCount, showMatchText = true) -> @hudUI.postMessage {name: "updateMatchesCount", matchCount, showMatchText} + search: (data) -> + # NOTE(mrmr1993): The following line is disabled as it is currently vulnerable to a race condition when a + # user types quickly. When all of the key handling is done in the HUD iframe, this should be uncommented, + # and the equivalent line in showFindMode should be deleted. + #findModeQuery.rawQuery = data.query + updateFindModeQuery() + performFindInPlace() + showFindModeHUDForQuery() + # Hide the HUD. # If :immediate is falsy, then the HUD is faded out smoothly (otherwise it is hidden immediately). # If :updateIndicator is truthy, then we also refresh the mode indicator. The only time we don't update the diff --git a/content_scripts/vimium.css b/content_scripts/vimium.css index 38a968fc..7b501f94 100644 --- a/content_scripts/vimium.css +++ b/content_scripts/vimium.css @@ -284,6 +284,11 @@ iframe.vimiumHUDFrame { opacity: 0; } +div.vimiumHUD span#hud-find-input * { + display: inline; + white-space: nowrap; +} + body.vimiumFindMode ::selection { background: #ff9632; } diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 46af3847..70a8fef7 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -5,7 +5,7 @@ # "domReady". # -findModeQuery = { rawQuery: "", matchCount: 0 } +window.findModeQuery = { rawQuery: "", matchCount: 0 } findModeQueryHasResults = false findModeAnchorNode = null findModeInitialRange = null @@ -666,7 +666,7 @@ FindModeHistory = ([ query ].concat rawQueryList.filter (q) => q != query)[0..@max] # should be called whenever rawQuery is modified. -updateFindModeQuery = -> +window.updateFindModeQuery = -> # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of # escape sequences. '\' is the escape character and needs to be escaped itself to be used as a normal # character. here we grep for the relevant escape sequences. @@ -711,14 +711,8 @@ updateFindModeQuery = -> text = document.body.innerText findModeQuery.matchCount = text.match(pattern)?.length -updateQueryForFindMode = (rawQuery) -> - findModeQuery.rawQuery = rawQuery - updateFindModeQuery() - performFindInPlace() - showFindModeHUDForQuery() - handleKeyCharForFindMode = (keyChar) -> - updateQueryForFindMode findModeQuery.rawQuery + keyChar + HUD.showFindMode findModeQuery.rawQuery + keyChar handleEscapeForFindMode = -> document.body.classList.remove("vimiumFindMode") @@ -737,7 +731,7 @@ handleDeleteForFindMode = -> HUD.hide() false else - updateQueryForFindMode findModeQuery.rawQuery.substring(0, findModeQuery.rawQuery.length - 1) + HUD.showFindMode findModeQuery.rawQuery.substring(0, findModeQuery.rawQuery.length - 1) true # sends us into insert mode if possible, but does not. @@ -774,12 +768,12 @@ class FindMode extends Mode if rawQuery = FindModeHistory.getQuery @historyIndex + 1 @historyIndex += 1 @partialQuery = findModeQuery.rawQuery if @historyIndex == 0 - updateQueryForFindMode rawQuery + HUD.showFindMode rawQuery @suppressEvent else if event.keyCode == keyCodes.downArrow @historyIndex = Math.max -1, @historyIndex - 1 rawQuery = if 0 <= @historyIndex then FindModeHistory.getQuery @historyIndex else @partialQuery - updateQueryForFindMode rawQuery + HUD.showFindMode rawQuery @suppressEvent else DomUtils.suppressPropagation(event) @@ -800,7 +794,7 @@ class FindMode extends Mode if findModeQueryHasResults and event?.type != "click" new PostFindMode -performFindInPlace = -> +window.performFindInPlace = -> # 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. findModeRestoreSelection() @@ -989,11 +983,10 @@ window.goNext = -> nextStrings = nextPatterns.split(",").filter( (s) -> s.trim().length ) findAndFollowRel("next") || findAndFollowLink(nextStrings) -showFindModeHUDForQuery = -> +window.showFindModeHUDForQuery = -> matchCount = if findModeQuery.parsedQuery.length > 0 then findModeQuery.matchCount else 0 showCount = findModeQuery.rawQuery.length > 0 - HUD.showFindMode findModeQuery.rawQuery HUD.updateMatchesCount matchCount, showCount getCurrentRange = -> @@ -1020,7 +1013,7 @@ window.enterFindMode = (options = {}) -> Marks.setPreviousPosition() # Save the selection, so performFindInPlace can restore it. findModeSaveSelection() - findModeQuery = rawQuery: "" + window.findModeQuery = rawQuery: "" findMode = new FindMode options HUD.showFindMode() findMode -- cgit v1.2.3 From dcc8d6ee0dadf4f7a473ab97bd8e20c282a3c445 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Fri, 29 May 2015 04:28:58 +0100 Subject: Accept input in the HUD iframe --- content_scripts/hud.coffee | 24 ++++++++++++++++-------- content_scripts/vimium.css | 15 +++++++++++++-- content_scripts/vimium_frontend.coffee | 2 +- 3 files changed, 30 insertions(+), 11 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 64ebd8e3..787be6a9 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -28,10 +28,6 @@ HUD = showFindMode: (text = "") -> return unless @enabled() - # NOTE(mrmr1993): We set findModeQuery.rawQuery here rather in search while we still handle keys in the - # main frame. When key handling is moved to the HUD iframe, this line should be deleted, and the - # equivalent in search should be uncommented. - findModeQuery.rawQuery = text @hudUI.show {name: "showFindMode", text} @tween.fade 1.0, 150 @@ -39,10 +35,7 @@ HUD = @hudUI.postMessage {name: "updateMatchesCount", matchCount, showMatchText} search: (data) -> - # NOTE(mrmr1993): The following line is disabled as it is currently vulnerable to a race condition when a - # user types quickly. When all of the key handling is done in the HUD iframe, this should be uncommented, - # and the equivalent line in showFindMode should be deleted. - #findModeQuery.rawQuery = data.query + findModeQuery.rawQuery = data.query updateFindModeQuery() performFindInPlace() showFindModeHUDForQuery() @@ -63,6 +56,21 @@ HUD = else @tween.fade 0, 150, => @hide true, updateIndicator + hideFindMode: (data) -> + # 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 + # this window. + window.focus() + focusNode = window.getSelection().focusNode + focusNode = focusNode.parentElement if focusNode? and focusNode.nodeType != focusNode.ELEMENT_NODE + if focusNode? and DomUtils.isFocusable focusNode + focusNode.focus() + else + document.activeElement?.blur() + + findModeQuery.rawQuery = data.query + handlerStack.bubbleEvent "keydown", data.event + isReady: do -> ready = false DomUtils.documentReady -> ready = true diff --git a/content_scripts/vimium.css b/content_scripts/vimium.css index 7b501f94..e02df7c2 100644 --- a/content_scripts/vimium.css +++ b/content_scripts/vimium.css @@ -284,9 +284,20 @@ iframe.vimiumHUDFrame { opacity: 0; } +div.vimiumHUD span#hud-find-input, div.vimiumHUD span#hud-match-count { + display: inline; + outline: none; + white-space: nowrap; + overflow-y: hidden; +} + +div.vimiumHUD span#hud-find-input br { + display: none; +} + div.vimiumHUD span#hud-find-input * { - display: inline; - white-space: nowrap; + display: inline; + white-space: nowrap; } body.vimiumFindMode ::selection { diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 70a8fef7..685dc527 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -776,7 +776,7 @@ class FindMode extends Mode HUD.showFindMode rawQuery @suppressEvent else - DomUtils.suppressPropagation(event) + DomUtils.suppressPropagation event if HandlerStack::isChromeEvent event handlerStack.stopBubblingAndFalse keypress: (event) => -- cgit v1.2.3 From 6ebc5ad32c26841ffe49895b5afc9b3792f68f4e Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Fri, 29 May 2015 14:06:28 +0100 Subject: Ensure focus is called on the appropriate element when closing the HUD --- content_scripts/hud.coffee | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 787be6a9..2b3748bf 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -61,12 +61,19 @@ HUD = # iframe. To end up with the correct modes active, we create a focus/blur event manually after refocusing # this window. window.focus() - focusNode = window.getSelection().focusNode - focusNode = focusNode.parentElement if focusNode? and focusNode.nodeType != focusNode.ELEMENT_NODE - if focusNode? and DomUtils.isFocusable focusNode - focusNode.focus() + + sel = window.getSelection() + focusNode = if not sel.focusNode? + null + else if sel.focusNode == sel.anchorNode and sel.focusOffset == sel.anchorOffset + # The selection either *is* an element, or is inside an opaque element (eg. ). + sel.focusNode.childNodes[sel.focusOffset] + else if sel.focusNode.nodeType != sel.focusNode.ELEMENT_NODE + sel.focusNode.parentElement else - document.activeElement?.blur() + sel.focusNode + document.activeElement?.blur() + focusNode?.focus() findModeQuery.rawQuery = data.query handlerStack.bubbleEvent "keydown", data.event -- cgit v1.2.3 From 99030011de309f1376ccae1de51e55f43250dad1 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 15:47:13 +0100 Subject: Remove unused code in handleDeleteForFindMode --- content_scripts/vimium_frontend.coffee | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 685dc527..68da4d2f 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -725,14 +725,7 @@ handleEscapeForFindMode = -> window.getSelection().addRange(range) focusFoundLink() || selectFoundInputElement() -# Return true if character deleted, false otherwise. -handleDeleteForFindMode = -> - if findModeQuery.rawQuery.length == 0 - HUD.hide() - false - else - HUD.showFindMode findModeQuery.rawQuery.substring(0, findModeQuery.rawQuery.length - 1) - true +handleDeleteForFindMode = -> HUD.hide(); false # sends us into insert mode if possible, but does not. # corresponds approximately to 'nevermind, I have found it already' while means 'I want to save -- cgit v1.2.3 From db70c69db0034132426a92bef2a76d8d49773a6f Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 15:48:34 +0100 Subject: Remove unused event listeners in FindMode --- content_scripts/vimium_frontend.coffee | 8 -------- 1 file changed, 8 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 68da4d2f..d875eb9a 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -772,14 +772,6 @@ class FindMode extends Mode DomUtils.suppressPropagation event if HandlerStack::isChromeEvent event handlerStack.stopBubblingAndFalse - keypress: (event) => - handlerStack.neverContinueBubbling => - if event.keyCode > 31 - keyChar = String.fromCharCode event.charCode - handleKeyCharForFindMode keyChar if keyChar - - keyup: (event) => @suppressEvent - exit: (event) -> super() handleEscapeForFindMode() if event?.type == "keydown" and KeyboardUtils.isEscape event -- cgit v1.2.3 From 51c427a30b568b9116baeaf5bafd64acc7a6409c Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 15:50:15 +0100 Subject: Remove unused function handleKeyCharForFindMode --- content_scripts/vimium_frontend.coffee | 3 --- 1 file changed, 3 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index d875eb9a..01e3399a 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -711,9 +711,6 @@ window.updateFindModeQuery = -> text = document.body.innerText findModeQuery.matchCount = text.match(pattern)?.length -handleKeyCharForFindMode = (keyChar) -> - HUD.showFindMode findModeQuery.rawQuery + keyChar - handleEscapeForFindMode = -> document.body.classList.remove("vimiumFindMode") # removing the class does not re-color existing selections. we recreate the current selection so it reverts -- cgit v1.2.3 From 7fc0492aa1f459588d6bd4e2397c4858fcdd6c4b Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 15:57:40 +0100 Subject: Remove redundant code from FindMode keydown handler, inline delete code --- content_scripts/vimium_frontend.coffee | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 01e3399a..413c06d0 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -722,8 +722,6 @@ handleEscapeForFindMode = -> window.getSelection().addRange(range) focusFoundLink() || selectFoundInputElement() -handleDeleteForFindMode = -> HUD.hide(); false - # sends us into insert mode if possible, but does not. # corresponds approximately to 'nevermind, I have found it already' while means 'I want to save # this query and do more searches with it' @@ -748,26 +746,20 @@ class FindMode extends Mode keydown: (event) => window.scrollTo @scrollX, @scrollY if options.returnToViewport if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey - @exit() unless handleDeleteForFindMode() - @suppressEvent + HUD.hide() + @exit() else if event.keyCode == keyCodes.enter handleEnterForFindMode() @exit() - @suppressEvent else if event.keyCode == keyCodes.upArrow if rawQuery = FindModeHistory.getQuery @historyIndex + 1 @historyIndex += 1 @partialQuery = findModeQuery.rawQuery if @historyIndex == 0 HUD.showFindMode rawQuery - @suppressEvent else if event.keyCode == keyCodes.downArrow @historyIndex = Math.max -1, @historyIndex - 1 rawQuery = if 0 <= @historyIndex then FindModeHistory.getQuery @historyIndex else @partialQuery HUD.showFindMode rawQuery - @suppressEvent - else - DomUtils.suppressPropagation event if HandlerStack::isChromeEvent event - handlerStack.stopBubblingAndFalse exit: (event) -> super() -- cgit v1.2.3 From b3cee0a042cdba8ce98f74edc1c08b7c15304492 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 16:08:36 +0100 Subject: Handle esc explicitly in the FindMode keydown handler, not by options --- content_scripts/vimium_frontend.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 413c06d0..eb94b6e8 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -740,7 +740,6 @@ class FindMode extends Mode super name: "find" indicator: false - exitOnEscape: true exitOnClick: true keydown: (event) => @@ -760,6 +759,8 @@ class FindMode extends Mode @historyIndex = Math.max -1, @historyIndex - 1 rawQuery = if 0 <= @historyIndex then FindModeHistory.getQuery @historyIndex else @partialQuery HUD.showFindMode rawQuery + else if KeyboardUtils.isEscape event + @exit event exit: (event) -> super() -- cgit v1.2.3 From 0e2edf6bb60893b7b8edf54539a90c1aeb9e3083 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 16:12:40 +0100 Subject: Move key-only code from FindMode.exit to its keydown handler --- content_scripts/vimium_frontend.coffee | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index eb94b6e8..86af6350 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -747,9 +747,11 @@ class FindMode extends Mode if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey HUD.hide() @exit() + new PostFindMode if findModeQueryHasResults else if event.keyCode == keyCodes.enter handleEnterForFindMode() @exit() + new PostFindMode if findModeQueryHasResults else if event.keyCode == keyCodes.upArrow if rawQuery = FindModeHistory.getQuery @historyIndex + 1 @historyIndex += 1 @@ -760,14 +762,13 @@ class FindMode extends Mode rawQuery = if 0 <= @historyIndex then FindModeHistory.getQuery @historyIndex else @partialQuery HUD.showFindMode rawQuery else if KeyboardUtils.isEscape event - @exit event + @exit() + handleEscapeForFindMode() + new PostFindMode if findModeQueryHasResults exit: (event) -> super() - handleEscapeForFindMode() if event?.type == "keydown" and KeyboardUtils.isEscape event - handleEscapeForFindMode() if event?.type == "click" - if findModeQueryHasResults and event?.type != "click" - new PostFindMode + handleEscapeForFindMode() if event window.performFindInPlace = -> # Restore the selection. That way, we're always searching forward from the same place, so we find the right -- cgit v1.2.3 From f17747e7d3f62dc966c8ff36c0937b69c35ef47e Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 16:28:44 +0100 Subject: Stop FindMode's keydown handler from using FindMode's scope and this --- content_scripts/vimium_frontend.coffee | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 86af6350..03659bf0 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -6,6 +6,7 @@ # window.findModeQuery = { rawQuery: "", matchCount: 0 } +window.findMode = null findModeQueryHasResults = false findModeAnchorNode = null findModeInitialRange = null @@ -731,10 +732,10 @@ handleEnterForFindMode = -> FindModeHistory.saveQuery findModeQuery.rawQuery class FindMode extends Mode - constructor: (options = {}) -> + constructor: (@options = {}) -> @historyIndex = -1 @partialQuery = "" - if options.returnToViewport + if @options.returnToViewport @scrollX = window.scrollX @scrollY = window.scrollY super @@ -743,26 +744,25 @@ class FindMode extends Mode exitOnClick: true keydown: (event) => - window.scrollTo @scrollX, @scrollY if options.returnToViewport + window.scrollTo findMode.scrollX, findMode.scrollY if findMode.options.returnToViewport if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey - HUD.hide() - @exit() + findMode.exit() new PostFindMode if findModeQueryHasResults else if event.keyCode == keyCodes.enter handleEnterForFindMode() - @exit() + findMode.exit() new PostFindMode if findModeQueryHasResults else if event.keyCode == keyCodes.upArrow - if rawQuery = FindModeHistory.getQuery @historyIndex + 1 - @historyIndex += 1 - @partialQuery = findModeQuery.rawQuery if @historyIndex == 0 + if rawQuery = FindModeHistory.getQuery findMode.historyIndex + 1 + findMode.historyIndex += 1 + findMode.partialQuery = findModeQuery.rawQuery if findMode.historyIndex == 0 HUD.showFindMode rawQuery else if event.keyCode == keyCodes.downArrow - @historyIndex = Math.max -1, @historyIndex - 1 - rawQuery = if 0 <= @historyIndex then FindModeHistory.getQuery @historyIndex else @partialQuery + findMode.historyIndex = Math.max -1, findMode.historyIndex - 1 + rawQuery = if 0 <= findMode.historyIndex then FindModeHistory.getQuery findMode.historyIndex else findMode.partialQuery HUD.showFindMode rawQuery else if KeyboardUtils.isEscape event - @exit() + findMode.exit() handleEscapeForFindMode() new PostFindMode if findModeQueryHasResults @@ -990,7 +990,7 @@ window.enterFindMode = (options = {}) -> # Save the selection, so performFindInPlace can restore it. findModeSaveSelection() window.findModeQuery = rawQuery: "" - findMode = new FindMode options + window.findMode = new FindMode options HUD.showFindMode() findMode -- cgit v1.2.3 From 870ca562bfe904971b6055a67259b5c21b1655ee Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 16:39:01 +0100 Subject: Fix returnToViewport support for FindMode This was broken by the move to taking input in an iframe, since the frontend was no longer getting keydown events for text changes, and so the viewport wasn't being scrolled back to its original position until the mode was exiting. --- content_scripts/hud.coffee | 1 + 1 file changed, 1 insertion(+) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 2b3748bf..f16fcca6 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -35,6 +35,7 @@ HUD = @hudUI.postMessage {name: "updateMatchesCount", matchCount, showMatchText} search: (data) -> + window.scrollTo findMode.scrollX, findMode.scrollY if findMode.options.returnToViewport findModeQuery.rawQuery = data.query updateFindModeQuery() performFindInPlace() -- cgit v1.2.3 From 673cd268223cdbf1c8ecd47a5826291a91b955d6 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 16:51:41 +0100 Subject: Move FindMode's keydown to the HUD --- content_scripts/hud.coffee | 25 +++++++++++++++++++++++- content_scripts/vimium_frontend.coffee | 35 ++++++---------------------------- 2 files changed, 30 insertions(+), 30 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index f16fcca6..c64670ee 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -77,7 +77,30 @@ HUD = focusNode?.focus() findModeQuery.rawQuery = data.query - handlerStack.bubbleEvent "keydown", data.event + @findModeKeydown data.event + + findModeKeydown: (event) -> + window.scrollTo findMode.scrollX, findMode.scrollY if findMode.options.returnToViewport + if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey + findMode.exit() + new PostFindMode if findModeQueryHasResults + else if event.keyCode == keyCodes.enter + handleEnterForFindMode() + findMode.exit() + new PostFindMode if findModeQueryHasResults + else if event.keyCode == keyCodes.upArrow + if rawQuery = FindModeHistory.getQuery findMode.historyIndex + 1 + findMode.historyIndex += 1 + findMode.partialQuery = findModeQuery.rawQuery if findMode.historyIndex == 0 + HUD.showFindMode rawQuery + else if event.keyCode == keyCodes.downArrow + findMode.historyIndex = Math.max -1, findMode.historyIndex - 1 + rawQuery = if 0 <= findMode.historyIndex then FindModeHistory.getQuery findMode.historyIndex else findMode.partialQuery + HUD.showFindMode rawQuery + else if KeyboardUtils.isEscape event + findMode.exit() + handleEscapeForFindMode() + new PostFindMode if findModeQueryHasResults isReady: do -> ready = false diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 03659bf0..e5a0f61a 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -7,7 +7,7 @@ window.findModeQuery = { rawQuery: "", matchCount: 0 } window.findMode = null -findModeQueryHasResults = false +window.findModeQueryHasResults = false findModeAnchorNode = null findModeInitialRange = null isShowingHelpDialog = false @@ -625,7 +625,7 @@ isValidFirstKey = (keyChar) -> # This implements find-mode query history (using the "findModeRawQueryList" setting) as a list of raw queries, # most recent first. -FindModeHistory = +window.FindModeHistory = storage: chrome.storage.local key: "findModeRawQueryList" max: 50 @@ -712,7 +712,7 @@ window.updateFindModeQuery = -> text = document.body.innerText findModeQuery.matchCount = text.match(pattern)?.length -handleEscapeForFindMode = -> +window.handleEscapeForFindMode = -> 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. @@ -726,7 +726,7 @@ handleEscapeForFindMode = -> # sends us into insert mode if possible, but does not. # corresponds approximately to 'nevermind, I have found it already' while means 'I want to save # this query and do more searches with it' -handleEnterForFindMode = -> +window.handleEnterForFindMode = -> focusFoundLink() document.body.classList.add("vimiumFindMode") FindModeHistory.saveQuery findModeQuery.rawQuery @@ -743,29 +743,6 @@ class FindMode extends Mode indicator: false exitOnClick: true - keydown: (event) => - window.scrollTo findMode.scrollX, findMode.scrollY if findMode.options.returnToViewport - if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey - findMode.exit() - new PostFindMode if findModeQueryHasResults - else if event.keyCode == keyCodes.enter - handleEnterForFindMode() - findMode.exit() - new PostFindMode if findModeQueryHasResults - else if event.keyCode == keyCodes.upArrow - if rawQuery = FindModeHistory.getQuery findMode.historyIndex + 1 - findMode.historyIndex += 1 - findMode.partialQuery = findModeQuery.rawQuery if findMode.historyIndex == 0 - HUD.showFindMode rawQuery - else if event.keyCode == keyCodes.downArrow - findMode.historyIndex = Math.max -1, findMode.historyIndex - 1 - rawQuery = if 0 <= findMode.historyIndex then FindModeHistory.getQuery findMode.historyIndex else findMode.partialQuery - HUD.showFindMode rawQuery - else if KeyboardUtils.isEscape event - findMode.exit() - handleEscapeForFindMode() - new PostFindMode if findModeQueryHasResults - exit: (event) -> super() handleEscapeForFindMode() if event @@ -775,7 +752,7 @@ window.performFindInPlace = -> # match as the user adds matching characters, or removes previously-matched characters. See #1434. findModeRestoreSelection() query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery - findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) + window.findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) # :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. executeFind = (query, options) -> @@ -845,7 +822,7 @@ findAndFocus = (backwards) -> Marks.setPreviousPosition() query = getFindModeQuery backwards - findModeQueryHasResults = + window.findModeQueryHasResults = executeFind(query, { backwards: backwards, caseSensitive: !findModeQuery.ignoreCase }) if findModeQueryHasResults -- cgit v1.2.3 From 36fe16dcafd57484bf4219262c67cc5612bad722 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 16:57:11 +0100 Subject: Make FindModeHistory independent of the rest of vimium_frontend --- content_scripts/vimium_frontend.coffee | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index e5a0f61a..295736c6 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -630,15 +630,16 @@ window.FindModeHistory = key: "findModeRawQueryList" max: 50 rawQueryList: null + isIncognitoMode: chrome.extension.inIncognitoContext init: -> unless @rawQueryList @rawQueryList = [] # Prevent repeated initialization. - @key = "findModeRawQueryListIncognito" if isIncognitoMode + @key = "findModeRawQueryListIncognito" if @isIncognitoMode @storage.get @key, (items) => unless chrome.runtime.lastError @rawQueryList = items[@key] if items[@key] - if isIncognitoMode and not items[@key] + if @isIncognitoMode and not items[@key] # This is the first incognito tab, so we need to initialize the incognito-mode query history. @storage.get "findModeRawQueryList", (items) => unless chrome.runtime.lastError @@ -657,7 +658,7 @@ window.FindModeHistory = newSetting = {}; newSetting[@key] = @rawQueryList @storage.set newSetting # If there are any active incognito-mode tabs, then propagte this query to those tabs too. - unless isIncognitoMode + unless @isIncognitoMode @storage.get "findModeRawQueryListIncognito", (items) => if not chrome.runtime.lastError and items.findModeRawQueryListIncognito @storage.set -- cgit v1.2.3 From 3e3ec8925d2646f9a7fbec3b69c75df8bdc5e86d Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 17:00:14 +0100 Subject: Move FindModeHistory to its own file to be used by the HUD iframe too --- content_scripts/vimium_frontend.coffee | 44 ---------------------------------- 1 file changed, 44 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 295736c6..ef554cae 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -623,50 +623,6 @@ window.refreshCompletionKeys = (response) -> isValidFirstKey = (keyChar) -> validFirstKeys[keyChar] || /^[1-9]/.test(keyChar) -# This implements find-mode query history (using the "findModeRawQueryList" setting) as a list of raw queries, -# most recent first. -window.FindModeHistory = - storage: chrome.storage.local - key: "findModeRawQueryList" - max: 50 - rawQueryList: null - isIncognitoMode: chrome.extension.inIncognitoContext - - init: -> - unless @rawQueryList - @rawQueryList = [] # Prevent repeated initialization. - @key = "findModeRawQueryListIncognito" if @isIncognitoMode - @storage.get @key, (items) => - unless chrome.runtime.lastError - @rawQueryList = items[@key] if items[@key] - if @isIncognitoMode and not items[@key] - # This is the first incognito tab, so we need to initialize the incognito-mode query history. - @storage.get "findModeRawQueryList", (items) => - unless chrome.runtime.lastError - @rawQueryList = items.findModeRawQueryList - @storage.set findModeRawQueryListIncognito: @rawQueryList - - chrome.storage.onChanged.addListener (changes, area) => - @rawQueryList = changes[@key].newValue if changes[@key] - - getQuery: (index = 0) -> - @rawQueryList[index] or "" - - saveQuery: (query) -> - if 0 < query.length - @rawQueryList = @refreshRawQueryList query, @rawQueryList - newSetting = {}; newSetting[@key] = @rawQueryList - @storage.set newSetting - # If there are any active incognito-mode tabs, then propagte this query to those tabs too. - unless @isIncognitoMode - @storage.get "findModeRawQueryListIncognito", (items) => - if not chrome.runtime.lastError and items.findModeRawQueryListIncognito - @storage.set - findModeRawQueryListIncognito: @refreshRawQueryList query, items.findModeRawQueryListIncognito - - refreshRawQueryList: (query, rawQueryList) -> - ([ query ].concat rawQueryList.filter (q) => q != query)[0..@max] - # should be called whenever rawQuery is modified. window.updateFindModeQuery = -> # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of -- cgit v1.2.3 From e92b9db2aeaa09b1855900936252e27e3535f9da Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Tue, 2 Jun 2015 17:09:00 +0100 Subject: Handle up and down keys directly in the HUD --- content_scripts/hud.coffee | 9 --------- content_scripts/vimium_frontend.coffee | 2 -- 2 files changed, 11 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index c64670ee..9eeb4579 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -88,15 +88,6 @@ HUD = handleEnterForFindMode() findMode.exit() new PostFindMode if findModeQueryHasResults - else if event.keyCode == keyCodes.upArrow - if rawQuery = FindModeHistory.getQuery findMode.historyIndex + 1 - findMode.historyIndex += 1 - findMode.partialQuery = findModeQuery.rawQuery if findMode.historyIndex == 0 - HUD.showFindMode rawQuery - else if event.keyCode == keyCodes.downArrow - findMode.historyIndex = Math.max -1, findMode.historyIndex - 1 - rawQuery = if 0 <= findMode.historyIndex then FindModeHistory.getQuery findMode.historyIndex else findMode.partialQuery - HUD.showFindMode rawQuery else if KeyboardUtils.isEscape event findMode.exit() handleEscapeForFindMode() diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index ef554cae..a6387da1 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -690,8 +690,6 @@ window.handleEnterForFindMode = -> class FindMode extends Mode constructor: (@options = {}) -> - @historyIndex = -1 - @partialQuery = "" if @options.returnToViewport @scrollX = window.scrollX @scrollY = window.scrollY -- cgit v1.2.3 From c176147ad4a52d0f9a0d63213c0280d1344fca00 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 00:18:14 +0100 Subject: Move code from enterFindMode into the FindMode constructor --- content_scripts/vimium_frontend.coffee | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index a6387da1..55082553 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -690,6 +690,9 @@ window.handleEnterForFindMode = -> class FindMode extends Mode constructor: (@options = {}) -> + # Save the selection, so performFindInPlace can restore it. + findModeSaveSelection() + window.findModeQuery = rawQuery: "" if @options.returnToViewport @scrollX = window.scrollX @scrollY = window.scrollY @@ -698,6 +701,8 @@ class FindMode extends Mode indicator: false exitOnClick: true + HUD.showFindMode() + exit: (event) -> super() handleEscapeForFindMode() if event @@ -919,12 +924,7 @@ findModeRestoreSelection = (range = findModeInitialRange) -> # Enters find mode. Returns the new find-mode instance. window.enterFindMode = (options = {}) -> Marks.setPreviousPosition() - # Save the selection, so performFindInPlace can restore it. - findModeSaveSelection() - window.findModeQuery = rawQuery: "" window.findMode = new FindMode options - HUD.showFindMode() - findMode window.showHelpDialog = (html, fid) -> return if (isShowingHelpDialog || !document.body || fid != frameId) -- cgit v1.2.3 From 3b5f9fd40cabeca1843631585356de81d4db92ac Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 00:19:44 +0100 Subject: Use FindMode constructor directly in visual/edit modes --- content_scripts/mode_visual_edit.coffee | 2 +- content_scripts/vimium_frontend.coffee | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index 8d1d96cc..2abccaac 100644 --- a/content_scripts/mode_visual_edit.coffee +++ b/content_scripts/mode_visual_edit.coffee @@ -366,7 +366,7 @@ class Movement extends CountPrefix @movements.n = (count) -> executeFind count, false @movements.N = (count) -> executeFind count, true @movements["/"] = -> - @findMode = window.enterFindMode returnToViewport: true + @findMode = new FindMode returnToViewport: true @findMode.onExit => @changeMode VisualMode # # End of Movement constructor. diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 55082553..5c1d5c6f 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -922,9 +922,9 @@ findModeRestoreSelection = (range = findModeInitialRange) -> selection.addRange range # Enters find mode. Returns the new find-mode instance. -window.enterFindMode = (options = {}) -> +window.enterFindMode = -> Marks.setPreviousPosition() - window.findMode = new FindMode options + window.findMode = new FindMode() window.showHelpDialog = (html, fid) -> return if (isShowingHelpDialog || !document.body || fid != frameId) -- cgit v1.2.3 From 337644921567e415b933874d84ec8b5e9415cb0d Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 00:24:11 +0100 Subject: Inline findModeSaveSelection, store initialRange in FindMode instance --- content_scripts/vimium_frontend.coffee | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 5c1d5c6f..b8bf9ef3 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -9,7 +9,6 @@ window.findModeQuery = { rawQuery: "", matchCount: 0 } window.findMode = null window.findModeQueryHasResults = false findModeAnchorNode = null -findModeInitialRange = null isShowingHelpDialog = false keyPort = null isEnabledForUrl = true @@ -691,7 +690,7 @@ window.handleEnterForFindMode = -> class FindMode extends Mode constructor: (@options = {}) -> # Save the selection, so performFindInPlace can restore it. - findModeSaveSelection() + @initialRange = getCurrentRange() window.findModeQuery = rawQuery: "" if @options.returnToViewport @scrollX = window.scrollX @@ -913,10 +912,8 @@ getCurrentRange = -> selection.collapseToStart() if selection.type == "Range" selection.getRangeAt 0 -findModeSaveSelection = -> - findModeInitialRange = getCurrentRange() - -findModeRestoreSelection = (range = findModeInitialRange) -> +findModeRestoreSelection = -> + range = findMode.initialRange selection = getSelection() selection.removeAllRanges() selection.addRange range -- cgit v1.2.3 From 915700fdf846fb1e5cc1ce71e51600257051e1a3 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 00:26:19 +0100 Subject: Move findModeRestoreSelection to FindMode::restoreSelection --- content_scripts/vimium_frontend.coffee | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index b8bf9ef3..26eace9c 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -706,10 +706,16 @@ class FindMode extends Mode super() handleEscapeForFindMode() if event + restoreSelection: -> + range = @initialRange + selection = getSelection() + selection.removeAllRanges() + selection.addRange range + window.performFindInPlace = -> # 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. - findModeRestoreSelection() + findMode.restoreSelection() query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery window.findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) @@ -912,12 +918,6 @@ getCurrentRange = -> selection.collapseToStart() if selection.type == "Range" selection.getRangeAt 0 -findModeRestoreSelection = -> - range = findMode.initialRange - selection = getSelection() - selection.removeAllRanges() - selection.addRange range - # Enters find mode. Returns the new find-mode instance. window.enterFindMode = -> Marks.setPreviousPosition() -- cgit v1.2.3 From 7d75b770f65b9ed0c95f694ac2dd59e185fd6a4f Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 00:52:28 +0100 Subject: Move finding the element at a selection's focus to a library function --- content_scripts/hud.coffee | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 9eeb4579..ca2eb7f2 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -63,16 +63,7 @@ HUD = # this window. window.focus() - sel = window.getSelection() - focusNode = if not sel.focusNode? - null - else if sel.focusNode == sel.anchorNode and sel.focusOffset == sel.anchorOffset - # The selection either *is* an element, or is inside an opaque element (eg. ). - sel.focusNode.childNodes[sel.focusOffset] - else if sel.focusNode.nodeType != sel.focusNode.ELEMENT_NODE - sel.focusNode.parentElement - else - sel.focusNode + focusNode = DomUtils.getSelectionFocusElement() document.activeElement?.blur() focusNode?.focus() -- cgit v1.2.3 From 850533b89c71a0320b610ad2d26161067a5ace3b Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 01:27:29 +0100 Subject: Remove global findModeAnchorNode and false comments justifying it --- content_scripts/vimium_frontend.coffee | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 26eace9c..633e4122 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -8,7 +8,6 @@ window.findModeQuery = { rawQuery: "", matchCount: 0 } window.findMode = null window.findModeQueryHasResults = false -findModeAnchorNode = null isShowingHelpDialog = false keyPort = null isEnabledForUrl = true @@ -739,9 +738,6 @@ executeFind = (query, options) -> if document.activeElement and DomUtils.isEditable document.activeElement document.activeElement.blur() unless DomUtils.isSelected document.activeElement - # we need to save the anchor node here because seems to nullify it, regardless of whether we do - # preventDefault() - findModeAnchorNode = document.getSelection().anchorNode result restoreDefaultSelectionHighlight = -> document.body.classList.remove("vimiumFindMode") @@ -752,10 +748,10 @@ focusFoundLink = -> link.focus() if link selectFoundInputElement = -> - # if the found text is in an input element, getSelection().anchorNode will be null, so we use activeElement - # instead. however, 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. + # 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 (findModeQueryHasResults && document.activeElement && DomUtils.isSelectable(document.activeElement) && DomUtils.isDOMDescendant(findModeAnchorNode, document.activeElement)) -- cgit v1.2.3 From 24c53c33ea8b4b603db91bc68fd3b7e50d7b5615 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 03:34:34 +0100 Subject: Integrate performFindInPlace into FindMode as findInPlace --- content_scripts/hud.coffee | 2 +- content_scripts/vimium_frontend.coffee | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index ca2eb7f2..4535ba69 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -38,7 +38,7 @@ HUD = window.scrollTo findMode.scrollX, findMode.scrollY if findMode.options.returnToViewport findModeQuery.rawQuery = data.query updateFindModeQuery() - performFindInPlace() + findMode.findInPlace() showFindModeHUDForQuery() # Hide the HUD. diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 633e4122..9e4d2ab3 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -688,7 +688,7 @@ window.handleEnterForFindMode = -> class FindMode extends Mode constructor: (@options = {}) -> - # Save the selection, so performFindInPlace can restore it. + # Save the selection, so findInPlace can restore it. @initialRange = getCurrentRange() window.findModeQuery = rawQuery: "" if @options.returnToViewport @@ -711,12 +711,12 @@ class FindMode extends Mode selection.removeAllRanges() selection.addRange range -window.performFindInPlace = -> - # 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. - findMode.restoreSelection() - query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery - window.findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) + findInPlace: -> + # 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() + query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery + window.findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) # :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. executeFind = (query, options) -> -- cgit v1.2.3 From fb3e4c2706fed44f76ff24395a488f2601865e54 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 04:15:24 +0100 Subject: Inline showFindModeHUDForQuery at its sole callsite --- content_scripts/hud.coffee | 5 ++++- content_scripts/vimium_frontend.coffee | 6 ------ 2 files changed, 4 insertions(+), 7 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 4535ba69..8d40a0c8 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -39,7 +39,10 @@ HUD = findModeQuery.rawQuery = data.query updateFindModeQuery() findMode.findInPlace() - showFindModeHUDForQuery() + + matchCount = if findModeQuery.parsedQuery.length > 0 then findModeQuery.matchCount else 0 + showCount = findModeQuery.rawQuery.length > 0 + HUD.updateMatchesCount matchCount, showCount # Hide the HUD. # If :immediate is falsy, then the HUD is faded out smoothly (otherwise it is hidden immediately). diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 9e4d2ab3..077da3fa 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -897,12 +897,6 @@ window.goNext = -> nextStrings = nextPatterns.split(",").filter( (s) -> s.trim().length ) findAndFollowRel("next") || findAndFollowLink(nextStrings) -window.showFindModeHUDForQuery = -> - matchCount = if findModeQuery.parsedQuery.length > 0 then findModeQuery.matchCount else 0 - showCount = findModeQuery.rawQuery.length > 0 - - HUD.updateMatchesCount matchCount, showCount - getCurrentRange = -> selection = getSelection() if selection.type == "None" -- cgit v1.2.3 From fca9fc1e9da4173370202d507493c92e2ddf97eb Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 04:19:55 +0100 Subject: Inline HUD.updateMatchesCount at its sole callsite --- content_scripts/hud.coffee | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 8d40a0c8..f114c000 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -31,18 +31,16 @@ HUD = @hudUI.show {name: "showFindMode", text} @tween.fade 1.0, 150 - updateMatchesCount: (matchCount, showMatchText = true) -> - @hudUI.postMessage {name: "updateMatchesCount", matchCount, showMatchText} - search: (data) -> window.scrollTo findMode.scrollX, findMode.scrollY if findMode.options.returnToViewport findModeQuery.rawQuery = data.query updateFindModeQuery() findMode.findInPlace() + # Show the number of matches in the HUD UI. matchCount = if findModeQuery.parsedQuery.length > 0 then findModeQuery.matchCount else 0 - showCount = findModeQuery.rawQuery.length > 0 - HUD.updateMatchesCount matchCount, showCount + showMatchText = findModeQuery.rawQuery.length > 0 + @hudUI.postMessage {name: "updateMatchesCount", matchCount, showMatchText} # Hide the HUD. # If :immediate is falsy, then the HUD is faded out smoothly (otherwise it is hidden immediately). -- cgit v1.2.3 From 20f2d34cec18d82a7969df8df3705a908e14db31 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 15:48:24 +0100 Subject: Expose getNextQueryFromRegexMatches on window --- content_scripts/vimium_frontend.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 077da3fa..77a1570b 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -757,7 +757,7 @@ selectFoundInputElement = -> DomUtils.isDOMDescendant(findModeAnchorNode, document.activeElement)) DomUtils.simulateSelect(document.activeElement) -getNextQueryFromRegexMatches = (stepSize) -> +window.getNextQueryFromRegexMatches = (stepSize) -> # find()ing an empty query always returns false return "" unless findModeQuery.regexMatches -- cgit v1.2.3 From 32457e8dbe9ed54808738e986694c19c67c3bdd1 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 16:17:51 +0100 Subject: Expose executeFind on window --- content_scripts/vimium_frontend.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 77a1570b..820645bc 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -719,7 +719,7 @@ class FindMode extends Mode window.findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) # :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. -executeFind = (query, options) -> +window.executeFind = (query, options) -> result = null options = options || {} -- cgit v1.2.3 From ffee870176040a77e5bd541a18d1cde002cc23fa Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 04:37:45 +0100 Subject: Move FindMode from vimium_frontend to mode_find --- content_scripts/mode_find.coffee | 44 ++++++++++++++++++++++++++++++++++ content_scripts/vimium_frontend.coffee | 43 --------------------------------- 2 files changed, 44 insertions(+), 43 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index ed08fbd5..2c4fea2a 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -54,5 +54,49 @@ class PostFindMode extends SuppressPrintable handlerStack.remove() @continueBubbling +class FindMode extends Mode + constructor: (@options = {}) -> + # Save the selection, so findInPlace can restore it. + @initialRange = getCurrentRange() + window.findModeQuery = rawQuery: "" + if @options.returnToViewport + @scrollX = window.scrollX + @scrollY = window.scrollY + super + name: "find" + indicator: false + exitOnClick: true + + HUD.showFindMode() + + exit: (event) -> + super() + handleEscapeForFindMode() if event + + restoreSelection: -> + range = @initialRange + selection = getSelection() + selection.removeAllRanges() + selection.addRange range + + findInPlace: -> + # 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() + query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery + window.findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) + +getCurrentRange = -> + selection = getSelection() + if selection.type == "None" + range = document.createRange() + range.setStart document.body, 0 + range.setEnd document.body, 0 + range + else + selection.collapseToStart() if selection.type == "Range" + selection.getRangeAt 0 + root = exports ? window root.PostFindMode = PostFindMode +root.FindMode = FindMode diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 820645bc..b6ca06a2 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -686,38 +686,6 @@ window.handleEnterForFindMode = -> document.body.classList.add("vimiumFindMode") FindModeHistory.saveQuery findModeQuery.rawQuery -class FindMode extends Mode - constructor: (@options = {}) -> - # Save the selection, so findInPlace can restore it. - @initialRange = getCurrentRange() - window.findModeQuery = rawQuery: "" - if @options.returnToViewport - @scrollX = window.scrollX - @scrollY = window.scrollY - super - name: "find" - indicator: false - exitOnClick: true - - HUD.showFindMode() - - exit: (event) -> - super() - handleEscapeForFindMode() if event - - restoreSelection: -> - range = @initialRange - selection = getSelection() - selection.removeAllRanges() - selection.addRange range - - findInPlace: -> - # 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() - query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery - window.findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) - # :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. window.executeFind = (query, options) -> result = null @@ -897,17 +865,6 @@ window.goNext = -> nextStrings = nextPatterns.split(",").filter( (s) -> s.trim().length ) findAndFollowRel("next") || findAndFollowLink(nextStrings) -getCurrentRange = -> - selection = getSelection() - if selection.type == "None" - range = document.createRange() - range.setStart document.body, 0 - range.setEnd document.body, 0 - range - else - selection.collapseToStart() if selection.type == "Range" - selection.getRangeAt 0 - # Enters find mode. Returns the new find-mode instance. window.enterFindMode = -> Marks.setPreviousPosition() -- cgit v1.2.3 From c5a40d4fd34e007d27849f3e480e2ce26829fa7c Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 04:45:58 +0100 Subject: Remove unused argument to HUD.showFindMode --- content_scripts/hud.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index f114c000..3637eede 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -26,9 +26,9 @@ HUD = @hudUI.show {name: "show", text} @tween.fade 1.0, 150 - showFindMode: (text = "") -> + showFindMode: -> return unless @enabled() - @hudUI.show {name: "showFindMode", text} + @hudUI.show {name: "showFindMode", text: ""} @tween.fade 1.0, 150 search: (data) -> -- cgit v1.2.3 From b80f4095b8b8767bb1dfb991fec1e756389f25a2 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 04:49:56 +0100 Subject: Remove global findMode and pass new FindMode instances direct to the HUD --- content_scripts/hud.coffee | 15 ++++++++------- content_scripts/mode_find.coffee | 2 +- content_scripts/vimium_frontend.coffee | 3 +-- 3 files changed, 10 insertions(+), 10 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 3637eede..5e1b955a 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -6,6 +6,7 @@ HUD = tween: null hudUI: null _displayElement: null + findMode: null # This HUD is styled to precisely mimick the chrome HUD on Mac. Use the "has_popup_and_link_hud.html" # test harness to tweak these styles to match Chrome's. One limitation of our HUD display is that @@ -26,16 +27,16 @@ HUD = @hudUI.show {name: "show", text} @tween.fade 1.0, 150 - showFindMode: -> + showFindMode: (@findMode = null) -> return unless @enabled() @hudUI.show {name: "showFindMode", text: ""} @tween.fade 1.0, 150 search: (data) -> - window.scrollTo findMode.scrollX, findMode.scrollY if findMode.options.returnToViewport + window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport findModeQuery.rawQuery = data.query updateFindModeQuery() - findMode.findInPlace() + @findMode.findInPlace() # Show the number of matches in the HUD UI. matchCount = if findModeQuery.parsedQuery.length > 0 then findModeQuery.matchCount else 0 @@ -72,16 +73,16 @@ HUD = @findModeKeydown data.event findModeKeydown: (event) -> - window.scrollTo findMode.scrollX, findMode.scrollY if findMode.options.returnToViewport + window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey - findMode.exit() + @findMode.exit() new PostFindMode if findModeQueryHasResults else if event.keyCode == keyCodes.enter handleEnterForFindMode() - findMode.exit() + @findMode.exit() new PostFindMode if findModeQueryHasResults else if KeyboardUtils.isEscape event - findMode.exit() + @findMode.exit() handleEscapeForFindMode() new PostFindMode if findModeQueryHasResults diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 2c4fea2a..67ec8dd9 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -67,7 +67,7 @@ class FindMode extends Mode indicator: false exitOnClick: true - HUD.showFindMode() + HUD.showFindMode this exit: (event) -> super() diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index b6ca06a2..c1c8288c 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -6,7 +6,6 @@ # window.findModeQuery = { rawQuery: "", matchCount: 0 } -window.findMode = null window.findModeQueryHasResults = false isShowingHelpDialog = false keyPort = null @@ -868,7 +867,7 @@ window.goNext = -> # Enters find mode. Returns the new find-mode instance. window.enterFindMode = -> Marks.setPreviousPosition() - window.findMode = new FindMode() + new FindMode() window.showHelpDialog = (html, fid) -> return if (isShowingHelpDialog || !document.body || fid != frameId) -- cgit v1.2.3 From c4d954394a322d9e5e7257c2ae883f1bb6a7b324 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 04:59:56 +0100 Subject: Move findModeQueryHasResults to findModeQuery.hasResults --- content_scripts/hud.coffee | 6 +++--- content_scripts/mode_find.coffee | 2 +- content_scripts/vimium_frontend.coffee | 13 ++++++------- 3 files changed, 10 insertions(+), 11 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 5e1b955a..7fb70bb5 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -76,15 +76,15 @@ HUD = window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey @findMode.exit() - new PostFindMode if findModeQueryHasResults + new PostFindMode if findModeQuery.hasResults else if event.keyCode == keyCodes.enter handleEnterForFindMode() @findMode.exit() - new PostFindMode if findModeQueryHasResults + new PostFindMode if findModeQuery.hasResults else if KeyboardUtils.isEscape event @findMode.exit() handleEscapeForFindMode() - new PostFindMode if findModeQueryHasResults + new PostFindMode if findModeQuery.hasResults isReady: do -> ready = false diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 67ec8dd9..c0e4d953 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -84,7 +84,7 @@ class FindMode extends Mode # match as the user adds matching characters, or removes previously-matched characters. See #1434. @restoreSelection() query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery - window.findModeQueryHasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) + window.findModeQuery.hasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) getCurrentRange = -> selection = getSelection() diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index c1c8288c..d8327a9f 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -5,8 +5,7 @@ # "domReady". # -window.findModeQuery = { rawQuery: "", matchCount: 0 } -window.findModeQueryHasResults = false +window.findModeQuery = { rawQuery: "", matchCount: 0, hasResults: false } isShowingHelpDialog = false keyPort = null isEnabledForUrl = true @@ -710,7 +709,7 @@ window.executeFind = (query, options) -> restoreDefaultSelectionHighlight = -> document.body.classList.remove("vimiumFindMode") focusFoundLink = -> - if (findModeQueryHasResults) + if (findModeQuery.hasResults) link = getLinkFromSelection() link.focus() if link @@ -719,7 +718,7 @@ selectFoundInputElement = -> # 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 (findModeQueryHasResults && document.activeElement && + if (findModeQuery.hasResults && document.activeElement && DomUtils.isSelectable(document.activeElement) && DomUtils.isDOMDescendant(findModeAnchorNode, document.activeElement)) DomUtils.simulateSelect(document.activeElement) @@ -750,12 +749,12 @@ findAndFocus = (backwards) -> Marks.setPreviousPosition() query = getFindModeQuery backwards - window.findModeQueryHasResults = + window.findModeQuery.hasResults = executeFind(query, { backwards: backwards, caseSensitive: !findModeQuery.ignoreCase }) - if findModeQueryHasResults + if findModeQuery.hasResults focusFoundLink() - new PostFindMode() if findModeQueryHasResults + new PostFindMode() if findModeQuery.hasResults else HUD.showForDuration("No matches for '" + findModeQuery.rawQuery + "'", 1000) -- cgit v1.2.3 From 767ea409e57169cd31ea826398ffa59e07eea353 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 05:18:33 +0100 Subject: Remove redundancy in HUD.findModeKeydown --- content_scripts/hud.coffee | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 7fb70bb5..33656210 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -74,17 +74,18 @@ HUD = findModeKeydown: (event) -> window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport - if event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey - @findMode.exit() - new PostFindMode if findModeQuery.hasResults - else if event.keyCode == keyCodes.enter + postExit = null + + if event.keyCode == keyCodes.enter handleEnterForFindMode() - @findMode.exit() - new PostFindMode if findModeQuery.hasResults else if KeyboardUtils.isEscape event - @findMode.exit() - handleEscapeForFindMode() - new PostFindMode if findModeQuery.hasResults + # We don't want FindMode to handle the click events that handleEscapeForFindMode can generate, so we + # wait until the mode is closed before running it. + postExit = handleEscapeForFindMode + + @findMode.exit() + postExit?() + new PostFindMode if findModeQuery.hasResults isReady: do -> ready = false -- cgit v1.2.3 From e0f2c18286e5485963e40d3711a0cf41d987541e Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 3 Jun 2015 05:21:56 +0100 Subject: Inline HUD.findModeKeydown at its sole callsite --- content_scripts/hud.coffee | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 33656210..30dece24 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -60,6 +60,8 @@ HUD = @tween.fade 0, 150, => @hide true, updateIndicator hideFindMode: (data) -> + window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport + # 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 # this window. @@ -70,11 +72,7 @@ HUD = focusNode?.focus() findModeQuery.rawQuery = data.query - @findModeKeydown data.event - - findModeKeydown: (event) -> - window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport - postExit = null + {event} = data if event.keyCode == keyCodes.enter handleEnterForFindMode() -- cgit v1.2.3 From d2b81a51a1e84c29f192a1ae305a247ca7916a1a Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 15:11:17 +0100 Subject: Move updateFindModeQuery to mode_find.coffee --- content_scripts/mode_find.coffee | 46 ++++++++++++++++++++++++++++++++++ content_scripts/vimium_frontend.coffee | 46 ---------------------------------- 2 files changed, 46 insertions(+), 46 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index c0e4d953..9f300b97 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -86,6 +86,52 @@ class FindMode extends Mode query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery window.findModeQuery.hasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) +# should be called whenever rawQuery is modified. +window.updateFindModeQuery = -> + # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of + # escape sequences. '\' is the escape character and needs to be escaped itself to be used as a normal + # character. here we grep for the relevant escape sequences. + findModeQuery.isRegex = Settings.get 'regexFindMode' + hasNoIgnoreCaseFlag = false + findModeQuery.parsedQuery = findModeQuery.rawQuery.replace /(\\{1,2})([rRI]?)/g, (match, slashes, flag) -> + return match if flag == "" or slashes.length != 1 + switch (flag) + when "r" + findModeQuery.isRegex = true + when "R" + findModeQuery.isRegex = false + when "I" + hasNoIgnoreCaseFlag = true + "" + + # default to 'smartcase' mode, unless noIgnoreCase is explicitly specified + findModeQuery.ignoreCase = !hasNoIgnoreCaseFlag && !Utils.hasUpperCase(findModeQuery.parsedQuery) + + # if we are dealing with a regex, grep for all matches in the text, and then call window.find() on them + # sequentially so the browser handles the scrolling / text selection. + if findModeQuery.isRegex + try + pattern = new RegExp(findModeQuery.parsedQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) + catch error + # if we catch a SyntaxError, assume the user is not done typing yet and return quietly + return + # innerText will not return the text of hidden elements, and strip out tags while preserving newlines + text = document.body.innerText + findModeQuery.regexMatches = text.match(pattern) + findModeQuery.activeRegexIndex = 0 + findModeQuery.matchCount = findModeQuery.regexMatches?.length + # if we are doing a basic plain string match, we still want to grep for matches of the string, so we can + # show a the number of results. We can grep on document.body.innerText, as it should be indistinguishable + # from the internal representation used by window.find. + else + # escape all special characters, so RegExp just parses the string 'as is'. + # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex + escapeRegExp = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g + parsedNonRegexQuery = findModeQuery.parsedQuery.replace(escapeRegExp, (char) -> "\\" + char) + pattern = new RegExp(parsedNonRegexQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) + text = document.body.innerText + findModeQuery.matchCount = text.match(pattern)?.length + getCurrentRange = -> selection = getSelection() if selection.type == "None" diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index d8327a9f..842e9aa3 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -619,52 +619,6 @@ window.refreshCompletionKeys = (response) -> isValidFirstKey = (keyChar) -> validFirstKeys[keyChar] || /^[1-9]/.test(keyChar) -# should be called whenever rawQuery is modified. -window.updateFindModeQuery = -> - # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of - # escape sequences. '\' is the escape character and needs to be escaped itself to be used as a normal - # character. here we grep for the relevant escape sequences. - findModeQuery.isRegex = Settings.get 'regexFindMode' - hasNoIgnoreCaseFlag = false - findModeQuery.parsedQuery = findModeQuery.rawQuery.replace /(\\{1,2})([rRI]?)/g, (match, slashes, flag) -> - return match if flag == "" or slashes.length != 1 - switch (flag) - when "r" - findModeQuery.isRegex = true - when "R" - findModeQuery.isRegex = false - when "I" - hasNoIgnoreCaseFlag = true - "" - - # default to 'smartcase' mode, unless noIgnoreCase is explicitly specified - findModeQuery.ignoreCase = !hasNoIgnoreCaseFlag && !Utils.hasUpperCase(findModeQuery.parsedQuery) - - # if we are dealing with a regex, grep for all matches in the text, and then call window.find() on them - # sequentially so the browser handles the scrolling / text selection. - if findModeQuery.isRegex - try - pattern = new RegExp(findModeQuery.parsedQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) - catch error - # if we catch a SyntaxError, assume the user is not done typing yet and return quietly - return - # innerText will not return the text of hidden elements, and strip out tags while preserving newlines - text = document.body.innerText - findModeQuery.regexMatches = text.match(pattern) - findModeQuery.activeRegexIndex = 0 - findModeQuery.matchCount = findModeQuery.regexMatches?.length - # if we are doing a basic plain string match, we still want to grep for matches of the string, so we can - # show a the number of results. We can grep on document.body.innerText, as it should be indistinguishable - # from the internal representation used by window.find. - else - # escape all special characters, so RegExp just parses the string 'as is'. - # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex - escapeRegExp = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g - parsedNonRegexQuery = findModeQuery.parsedQuery.replace(escapeRegExp, (char) -> "\\" + char) - pattern = new RegExp(parsedNonRegexQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) - text = document.body.innerText - findModeQuery.matchCount = text.match(pattern)?.length - window.handleEscapeForFindMode = -> document.body.classList.remove("vimiumFindMode") # removing the class does not re-color existing selections. we recreate the current selection so it reverts -- cgit v1.2.3 From dbf7d4eb053cc99f2c29b56ea61a671ec46fac62 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 15:14:37 +0100 Subject: Move updateFindModeQuery to FindMode.updateQuery --- content_scripts/hud.coffee | 2 +- content_scripts/mode_find.coffee | 86 +++++++++++++++++----------------- content_scripts/vimium_frontend.coffee | 2 +- 3 files changed, 45 insertions(+), 45 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 30dece24..7c4b021c 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -35,7 +35,7 @@ HUD = search: (data) -> window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport findModeQuery.rawQuery = data.query - updateFindModeQuery() + FindMode.updateQuery() @findMode.findInPlace() # Show the number of matches in the HUD UI. diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 9f300b97..54c8438a 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -86,51 +86,51 @@ class FindMode extends Mode query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery window.findModeQuery.hasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) -# should be called whenever rawQuery is modified. -window.updateFindModeQuery = -> - # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of - # escape sequences. '\' is the escape character and needs to be escaped itself to be used as a normal - # character. here we grep for the relevant escape sequences. - findModeQuery.isRegex = Settings.get 'regexFindMode' - hasNoIgnoreCaseFlag = false - findModeQuery.parsedQuery = findModeQuery.rawQuery.replace /(\\{1,2})([rRI]?)/g, (match, slashes, flag) -> - return match if flag == "" or slashes.length != 1 - switch (flag) - when "r" - findModeQuery.isRegex = true - when "R" - findModeQuery.isRegex = false - when "I" - hasNoIgnoreCaseFlag = true - "" + # should be called whenever rawQuery is modified. + @updateQuery: -> + # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of + # escape sequences. '\' is the escape character and needs to be escaped itself to be used as a normal + # character. here we grep for the relevant escape sequences. + findModeQuery.isRegex = Settings.get 'regexFindMode' + hasNoIgnoreCaseFlag = false + findModeQuery.parsedQuery = findModeQuery.rawQuery.replace /(\\{1,2})([rRI]?)/g, (match, slashes, flag) -> + return match if flag == "" or slashes.length != 1 + switch (flag) + when "r" + findModeQuery.isRegex = true + when "R" + findModeQuery.isRegex = false + when "I" + hasNoIgnoreCaseFlag = true + "" - # default to 'smartcase' mode, unless noIgnoreCase is explicitly specified - findModeQuery.ignoreCase = !hasNoIgnoreCaseFlag && !Utils.hasUpperCase(findModeQuery.parsedQuery) + # default to 'smartcase' mode, unless noIgnoreCase is explicitly specified + findModeQuery.ignoreCase = !hasNoIgnoreCaseFlag && !Utils.hasUpperCase(findModeQuery.parsedQuery) - # if we are dealing with a regex, grep for all matches in the text, and then call window.find() on them - # sequentially so the browser handles the scrolling / text selection. - if findModeQuery.isRegex - try - pattern = new RegExp(findModeQuery.parsedQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) - catch error - # if we catch a SyntaxError, assume the user is not done typing yet and return quietly - return - # innerText will not return the text of hidden elements, and strip out tags while preserving newlines - text = document.body.innerText - findModeQuery.regexMatches = text.match(pattern) - findModeQuery.activeRegexIndex = 0 - findModeQuery.matchCount = findModeQuery.regexMatches?.length - # if we are doing a basic plain string match, we still want to grep for matches of the string, so we can - # show a the number of results. We can grep on document.body.innerText, as it should be indistinguishable - # from the internal representation used by window.find. - else - # escape all special characters, so RegExp just parses the string 'as is'. - # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex - escapeRegExp = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g - parsedNonRegexQuery = findModeQuery.parsedQuery.replace(escapeRegExp, (char) -> "\\" + char) - pattern = new RegExp(parsedNonRegexQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) - text = document.body.innerText - findModeQuery.matchCount = text.match(pattern)?.length + # if we are dealing with a regex, grep for all matches in the text, and then call window.find() on them + # sequentially so the browser handles the scrolling / text selection. + if findModeQuery.isRegex + try + pattern = new RegExp(findModeQuery.parsedQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) + catch error + # if we catch a SyntaxError, assume the user is not done typing yet and return quietly + return + # innerText will not return the text of hidden elements, and strip out tags while preserving newlines + text = document.body.innerText + findModeQuery.regexMatches = text.match(pattern) + findModeQuery.activeRegexIndex = 0 + findModeQuery.matchCount = findModeQuery.regexMatches?.length + # if we are doing a basic plain string match, we still want to grep for matches of the string, so we can + # show a the number of results. We can grep on document.body.innerText, as it should be indistinguishable + # from the internal representation used by window.find. + else + # escape all special characters, so RegExp just parses the string 'as is'. + # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex + escapeRegExp = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g + parsedNonRegexQuery = findModeQuery.parsedQuery.replace(escapeRegExp, (char) -> "\\" + char) + pattern = new RegExp(parsedNonRegexQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) + text = document.body.innerText + findModeQuery.matchCount = text.match(pattern)?.length getCurrentRange = -> selection = getSelection() diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 842e9aa3..13e07e7a 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -692,7 +692,7 @@ window.getFindModeQuery = (backwards) -> mostRecentQuery = FindModeHistory.getQuery() if (mostRecentQuery != findModeQuery.rawQuery) findModeQuery.rawQuery = mostRecentQuery - updateFindModeQuery() + FindMode.updateQuery() if findModeQuery.isRegex getNextQueryFromRegexMatches(if backwards then -1 else 1) -- cgit v1.2.3 From 036a85c275fe7404e47b8c1657232fba971a803e Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 15:52:07 +0100 Subject: Move getNextQueryFromFindModeMatches and getFindModeQuery into mode_find --- content_scripts/mode_find.coffee | 22 ++++++++++++++++++++++ content_scripts/vimium_frontend.coffee | 22 ---------------------- 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 54c8438a..e65450f2 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -132,6 +132,28 @@ class FindMode extends Mode text = document.body.innerText findModeQuery.matchCount = text.match(pattern)?.length +window.getNextQueryFromRegexMatches = (stepSize) -> + # find()ing an empty query always returns false + return "" unless findModeQuery.regexMatches + + totalMatches = findModeQuery.regexMatches.length + findModeQuery.activeRegexIndex += stepSize + totalMatches + findModeQuery.activeRegexIndex %= totalMatches + + findModeQuery.regexMatches[findModeQuery.activeRegexIndex] + +window.getFindModeQuery = (backwards) -> + # check if the query has been changed by a script in another frame + mostRecentQuery = FindModeHistory.getQuery() + if (mostRecentQuery != findModeQuery.rawQuery) + findModeQuery.rawQuery = mostRecentQuery + FindMode.updateQuery() + + if findModeQuery.isRegex + getNextQueryFromRegexMatches(if backwards then -1 else 1) + else + findModeQuery.parsedQuery + getCurrentRange = -> selection = getSelection() if selection.type == "None" diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 13e07e7a..80da43bb 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -677,28 +677,6 @@ selectFoundInputElement = -> DomUtils.isDOMDescendant(findModeAnchorNode, document.activeElement)) DomUtils.simulateSelect(document.activeElement) -window.getNextQueryFromRegexMatches = (stepSize) -> - # find()ing an empty query always returns false - return "" unless findModeQuery.regexMatches - - totalMatches = findModeQuery.regexMatches.length - findModeQuery.activeRegexIndex += stepSize + totalMatches - findModeQuery.activeRegexIndex %= totalMatches - - findModeQuery.regexMatches[findModeQuery.activeRegexIndex] - -window.getFindModeQuery = (backwards) -> - # check if the query has been changed by a script in another frame - mostRecentQuery = FindModeHistory.getQuery() - if (mostRecentQuery != findModeQuery.rawQuery) - findModeQuery.rawQuery = mostRecentQuery - FindMode.updateQuery() - - if findModeQuery.isRegex - getNextQueryFromRegexMatches(if backwards then -1 else 1) - else - findModeQuery.parsedQuery - findAndFocus = (backwards) -> Marks.setPreviousPosition() query = getFindModeQuery backwards -- cgit v1.2.3 From 1c076fbcf9d37533cc24e30476e23c050f1eda0f Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 15:56:30 +0100 Subject: Integrate functions exposed on window into FindMode --- content_scripts/mode_find.coffee | 36 ++++++++++++++++----------------- content_scripts/mode_visual_edit.coffee | 2 +- content_scripts/vimium_frontend.coffee | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index e65450f2..4d7f3972 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -83,7 +83,7 @@ class FindMode extends Mode # 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() - query = if findModeQuery.isRegex then getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery + query = if findModeQuery.isRegex then FindMode.getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery window.findModeQuery.hasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) # should be called whenever rawQuery is modified. @@ -132,27 +132,27 @@ class FindMode extends Mode text = document.body.innerText findModeQuery.matchCount = text.match(pattern)?.length -window.getNextQueryFromRegexMatches = (stepSize) -> - # find()ing an empty query always returns false - return "" unless findModeQuery.regexMatches + @getNextQueryFromRegexMatches: (stepSize) -> + # find()ing an empty query always returns false + return "" unless findModeQuery.regexMatches - totalMatches = findModeQuery.regexMatches.length - findModeQuery.activeRegexIndex += stepSize + totalMatches - findModeQuery.activeRegexIndex %= totalMatches + totalMatches = findModeQuery.regexMatches.length + findModeQuery.activeRegexIndex += stepSize + totalMatches + findModeQuery.activeRegexIndex %= totalMatches - findModeQuery.regexMatches[findModeQuery.activeRegexIndex] + findModeQuery.regexMatches[findModeQuery.activeRegexIndex] -window.getFindModeQuery = (backwards) -> - # check if the query has been changed by a script in another frame - mostRecentQuery = FindModeHistory.getQuery() - if (mostRecentQuery != findModeQuery.rawQuery) - findModeQuery.rawQuery = mostRecentQuery - FindMode.updateQuery() + @getQuery: (backwards) -> + # check if the query has been changed by a script in another frame + mostRecentQuery = FindModeHistory.getQuery() + if (mostRecentQuery != findModeQuery.rawQuery) + findModeQuery.rawQuery = mostRecentQuery + FindMode.updateQuery() - if findModeQuery.isRegex - getNextQueryFromRegexMatches(if backwards then -1 else 1) - else - findModeQuery.parsedQuery + if findModeQuery.isRegex + @getNextQueryFromRegexMatches(if backwards then -1 else 1) + else + findModeQuery.parsedQuery getCurrentRange = -> selection = getSelection() diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index 2abccaac..e01df819 100644 --- a/content_scripts/mode_visual_edit.coffee +++ b/content_scripts/mode_visual_edit.coffee @@ -352,7 +352,7 @@ class Movement extends CountPrefix unless @options.parentMode or options.oneMovementOnly do => executeFind = (count, findBackwards) => - if query = getFindModeQuery findBackwards + if query = FindMode.getQuery findBackwards initialRange = @selection.getRangeAt(0).cloneRange() for [0...count] unless window.find query, Utils.hasUpperCase(query), findBackwards, true, false, true, false diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 80da43bb..a71a094e 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -679,7 +679,7 @@ selectFoundInputElement = -> findAndFocus = (backwards) -> Marks.setPreviousPosition() - query = getFindModeQuery backwards + query = FindMode.getQuery backwards window.findModeQuery.hasResults = executeFind(query, { backwards: backwards, caseSensitive: !findModeQuery.ignoreCase }) -- cgit v1.2.3 From 409fce2a3663f7e100705dc9d0617514eed48a99 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 15:58:32 +0100 Subject: Remove redundant conditional --- content_scripts/vimium_frontend.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index a71a094e..89a212c1 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -686,7 +686,7 @@ findAndFocus = (backwards) -> if findModeQuery.hasResults focusFoundLink() - new PostFindMode() if findModeQuery.hasResults + new PostFindMode() else HUD.showForDuration("No matches for '" + findModeQuery.rawQuery + "'", 1000) -- cgit v1.2.3 From 25402831343e647a7d9ab96f390d48b07cb44488 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 16:22:57 +0100 Subject: Add FindMode.saveQuery to avoid addressing findModeQuery directly --- content_scripts/mode_find.coffee | 2 ++ content_scripts/vimium_frontend.coffee | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 4d7f3972..ad3d3408 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -154,6 +154,8 @@ class FindMode extends Mode else findModeQuery.parsedQuery + @saveQuery: -> FindModeHistory.saveQuery findModeQuery.rawQuery + getCurrentRange = -> selection = getSelection() if selection.type == "None" diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 89a212c1..8f53282b 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -636,7 +636,7 @@ window.handleEscapeForFindMode = -> window.handleEnterForFindMode = -> focusFoundLink() document.body.classList.add("vimiumFindMode") - FindModeHistory.saveQuery findModeQuery.rawQuery + FindMode.saveQuery() # :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. window.executeFind = (query, options) -> -- cgit v1.2.3 From d51bd435ecac2890e38bcb420ece5ada52c4fc21 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 16:30:26 +0100 Subject: Simplify executeFind by hardcoding default arguments --- content_scripts/mode_find.coffee | 2 +- content_scripts/vimium_frontend.coffee | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index ad3d3408..a252193d 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -84,7 +84,7 @@ class FindMode extends Mode # match as the user adds matching characters, or removes previously-matched characters. See #1434. @restoreSelection() query = if findModeQuery.isRegex then FindMode.getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery - window.findModeQuery.hasResults = executeFind(query, { caseSensitive: !findModeQuery.ignoreCase }) + window.findModeQuery.hasResults = executeFind query # should be called whenever rawQuery is modified. @updateQuery: -> diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 8f53282b..8a5114cd 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -641,7 +641,8 @@ window.handleEnterForFindMode = -> # :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. window.executeFind = (query, options) -> result = null - options = options || {} + options = extend {backwards: false, caseSensitive: !findModeQuery.ignoreCase}, options + query ?= FindMode.getQuery options.backwards document.body.classList.add("vimiumFindMode") @@ -679,10 +680,8 @@ selectFoundInputElement = -> findAndFocus = (backwards) -> Marks.setPreviousPosition() - query = FindMode.getQuery backwards - window.findModeQuery.hasResults = - executeFind(query, { backwards: backwards, caseSensitive: !findModeQuery.ignoreCase }) + executeFind null, {backwards} if findModeQuery.hasResults focusFoundLink() -- cgit v1.2.3 From f7cd2560ffa40d046065d351bc73481c51b9388e Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 16:32:37 +0100 Subject: Coffee-ify strings, fix no match regexp search message in visual mode --- content_scripts/mode_visual_edit.coffee | 2 +- content_scripts/vimium_frontend.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index e01df819..1be5def2 100644 --- a/content_scripts/mode_visual_edit.coffee +++ b/content_scripts/mode_visual_edit.coffee @@ -357,7 +357,7 @@ class Movement extends CountPrefix for [0...count] unless window.find query, Utils.hasUpperCase(query), findBackwards, true, false, true, false @setSelectionRange initialRange - HUD.showForDuration("No matches for '" + query + "'", 1000) + HUD.showForDuration("No matches for '#{findModeQuery.rawQuery}'", 1000) return # The find was successfull. If we're in caret mode, then we should now have a selection, so we can # drop back into visual mode. diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 8a5114cd..29267c68 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -687,7 +687,7 @@ findAndFocus = (backwards) -> focusFoundLink() new PostFindMode() else - HUD.showForDuration("No matches for '" + findModeQuery.rawQuery + "'", 1000) + HUD.showForDuration("No matches for '#{findModeQuery.rawQuery}'", 1000) window.performFind = -> findAndFocus() -- cgit v1.2.3 From cef0526a2c2ef3a99a88b1c52d0c027e7e5cd0b9 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 16:47:47 +0100 Subject: Make find from visual mode behave the same as a normal find --- content_scripts/mode_visual_edit.coffee | 27 +++++++++++++-------------- content_scripts/vimium_frontend.coffee | 21 ++++++++++++++------- 2 files changed, 27 insertions(+), 21 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index 1be5def2..a62b2738 100644 --- a/content_scripts/mode_visual_edit.coffee +++ b/content_scripts/mode_visual_edit.coffee @@ -351,20 +351,19 @@ class Movement extends CountPrefix # element), or if this instance has been created to execute only a single movement. unless @options.parentMode or options.oneMovementOnly do => - executeFind = (count, findBackwards) => - if query = FindMode.getQuery findBackwards - initialRange = @selection.getRangeAt(0).cloneRange() - for [0...count] - unless window.find query, Utils.hasUpperCase(query), findBackwards, true, false, true, false - @setSelectionRange initialRange - HUD.showForDuration("No matches for '#{findModeQuery.rawQuery}'", 1000) - return - # The find was successfull. If we're in caret mode, then we should now have a selection, so we can - # drop back into visual mode. - @changeMode VisualMode if @name == "caret" and 0 < @selection.toString().length - - @movements.n = (count) -> executeFind count, false - @movements.N = (count) -> executeFind count, true + doFind = (count, backwards) => + initialRange = @selection.getRangeAt(0).cloneRange() + for [0...count] by 1 + unless executeFind null, {colorSelection: false, backwards} + @setSelectionRange initialRange + HUD.showForDuration("No matches for '#{findModeQuery.rawQuery}'", 1000) + return + # The find was successfull. If we're in caret mode, then we should now have a selection, so we can + # drop back into visual mode. + @changeMode VisualMode if @name == "caret" and 0 < @selection.toString().length + + @movements.n = (count) -> doFind count, false + @movements.N = (count) -> doFind count, true @movements["/"] = -> @findMode = new FindMode returnToViewport: true @findMode.onExit => @changeMode VisualMode diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 29267c68..6ea5080d 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -641,17 +641,24 @@ window.handleEnterForFindMode = -> # :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. window.executeFind = (query, options) -> result = null - options = extend {backwards: false, caseSensitive: !findModeQuery.ignoreCase}, options + options = extend { + backwards: false + caseSensitive: !findModeQuery.ignoreCase + colorSelection: true + }, options query ?= FindMode.getQuery options.backwards - document.body.classList.add("vimiumFindMode") + if options.colorSelection + document.body.classList.add("vimiumFindMode") + # ignore the selectionchange event generated by find() + document.removeEventListener("selectionchange",restoreDefaultSelectionHighlight, true) - # ignore the selectionchange event generated by find() - document.removeEventListener("selectionchange",restoreDefaultSelectionHighlight, true) result = window.find(query, options.caseSensitive, options.backwards, true, false, true, false) - setTimeout( - -> document.addEventListener("selectionchange", restoreDefaultSelectionHighlight, true) - 0) + + if options.colorSelection + setTimeout( + -> document.addEventListener("selectionchange", restoreDefaultSelectionHighlight, true) + , 0) # We are either in normal mode ("n"), or find mode ("/"). We are not in insert mode. Nevertheless, if a # previous find landed in an editable element, then that element may still be activated. In this case, we -- cgit v1.2.3 From 5bace210e738c56a8e7bee946f21041158fea7b1 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 17:06:31 +0100 Subject: Move findModeQuery to FindMode.query --- content_scripts/hud.coffee | 10 +++--- content_scripts/mode_find.coffee | 59 ++++++++++++++++++--------------- content_scripts/mode_visual_edit.coffee | 2 +- content_scripts/vimium_frontend.coffee | 14 ++++---- 4 files changed, 44 insertions(+), 41 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 7c4b021c..03edb52e 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -34,13 +34,13 @@ HUD = search: (data) -> window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport - findModeQuery.rawQuery = data.query + FindMode.query.rawQuery = data.query FindMode.updateQuery() @findMode.findInPlace() # Show the number of matches in the HUD UI. - matchCount = if findModeQuery.parsedQuery.length > 0 then findModeQuery.matchCount else 0 - showMatchText = findModeQuery.rawQuery.length > 0 + matchCount = if FindMode.query.parsedQuery.length > 0 then FindMode.query.matchCount else 0 + showMatchText = FindMode.query.rawQuery.length > 0 @hudUI.postMessage {name: "updateMatchesCount", matchCount, showMatchText} # Hide the HUD. @@ -71,7 +71,7 @@ HUD = document.activeElement?.blur() focusNode?.focus() - findModeQuery.rawQuery = data.query + FindMode.query.rawQuery = data.query {event} = data if event.keyCode == keyCodes.enter @@ -83,7 +83,7 @@ HUD = @findMode.exit() postExit?() - new PostFindMode if findModeQuery.hasResults + new PostFindMode if FindMode.query.hasResults isReady: do -> ready = false diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index a252193d..e6884a83 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -55,10 +55,15 @@ class PostFindMode extends SuppressPrintable @continueBubbling class FindMode extends Mode + @query: + rawQuery: "" + matchCount: 0 + hasResults: false + constructor: (@options = {}) -> # Save the selection, so findInPlace can restore it. @initialRange = getCurrentRange() - window.findModeQuery = rawQuery: "" + FindMode.query = rawQuery: "" if @options.returnToViewport @scrollX = window.scrollX @scrollY = window.scrollY @@ -83,43 +88,43 @@ class FindMode extends Mode # 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() - query = if findModeQuery.isRegex then FindMode.getNextQueryFromRegexMatches(0) else findModeQuery.parsedQuery - window.findModeQuery.hasResults = executeFind query + query = if FindMode.query.isRegex then FindMode.getNextQueryFromRegexMatches(0) else FindMode.query.parsedQuery + FindMode.query.hasResults = executeFind query # should be called whenever rawQuery is modified. @updateQuery: -> # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of # escape sequences. '\' is the escape character and needs to be escaped itself to be used as a normal # character. here we grep for the relevant escape sequences. - findModeQuery.isRegex = Settings.get 'regexFindMode' + @query.isRegex = Settings.get 'regexFindMode' hasNoIgnoreCaseFlag = false - findModeQuery.parsedQuery = findModeQuery.rawQuery.replace /(\\{1,2})([rRI]?)/g, (match, slashes, flag) -> + @query.parsedQuery = @query.rawQuery.replace /(\\{1,2})([rRI]?)/g, (match, slashes, flag) -> return match if flag == "" or slashes.length != 1 switch (flag) when "r" - findModeQuery.isRegex = true + @query.isRegex = true when "R" - findModeQuery.isRegex = false + @query.isRegex = false when "I" hasNoIgnoreCaseFlag = true "" # default to 'smartcase' mode, unless noIgnoreCase is explicitly specified - findModeQuery.ignoreCase = !hasNoIgnoreCaseFlag && !Utils.hasUpperCase(findModeQuery.parsedQuery) + @query.ignoreCase = !hasNoIgnoreCaseFlag && !Utils.hasUpperCase(@query.parsedQuery) # if we are dealing with a regex, grep for all matches in the text, and then call window.find() on them # sequentially so the browser handles the scrolling / text selection. - if findModeQuery.isRegex + if @query.isRegex try - pattern = new RegExp(findModeQuery.parsedQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) + pattern = new RegExp(@query.parsedQuery, "g" + (if @query.ignoreCase then "i" else "")) catch error # if we catch a SyntaxError, assume the user is not done typing yet and return quietly return # innerText will not return the text of hidden elements, and strip out tags while preserving newlines text = document.body.innerText - findModeQuery.regexMatches = text.match(pattern) - findModeQuery.activeRegexIndex = 0 - findModeQuery.matchCount = findModeQuery.regexMatches?.length + @query.regexMatches = text.match(pattern) + @query.activeRegexIndex = 0 + @query.matchCount = @query.regexMatches?.length # if we are doing a basic plain string match, we still want to grep for matches of the string, so we can # show a the number of results. We can grep on document.body.innerText, as it should be indistinguishable # from the internal representation used by window.find. @@ -127,34 +132,34 @@ class FindMode extends Mode # escape all special characters, so RegExp just parses the string 'as is'. # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex escapeRegExp = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g - parsedNonRegexQuery = findModeQuery.parsedQuery.replace(escapeRegExp, (char) -> "\\" + char) - pattern = new RegExp(parsedNonRegexQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) + parsedNonRegexQuery = @query.parsedQuery.replace(escapeRegExp, (char) -> "\\" + char) + pattern = new RegExp(parsedNonRegexQuery, "g" + (if @query.ignoreCase then "i" else "")) text = document.body.innerText - findModeQuery.matchCount = text.match(pattern)?.length + @query.matchCount = text.match(pattern)?.length @getNextQueryFromRegexMatches: (stepSize) -> # find()ing an empty query always returns false - return "" unless findModeQuery.regexMatches + return "" unless @query.regexMatches - totalMatches = findModeQuery.regexMatches.length - findModeQuery.activeRegexIndex += stepSize + totalMatches - findModeQuery.activeRegexIndex %= totalMatches + totalMatches = @query.regexMatches.length + @query.activeRegexIndex += stepSize + totalMatches + @query.activeRegexIndex %= totalMatches - findModeQuery.regexMatches[findModeQuery.activeRegexIndex] + @query.regexMatches[@query.activeRegexIndex] @getQuery: (backwards) -> # check if the query has been changed by a script in another frame mostRecentQuery = FindModeHistory.getQuery() - if (mostRecentQuery != findModeQuery.rawQuery) - findModeQuery.rawQuery = mostRecentQuery - FindMode.updateQuery() + if (mostRecentQuery != @query.rawQuery) + @query.rawQuery = mostRecentQuery + @updateQuery() - if findModeQuery.isRegex + if @query.isRegex @getNextQueryFromRegexMatches(if backwards then -1 else 1) else - findModeQuery.parsedQuery + @query.parsedQuery - @saveQuery: -> FindModeHistory.saveQuery findModeQuery.rawQuery + @saveQuery: -> FindModeHistory.saveQuery @query.rawQuery getCurrentRange = -> selection = getSelection() diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index a62b2738..0df93ab7 100644 --- a/content_scripts/mode_visual_edit.coffee +++ b/content_scripts/mode_visual_edit.coffee @@ -356,7 +356,7 @@ class Movement extends CountPrefix for [0...count] by 1 unless executeFind null, {colorSelection: false, backwards} @setSelectionRange initialRange - HUD.showForDuration("No matches for '#{findModeQuery.rawQuery}'", 1000) + HUD.showForDuration("No matches for '#{FindMode.query.rawQuery}'", 1000) return # The find was successfull. If we're in caret mode, then we should now have a selection, so we can # drop back into visual mode. diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 6ea5080d..65527880 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -5,7 +5,6 @@ # "domReady". # -window.findModeQuery = { rawQuery: "", matchCount: 0, hasResults: false } isShowingHelpDialog = false keyPort = null isEnabledForUrl = true @@ -643,7 +642,7 @@ window.executeFind = (query, options) -> result = null options = extend { backwards: false - caseSensitive: !findModeQuery.ignoreCase + caseSensitive: !FindMode.query.ignoreCase colorSelection: true }, options query ?= FindMode.getQuery options.backwards @@ -671,7 +670,7 @@ window.executeFind = (query, options) -> restoreDefaultSelectionHighlight = -> document.body.classList.remove("vimiumFindMode") focusFoundLink = -> - if (findModeQuery.hasResults) + if (FindMode.query.hasResults) link = getLinkFromSelection() link.focus() if link @@ -680,21 +679,20 @@ selectFoundInputElement = -> # 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 (findModeQuery.hasResults && document.activeElement && + if (FindMode.query.hasResults && document.activeElement && DomUtils.isSelectable(document.activeElement) && DomUtils.isDOMDescendant(findModeAnchorNode, document.activeElement)) DomUtils.simulateSelect(document.activeElement) findAndFocus = (backwards) -> Marks.setPreviousPosition() - window.findModeQuery.hasResults = - executeFind null, {backwards} + FindMode.query.hasResults = executeFind null, {backwards} - if findModeQuery.hasResults + if FindMode.query.hasResults focusFoundLink() new PostFindMode() else - HUD.showForDuration("No matches for '#{findModeQuery.rawQuery}'", 1000) + HUD.showForDuration("No matches for '#{FindMode.query.rawQuery}'", 1000) window.performFind = -> findAndFocus() -- cgit v1.2.3 From 6993359b4636ed53e558218beecc8ec9deb4ed70 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 17:12:09 +0100 Subject: Integrate executeFind into FindMode as FindMode.execute --- content_scripts/mode_find.coffee | 34 ++++++++++++++++++++++++++++++++- content_scripts/mode_visual_edit.coffee | 2 +- content_scripts/vimium_frontend.coffee | 34 +-------------------------------- 3 files changed, 35 insertions(+), 35 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index e6884a83..79d2be7e 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -89,7 +89,7 @@ class FindMode extends Mode # match as the user adds matching characters, or removes previously-matched characters. See #1434. @restoreSelection() query = if FindMode.query.isRegex then FindMode.getNextQueryFromRegexMatches(0) else FindMode.query.parsedQuery - FindMode.query.hasResults = executeFind query + FindMode.query.hasResults = FindMode.execute query # should be called whenever rawQuery is modified. @updateQuery: -> @@ -161,6 +161,38 @@ class FindMode extends Mode @saveQuery: -> FindModeHistory.saveQuery @query.rawQuery + # :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. + @execute: (query, options) -> + result = null + options = extend { + backwards: false + caseSensitive: !@query.ignoreCase + colorSelection: true + }, options + query ?= FindMode.getQuery options.backwards + + if options.colorSelection + document.body.classList.add("vimiumFindMode") + # ignore the selectionchange event generated by find() + document.removeEventListener("selectionchange", @restoreDefaultSelectionHighlight, true) + + result = window.find(query, options.caseSensitive, options.backwards, true, false, true, false) + + if options.colorSelection + setTimeout( + -> document.addEventListener("selectionchange", @restoreDefaultSelectionHighlight, true) + , 0) + + # We are either in normal mode ("n"), or find mode ("/"). We are not in insert mode. Nevertheless, if a + # previous find landed in an editable element, then that element may still be activated. In this case, we + # don't want to leave it behind (see #1412). + if document.activeElement and DomUtils.isEditable document.activeElement + document.activeElement.blur() unless DomUtils.isSelected document.activeElement + + result + + @restoreDefaultSelectionHighlight: -> document.body.classList.remove("vimiumFindMode") + getCurrentRange = -> selection = getSelection() if selection.type == "None" diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index 0df93ab7..eaaf94d4 100644 --- a/content_scripts/mode_visual_edit.coffee +++ b/content_scripts/mode_visual_edit.coffee @@ -354,7 +354,7 @@ class Movement extends CountPrefix doFind = (count, backwards) => initialRange = @selection.getRangeAt(0).cloneRange() for [0...count] by 1 - unless executeFind null, {colorSelection: false, backwards} + unless FindMode.execute null, {colorSelection: false, backwards} @setSelectionRange initialRange HUD.showForDuration("No matches for '#{FindMode.query.rawQuery}'", 1000) return diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 65527880..6f50fb9d 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -637,38 +637,6 @@ window.handleEnterForFindMode = -> document.body.classList.add("vimiumFindMode") FindMode.saveQuery() -# :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'. -window.executeFind = (query, options) -> - result = null - options = extend { - backwards: false - caseSensitive: !FindMode.query.ignoreCase - colorSelection: true - }, options - query ?= FindMode.getQuery options.backwards - - if options.colorSelection - document.body.classList.add("vimiumFindMode") - # ignore the selectionchange event generated by find() - document.removeEventListener("selectionchange",restoreDefaultSelectionHighlight, true) - - result = window.find(query, options.caseSensitive, options.backwards, true, false, true, false) - - if options.colorSelection - setTimeout( - -> document.addEventListener("selectionchange", restoreDefaultSelectionHighlight, true) - , 0) - - # We are either in normal mode ("n"), or find mode ("/"). We are not in insert mode. Nevertheless, if a - # previous find landed in an editable element, then that element may still be activated. In this case, we - # don't want to leave it behind (see #1412). - if document.activeElement and DomUtils.isEditable document.activeElement - document.activeElement.blur() unless DomUtils.isSelected document.activeElement - - result - -restoreDefaultSelectionHighlight = -> document.body.classList.remove("vimiumFindMode") - focusFoundLink = -> if (FindMode.query.hasResults) link = getLinkFromSelection() @@ -686,7 +654,7 @@ selectFoundInputElement = -> findAndFocus = (backwards) -> Marks.setPreviousPosition() - FindMode.query.hasResults = executeFind null, {backwards} + FindMode.query.hasResults = FindMode.execute null, {backwards} if FindMode.query.hasResults focusFoundLink() -- cgit v1.2.3 From 34453f7e762a4cf3899d8cd3b83ad72ea974576f Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Wed, 10 Jun 2015 17:48:04 +0100 Subject: Update rawQuery directly from FindMode.updateQuery --- content_scripts/hud.coffee | 4 +--- content_scripts/mode_find.coffee | 7 +++---- 2 files changed, 4 insertions(+), 7 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index 03edb52e..fcd465c8 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -34,8 +34,7 @@ HUD = search: (data) -> window.scrollTo @findMode.scrollX, @findMode.scrollY if @findMode.options.returnToViewport - FindMode.query.rawQuery = data.query - FindMode.updateQuery() + FindMode.updateQuery data.query @findMode.findInPlace() # Show the number of matches in the HUD UI. @@ -71,7 +70,6 @@ HUD = document.activeElement?.blur() focusNode?.focus() - FindMode.query.rawQuery = data.query {event} = data if event.keyCode == keyCodes.enter diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 79d2be7e..ff9506e7 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -91,8 +91,8 @@ class FindMode extends Mode query = if FindMode.query.isRegex then FindMode.getNextQueryFromRegexMatches(0) else FindMode.query.parsedQuery FindMode.query.hasResults = FindMode.execute query - # should be called whenever rawQuery is modified. - @updateQuery: -> + @updateQuery: (query) -> + @query.rawQuery = query # the query can be treated differently (e.g. as a plain string versus regex depending on the presence of # escape sequences. '\' is the escape character and needs to be escaped itself to be used as a normal # character. here we grep for the relevant escape sequences. @@ -151,8 +151,7 @@ class FindMode extends Mode # check if the query has been changed by a script in another frame mostRecentQuery = FindModeHistory.getQuery() if (mostRecentQuery != @query.rawQuery) - @query.rawQuery = mostRecentQuery - @updateQuery() + @updateQuery mostRecentQuery if @query.isRegex @getNextQueryFromRegexMatches(if backwards then -1 else 1) -- cgit v1.2.3 From 26a0653318be7bae4254e2e643dcb5ae5e2498e9 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Thu, 11 Jun 2015 15:22:37 +0100 Subject: Make exiting FindMode with work as it should again This undoes the effect of the breaking refactor in 73f66f25e6b8e5b5b8456074ad4fa79ba1d3ca4d, where PostFindMode was entered whenever FindMode was exited, instead of only when it was exited with . --- content_scripts/hud.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'content_scripts') diff --git a/content_scripts/hud.coffee b/content_scripts/hud.coffee index fcd465c8..cd34efe8 100644 --- a/content_scripts/hud.coffee +++ b/content_scripts/hud.coffee @@ -74,6 +74,8 @@ HUD = if event.keyCode == keyCodes.enter handleEnterForFindMode() + if FindMode.query.hasResults + postExit = -> new PostFindMode else if KeyboardUtils.isEscape event # We don't want FindMode to handle the click events that handleEscapeForFindMode can generate, so we # wait until the mode is closed before running it. @@ -81,7 +83,6 @@ HUD = @findMode.exit() postExit?() - new PostFindMode if FindMode.query.hasResults isReady: do -> ready = false -- cgit v1.2.3 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 From c201ee72988c0cacc326023c3ff7f1cacc2b62e0 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Sat, 13 Jun 2015 14:26:19 +0100 Subject: Clean up code after merge --- content_scripts/mode_find.coffee | 1 - content_scripts/mode_visual_edit.coffee | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index bba457b6..d7c628be 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -61,7 +61,6 @@ class FindMode extends Mode hasResults: false constructor: (options = {}) -> - console.log "constructor", @options # Save the selection, so findInPlace can restore it. @initialRange = getCurrentRange() FindMode.query = rawQuery: "" diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee index 170071ac..eaaf94d4 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] + for [0...count] by 1 unless FindMode.execute null, {colorSelection: false, backwards} @setSelectionRange initialRange HUD.showForDuration("No matches for '#{FindMode.query.rawQuery}'", 1000) -- cgit v1.2.3