Skip to content

Conversation

@megaden
Copy link
Contributor

@megaden megaden commented Oct 12, 2022

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_placeholder is a bit controversial but saves updates to .po files. PLMK if you'd like to change the localizable string instead.

@pauloxnet
Copy link
Member

pauloxnet commented Oct 12, 2022

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.

Copy link
Member

@adamchainz adamchainz left a 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?

@megaden
Copy link
Contributor Author

megaden commented Oct 12, 2022

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.

  • Try to replace shortcut with Command-K on MacOS

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)

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).

Copy link
Contributor Author

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.

@adamchainz
Copy link
Member

Anyways let me look into that, perhaps user agent header server side will be sufficient.

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.

@megaden
Copy link
Contributor Author

megaden commented Oct 12, 2022

@adamchainz Hey Adam, pls take a look at the latest version when you'll have a minute.

@pauloxnet
Copy link
Member

@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) {
Copy link
Member

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

Comment on lines 16 to 18
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})`);
Copy link
Member

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:

Suggested change
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 😉

@adamchainz
Copy link
Member

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 django.contrib.admin.

That said, I found a neat pure-JS example of the keyboard listener in the eslint codebase:

https://github.com/eslint/eslint/blob/d336cfc9145a72bf8730250ee1e331a135e6ee2c/docs/src/assets/js/search.js#L164-L168

The smooth scrolling into view seems like a nice addition.

@pauloxnet
Copy link
Member

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 django.contrib.admin.

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.

@megaden
Copy link
Contributor Author

megaden commented Oct 13, 2022

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:

  • Docker Compose set up works great overall, kudos for it 👍

  • While running tox I kept getting py38-tests failures complaining about "The SECRET_KEY setting must not be empty". Since SECRET_KEY is provided via env var in docker-compose.yml I tried to add SECRET_KEY to passenv in tox.ini but then it started to fail with "TypeError: can only concatenate str (not "NoneType") to str".

  • While running tests via manage.py there also were errors (see pic below). It'll be a great deal for contributors using Docker Compose to ensure test suite works.
    Screenshot from 2022-10-13 11-06-38

  • I kept fighting whitespace in search-key.js due to .editorconfig:17 saying indent_style = tab while as far as I can tell files in "djangoproject/static/js/mod" have spaces rather than tabs.

Anyways let me know what do you think about current version.

@adamchainz
Copy link
Member

  • I kept fighting whitespace in search-key.js due to .editorconfig:17 saying indent_style = tab while as far as I can tell files in "djangoproject/static/js/mod" have spaces rather than tabs.

I think we should make an issue to update the editorconfig, and stop using tabs. Perhaps we could sync with django's editorconfig?

@pauloxnet
Copy link
Member

  • I kept fighting whitespace in search-key.js due to .editorconfig:17 saying indent_style = tab while as far as I can tell files in "djangoproject/static/js/mod" have spaces rather than tabs.

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 .editorconfig or .tox.ini

@pauloxnet
Copy link
Member

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 slightly_smiling_face 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.

Ok, I agree with what you say.
I leave @adamchainz the revision of the latest changes.

@megaden
Copy link
Contributor Author

megaden commented Nov 4, 2022

Hey guys, any luck with this getting merged? It was up in the air for a while already.

@pauloxnet
Copy link
Member

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/

@pauloxnet
Copy link
Member

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).

@megaden
Copy link
Contributor Author

megaden commented Nov 7, 2022

Thanks, Paolo! Sure, whenever it'd be possible to pick this up.

Copy link

@valentinogagliardi valentinogagliardi left a 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!

@pauloxnet
Copy link
Member

@megaden at this point if you can rebase this PR we can merge it.
@valentinogagliardi thanks for your help

@megaden megaden force-pushed the new-searchbox-shortcut branch from c05c2f6 to 036e484 Compare December 22, 2022 16:12
@megaden
Copy link
Contributor Author

megaden commented Dec 22, 2022

@pauloxnet I synced my fork & rebased the branch on top of the latest commit to main.

@pauloxnet
Copy link
Member

@pauloxnet I synced my fork & rebased the branch on top of the latest commit to main.

Unfortunately tox is failing

@megaden
Copy link
Contributor Author

megaden commented Dec 22, 2022

Unfortunately tox is failing

The same happens for the tip of the main branch - https://github.com/django/djangoproject.com/actions/runs/3650268494/jobs/6166491637 - when it'll be sorted I can rebase once again.

@pauloxnet
Copy link
Member

The same happens for the tip of the main branch - https://github.com/django/djangoproject.com/actions/runs/3650268494/jobs/6166491637 - when it'll be sorted I can rebase once again.

@megaden tox was not pinned and since tox 4 was published, the tests have started to fail.

I marked tox<4 in the main branch.

If you managed to make the last rebase I'm sure that we will finally be able to merge this PR.

@megaden megaden force-pushed the new-searchbox-shortcut branch from 036e484 to 633dfd0 Compare December 23, 2022 09:58
@megaden
Copy link
Contributor Author

megaden commented Dec 23, 2022

@pauloxnet I rebased the branch, could you please approve the workflow, so we'll ensure everything is good now?

@pauloxnet
Copy link
Member

@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.

@pauloxnet pauloxnet merged commit b734015 into django:main Dec 23, 2022
@megaden megaden deleted the new-searchbox-shortcut branch December 23, 2022 11:08
mick88 added a commit to mick88/djangoproject.com that referenced this pull request Feb 11, 2023
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.
pauloxnet pushed a commit that referenced this pull request Feb 11, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change searchbox shortcut to ctrl-k/command-k, and make it visible

5 participants