-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathindex.ts
66 lines (57 loc) · 2.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {Dictionary} from 'lodash'
import {Feature, prepareSettings, Settings, Shortcut} from '../settings'
import {config as incDec} from './inc-dec-value'
import {config as srs} from '../srs/srs'
import {config as blockManipulation} from './block-manipulation'
import {config as vimMode} from './vim-mode'
import {config as spatialMode} from './spatial-mode'
import {config as estimate} from './estimates'
import {config as navigation} from './navigation'
import {config as livePreview} from './livePreview'
import {config as dateTitle} from './day-title'
import {config as fuzzyDate} from './fuzzy_date'
import {config as randomPage} from './random-page'
import {filterAsync, mapAsync} from '../common/async'
import {Handler} from 'src/core/react-hotkeys/key-handler'
import {KeySequenceString} from 'src/core/react-hotkeys/key-sequence'
export const Features = {
all: prepareSettings([
incDec, // prettier
srs,
blockManipulation,
vimMode,
spatialMode,
estimate,
navigation,
dateTitle,
fuzzyDate,
livePreview,
randomPage,
]),
isActive: Settings.isActive,
async getActiveFeatures(): Promise<Feature[]> {
return filterAsync(this.all, it => this.isActive(it.id))
},
getShortcutHandlers: (): Dictionary<Handler> =>
getAllShortcuts(Features.all).reduce((acc: any, current) => {
acc[current.id] = current.onPress
return acc
}, {}),
async getCurrentKeyMap(): Promise<Dictionary<KeySequenceString>> {
const features = (await Features.getActiveFeatures()).filter(it => it.settings)
const allShortcuts = (await mapAsync(features, this.getKeyMapFor)).flat().filter(it => it[1])
return allShortcuts.reduce((acc: any, current) => {
acc[current[0]] = current[1]
return acc
}, {})
},
async getKeyMapFor(feature: Feature) {
return mapAsync(feature.settings!, async it => [it.id, await Settings.get(feature.id, it.id)])
},
}
export const getAllShortcuts = (features: Feature[]): Shortcut[] =>
features
.filter(it => it.settings)
.flatMap(it => it.settings)
.filter(it => it!.type === 'shortcut')
.map(it => it as Shortcut)