fix: prevent window scrolling on arrow up and down (#2)

* fix: prevent window scrolling on arrow up and down

* fix: add standard to dev deps

* chore: add semantic-release

* configure standard for jest

* lint
This commit is contained in:
Zeke Sikelianos 2018-06-18 13:11:33 -07:00 коммит произвёл GitHub
Родитель e41bab55fe
Коммит f2b9faa2b5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 43 добавлений и 21 удалений

13
.travis.yml Normal file
Просмотреть файл

@ -0,0 +1,13 @@
language: node_js
cache:
directories:
- ~/.npm
notifications:
email: false
node_js:
- '10'
after_success:
- npm run travis-deploy-once "npm run semantic-release"
branches:
except:
- /^v\d+\.\d+\.\d+$/

Просмотреть файл

@ -5,7 +5,7 @@ context('Actions', () => {
it('.type() - type into a DOM element', () => {
// https://on.cypress.io/type
cy.get('#search').type('cool').should('have.value', 'cool')
cy.get('#hits > *').first()

Просмотреть файл

@ -24,4 +24,4 @@ const $main = html`
document.addEventListener('DOMContentLoaded', () => {
document.body.appendChild($main)
searchWithYourKeyboard('#search', '#hits')
})
})

Просмотреть файл

@ -8,10 +8,6 @@ module.exports = function searchWithYourKeyboard (inputSelector, hitsSelector) {
let activeIndex = 0
const targetEventCodes = ['up', 'down', 'enter', '/', 'esc']
const input = document.querySelector(inputSelector)
const hits = document.querySelector(hitsSelector)
// assert(input, `no element found for inputSelector: ${inputSelector}`)
// assert(hits, `no element found for hitsSelector: ${hitsSelector}`)
// deactivate any active hit when search input is focused by a mouse click
input.addEventListener('focus', () => {
@ -20,45 +16,49 @@ module.exports = function searchWithYourKeyboard (inputSelector, hitsSelector) {
})
// deactivate any active hit when typing in search box
input.addEventListener('keydown', () => {
input.addEventListener('keydown', event => {
if (!targetEventCodes.includes(event.code)) {
activeIndex = 0
deactivateHits()
}
})
document.addEventListener('keyup', event => {
document.addEventListener('keydown', event => {
// bail early if key code is not one that we're explicity expecting
if (!event || !event.code || !targetEventCodes.includes(keycode(event))) return
const hits = Array.from(document.querySelectorAll(hitsSelector))
switch(keycode(event)) {
switch (keycode(event)) {
case 'esc':
input.focus()
input.select()
input.value = ''
return
break
case '/':
// when the input is focused, `/` should have no special behavior
if (event.target !== input) {
input.focus()
input.select()
return
}
break
case 'up':
// search input is the zero index (don't go beyond it)
if (activeIndex > 0) activeIndex--
if (activeIndex > 0) {
activeIndex--
event.preventDefault() // prevent window scrolling
}
updateActiveHit()
break
case 'down':
// last hit is the last index (don't go beyond it)
if (activeIndex < hits.length) activeIndex++
if (activeIndex < hits.length) {
activeIndex++
event.preventDefault() // prevent window scrolling
}
updateActiveHit()
break
@ -74,17 +74,16 @@ module.exports = function searchWithYourKeyboard (inputSelector, hitsSelector) {
window.location = href
}
break
}
}
})
function deactivateHits () {
Array.from(document.querySelectorAll(hitsSelector)).forEach(hit => {
hit.classList.remove('active')
})
}
function updateActiveHit() {
function updateActiveHit () {
deactivateHits()
if (activeIndex === 0) {
@ -96,4 +95,4 @@ module.exports = function searchWithYourKeyboard (inputSelector, hitsSelector) {
input.blur()
}
}
}
}

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "search-with-your-keyboard",
"version": "1.0.0",
"version": "0.0.0-development",
"description": "Add keyboard navigation to your existing client-side search interface",
"main": "src.js",
"repository": "https://github.com/electron/search-with-your-keyboard",
@ -10,16 +10,26 @@
"budo": "^11.2.2",
"cypress": "^3.0.0",
"nanohtml": "^1.2.4",
"wait-on": "^2.1.0"
"standard": "^11.0.1",
"wait-on": "^2.1.0",
"travis-deploy-once": "^5.0.0",
"semantic-release": "^15.5.5"
},
"scripts": {
"start": "budo demo.js --live --no-debug --open --css demo.css",
"test": "script/test.sh && standard --fix"
"test": "script/test.sh && npm run lint",
"travis-deploy-once": "travis-deploy-once",
"semantic-release": "semantic-release",
"lint": "standard --fix"
},
"files": [
"index.js"
],
"dependencies": {
"keycode": "^2.2.0"
},
"standard": {
"env": ["jest"],
"ignore": ["/cypress"]
}
}
}