From d8f91959a75fe1348d421a2c33d52dfa70416962 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 13 Apr 2021 17:59:11 +0530 Subject: [PATCH] Viewer preferences: Allow searching for keyboard shortcuts --- src/pyj/complete.pyj | 1 + src/pyj/read_book/prefs/keyboard.pyj | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/pyj/complete.pyj b/src/pyj/complete.pyj index b30c9bbaaa..db292ec7b1 100644 --- a/src/pyj/complete.pyj +++ b/src/pyj/complete.pyj @@ -99,6 +99,7 @@ def create_search_bar(action, name, tooltip=None, placeholder=None, button=None, local_storage().set(history_name, uniq(items[:history_size])) update_completion_items() ewc.onenterkey = trigger + parent.querySelector('input').addEventListener('search', trigger) if button: ewc.add_associated_widget(button) diff --git a/src/pyj/read_book/prefs/keyboard.pyj b/src/pyj/read_book/prefs/keyboard.pyj index 773e2abf9d..d4e1dfdde3 100644 --- a/src/pyj/read_book/prefs/keyboard.pyj +++ b/src/pyj/read_book/prefs/keyboard.pyj @@ -6,6 +6,7 @@ from elementmaker import E from gettext import gettext as _ from book_list.globals import get_session_data +from complete import create_search_bar from book_list.item_list import create_item, create_item_list from dom import clear, svgicon, unique_id from read_book.shortcuts import ( @@ -144,6 +145,15 @@ def customize_shortcut(sc_name): )) +def run_search(): + container = get_container() + query = container.querySelector(f'[name=search-for-sc]').value or '' + query = query.toLowerCase() + for item in get_container().querySelectorAll('[data-user-data]'): + q = item.textContent.toLowerCase() + matches = not query or q.indexOf(query) > -1 + item.style.display = 'list-item' if matches else 'none' + def create_keyboard_panel(container, apply_func, cancel_func, onchange): create_keyboard_panel.onchange = onchange @@ -151,9 +161,16 @@ def create_keyboard_panel(container, apply_func, cancel_func, onchange): container = container.lastChild container.dataset.changed = 'false' get_container.id = container.id + search_button = create_button(_('Search'), icon='search') + sb = create_search_bar( + run_search, 'search-for-sc', placeholder=_('Search for shortcut'), button=search_button) + container.appendChild(E.div( + style='margin-bottom: 1ex; display: flex; width: 100%; justify-content: space-between', sb, E.span('\xa0\xa0'), search_button + )) + container.firstChild.firstChild.style.flexGrow = '100' container.appendChild(E.div()) container.appendChild(E.div(style='display: none')) - container = container.firstChild + container = container.firstChild.nextSibling sd = get_session_data() custom_shortcuts = sd.get('keyboard_shortcuts') groups = as_groups(shortcuts_definition())