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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
import filter from 'lodash/filter'
import get from 'lodash/get'
import isEmpty from 'lodash/isEmpty'
import keyBy from 'lodash/keyBy'
import times from 'lodash/times'
import PropTypes from 'prop-types'
import { useContext, useMemo, useState } from 'react'
import KeyboardLayout from './KeyboardLayout'
import LayerSelector from './LayerSelector'
import { getKeyBoundingBox } from '../key-units'
import { DefinitionsContext, SearchContext } from '../providers'
function Keyboard(props) {
const { layout, keymap, onUpdate } = props
const [activeLayer, setActiveLayer] = useState(0)
const {keycodes, behaviours} = useContext(DefinitionsContext)
const availableLayers = useMemo(() => isEmpty(keymap) ? [] : (
keymap.layers.map((_, i) => ({
code: i,
description: keymap.layer_names[i] || `Layer ${i}`
}))
), [keymap])
const sources = useMemo(() => ({
kc: keycodes.indexed,
code: keycodes.indexed,
mod: keyBy(filter(keycodes, 'isModifier'), 'code'),
behaviours: behaviours.indexed,
layer: keyBy(availableLayers, 'code')
}), [keycodes, behaviours, availableLayers])
// TODO: this may be unnecessary
const isReady = useMemo(() => function() {
return (
Object.keys(keycodes.indexed).length > 0 &&
Object.keys(behaviours.indexed).length > 0 &&
get(keymap, 'layers.length', 0) > 0
)
}, [keycodes, behaviours, keymap])
const getSearchTargets = useMemo(() => function (param, behaviour) {
// Special case for behaviour commands which can dynamically add another
// parameter that isn't defined at the root level of the behaviour.
// Currently this is just `&bt BT_SEL` and is only represented as an enum.
if (param.enum) {
return param.enum.map(v => ({ code: v }))
}
switch (param) {
case 'behaviour':
return behaviours
case 'layer':
return availableLayers
case 'mod':
return filter(keycodes, 'isModifier')
case 'command':
return get(sources, ['behaviours', behaviour, 'commands'], [])
case 'kc':
default:
return keycodes
}
}, [keycodes, behaviours, sources, availableLayers])
const boundingBox = useMemo(() => function () {
return layout.map(key => getKeyBoundingBox(
{ x: key.x, y: key.y },
{ u: key.u || key.w || 1, h: key.h || 1 },
{ x: key.rx, y: key.ry, a: key.r }
)).reduce(({ x, y }, { max }) => ({
x: Math.max(x, max.x),
y: Math.max(y, max.y)
}), { x: 0, y: 0 })
}, [layout])
const getWrapperStyle = useMemo(() => function () {
const bbox = boundingBox()
return {
width: `${bbox.x}px`,
height: `${bbox.y}px`,
margin: '0 auto',
padding: '40px'
}
}, [boundingBox])
const handleCreateLayer = useMemo(() => function () {
const layer = keymap.layers.length
const binding = '&trans'
const makeKeycode = () => ({ value: binding, params: [] })
const newLayer = times(layout.length, makeKeycode)
const updatedLayerNames = [ ...keymap.layer_names, `Layer #${layer}` ]
const layers = [ ...keymap.layers, newLayer ]
onUpdate({ ...keymap, layer_names: updatedLayerNames, layers })
}, [keymap, layout, onUpdate])
const handleUpdateLayer = useMemo(() => function(layerIndex, updatedLayer) {
const original = keymap.layers
const layers = [
...original.slice(0, layerIndex),
updatedLayer,
...original.slice(layerIndex + 1)
]
onUpdate({ ...keymap, layers })
}, [keymap, onUpdate])
const handleRenameLayer = useMemo(() => function (layerName) {
const layer_names = [
...keymap.layer_names.slice(0, activeLayer),
layerName,
...keymap.layer_names.slice(activeLayer + 1)
]
onUpdate({ ...keymap, layer_names })
}, [keymap, activeLayer, onUpdate])
const handleDeleteLayer = useMemo(() => function (layerIndex) {
const layer_names = [...keymap.layer_names]
layer_names.splice(layerIndex, 1)
const layers = [...keymap.layers]
layers.splice(layerIndex, 1)
setActiveLayer(Math.max(0, layers.length - 1))
onUpdate({ ...keymap, layers, layer_names })
}, [keymap, setActiveLayer, onUpdate])
return (
<>
<LayerSelector
layers={keymap.layer_names}
activeLayer={activeLayer}
onSelect={setActiveLayer}
onNewLayer={handleCreateLayer}
onRenameLayer={handleRenameLayer}
onDeleteLayer={handleDeleteLayer}
/>
<SearchContext.Provider value={{ getSearchTargets }}>
<div style={getWrapperStyle()}>
{isReady() && (
<KeyboardLayout
data-layer={activeLayer}
layout={layout}
bindings={keymap.layers[activeLayer]}
onUpdate={event => handleUpdateLayer(activeLayer, event)}
/>
)}
</div>
</SearchContext.Provider>
</>
)
}
Keyboard.propTypes = {
layout: PropTypes.array.isRequired,
keymap: PropTypes.object.isRequired,
onUpdate: PropTypes.func.isRequired
}
export default Keyboard
|