Browse Source

Handle escape and up/down arrows in context menu

tags/v0.1.0
Gerben 5 years ago
parent
commit
01147678e0
2 changed files with 36 additions and 9 deletions
  1. +2
    -1
      app/assets/main.css
  2. +34
    -8
      app/create-bookmarks/contentscript.js

+ 2
- 1
app/assets/main.css View File

@@ -106,7 +106,8 @@ input#volume:focus {
font-size: 1em; font-size: 1em;
padding: 4px 20px; padding: 4px 20px;
} }
.context-menu button:hover {
.context-menu button:hover,
.context-menu button:focus {
background: #dddddd99; background: #dddddd99;
} }




+ 34
- 8
app/create-bookmarks/contentscript.js View File

@@ -28,12 +28,13 @@ function describeMediaFragment({ start, end }) {
} }


export default async function init(playlist) { export default async function init(playlist) {
let menuEl;
let menuEl, menuItemElements;


function hideMenu() { function hideMenu() {
if (!menuEl) return; if (!menuEl) return;
menuEl.parentNode.removeChild(menuEl); menuEl.parentNode.removeChild(menuEl);
menuEl = undefined; menuEl = undefined;
document.body.removeEventListener('keydown', handleKeyDown);
} }


function onContextMenu(event) { function onContextMenu(event) {
@@ -81,21 +82,46 @@ export default async function init(playlist) {
}, },
]; ];


menuItemElements = menuItems.map(({ title, action }) => html`
<li>
<button onclick=${event => { hideMenu(); action(event); }}>
${title}
</button>
</li>
`);

hideMenu(); hideMenu();


menuEl = html` menuEl = html`
<ul class="context-menu" style="top: ${top}; left: ${left};"> <ul class="context-menu" style="top: ${top}; left: ${left};">
${menuItems.map(({ title, action }) => html`
<li>
<button onclick=${event => { hideMenu(); action(event); }}>
${title}
</button>
</li>
`)}
${menuItemElements}
</ul> </ul>
`; `;


document.body.appendChild(menuEl); document.body.appendChild(menuEl);

document.body.addEventListener('keydown', handleKeyDown);
}

function handleKeyDown(event) {
if (event.code === 'Escape') {
hideMenu();
return;
}

const currentIndex = menuItemElements.findIndex(el => el.querySelector('button:focus'));
if (event.code === 'ArrowDown') {
const newIndex = (currentIndex === -1 || currentIndex === menuItemElements.length - 1)
? 0
: currentIndex + 1;
menuItemElements[newIndex].querySelector('button').focus();
}
if (event.code === 'ArrowUp') {
const newIndex = (currentIndex === -1 || currentIndex === 0)
? menuItemElements.length - 1
: currentIndex - 1;
menuItemElements[newIndex].querySelector('button').focus();
}
} }


function onMouseDown(event) { function onMouseDown(event) {


Loading…
Cancel
Save