-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Updated search box shortcut and made it visible #1236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
LGTM It would be nice if @adamchainz (I know he's very busy) also could review this PR as the author of the original issue #1112 confirming that the PR satisfies his original requests. |
adamchainz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make it command-k on macOS?
Tailwind does that but it's React-based, so I wonder if they figure it out client side. Anyways let me look into that, perhaps user agent header server side will be sufficient.
|
docs/forms.py
Outdated
| def __init__(self, data=None, **kwargs): | ||
| self.release = kwargs.pop('release') | ||
| super().__init__(data=data, **kwargs) | ||
| complete_search_placeholder = '%s (Ctrl + K)' % (localizable_search_placeholder % self.release.human_version) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: two string interpolations in one line look weird and error-prone, I'd split this in two statements or used two different ways of interpolation (f-string and % interpolation, or format() and % interpolation).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed but in the latest version we do everything client side, so doesn't matter anymore.
Please don't try use the User-Agent header, it's only robust to do it client side. Should be possible to detect it and update the placeholder text in a small document ready handler. |
|
@adamchainz Hey Adam, pls take a look at the latest version when you'll have a minute. |
|
@megaden thanks for your work. I was thinking that given the shortness of this JavaScript file, could it make sense to rewrite it by removing the JQuery dependency and using a modern syntax (eg: let, const)? |
| search_form_input.attr('placeholder', `${raw_placeholder} (${kbd_shortcut_suffix})`); | ||
|
|
||
| $(window).keydown(function(e) { | ||
| if (e.key === 'k' && e.ctrlKey && $('input:focus, textarea:focus').length === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to check for metaKey on macOS, rather than ctrlKey, to detect “Command”: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/metaKey
| var kbd_shortcut_suffix = "Ctrl + K"; | ||
| if (navigator.userAgent.indexOf("Mac") !== -1) kbd_shortcut_suffix = "⌘ + K"; | ||
| search_form_input.attr('placeholder', `${raw_placeholder} (${kbd_shortcut_suffix})`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more idiomatic in JavaScript to use the ternary operator for such conditional assignments:
| var kbd_shortcut_suffix = "Ctrl + K"; | |
| if (navigator.userAgent.indexOf("Mac") !== -1) kbd_shortcut_suffix = "⌘ + K"; | |
| search_form_input.attr('placeholder', `${raw_placeholder} (${kbd_shortcut_suffix})`); | |
| const shortcut = (navigator.userAgent.indexOf("Mac") !== -1) ? "⌘ + K" : "Ctrl + K"; | |
| search_form_input.attr('placeholder', `${raw_placeholder} (${kbd_shortcut_suffix})`); |
This also allows us to use const to declare that the variable will not change.
Also there's no need to use a verbose variable name when the variable has a single use on the next line 😉
|
Removing jQuery would be cool, but also deferrable. We've got a lot of jQuery in the project, it would be best to track rewriting to not use it in an issue - as we did for That said, I found a neat pure-JS example of the keyboard listener in the eslint codebase: The smooth scrolling into view seems like a nice addition. |
The jQuery removal was only suggestion, because I saw how small is the JavaScript file modified in this PR, but it's also deferrable because I don't want to eventually block this issue to be merged only for this. But if rewriting this file from scratch in pureJS require it less or equal time I would prefer this opportunity. |
|
I aimed at achieving the goal with minimal updates hence tried to keep plumbing as is. In regards to modern JS obviously I love it too 🙂 I assume you're happy with existing browser coverage since I haven't spot any postprocessing of JS (including production website). In terms of jQuery removal I suggest to push that to a separate initiative since there's some plumbing missing now. For instance, to avoid messing with DOM readiness everywhere a dedicated RequireJS module could be used. A bit of feedback from dev experience side of things which you may find useful:
Anyways let me know what do you think about current version. |
I think we should make an issue to update the editorconfig, and stop using tabs. Perhaps we could sync with django's editorconfig? |
I agree with that feel free to open an issue and proposed a fix to the |
Ok, I agree with what you say. |
|
Hey guys, any luck with this getting merged? It was up in the air for a while already. |
I was waiting for a review from @adamchainz who is the creator of the issue, but I try to review again too. P.S. next time try to use a more inclusive salutation form. See https://heyguys.cc/ |
|
I approved the PR but I ask you to way some other days to let @adamchainz to review the code (I think he is a bit busy this days). |
|
Thanks, Paolo! Sure, whenever it'd be possible to pick this up. |
valentinogagliardi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! @pauloxnet asked me to take a quick look at this PR. Looks good to me. Thanks!
|
@megaden at this point if you can rebase this PR we can merge it. |
c05c2f6 to
036e484
Compare
|
@pauloxnet I synced my fork & rebased the branch on top of the latest commit to |
Unfortunately tox is failing |
The same happens for the tip of the |
@megaden I marked If you managed to make the last rebase I'm sure that we will finally be able to merge this PR. |
036e484 to
633dfd0
Compare
|
@pauloxnet I rebased the branch, could you please approve the workflow, so we'll ensure everything is good now? |
Finally it worked, I'm going to squash and merge this PR. |
Current logic incorrectly shows "⌘ + K" in Chrome on Windows (user agent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'). It does not contain "Mac". The logic was introduced in django#1236 and appears to be swapped - showing Windows/Linux shortcut on mac and vice-versa.
Current logic incorrectly shows "⌘ + K" in Chrome on Windows (user agent 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'). It does not contain "Mac". The logic was introduced in #1236 and appears to be swapped - showing Windows/Linux shortcut on mac and vice-versa.

Hi guys, this is supposed to fix #1112 - a small bit of help & my first contribution to OSS, so pls cut me some slack :)
Usage of
complete_search_placeholder/localizable_search_placeholderis a bit controversial but saves updates to .po files. PLMK if you'd like to change the localizable string instead.