Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions doc/api_assets/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@
updateHashes();
}

function setupFlavorSelectors() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function setupFlavorSelectors() {
function setupFlavorToggles() {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment applies to all current suggestions:

If you want to change every mention of "select" to "toggle", then we also need to change all the CSS files and also all the existing functions in api.js and possibly more. There are also test cases that might be affected.

I think this renaming thing should go in a separate issue so we can land this feature. No need to spend time risking introducing other issues on something that is not really a problem imho.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've done all the preparation work in #49536, do you think I've missed something? We can't land this PR as is because it's now inconsistent with main, but I think all you need to do is to merge my suggestions and this can land as soon as we have a green CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so you've committed the renaming directly onto main already?

The "setupCopyButton" function in doc/api_assets/api.js also uses the name "selector" everywhere, is that included? Not sure what else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed merge and rename.

const kFlavorPreference = 'customFlavor';
const flavorSetting = sessionStorage.getItem(kFlavorPreference);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see any value in using a sessionStorage here. As the lifecycle would be contained to the current session. A localStorage makes more sense here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a new version with localStorage. This leaves some "garbage" values there though since it doesn't get removed, which may be more of an issue with localStorage than with sessionStorage. Adding a "removeItem" when the value is not "true" could fix that and keep it clean, but maybe not a big issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that localStorage should sync with the user preference. Either true or false :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it's not really necessary to store the string "false" it can just be undefined aka be removed (removeItem) to avoid wasting localStorage space. But if it's fine for you, then it's fine for me, may just be nitpicking.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry, what I meant is that we should track both states. And for me the absence of a value is more than enough.

But we should remove then the value when set to false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, pushed a fix to remove value when false.

const flavorSelectors = document.querySelectorAll('.js-flavor-selector');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const flavorSelectors = document.querySelectorAll('.js-flavor-selector');
const flavorToggles = document.querySelectorAll('.js-flavor-toggle');


flavorSelectors.forEach((selector) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flavorSelectors.forEach((selector) => {
flavorToggles.forEach((toggle) => {

selector.checked = flavorSetting === 'true'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unoptimized. You're running this same check for every checkbox, knowing that this check is only needed once.

Also you probably don't need to set the value of the checkbox if flavorSetting is different from true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed optimized code. You need to set the checkbox value also when it's not true, leaving that unchanged.

selector.addEventListener('change', (e) => {
const checked = e.target.checked;

sessionStorage.setItem(kFlavorPreference, checked);

flavorSelectors.forEach((el) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flavorSelectors.forEach((el) => {
flavorToggles.forEach((el) => {

el.checked = checked;
});
});
});
}

function setupCopyButton() {
const buttons = document.querySelectorAll('.copy-button');
buttons.forEach((button) => {
Expand Down Expand Up @@ -182,6 +201,8 @@
// Make link to other versions of the doc open to the same hash target (if it exists).
setupAltDocsLink();

setupFlavorSelectors();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setupFlavorSelectors();
setupFlavorToggles();


setupCopyButton();
}

Expand Down