Skip to content

now detecting accessKey modifiers based on userAgent #1

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
42 changes: 35 additions & 7 deletions src/workflowyfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ $(`<button accesskey="n">`)
return false
})

var modsHtml;
var mods = accessKeyModifiers(navigator.userAgent)
if (mods) {
modsHtml = `<kbd>${mods.join('</kbd> + <kbd>')}</kbd> + `
Copy link
Member

Choose a reason for hiding this comment

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

The kbd made me curious. I thought you might've picked them up from the original source, so I went looking there. Look what I found:

<td class="commandDescription">
	<span class="showOnWindows showOnLinux">Alt</span>
	<span class="showOnChromeOS">Control</span>
	<span class="hideOnWindows hideOnLinux hideOnChromeOS"></span> + Shift + Up/Down
</td>

I'd like to stay close to original so unless you think this is a terrible idea, I'd want to go that way. Would eliminate any browser sniffing code from the extension (for now), which is not bad either. What do you think?

} else {
modsHtml = `<a href="https://developer.mozilla.org/docs/Web/HTML/Global_attributes/accesskey" target="_blank"><em>ACC</em></a> + `
}

// add it to shortcut list
afterCommand(`Add a note`).addAccessKey(`Show/hide all notes`, `N`)

Expand Down Expand Up @@ -74,7 +82,7 @@ function afterCommand(name) {
afterRowAddNewCommand($commandRow, name, description)
},
addAccessKey : function(name, accessKey) {
afterRowAddNewCommand($commandRow, name, accessKeyText(accessKey))
afterRowAddNewCommand($commandRow, name, accessKeyHtml(accessKey))
}
}
}
Expand All @@ -88,12 +96,32 @@ function afterRowAddNewCommand($row, name, description) {
$row.after(newRow)
}

function accessKeyText(accessKey) {
return accessKeyModifier() + ` + ` + accessKey
function accessKeyHtml(accessKey) {
return (modsHTML + accessKey)
Copy link
Member

Choose a reason for hiding this comment

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

Compile error. 😜

}

// see https://developer.mozilla.org/docs/Web/HTML/Global_attributes/accesskey
function accessKeyModifiers(ua) {
var uaFlags = parseUserAgent(ua)

var isKnownPlatform = (uaFlags.lin || uaFlags.mac || uaFlags.win)
var isKnownApp = (uaFlags.chrome || uaFlags.firefox)
if (! isKnownPlatform || ! isKnownApp) return

if (uaFlags.mac) return [ `Ctrl`, `Alt` ]
if (uaFlags.chrome) return [ `Alt` ]
if (uaFlags.firefox) return [ `Alt`, `Shift` ]
}

function accessKeyModifier() {
// TODO: determine shortcut according to browser and OS
// TODO: memoize
return `<a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/accesskey" target="_blank">ACC</a>`
function parseUserAgent(ua) {
var uaLower = ua.toLowerCase()

return {
lin: uaLower.indexOf(`lin`) != -1,
mac: uaLower.indexOf(`mac`) != -1,
win: uaLower.indexOf(`win`) != -1,

chrome: uaLower.indexOf(`chrome`) != -1,
firefox: uaLower.indexOf(`firefox`) != -1
}
}