-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make keyboard interactions in the settings menu more pleasant #78876
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Local js definitions: | ||
/* global getCurrentValue, updateLocalStorage, updateSystemTheme */ | ||
/* global getCurrentValue, getVirtualKey, updateLocalStorage, updateSystemTheme */ | ||
|
||
(function () { | ||
function changeSetting(settingName, value) { | ||
|
@@ -14,41 +14,47 @@ | |
} | ||
} | ||
|
||
function setEvents() { | ||
var elems = { | ||
toggles: document.getElementsByClassName("slider"), | ||
selects: document.getElementsByClassName("select-wrapper") | ||
}; | ||
var i; | ||
|
||
if (elems.toggles && elems.toggles.length > 0) { | ||
for (i = 0; i < elems.toggles.length; ++i) { | ||
var toggle = elems.toggles[i].previousElementSibling; | ||
var settingId = toggle.id; | ||
var settingValue = getSettingValue(settingId); | ||
if (settingValue !== null) { | ||
toggle.checked = settingValue === "true"; | ||
} | ||
toggle.onchange = function() { | ||
changeSetting(this.id, this.checked); | ||
}; | ||
} | ||
function handleKey(ev) { | ||
// Don't interfere with browser shortcuts | ||
if (ev.ctrlKey || ev.altKey || ev.metaKey) { | ||
return; | ||
} | ||
switch (getVirtualKey(ev)) { | ||
case "Enter": | ||
case "Return": | ||
case "Space": | ||
ev.target.checked = !ev.target.checked; | ||
ev.preventDefault(); | ||
break; | ||
} | ||
} | ||
|
||
if (elems.selects && elems.selects.length > 0) { | ||
for (i = 0; i < elems.selects.length; ++i) { | ||
var select = elems.selects[i].getElementsByTagName("select")[0]; | ||
var settingId = select.id; | ||
var settingValue = getSettingValue(settingId); | ||
if (settingValue !== null) { | ||
select.value = settingValue; | ||
} | ||
select.onchange = function() { | ||
changeSetting(this.id, this.value); | ||
}; | ||
function setEvents() { | ||
onEachLazy(document.getElementsByClassName("slider"), function(elem) { | ||
var toggle = elem.previousElementSibling; | ||
var settingId = toggle.id; | ||
var settingValue = getSettingValue(settingId); | ||
if (settingValue !== null) { | ||
toggle.checked = settingValue === "true"; | ||
} | ||
} | ||
toggle.onchange = function() { | ||
changeSetting(this.id, this.checked); | ||
}; | ||
toggle.onkeyup = handleKey; | ||
toggle.onkeyrelease = handleKey; | ||
}); | ||
onEachLazy(document.getElementsByClassName("select-wrapper"), function(elem) { | ||
var select = elem.getElementsByTagName("select")[0]; | ||
var settingId = select.id; | ||
var settingValue = getSettingValue(settingId); | ||
if (settingValue !== null) { | ||
select.value = settingValue; | ||
} | ||
select.onchange = function() { | ||
changeSetting(this.id, this.value); | ||
}; | ||
}); | ||
} | ||
|
||
setEvents(); | ||
window.addEventListener("DOMContentLoaded", setEvents); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realized that the script was loaded before the DOM. In itself, it seems to be working but I guess it doesn't hurt to be sure that the DOM is actually there before trying to work on it. |
||
})(); |
Uh oh!
There was an error while loading. Please reload this page.