From a9281a06ec12f3623e30761c923ebc57ec2585a3 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Sun, 8 Feb 2015 20:42:16 +0100 Subject: [PATCH] Fix #47: 'gi' to focus last focused or first text input 'gi' focuses the last focused or the first -- or the count:th to be precise -- text input of the page. Moreover, after you have focused something with 'gi', and switch between only text inputs. That's convenient, because 'gi' is then equivalent to '3gi'. Instead of trying 'gi' followed by '2gi' followed by '3gi' etc. until you get to the input you want, you can just type 'gi' instead. This required updating vim-like-key-notation to support more reliably. --- extension/lib/commands.coffee | 17 ++++++++++++++ extension/lib/events.coffee | 4 ++++ extension/lib/modes.coffee | 31 ++++++++++++++++++++++--- extension/lib/vim.coffee | 1 + extension/locale/de/vimfx.properties | 1 + extension/locale/el-GR/vimfx.properties | 1 + extension/locale/en-US/vimfx.properties | 1 + extension/locale/hu/vimfx.properties | 1 + extension/locale/id/vimfx.properties | 1 + extension/locale/it/vimfx.properties | 1 + extension/locale/ja/vimfx.properties | 1 + extension/locale/nl/vimfx.properties | 1 + extension/locale/pl/vimfx.properties | 1 + extension/locale/ru/vimfx.properties | 1 + extension/locale/sv-SE/vimfx.properties | 1 + extension/locale/zh-CN/vimfx.properties | 1 + extension/locale/zh-TW/vimfx.properties | 1 + package.json | 2 +- 18 files changed, 64 insertions(+), 4 deletions(-) diff --git a/extension/lib/commands.coffee b/extension/lib/commands.coffee index c9a8162..c89e635 100644 --- a/extension/lib/commands.coffee +++ b/extension/lib/commands.coffee @@ -440,6 +440,22 @@ command_follow_prev = helper_follow_pattern.bind(undefined, 'prev') # Follow next page. command_follow_next = helper_follow_pattern.bind(undefined, 'next') +# Focus last focused or first text input and enter text input mode. +command_text_input = (vim, event, count) -> + { lastFocusedTextInput } = vim.state + inputs = Array.filter( + vim.window.document.querySelectorAll('input, textarea'), (element) -> + return utils.isTextInputElement(element) and utils.area(element) > 0 + ) + if lastFocusedTextInput and lastFocusedTextInput not in inputs + inputs.push(lastFocusedTextInput) + return unless inputs.length > 0 + inputs.sort((a, b) -> a.tabIndex - b.tabIndex) + if count == 1 and lastFocusedTextInput + count = inputs.indexOf(lastFocusedTextInput) + 1 + inputs[count - 1].select() + vim.enterMode('text-input', inputs) + # Go up one level in the URL hierarchy. command_go_up_path = (vim, event, count) -> { pathname } = vim.window.location @@ -584,6 +600,7 @@ commands = [ new Command('browse', 'follow_multiple', command_follow_multiple, [['a', 'f']]) new Command('browse', 'follow_previous', command_follow_prev, [['[']]) new Command('browse', 'follow_next', command_follow_next, [[']']]) + new Command('browse', 'text_input', command_text_input, [['g', 'i']]) new Command('browse', 'go_up_path', command_go_up_path, [['g', 'u']]) new Command('browse', 'go_to_root', command_go_to_root, [['g', 'U']]) new Command('browse', 'back', command_back, [['H']]) diff --git a/extension/lib/events.coffee b/extension/lib/events.coffee index 6d7a19c..9fd6d33 100644 --- a/extension/lib/events.coffee +++ b/extension/lib/events.coffee @@ -149,6 +149,10 @@ windowsListeners = vim.enterMode('find') return + if target.ownerDocument instanceof HTMLDocument and + utils.isTextInputElement(target) + vim.state.lastFocusedTextInput = target + # If the user has interacted with the page and the `window` of the page gets # focus, it means that the user just switched back to the page from another # window or tab. If a text input was focused when the user focused _away_ diff --git a/extension/lib/modes.coffee b/extension/lib/modes.coffee index 8d25067..ebb76d1 100644 --- a/extension/lib/modes.coffee +++ b/extension/lib/modes.coffee @@ -123,6 +123,33 @@ exports['insert'] = commands: exit: [''] +exports['text-input'] = + onEnter: (vim, storage, inputs = []) -> + storage.inputs = inputs + onLeave: (vim, storage) -> + storage.inputs = null + onInput: (vim, storage, keyStr) -> + { inputs } = storage + index = inputs.indexOf(vim.window.document.activeElement) + switch + when index == -1 + vim.enterMode('normal') + return false + when escapeCommand.match(keyStr) + utils.blurActiveElement(vim.window) + vim.enterMode('normal') + return true + # Override the built-in shortcuts and to switch between + # focusable inputs. + when keyStr == '' + index++ + when keyStr == '' + index-- + else + return false + inputs[index %% inputs.length].select() + return true + exports['find'] = onEnter: -> @@ -214,9 +241,7 @@ exports['hints'] = rotate_markers_backward: [''] delete_hint_char: [''] -for modeName of exports - mode = exports[modeName] - continue if Array.isArray(mode.commands) +for modeName, mode of exports when not Array.isArray(mode.commands ? []) for commandName of mode.commands name = "mode_#{ modeName }_#{ commandName }" keys = mode.commands[commandName].map((key) -> [key]) diff --git a/extension/lib/vim.coffee b/extension/lib/vim.coffee index fc3c4d8..306c8ba 100644 --- a/extension/lib/vim.coffee +++ b/extension/lib/vim.coffee @@ -36,6 +36,7 @@ module.exports = class Vim lastAutofocusPrevention: null scrollableElements: new WeakMap() largestScrollableElement: null + lastFocusedTextInput: null enterMode: (mode, args...) -> # `args` is an array of arguments to be passed to the mode's `onEnter` diff --git a/extension/locale/de/vimfx.properties b/extension/locale/de/vimfx.properties index 3e23a2f..e9a88ba 100644 --- a/extension/locale/de/vimfx.properties +++ b/extension/locale/de/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Zur vorherigen Seite springen help_command_follow_next=Zur nächsten Seite springen +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Einen Level nach oben in der URL-Hierarchie springen help_command_go_to_root=Zum Stammverzeichnis der URL-Hierarchie springen help_command_back=Eine Seite zurück in der Chronik diff --git a/extension/locale/el-GR/vimfx.properties b/extension/locale/el-GR/vimfx.properties index 3bde05e..153688a 100644 --- a/extension/locale/el-GR/vimfx.properties +++ b/extension/locale/el-GR/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Πήγαινε στην προηγούμενη σελίδα help_command_follow_next=Πήγαινε στην επόμενη σελίδα +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Πήγαινε ένα επίπεδο πάνω στην ιεραρχία του URL help_command_go_to_root=Πήγαινε στο πιο πάνω επίπεδο της ιεραρχίας του URL help_command_back=Πήγαινε πίσω στο ιστορικό diff --git a/extension/locale/en-US/vimfx.properties b/extension/locale/en-US/vimfx.properties index 04159d0..d7f20e1 100644 --- a/extension/locale/en-US/vimfx.properties +++ b/extension/locale/en-US/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Go to the previous page help_command_follow_next=Go to the next page +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Go up one level in the URL help_command_go_to_root=Go to the root in the URL help_command_back=Go back in history diff --git a/extension/locale/hu/vimfx.properties b/extension/locale/hu/vimfx.properties index b9f8104..c42af82 100644 --- a/extension/locale/hu/vimfx.properties +++ b/extension/locale/hu/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Go to previous page help_command_follow_next=Go to next page +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Go up one level in the URL hierarchy help_command_go_to_root=Go up to root of the URL hierarchy help_command_back=Vissza az előzményekben diff --git a/extension/locale/id/vimfx.properties b/extension/locale/id/vimfx.properties index 01c8880..271efd8 100644 --- a/extension/locale/id/vimfx.properties +++ b/extension/locale/id/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Go to previous page help_command_follow_next=Go to next page +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Go up one level in the URL hierarchy help_command_go_to_root=Go up to root of the URL hierarchy help_command_back=Mundur dalam history diff --git a/extension/locale/it/vimfx.properties b/extension/locale/it/vimfx.properties index cdc18c0..d43d31b 100644 --- a/extension/locale/it/vimfx.properties +++ b/extension/locale/it/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Vai alla pagina precedente help_command_follow_next=Vai alla prossima pagina +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Sali di un livello nella gerarchia dell'URL help_command_go_to_root=Vai alla radice della gerarchia dell'URL help_command_back=Torna indietro nella cronologia diff --git a/extension/locale/ja/vimfx.properties b/extension/locale/ja/vimfx.properties index 42b6eb4..fb897e0 100644 --- a/extension/locale/ja/vimfx.properties +++ b/extension/locale/ja/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=前のページに移動 help_command_follow_next=次のページに移動 +help_command_text_input=Focus last focused or first text input help_command_go_up_path=URLを一階層上る help_command_go_to_root=URLを最上階まで上る help_command_back=履歴を戻る diff --git a/extension/locale/nl/vimfx.properties b/extension/locale/nl/vimfx.properties index b6df933..211bd3d 100644 --- a/extension/locale/nl/vimfx.properties +++ b/extension/locale/nl/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Go to previous page help_command_follow_next=Go to next page +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Go up one level in the URL hierarchy help_command_go_to_root=Go up to root of the URL hierarchy help_command_back=Ga terug in de geschiedenis diff --git a/extension/locale/pl/vimfx.properties b/extension/locale/pl/vimfx.properties index 160a60a..50d6501 100644 --- a/extension/locale/pl/vimfx.properties +++ b/extension/locale/pl/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Idź do poprzedniej strony help_command_follow_next=Idź do następnej strony +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Idź poziom wyżej w hierarchii URL help_command_go_to_root=Idź do korzenia w hierarchii URL help_command_back=Wstecz diff --git a/extension/locale/ru/vimfx.properties b/extension/locale/ru/vimfx.properties index b995e95..c22ee2e 100644 --- a/extension/locale/ru/vimfx.properties +++ b/extension/locale/ru/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=Go to previous page help_command_follow_next=Go to next page +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Go up one level in the URL hierarchy help_command_go_to_root=Go up to root of the URL hierarchy help_command_back=Предыдущая страница diff --git a/extension/locale/sv-SE/vimfx.properties b/extension/locale/sv-SE/vimfx.properties index 34f67fe..18aad86 100644 --- a/extension/locale/sv-SE/vimfx.properties +++ b/extension/locale/sv-SE/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Följ länk i ny förgrundsflik help_command_follow_multiple=Följ flera länkar i nya bakgrundsflikar, fokusera textruta eller klicka på flera knappar help_command_follow_previous=Gå till föregående sida help_command_follow_next=Gå till nästa sida +help_command_text_input=Focus last focused or first text input help_command_go_up_path=Gå upp en nivå i URL:en help_command_go_to_root=Gå till roten av URL:en help_command_back=Gå tillbaka i historiken diff --git a/extension/locale/zh-CN/vimfx.properties b/extension/locale/zh-CN/vimfx.properties index 27c56ea..47fa2c2 100644 --- a/extension/locale/zh-CN/vimfx.properties +++ b/extension/locale/zh-CN/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=打开上一页 help_command_follow_next=打开下一页 +help_command_text_input=Focus last focused or first text input help_command_go_up_path=打开上一级页面 help_command_go_to_root=打开根目录页面 help_command_back=后退 diff --git a/extension/locale/zh-TW/vimfx.properties b/extension/locale/zh-TW/vimfx.properties index e86a829..b37dfb0 100644 --- a/extension/locale/zh-TW/vimfx.properties +++ b/extension/locale/zh-TW/vimfx.properties @@ -57,6 +57,7 @@ help_command_follow_in_focused_tab=Follow link in a new foreground tab help_command_follow_multiple=Follow multiple links in new background tabs, focus text input or click multiple buttons help_command_follow_previous=到上一頁 help_command_follow_next=到下一頁 +help_command_text_input=Focus last focused or first text input help_command_go_up_path=打開網址的上一級目錄 help_command_go_to_root=打開網址的根目錄 help_command_back=後退 diff --git a/package.json b/package.json index 8095d33..5f51746 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "scripts": {}, "dependencies": { "n-ary-huffman": "^2.1.1", - "vim-like-key-notation": "~0.1.2" + "vim-like-key-notation": "~0.1.3" }, "devDependencies": { "coffee-script": "~1.9.0", -- 2.39.3