From a938c55e0a08d82abbd200a8e7609ceaa8ee539a Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 18 Jan 2015 13:07:39 +0000 Subject: Give focusInput a memory. - Track the most recently-focused input. - When focusInput is launched (with a count of 1) start with the most recently focused input. This does two things: - If the user is editing an element then comes back, they end up back where they started. - It gives focusInput a memory. --- content_scripts/vimium_frontend.coffee | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 35d92f3c..c49c27e2 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -331,6 +331,17 @@ extend window, enterVisualMode: => new VisualMode() +# Track the most-recently focused input element. This is used by focusInput to decide which input to initially +# highlight. +getFocusedElementIndexByRecency = do -> + focusedElement = null + installListener window, "focus", (event) -> + focusedElement = event.target if DomUtils.isEditable event.target + + (elements) -> + Math.max 0, elements.indexOf focusedElement + +extend window, focusInput: (count) -> # Focus the first input element on the page, and create overlays to highlight all the input elements, with # the currently-focused element highlighted specially. Tabbing will shift focus to the next input element. @@ -345,7 +356,11 @@ extend window, return if visibleInputs.length == 0 - selectedInputIndex = Math.min(count - 1, visibleInputs.length - 1) + selectedInputIndex = + if count == 1 + getFocusedElementIndexByRecency visibleInputs.map (visibleInput) -> visibleInput.element + else + Math.min(count, visibleInputs.length) - 1 hints = for tuple in visibleInputs hint = document.createElement("div") -- cgit v1.2.3