forked from hotwired/stimulus
/
schema.ts
36 lines (34 loc) · 1.12 KB
/
schema.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
export interface Schema {
controllerAttribute: string
actionAttribute: string
targetAttribute: string
targetAttributeForScope(identifier: string): string
outletAttributeForScope(identifier: string, outlet: string): string
keyMappings: {[key: string]: string}
}
export const defaultSchema: Schema = {
controllerAttribute: "data-controller",
actionAttribute: "data-action",
targetAttribute: "data-target",
targetAttributeForScope: (identifier) => `data-${identifier}-target`,
outletAttributeForScope: (identifier, outlet) => `data-${identifier}-${outlet}-outlet`,
keyMappings: {
"enter": "Enter",
"tab": "Tab",
"esc": "Escape",
"space": " ",
"up": "ArrowUp",
"down": "ArrowDown",
"left": "ArrowLeft",
"right": "ArrowRight",
"home": "Home",
"end": "End",
// [a-z]
...objectFromEntries("abcdefghijklmnopqrstuvwxyz".split("").map(c => [c, c])),
// [0-9]
...objectFromEntries("0123456789".split("").map(n => [n, n]))
}
}
function objectFromEntries(array: [string, any][]): object { // polyfill
return array.reduce((memo, [k, v]) => ({...memo, [k]: v}), {})
}