Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 69d3d42

Browse files
committed
🎨 scroll on key press (pageup/pagedown/up/down/home/end)
- use dom-listener to handle keydown event - add styleguideView, domListener in styleguide.coffee - add onKeyDownScroll() to change scrollTop property in styleguide-view.js
1 parent 7378d4d commit 69d3d42

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/styleguide-view.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ export default class StyleguideView {
1212
this.uri = props.uri
1313
this.collapsedSections = props.collapsedSections ? new Set(props.collapsedSections) : new Set()
1414
this.sections = []
15+
this.mainSections = null
16+
this.keyCodeToScrollStep = {
17+
'33': -200, // pageup
18+
'34': 200, // pagedown
19+
'38': -100, // up
20+
'40': 100, // down
21+
'36': Number.MIN_SAFE_INTEGER, // home
22+
'35': Number.MAX_SAFE_INTEGER, // end
23+
}
1524
etch.initialize(this)
1625
for (const section of this.sections) {
1726
if (this.collapsedSections.has(section.name)) {
@@ -24,6 +33,7 @@ export default class StyleguideView {
2433

2534
destroy () {
2635
this.sections = null
36+
this.mainSections = null
2737
}
2838

2939
serialize () {
@@ -62,6 +72,16 @@ export default class StyleguideView {
6272
}
6373
}
6474

75+
onKeyDownScroll(keyCode) {
76+
if (!this.keyCodeToScrollStep[keyCode]) return
77+
if (!this.mainSections) {
78+
this.mainSections = document.querySelector('.styleguide main.styleguide-sections')
79+
}
80+
if (this.mainSections) {
81+
this.mainSections.scrollTop += this.keyCodeToScrollStep[keyCode]
82+
}
83+
}
84+
6585
render () {
6686
return (
6787
<div className='styleguide pane-item native-key-bindings' tabIndex='-1'>

lib/styleguide.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {CompositeDisposable} = require('atom')
2+
const DOMListener = require('dom-listener')
23
let StyleguideView = null
34

45
const STYLEGUIDE_URI = 'atom://styleguide'
@@ -9,8 +10,14 @@ module.exports = {
910
this.subscriptions.add(atom.workspace.addOpener(filePath => {
1011
if (filePath === STYLEGUIDE_URI) return this.createStyleguideView({uri: STYLEGUIDE_URI})
1112
}))
12-
this.subscriptions.add(atom.commands.add('atom-workspace', 'styleguide:show', () => atom.workspace.open(STYLEGUIDE_URI))
13-
)
13+
this.subscriptions.add(atom.commands.add('atom-workspace', 'styleguide:show',
14+
() => atom.workspace.open(STYLEGUIDE_URI).then(() => {
15+
this.domListener = new DOMListener(document.querySelector('atom-workspace'))
16+
return this.domListener.add('.styleguide', 'keydown', event => {
17+
return this.styleguideView.onKeyDownScroll(event.keyCode)
18+
})
19+
})
20+
))
1421
},
1522

1623
deactivate () {
@@ -19,6 +26,6 @@ module.exports = {
1926

2027
createStyleguideView (state) {
2128
if (StyleguideView == null) StyleguideView = require('./styleguide-view')
22-
return new StyleguideView(state)
29+
return this.styleguideView = new StyleguideView(state);
2330
}
2431
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dependencies": {
99
"atom-select-list": "^0.7.0",
1010
"dedent": "^0.7.0",
11+
"dom-listener": "^0.1.2",
1112
"etch": "0.9.0",
1213
"highlights": "^3.1.1"
1314
},

0 commit comments

Comments
 (0)