@mattebox/player-spatial-nav 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -11
- package/dist/cdn/mattebox-player-spatial-nav-gamepad.min.js +1 -0
- package/dist/cdn/mattebox-player-spatial-nav.min.js +1 -1
- package/dist/component.js +40 -44
- package/dist/component.js.map +1 -1
- package/dist/es2015/gamepad-help.js +139 -0
- package/dist/es2015/gamepad-index.js +17 -0
- package/dist/es2015/gamepad.js +321 -0
- package/dist/es2015/host.js +10 -1
- package/dist/es2015/index.js +3 -0
- package/dist/es2015/pad.js +157 -0
- package/dist/es2015/spatial-nav.js +2 -7
- package/dist/gamepad-help.d.ts +30 -0
- package/dist/gamepad-help.d.ts.map +1 -0
- package/dist/gamepad-help.js +137 -0
- package/dist/gamepad-help.js.map +1 -0
- package/dist/gamepad-index.d.ts +24 -0
- package/dist/gamepad-index.d.ts.map +1 -0
- package/dist/gamepad-index.js +19 -0
- package/dist/gamepad-index.js.map +1 -0
- package/dist/gamepad.d.ts +98 -0
- package/dist/gamepad.d.ts.map +1 -0
- package/dist/gamepad.js +316 -0
- package/dist/gamepad.js.map +1 -0
- package/dist/host.d.ts +2 -0
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +23 -14
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -11
- package/dist/index.js.map +1 -1
- package/dist/keys.js +31 -30
- package/dist/keys.js.map +1 -1
- package/dist/navigate.js +78 -85
- package/dist/navigate.js.map +1 -1
- package/dist/pad.d.ts +48 -0
- package/dist/pad.d.ts.map +1 -0
- package/dist/pad.js +157 -0
- package/dist/pad.js.map +1 -0
- package/dist/spatial-nav.d.ts.map +1 -1
- package/dist/spatial-nav.js +173 -201
- package/dist/spatial-nav.js.map +1 -1
- package/package.json +12 -6
package/dist/pad.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// src/pad.ts
|
|
2
|
+
/** Every button, in the standard layout's order: the index is the button's. */
|
|
3
|
+
const BUTTONS = [
|
|
4
|
+
"south",
|
|
5
|
+
"east",
|
|
6
|
+
"west",
|
|
7
|
+
"north",
|
|
8
|
+
"lb",
|
|
9
|
+
"rb",
|
|
10
|
+
"lt",
|
|
11
|
+
"rt",
|
|
12
|
+
"select",
|
|
13
|
+
"start",
|
|
14
|
+
"ls",
|
|
15
|
+
"rs",
|
|
16
|
+
"up",
|
|
17
|
+
"down",
|
|
18
|
+
"left",
|
|
19
|
+
"right"
|
|
20
|
+
];
|
|
21
|
+
/** The default mapping, for an Xbox or PlayStation pad. */
|
|
22
|
+
const DEFAULTS = {
|
|
23
|
+
up: "up",
|
|
24
|
+
down: "down",
|
|
25
|
+
left: "left",
|
|
26
|
+
right: "right",
|
|
27
|
+
south: "activate",
|
|
28
|
+
east: "back",
|
|
29
|
+
west: null,
|
|
30
|
+
north: "subtitles",
|
|
31
|
+
lb: "previous",
|
|
32
|
+
rb: "next",
|
|
33
|
+
lt: "rewind",
|
|
34
|
+
rt: "fast-forward",
|
|
35
|
+
select: "help",
|
|
36
|
+
start: "play-pause",
|
|
37
|
+
ls: null,
|
|
38
|
+
rs: null
|
|
39
|
+
};
|
|
40
|
+
/** The default mapping for `kind`: a Nintendo pad selects with the right face button, its A. */
|
|
41
|
+
function defaults(kind) {
|
|
42
|
+
const map = Object.assign({}, DEFAULTS);
|
|
43
|
+
if (kind === "nintendo") {
|
|
44
|
+
map.south = "back";
|
|
45
|
+
map.east = "activate";
|
|
46
|
+
}
|
|
47
|
+
return map;
|
|
48
|
+
}
|
|
49
|
+
/** The USB vendor ids of the makers whose pads are drawn differently, as four hex digits. */
|
|
50
|
+
const VENDORS = {
|
|
51
|
+
"054c": "playstation",
|
|
52
|
+
"057e": "nintendo"
|
|
53
|
+
};
|
|
54
|
+
/** Words in a pad's id that give its make away where no vendor id is reported. */
|
|
55
|
+
const NAMES = [[/dualsense|dualshock|playstation|\bps[345]\b/i, "playstation"], [/nintendo|switch|joy-?con|pro controller/i, "nintendo"]];
|
|
56
|
+
/**
|
|
57
|
+
* The make of a pad, from the id the browser reports: Chromium writes
|
|
58
|
+
* `Vendor: 054c`, Firefox starts the id with `054c-`. Any other pad is
|
|
59
|
+
* taken for an Xbox pad, the layout most pads follow.
|
|
60
|
+
*/
|
|
61
|
+
function kindOf(id) {
|
|
62
|
+
const vendor = (/vendor:\s*([0-9a-f]{4})/i.exec(id) ?? /^([0-9a-f]{4})-[0-9a-f]{4}-/i.exec(id))?.[1]?.toLowerCase();
|
|
63
|
+
if (vendor !== void 0) return VENDORS[vendor] ?? "xbox";
|
|
64
|
+
for (const [words, kind] of NAMES) if (words.test(id)) return kind;
|
|
65
|
+
return "xbox";
|
|
66
|
+
}
|
|
67
|
+
/** What each make prints on each button, for the controls bubble. */
|
|
68
|
+
const GLYPHS = {
|
|
69
|
+
xbox: {
|
|
70
|
+
south: "A",
|
|
71
|
+
east: "B",
|
|
72
|
+
west: "X",
|
|
73
|
+
north: "Y",
|
|
74
|
+
lb: "LB",
|
|
75
|
+
rb: "RB",
|
|
76
|
+
lt: "LT",
|
|
77
|
+
rt: "RT",
|
|
78
|
+
select: "View",
|
|
79
|
+
start: "Menu",
|
|
80
|
+
ls: "LS",
|
|
81
|
+
rs: "RS",
|
|
82
|
+
up: "↑",
|
|
83
|
+
down: "↓",
|
|
84
|
+
left: "←",
|
|
85
|
+
right: "→"
|
|
86
|
+
},
|
|
87
|
+
playstation: {
|
|
88
|
+
south: "✕",
|
|
89
|
+
east: "○",
|
|
90
|
+
west: "□",
|
|
91
|
+
north: "△",
|
|
92
|
+
lb: "L1",
|
|
93
|
+
rb: "R1",
|
|
94
|
+
lt: "L2",
|
|
95
|
+
rt: "R2",
|
|
96
|
+
select: "Create",
|
|
97
|
+
start: "Options",
|
|
98
|
+
ls: "L3",
|
|
99
|
+
rs: "R3",
|
|
100
|
+
up: "↑",
|
|
101
|
+
down: "↓",
|
|
102
|
+
left: "←",
|
|
103
|
+
right: "→"
|
|
104
|
+
},
|
|
105
|
+
nintendo: {
|
|
106
|
+
south: "B",
|
|
107
|
+
east: "A",
|
|
108
|
+
west: "Y",
|
|
109
|
+
north: "X",
|
|
110
|
+
lb: "L",
|
|
111
|
+
rb: "R",
|
|
112
|
+
lt: "ZL",
|
|
113
|
+
rt: "ZR",
|
|
114
|
+
select: "−",
|
|
115
|
+
start: "+",
|
|
116
|
+
ls: "LS",
|
|
117
|
+
rs: "RS",
|
|
118
|
+
up: "↑",
|
|
119
|
+
down: "↓",
|
|
120
|
+
left: "←",
|
|
121
|
+
right: "→"
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
/** The PlayStation symbols by name, for a screen reader, which may not read the symbol. */
|
|
125
|
+
const SPOKEN = {
|
|
126
|
+
"✕": "Cross",
|
|
127
|
+
"○": "Circle",
|
|
128
|
+
"□": "Square",
|
|
129
|
+
"△": "Triangle"
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* The overrides in a `map` attribute: `button:action` pairs apart by spaces,
|
|
133
|
+
* as `rb:fast-forward west:mute select:none`. `none` unbinds the button. A
|
|
134
|
+
* pair with an unknown button is left out.
|
|
135
|
+
*/
|
|
136
|
+
function parseMap(text) {
|
|
137
|
+
const out = {};
|
|
138
|
+
for (const pair of (text ?? "").split(/\s+/)) {
|
|
139
|
+
const [button, action] = pair.split(":");
|
|
140
|
+
if (button === void 0 || action === void 0 || action === "") continue;
|
|
141
|
+
if (!BUTTONS.includes(button)) continue;
|
|
142
|
+
out[button] = action === "none" ? null : action;
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
/** The mapping for `kind` with the overrides on top. */
|
|
147
|
+
function mapping(kind, ...overrides) {
|
|
148
|
+
const out = defaults(kind);
|
|
149
|
+
for (const layer of overrides) for (const button of BUTTONS) {
|
|
150
|
+
const action = layer[button];
|
|
151
|
+
if (action !== void 0) out[button] = action;
|
|
152
|
+
}
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
export { BUTTONS, GLYPHS, SPOKEN, defaults, kindOf, mapping, parseMap };
|
|
156
|
+
|
|
157
|
+
//# sourceMappingURL=pad.js.map
|
package/dist/pad.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pad.js","names":[],"sources":["../src/pad.ts"],"sourcesContent":["/**\n * The buttons of a game controller, the makes the player tells apart, and\n * the default mapping from buttons to the player's actions.\n *\n * Buttons are named by place, not by letter: `south` is the lower face\n * button on every make, A on an Xbox pad, Cross on a PlayStation pad and B\n * on a Nintendo pad. The indices are the Gamepad API's `standard` layout.\n *\n * No console maker publishes a mapping for a video player. The defaults\n * follow what their own video apps share: the lower face button selects and\n * the right one goes back, Nintendo's the other way round as its letters\n * are; the shoulders go to the previous and next item; the triggers scan\n * while held.\n */\n\n/** A button of a controller in the standard layout, by place. */\nexport type PadButton =\n | 'south'\n | 'east'\n | 'west'\n | 'north'\n | 'lb'\n | 'rb'\n | 'lt'\n | 'rt'\n | 'select'\n | 'start'\n | 'ls'\n | 'rs'\n | 'up'\n | 'down'\n | 'left'\n | 'right';\n\n/** Every button, in the standard layout's order: the index is the button's. */\nexport const BUTTONS: readonly PadButton[] = [\n 'south',\n 'east',\n 'west',\n 'north',\n 'lb',\n 'rb',\n 'lt',\n 'rt',\n 'select',\n 'start',\n 'ls',\n 'rs',\n 'up',\n 'down',\n 'left',\n 'right',\n];\n\n/** An action of the player a button can take. A page may name its own, which only fires the event. */\nexport type PadAction =\n | 'up'\n | 'down'\n | 'left'\n | 'right'\n | 'activate'\n | 'back'\n | 'play-pause'\n | 'previous'\n | 'next'\n | 'rewind'\n | 'fast-forward'\n | 'subtitles'\n | 'mute'\n | 'fullscreen'\n | 'help';\n\n/**\n * A mapping from buttons to actions: a `PadAction`, or a name of the\n * page's own, which only fires `gamepadaction`. Null unbinds a button.\n */\nexport type PadMap = Partial<Record<PadButton, string | null>>;\n\n/** The makes of controller whose buttons are drawn differently. */\nexport type PadKind = 'xbox' | 'playstation' | 'nintendo';\n\n/** The default mapping, for an Xbox or PlayStation pad. */\nconst DEFAULTS: Readonly<Record<PadButton, PadAction | null>> = {\n up: 'up',\n down: 'down',\n left: 'left',\n right: 'right',\n south: 'activate',\n east: 'back',\n west: null,\n north: 'subtitles',\n lb: 'previous',\n rb: 'next',\n lt: 'rewind',\n rt: 'fast-forward',\n select: 'help',\n start: 'play-pause',\n ls: null,\n rs: null,\n};\n\n/** The default mapping for `kind`: a Nintendo pad selects with the right face button, its A. */\nexport function defaults(kind: PadKind): Record<PadButton, PadAction | null> {\n // Object.assign, not a spread: the ES2015 build would lower a spread into a helper.\n const map: Record<PadButton, PadAction | null> = Object.assign({}, DEFAULTS);\n if (kind === 'nintendo') {\n map.south = 'back';\n map.east = 'activate';\n }\n return map;\n}\n\n/** The USB vendor ids of the makers whose pads are drawn differently, as four hex digits. */\nconst VENDORS: Readonly<Record<string, PadKind>> = { '054c': 'playstation', '057e': 'nintendo' };\n\n/** Words in a pad's id that give its make away where no vendor id is reported. */\nconst NAMES: ReadonlyArray<readonly [RegExp, PadKind]> = [\n [/dualsense|dualshock|playstation|\\bps[345]\\b/i, 'playstation'],\n [/nintendo|switch|joy-?con|pro controller/i, 'nintendo'],\n];\n\n/**\n * The make of a pad, from the id the browser reports: Chromium writes\n * `Vendor: 054c`, Firefox starts the id with `054c-`. Any other pad is\n * taken for an Xbox pad, the layout most pads follow.\n */\nexport function kindOf(id: string): PadKind {\n const found = /vendor:\\s*([0-9a-f]{4})/i.exec(id) ?? /^([0-9a-f]{4})-[0-9a-f]{4}-/i.exec(id);\n const vendor = found?.[1]?.toLowerCase();\n if (vendor !== undefined) return VENDORS[vendor] ?? 'xbox';\n for (const [words, kind] of NAMES) if (words.test(id)) return kind;\n return 'xbox';\n}\n\n/** What each make prints on each button, for the controls bubble. */\nexport const GLYPHS: Readonly<Record<PadKind, Readonly<Record<PadButton, string>>>> = {\n xbox: {\n south: 'A',\n east: 'B',\n west: 'X',\n north: 'Y',\n lb: 'LB',\n rb: 'RB',\n lt: 'LT',\n rt: 'RT',\n select: 'View',\n start: 'Menu',\n ls: 'LS',\n rs: 'RS',\n up: '↑',\n down: '↓',\n left: '←',\n right: '→',\n },\n playstation: {\n south: '✕',\n east: '○',\n west: '□',\n north: '△',\n lb: 'L1',\n rb: 'R1',\n lt: 'L2',\n rt: 'R2',\n select: 'Create',\n start: 'Options',\n ls: 'L3',\n rs: 'R3',\n up: '↑',\n down: '↓',\n left: '←',\n right: '→',\n },\n nintendo: {\n south: 'B',\n east: 'A',\n west: 'Y',\n north: 'X',\n lb: 'L',\n rb: 'R',\n lt: 'ZL',\n rt: 'ZR',\n select: '−',\n start: '+',\n ls: 'LS',\n rs: 'RS',\n up: '↑',\n down: '↓',\n left: '←',\n right: '→',\n },\n};\n\n/** The PlayStation symbols by name, for a screen reader, which may not read the symbol. */\nexport const SPOKEN: Readonly<Record<string, string>> = {\n '✕': 'Cross',\n '○': 'Circle',\n '□': 'Square',\n '△': 'Triangle',\n};\n\n/**\n * The overrides in a `map` attribute: `button:action` pairs apart by spaces,\n * as `rb:fast-forward west:mute select:none`. `none` unbinds the button. A\n * pair with an unknown button is left out.\n */\nexport function parseMap(text: string | null): PadMap {\n const out: PadMap = {};\n for (const pair of (text ?? '').split(/\\s+/)) {\n const [button, action] = pair.split(':');\n if (button === undefined || action === undefined || action === '') continue;\n if (!(BUTTONS as readonly string[]).includes(button)) continue;\n out[button as PadButton] = action === 'none' ? null : action;\n }\n return out;\n}\n\n/** The mapping for `kind` with the overrides on top. */\nexport function mapping(\n kind: PadKind,\n ...overrides: readonly PadMap[]\n): Record<PadButton, string | null> {\n const out: Record<PadButton, string | null> = defaults(kind);\n for (const layer of overrides) {\n for (const button of BUTTONS) {\n const action = layer[button];\n if (action !== undefined) out[button] = action;\n }\n }\n return out;\n}\n"],"mappings":";;AAmCA,MAAa,UAAgC;CAC3C;CACA;CACA;C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AI,CAAsE;IAC1F,6CAA6C;IAC7C,QAAgB,OAAO,CAAqB;IAC5C,gFAAgF;IAChF,QAAgB,OAAO,CAAU;IACjC,uEAAuE;IACvE,QAAgB,OAAO,CAAe;;cAWnB,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAgBnD,4EAA4E;IAC5E,OAAO,CAAC,OAAO;
|
|
1
|
+
{"version":3,"file":"spatial-nav.d.ts","sourceRoot":"","sources":["../src/spatial-nav.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAQ5C,qBAAa,aAAc,SAAQ,SAAS;IAC1C,kDAAkD;IAClD,QAAgB,IAAI,CAAqB;IACzC,oFAAoF;IACpF,QAAgB,IAAI,CAAsE;IAC1F,6CAA6C;IAC7C,QAAgB,OAAO,CAAqB;IAC5C,gFAAgF;IAChF,QAAgB,OAAO,CAAU;IACjC,uEAAuE;IACvE,QAAgB,OAAO,CAAe;;cAWnB,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;IAgBnD,4EAA4E;IAC5E,OAAO,CAAC,OAAO;IAIf,qFAAqF;IACrF,OAAO,CAAC,MAAM;IAOd,yFAAyF;IACzF,OAAO,CAAC,KAAK;IAab,OAAO,CAAC,GAAG;IAwDX;;;;OAIG;IACH,OAAO,CAAC,IAAI;IAcZ,kFAAkF;IAClF,OAAO,CAAC,KAAK;IAWb,qGAAqG;IACrG,OAAO,CAAC,IAAI;CAQb"}
|
package/dist/spatial-nav.js
CHANGED
|
@@ -1,203 +1,175 @@
|
|
|
1
|
+
import { Component } from "./component.js";
|
|
2
|
+
import { above, focusedIn } from "./host.js";
|
|
3
|
+
import { isBack, keyOf } from "./keys.js";
|
|
4
|
+
import { DIRECTIONS, OPPOSITE, candidates, nearest } from "./navigate.js";
|
|
5
|
+
// src/spatial-nav.ts
|
|
1
6
|
/**
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (current === null)
|
|
171
|
-
return;
|
|
172
|
-
const cancel = new KeyboardEvent('keydown', {
|
|
173
|
-
key: 'Escape',
|
|
174
|
-
bubbles: true,
|
|
175
|
-
composed: true,
|
|
176
|
-
cancelable: true,
|
|
177
|
-
});
|
|
178
|
-
event.stopPropagation();
|
|
179
|
-
event.preventDefault();
|
|
180
|
-
if (this.send(current, cancel))
|
|
181
|
-
current.blur();
|
|
182
|
-
}
|
|
183
|
-
/** Sends `event` on again under the standard `name`, in place of the original. */
|
|
184
|
-
again(event, name) {
|
|
185
|
-
const target = event.composedPath()[0];
|
|
186
|
-
if (target === undefined)
|
|
187
|
-
return;
|
|
188
|
-
event.stopPropagation();
|
|
189
|
-
event.preventDefault();
|
|
190
|
-
this.send(target, new KeyboardEvent('keydown', { key: name, bubbles: true, composed: true, cancelable: true }));
|
|
191
|
-
}
|
|
192
|
-
/** Dispatches `event` on `target` past this element's own listener; false when a handler took it. */
|
|
193
|
-
send(target, event) {
|
|
194
|
-
this.sending = event;
|
|
195
|
-
try {
|
|
196
|
-
return target.dispatchEvent(event);
|
|
197
|
-
}
|
|
198
|
-
finally {
|
|
199
|
-
this.sending = null;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
7
|
+
* <mbx-spatial-nav>: the remote control for the player on a TV. Placed
|
|
8
|
+
* inside the player, it renders nothing, and it moves focus between the
|
|
9
|
+
* controls with the arrows of a five-way pad.
|
|
10
|
+
*
|
|
11
|
+
* It listens on the player in the capture phase, so it decides before the
|
|
12
|
+
* focused control and the control bar, and takes a key by preventing it,
|
|
13
|
+
* which they then leave alone. It is the one capture-phase listener in the
|
|
14
|
+
* player.
|
|
15
|
+
*
|
|
16
|
+
* - An arrow moves focus to the nearest control on that side (`navigate.ts`).
|
|
17
|
+
* The opposite arrow returns to where the last move came from, so down
|
|
18
|
+
* then up lands where it started.
|
|
19
|
+
* - On a slider, left and right stay the slider's: they seek or set the
|
|
20
|
+
* volume. Up and down move focus.
|
|
21
|
+
* - Inside an element that carries `open`, a menu, every arrow and Enter
|
|
22
|
+
* stay the menu's.
|
|
23
|
+
* - While the player carries `idle`, or after the pointer moved, the first
|
|
24
|
+
* arrow or Enter only places focus, on the control focused last, the one
|
|
25
|
+
* the pointer was over, or the one `initial` names, `mbx-play-button` by
|
|
26
|
+
* default. A stray arrow never seeks.
|
|
27
|
+
* - An arrow with no control on its side fires `navigateout` on this
|
|
28
|
+
* element, bubbling and composed, with the direction as `detail`, so the
|
|
29
|
+
* application's own navigation takes focus out of the player. The arrow
|
|
30
|
+
* is taken all the same: left alone, the control bar would seek on it.
|
|
31
|
+
* - Enter clicks a focused control that is not a native button, which
|
|
32
|
+
* clicks itself.
|
|
33
|
+
* - Back is Escape for the focused control first: a menu goes back a page
|
|
34
|
+
* or closes, the seek bar drops its target. Nothing took it: focus leaves
|
|
35
|
+
* the controls, and the bar fades as it does after any pause. Focus
|
|
36
|
+
* already out: Back is the application's, to leave the player.
|
|
37
|
+
*
|
|
38
|
+
* TV sets report some remote keys by code alone (`keys.ts`). The element
|
|
39
|
+
* sends each on again under the standard name the player reads, so the
|
|
40
|
+
* control bar plays, pauses and seeks on the remote's media keys. The
|
|
41
|
+
* application registers those keys with the platform itself, such as
|
|
42
|
+
* `tizen.tvinputdevice.registerKey`: the player never does.
|
|
43
|
+
*/
|
|
44
|
+
const INITIAL = "mbx-play-button";
|
|
45
|
+
var MbxSpatialNav = class extends Component {
|
|
46
|
+
constructor() {
|
|
47
|
+
super();
|
|
48
|
+
this.last = null;
|
|
49
|
+
this.move = null;
|
|
50
|
+
this.pointed = null;
|
|
51
|
+
this.resting = false;
|
|
52
|
+
this.sending = null;
|
|
53
|
+
}
|
|
54
|
+
attach(player) {
|
|
55
|
+
this.on(player, "keydown", (event) => this.key(player, event), { capture: true });
|
|
56
|
+
this.on(player, "pointermove", (event) => {
|
|
57
|
+
this.resting = true;
|
|
58
|
+
const list = candidates(player);
|
|
59
|
+
const over = event.composedPath().find((node) => list.includes(node));
|
|
60
|
+
if (over !== void 0) this.pointed = over;
|
|
61
|
+
});
|
|
62
|
+
this.on(player, "focusin", () => {
|
|
63
|
+
const current = this.focused(player);
|
|
64
|
+
if (current !== null) this.last = current;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** The control focused inside the player, through shadow roots, or null. */
|
|
68
|
+
focused(player) {
|
|
69
|
+
return focusedIn(player);
|
|
70
|
+
}
|
|
71
|
+
/** Whether `element` sits inside something that carries `open`, below the player. */
|
|
72
|
+
inOpen(player, element) {
|
|
73
|
+
for (let node = element; node !== null && node !== player; node = above(node)) if (node instanceof HTMLElement && node.hasAttribute("open")) return true;
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
/** Where the first key puts focus: the pointer's control, the last one, or `initial`. */
|
|
77
|
+
start(player) {
|
|
78
|
+
const list = candidates(player);
|
|
79
|
+
for (const known of [this.pointed, this.last]) if (known !== null && list.includes(known)) return known;
|
|
80
|
+
const named = player.querySelector(this.getAttribute("initial") ?? INITIAL);
|
|
81
|
+
return list.find((element) => named?.contains(element) || named?.shadowRoot?.contains(element)) ?? list[0] ?? null;
|
|
82
|
+
}
|
|
83
|
+
key(player, event) {
|
|
84
|
+
if (event === this.sending) return;
|
|
85
|
+
if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey) return;
|
|
86
|
+
const name = keyOf(event);
|
|
87
|
+
const direction = DIRECTIONS[name];
|
|
88
|
+
if (direction === void 0 && name !== "Enter") {
|
|
89
|
+
if (isBack(name)) this.back(player, event);
|
|
90
|
+
else if (name !== event.key) this.again(event, name);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const current = this.focused(player);
|
|
94
|
+
if (current === null || this.resting || player.hasAttribute("idle")) {
|
|
95
|
+
this.resting = false;
|
|
96
|
+
const start = this.start(player);
|
|
97
|
+
if (start === null) return;
|
|
98
|
+
event.preventDefault();
|
|
99
|
+
start.focus();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (this.inOpen(player, current)) return;
|
|
103
|
+
if (direction === void 0) {
|
|
104
|
+
if (current instanceof HTMLButtonElement && name === event.key || current.getAttribute("role") === "slider") return;
|
|
105
|
+
event.preventDefault();
|
|
106
|
+
current.click();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (current.getAttribute("role") === "slider" && (direction === "left" || direction === "right")) {
|
|
110
|
+
if (name !== event.key) this.again(event, name);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const list = candidates(player).filter((element) => element !== current);
|
|
114
|
+
const back = this.move;
|
|
115
|
+
const next = back !== null && back.to === current && back.direction === OPPOSITE[direction] && list.includes(back.from) ? back.from : nearest(current.getBoundingClientRect(), list, direction);
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
if (next === null) {
|
|
118
|
+
this.dispatchEvent(new CustomEvent("navigateout", {
|
|
119
|
+
detail: direction,
|
|
120
|
+
bubbles: true,
|
|
121
|
+
composed: true
|
|
122
|
+
}));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.move = {
|
|
126
|
+
from: current,
|
|
127
|
+
to: next,
|
|
128
|
+
direction
|
|
129
|
+
};
|
|
130
|
+
next.focus();
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Back: Escape for the focused control first. Nothing took it and focus
|
|
134
|
+
* is on a control: focus leaves, and the bar fades. Otherwise it is the
|
|
135
|
+
* application's.
|
|
136
|
+
*/
|
|
137
|
+
back(player, event) {
|
|
138
|
+
const current = this.focused(player);
|
|
139
|
+
if (current === null) return;
|
|
140
|
+
const cancel = new KeyboardEvent("keydown", {
|
|
141
|
+
key: "Escape",
|
|
142
|
+
bubbles: true,
|
|
143
|
+
composed: true,
|
|
144
|
+
cancelable: true
|
|
145
|
+
});
|
|
146
|
+
event.stopPropagation();
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
if (this.send(current, cancel)) current.blur();
|
|
149
|
+
}
|
|
150
|
+
/** Sends `event` on again under the standard `name`, in place of the original. */
|
|
151
|
+
again(event, name) {
|
|
152
|
+
const target = event.composedPath()[0];
|
|
153
|
+
if (target === void 0) return;
|
|
154
|
+
event.stopPropagation();
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
this.send(target, new KeyboardEvent("keydown", {
|
|
157
|
+
key: name,
|
|
158
|
+
bubbles: true,
|
|
159
|
+
composed: true,
|
|
160
|
+
cancelable: true
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
/** Dispatches `event` on `target` past this element's own listener; false when a handler took it. */
|
|
164
|
+
send(target, event) {
|
|
165
|
+
this.sending = event;
|
|
166
|
+
try {
|
|
167
|
+
return target.dispatchEvent(event);
|
|
168
|
+
} finally {
|
|
169
|
+
this.sending = null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
export { MbxSpatialNav };
|
|
174
|
+
|
|
203
175
|
//# sourceMappingURL=spatial-nav.js.map
|
package/dist/spatial-nav.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spatial-nav.js","sourceRoot":"","sources":["../src/spatial-nav.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE1E,MAAM,OAAO,GAAG,iBAAiB,CAAC;AAElC,MAAM,OAAO,aAAc,SAAQ,SAAS;IAY1C;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;IACtB,CAAC;IAEkB,MAAM,CAAC,MAAkB;QAC1C,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,KAAsB,CAAC,EAAE;YAC9E,OAAO,EAAE,IAAI;SACd,CAAC,CAAC;QACH,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,aAAa,EAAE,CAAC,KAAK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M,GAAG,IAAI,aAAa,CAAC,SAAS,EAAE;YAC1C,GAAG,EAAE,QAAQ;YACb,OAAO,EAAE,IAAI;YACb,QAAQ,EAAE,IAAI;YACd,UAAU,EAAE,IAAI;SACjB,CAAC,CAAC;QACH,KAAK,CAAC,eAAe,EAAE,CAAC;QACxB,KAAK,CAAC,cAAc,EAAE,CAAC;QACvB,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC;YAAE,OAAO,CAAC,IAAI,EAAE,CAAC;IACjD,CAAC;IAED,kFAAkF;IAC1E,KAAK,CAAC,KAAoB,EAAE,IAAY;QAC9C,MAAM,MAAM,GAAG,KAAK,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;QACvC,IAAI,MAAM,KAAK,SAAS;YAAE,OAAO;QACjC,KAAK,CAAC,eAAe,EAAE,CAAC;QACxB,KAAK,CAAC,cAAc,EAAE,CAAC;QACvB,IAAI,CAAC,IAAI,CACP,MAAM,EACN,IAAI,aAAa,CAAC,SAAS,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAC7F,CAAC;IACJ,CAAC;IAED,qGAAqG;IAC7F,IAAI,CAAC,MAAmB,EAAE,KAAY;QAC5C,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC;YACH,OAAO,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;QACrC,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACtB,CAAC;IACH,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"spatial-nav.js","names":[],"sources":["../src/spatial-nav.ts"],"sourcesContent":["/**\n * <mbx-spatial-nav>: the remote control for the player on a TV. Placed\n * inside the player, it renders nothing, and it moves focus between the\n * controls with the arrows of a five-way pad.\n *\n * It listens on the player in the capture phase, so it decides before the\n * focused control and the control bar, and takes a key by preventing it,\n * which they then leave alone. It is the one capture-phase listener in the\n * player.\n *\n * - An arrow moves focus to the nearest control on that side (`navigate.ts`).\n * The opposite arrow returns to where the last move came from, so down\n * then up lands where it started.\n * - On a slider, left and right stay the slider's: they seek or set the\n * volume. Up and down move focus.\n * - Inside an element that carries `open`, a menu, every arrow and Enter\n * stay the menu's.\n * - While the player carries `idle`, or after the pointer moved, the first\n * arrow or Enter only places focus, on the control focused last, the one\n * the pointer was over, or the one `initial` names, `mbx-play-button` by\n * default. A stray arrow never seeks.\n * - An arrow with no control on its side fires `navigateout` on this\n * element, bubbling and composed, with the direction as `detail`, so the\n * application's own navigation takes focus out of the player. The arrow\n * is taken all the same: left alone, the control bar would seek on it.\n * - Enter clicks a focused control that is not a native button, which\n * clicks itself.\n * - Back is Escape for the focused control first: a menu goes back a page\n * or closes, the seek bar drops its target. Nothing took it: focus leaves\n * the controls, and the bar fades as it does after any pause. Focus\n * already out: Back is the application's, to leave the player.\n *\n * TV sets report some remote keys by code alone (`keys.ts`). The element\n * sends each on again under the standard name the player reads, so the\n * control bar plays, pauses and seeks on the remote's media keys. The\n * application registers those keys with the platform itself, such as\n * `tizen.tvinputdevice.registerKey`: the player never does.\n */\nimport { Component } from './component.js';\nimport type { PlayerHost } from './host.js';\nimport { above, focusedIn } from './host.js';\nimport { isBack, keyOf } from './keys.js';\nimport type { Direction } from './navigate.js';\nimport { candidates, DIRECTIONS, nearest, OPPOSITE } from './navigate.js';\n\nconst INITIAL = 'mbx-play-button';\n\nexport class MbxSpatialNav extends Component {\n /** The control focused last inside the player. */\n declare private last: HTMLElement | null;\n /** The last move: from where, to where, which way. The opposite arrow goes back. */\n declare private move: { from: HTMLElement; to: HTMLElement; direction: Direction } | null;\n /** The control the pointer was over last. */\n declare private pointed: HTMLElement | null;\n /** The pointer moved: the next key places focus, from where the pointer was. */\n declare private resting: boolean;\n /** The key this element sends on, which its own listener lets pass. */\n declare private sending: Event | null;\n\n constructor() {\n super();\n this.last = null;\n this.move = null;\n this.pointed = null;\n this.resting = false;\n this.sending = null;\n }\n\n protected override attach(player: PlayerHost): void {\n this.on(player, 'keydown', (event) => this.key(player, event as KeyboardEvent), {\n capture: true,\n });\n this.on(player, 'pointermove', (event) => {\n this.resting = true;\n const list = candidates(player);\n const over = event.composedPath().find((node) => list.includes(node as HTMLElement));\n if (over !== undefined) this.pointed = over as HTMLElement;\n });\n this.on(player, 'focusin', () => {\n const current = this.focused(player);\n if (current !== null) this.last = current;\n });\n }\n\n /** The control focused inside the player, through shadow roots, or null. */\n private focused(player: PlayerHost): HTMLElement | null {\n return focusedIn(player);\n }\n\n /** Whether `element` sits inside something that carries `open`, below the player. */\n private inOpen(player: PlayerHost, element: HTMLElement): boolean {\n for (let node: Node | null = element; node !== null && node !== player; node = above(node)) {\n if (node instanceof HTMLElement && node.hasAttribute('open')) return true;\n }\n return false;\n }\n\n /** Where the first key puts focus: the pointer's control, the last one, or `initial`. */\n private start(player: PlayerHost): HTMLElement | null {\n const list = candidates(player);\n for (const known of [this.pointed, this.last]) {\n if (known !== null && list.includes(known)) return known;\n }\n const named = player.querySelector(this.getAttribute('initial') ?? INITIAL);\n return (\n list.find((element) => named?.contains(element) || named?.shadowRoot?.contains(element)) ??\n list[0] ??\n null\n );\n }\n\n private key(player: PlayerHost, event: KeyboardEvent): void {\n if (event === this.sending) return;\n if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey) return;\n const name = keyOf(event);\n const direction = DIRECTIONS[name];\n if (direction === undefined && name !== 'Enter') {\n if (isBack(name)) this.back(player, event);\n else if (name !== event.key) this.again(event, name);\n return;\n }\n const current = this.focused(player);\n // The first key while the bar sleeps, or after the pointer, only places\n // focus, so a stray arrow does not seek.\n if (current === null || this.resting || player.hasAttribute('idle')) {\n this.resting = false;\n const start = this.start(player);\n if (start === null) return;\n event.preventDefault();\n start.focus();\n return;\n }\n if (this.inOpen(player, current)) return;\n if (direction === undefined) {\n // Enter. A native button clicks itself on a real Enter; a code a TV\n // set sent without a name is not one, so the click comes from here.\n const native = current instanceof HTMLButtonElement && name === event.key;\n if (native || current.getAttribute('role') === 'slider') return;\n event.preventDefault();\n current.click();\n return;\n }\n const slider = current.getAttribute('role') === 'slider';\n if (slider && (direction === 'left' || direction === 'right')) {\n if (name !== event.key) this.again(event, name);\n return;\n }\n const list = candidates(player).filter((element) => element !== current);\n const back = this.move;\n const next =\n back !== null &&\n back.to === current &&\n back.direction === OPPOSITE[direction] &&\n list.includes(back.from)\n ? back.from\n : nearest(current.getBoundingClientRect(), list, direction);\n event.preventDefault();\n if (next === null) {\n this.dispatchEvent(\n new CustomEvent('navigateout', { detail: direction, bubbles: true, composed: true }),\n );\n return;\n }\n this.move = { from: current, to: next, direction };\n next.focus();\n }\n\n /**\n * Back: Escape for the focused control first. Nothing took it and focus\n * is on a control: focus leaves, and the bar fades. Otherwise it is the\n * application's.\n */\n private back(player: PlayerHost, event: KeyboardEvent): void {\n const current = this.focused(player);\n if (current === null) return;\n const cancel = new KeyboardEvent('keydown', {\n key: 'Escape',\n bubbles: true,\n composed: true,\n cancelable: true,\n });\n event.stopPropagation();\n event.preventDefault();\n if (this.send(current, cancel)) current.blur();\n }\n\n /** Sends `event` on again under the standard `name`, in place of the original. */\n private again(event: KeyboardEvent, name: string): void {\n const target = event.composedPath()[0];\n if (target === undefined) return;\n event.stopPropagation();\n event.preventDefault();\n this.send(\n target,\n new KeyboardEvent('keydown', { key: name, bubbles: true, composed: true, cancelable: true }),\n );\n }\n\n /** Dispatches `event` on `target` past this element's own listener; false when a handler took it. */\n private send(target: EventTarget, event: Event): boolean {\n this.sending = event;\n try {\n return target.dispatchEvent(event);\n } finally {\n this.sending = null;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,MAAM,UAAU;AAEhB,IAAa,gBAAb,cAAmC,UAAU;CAY3C,cAAc;EACZ,MAAM;EACN,KAAK,OAAO;EACZ,KAAK,O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