@mattebox/player-spatial-nav 0.0.0 → 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Josep Boix Requesens
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ <h1>
2
+ <img src="https://raw.githubusercontent.com/jboix/mattebox-player/main/docs/logo.svg" width="44" height="44" align="absmiddle" alt="">
3
+ @mattebox/player-spatial-nav
4
+ </h1>
5
+
6
+ `<mbx-spatial-nav>` for the
7
+ [Mattebox player](https://github.com/jboix/mattebox-player): the remote
8
+ control on a TV.
9
+
10
+ - The arrows move focus to the nearest control on their side. The opposite arrow goes back.
11
+ - On a slider, left and right seek or set the volume. In an open menu, every arrow is the menu's.
12
+ - The remote's Back goes back a step: the menu, then the controls, then your application.
13
+ - The remote's media keys reach the player under their standard names, on Tizen and webOS too.
14
+
15
+ The import registers the element. It is a package of its own because most
16
+ pages never run on a TV.
17
+
18
+ ```sh
19
+ npm install @mattebox/player-spatial-nav @mattebox/player mattebox
20
+ ```
21
+
22
+ ```html
23
+ <mattebox-player src="https://example.com/vod/master.m3u8" controls="custom">
24
+ <mbx-spatial-nav></mbx-spatial-nav>
25
+ <mbx-control-bar>
26
+ <mbx-seek-bar key-mode="preview"></mbx-seek-bar>
27
+ <mbx-play-button></mbx-play-button>
28
+ <mbx-spacer></mbx-spacer>
29
+ <mbx-subtitles-menu></mbx-subtitles-menu>
30
+ </mbx-control-bar>
31
+ </mattebox-player>
32
+
33
+ <script type="module">
34
+ import '@mattebox/player';
35
+ import '@mattebox/player-spatial-nav';
36
+ </script>
37
+ ```
38
+
39
+ | Attribute | What it does |
40
+ | --------- | -------------------------------------------------------------------------- |
41
+ | `initial` | The tag of the control the first key focuses. `mbx-play-button` by default |
42
+
43
+ | Event | When |
44
+ | ------------- | -------------------------------------------------------------------------------------------------- |
45
+ | `navigateout` | An arrow finds no control on its side. `detail` is the direction. Your application moves focus out |
46
+
47
+ Your application registers the remote's media keys with the platform, for
48
+ example `tizen.tvinputdevice.registerKey` on Tizen. The player never does.
49
+
50
+ The [player guide](https://github.com/jboix/mattebox-player/blob/main/docs/guide/06-tv.md)
51
+ covers the element in chapter 06.
52
+
53
+ ## License
54
+
55
+ MIT
@@ -0,0 +1 @@
1
+ var matteboxPlayerSpatialNav=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function t(e){let t=e.parentNode;return t===null?e instanceof ShadowRoot?e.host:null:t}function n(e){let n=e;for(;n!==null;){if(n instanceof HTMLElement&&n.localName===`mattebox-player`)return n;n=t(n)}return null}var r=class extends HTMLElement{constructor(){super(),this.player=null,this.offs=[],this.epoch=0}connectedCallback(){let e=n(this);if(e===null)return;this.epoch+=1;let t=this.epoch,r=()=>{t===this.epoch&&this.isConnected&&(customElements.upgrade(e),`video`in e&&(this.player=e,this.attach(this.player)))};`video`in e?r():customElements.whenDefined(`mattebox-player`).then(r)}disconnectedCallback(){this.epoch+=1;for(let e of this.offs)e();this.offs=[],this.player=null}on(e,t,n,r){e.addEventListener(t,n,r),this.offs.push(()=>{e.removeEventListener(t,n,r)})}};let i={10009:`GoBack`,461:`GoBack`,10252:`MediaPlayPause`,415:`MediaPlay`,19:`MediaPause`,413:`MediaStop`,417:`MediaFastForward`,412:`MediaRewind`},a={13:`Enter`,37:`ArrowLeft`,38:`ArrowUp`,39:`ArrowRight`,40:`ArrowDown`};function o(e){var t;let n=i[e.keyCode];return n===void 0?e.key!==``&&e.key!==`Unidentified`||(t=a[e.keyCode])==null?e.key:t:n}function s(e){return e===`GoBack`||e===`BrowserBack`||e===`Escape`}let c={ArrowUp:`up`,ArrowDown:`down`,ArrowLeft:`left`,ArrowRight:`right`},l={up:`down`,down:`up`,left:`right`,right:`left`};function u(e){return e instanceof HTMLElement?e instanceof HTMLButtonElement?!e.disabled:e.getAttribute(`role`)===`slider`||e.hasAttribute(`tabindex`)&&e.tabIndex>=0:!1}function d(e){let t=[],n=r=>{var i,a;r!==e&&u(r)&&r.getClientRects().length>0&&t.push(r);let o=r instanceof HTMLSlotElement?r.assignedElements({flatten:!0}):[...(i=(a=r.shadowRoot)==null?void 0:a.children)==null?[]:i,...r.shadowRoot?[]:r.children];for(let e of o)n(e)};return n(e),t}function f(e,t,n,r){return Math.max(0,n-t,e-r)}function p(e,t,n){let r=n===`left`||n===`right`,i=null,a=[2,0,0];for(let o of t){let t=o.getBoundingClientRect(),s;if(s=n===`right`?t.left-e.right:n===`left`?e.left-t.right:n===`down`?t.top-e.bottom:e.top-t.bottom,s<-1)continue;let c=r?f(e.top,e.bottom,t.top,t.bottom):f(e.left,e.right,t.left,t.right),l=c===0?0:1;if(r&&l===1)continue;let u=[l,Math.max(0,s),c];m(u,a)&&(i=o,a=u)}return i}function m(e,t){for(let i=0;i<e.length;i+=1){var n,r;let a=(n=e[i])==null?0:n,o=(r=t[i])==null?0:r;if(a!==o)return a<o}return!1}var h=class extends r{constructor(){super(),this.last=null,this.move=null,this.pointed=null,this.resting=!1,this.sending=null}attach(e){this.on(e,`keydown`,t=>this.key(e,t),{capture:!0}),this.on(e,`pointermove`,t=>{this.resting=!0;let n=d(e),r=t.composedPath().find(e=>n.includes(e));r!==void 0&&(this.pointed=r)}),this.on(e,`focusin`,()=>{let t=this.focused(e);t!==null&&(this.last=t)})}focused(e){var n;let r=document.activeElement;for(;(r==null||(n=r.shadowRoot)==null?void 0:n.activeElement)!=null;)r=r.shadowRoot.activeElement;if(!(r instanceof HTMLElement)||r===e)return null;for(let n=r;n!==null;n=t(n))if(n===e)return r;return null}inOpen(e,n){for(let r=n;r!==null&&r!==e;r=t(r))if(r instanceof HTMLElement&&r.hasAttribute(`open`))return!0;return!1}start(e){var t,n,r;let i=d(e);for(let e of[this.pointed,this.last])if(e!==null&&i.includes(e))return e;let a=e.querySelector((t=this.getAttribute(`initial`))==null?`mbx-play-button`:t);return(n=(r=i.find(e=>{var t;return(a==null?void 0:a.contains(e))||(a==null||(t=a.shadowRoot)==null?void 0:t.contains(e))}))==null?i[0]:r)==null?null:n}key(e,t){if(t===this.sending||t.defaultPrevented||t.altKey||t.ctrlKey||t.metaKey)return;let n=o(t),r=c[n];if(r===void 0&&n!==`Enter`){s(n)?this.back(e,t):n!==t.key&&this.again(t,n);return}let i=this.focused(e);if(i===null||this.resting||e.hasAttribute(`idle`)){this.resting=!1;let n=this.start(e);if(n===null)return;t.preventDefault(),n.focus();return}if(this.inOpen(e,i))return;if(r===void 0){if(i instanceof HTMLButtonElement&&n===t.key||i.getAttribute(`role`)===`slider`)return;t.preventDefault(),i.click();return}if(i.getAttribute(`role`)===`slider`&&(r===`left`||r===`right`)){n!==t.key&&this.again(t,n);return}let a=d(e).filter(e=>e!==i),u=this.move,f=u!==null&&u.to===i&&u.direction===l[r]&&a.includes(u.from)?u.from:p(i.getBoundingClientRect(),a,r);if(t.preventDefault(),f===null){this.dispatchEvent(new CustomEvent(`navigateout`,{detail:r,bubbles:!0,composed:!0}));return}this.move={from:i,to:f,direction:r},f.focus()}back(e,t){let n=this.focused(e);if(n===null)return;let r=new KeyboardEvent(`keydown`,{key:`Escape`,bubbles:!0,composed:!0,cancelable:!0});t.stopPropagation(),t.preventDefault(),this.send(n,r)&&n.blur()}again(e,t){let n=e.composedPath()[0];n!==void 0&&(e.stopPropagation(),e.preventDefault(),this.send(n,new KeyboardEvent(`keydown`,{key:t,bubbles:!0,composed:!0,cancelable:!0})))}send(e,t){this.sending=t;try{return e.dispatchEvent(t)}finally{this.sending=null}}};let g=`mbx-spatial-nav`;return customElements.get(`mbx-spatial-nav`)===void 0&&customElements.define(g,h),e.MbxSpatialNav=h,e.SPATIAL_NAV=g,e})({});
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Finding the player, and letting go of it: the part of the player's own
3
+ * control base this package needs. The player exports no base class, so a
4
+ * control of the page's own, this one included, implements the contract
5
+ * from outside (player guide, chapter 03).
6
+ *
7
+ * The control attaches when it is connected inside a player, once the
8
+ * player is defined and upgraded, and detaches when it is removed. What it
9
+ * adds goes through `keep`, so detaching drops it all.
10
+ *
11
+ * No class fields: the ES2015 build would lower them into helpers, and the
12
+ * emit check bans helpers. State is declared, then assigned in the
13
+ * constructor.
14
+ */
15
+ import type { PlayerHost } from './host.js';
16
+ export declare abstract class Component extends HTMLElement {
17
+ protected player: PlayerHost | null;
18
+ private offs;
19
+ /** Bumped on every connect and disconnect, so a late upgrade attaches only to the current connection. */
20
+ private epoch;
21
+ constructor();
22
+ connectedCallback(): void;
23
+ disconnectedCallback(): void;
24
+ /** Adds `fn` for `name` on `target`, removed on detach. */
25
+ protected on(target: EventTarget, name: string, fn: (event: Event) => void, options?: AddEventListenerOptions): void;
26
+ /** The control is connected inside an upgraded player. Subscribe. */
27
+ protected abstract attach(player: PlayerHost): void;
28
+ }
29
+ //# sourceMappingURL=component.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"component.d.ts","sourceRoot":"","sources":["../src/component.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAG5C,8BAAsB,SAAU,SAAQ,WAAW;IACjD,UAAkB,MAAM,EAAE,UAAU,GAAG,IAAI,CAAC;IAC5C,QAAgB,IAAI,CAAoB;IACxC,yGAAyG;IACzG,QAAgB,KAAK,CAAS;;IAS9B,iBAAiB,IAAI,IAAI;IAiBzB,oBAAoB,IAAI,IAAI;IAO5B,2DAA2D;IAC3D,SAAS,CAAC,EAAE,CACV,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,EAC1B,OAAO,CAAC,EAAE,uBAAuB,GAChC,IAAI;IAOP,qEAAqE;IACrE,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI;CACpD"}
@@ -0,0 +1,45 @@
1
+ import { findPlayer, PLAYER } from './host.js';
2
+ export class Component extends HTMLElement {
3
+ constructor() {
4
+ super();
5
+ this.player = null;
6
+ this.offs = [];
7
+ this.epoch = 0;
8
+ }
9
+ connectedCallback() {
10
+ const found = findPlayer(this);
11
+ if (found === null)
12
+ return;
13
+ this.epoch += 1;
14
+ const epoch = this.epoch;
15
+ const attach = () => {
16
+ if (epoch !== this.epoch || !this.isConnected)
17
+ return;
18
+ // Defined, and this instance upgraded: `video` is on the prototype.
19
+ customElements.upgrade(found);
20
+ if (!('video' in found))
21
+ return;
22
+ this.player = found;
23
+ this.attach(this.player);
24
+ };
25
+ if ('video' in found)
26
+ attach();
27
+ else
28
+ void customElements.whenDefined(PLAYER).then(attach);
29
+ }
30
+ disconnectedCallback() {
31
+ this.epoch += 1;
32
+ for (const off of this.offs)
33
+ off();
34
+ this.offs = [];
35
+ this.player = null;
36
+ }
37
+ /** Adds `fn` for `name` on `target`, removed on detach. */
38
+ on(target, name, fn, options) {
39
+ target.addEventListener(name, fn, options);
40
+ this.offs.push(() => {
41
+ target.removeEventListener(name, fn, options);
42
+ });
43
+ }
44
+ }
45
+ //# sourceMappingURL=component.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"component.js","sourceRoot":"","sources":["../src/component.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAE/C,MAAM,OAAgB,SAAU,SAAQ,WAAW;IAMjD;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;IACjB,CAAC;IAED,iBAAiB;QACf,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;QAC/B,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO;QAC3B,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC;QAChB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACzB,MAAM,MAAM,GAAG,GAAS,EAAE;YACxB,IAAI,KAAK,KAAK,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,WAAW;gBAAE,OAAO;YACtD,oEAAoE;YACpE,cAAc,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YAC9B,IAAI,CAAC,CAAC,OAAO,IAAI,KAAK,CAAC;gBAAE,OAAO;YAChC,IAAI,CAAC,MAAM,GAAG,KAAmB,CAAC;YAClC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC3B,CAAC,CAAC;QACF,IAAI,OAAO,IAAI,KAAK;YAAE,MAAM,EAAE,CAAC;;YAC1B,KAAK,cAAc,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5D,CAAC;IAED,oBAAoB;QAClB,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC;QAChB,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,IAAI;YAAE,GAAG,EAAE,CAAC;QACnC,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;IACrB,CAAC;IAED,2DAA2D;IACjD,EAAE,CACV,MAAmB,EACnB,IAAY,EACZ,EAA0B,EAC1B,OAAiC;QAEjC,MAAM,CAAC,gBAAgB,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QAC3C,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE;YAClB,MAAM,CAAC,mBAAmB,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC,CAAC,CAAC;IACL,CAAC;CAIF"}
@@ -0,0 +1,40 @@
1
+ import { PLAYER, findPlayer } from "./host.js";
2
+ //#region src/component.ts
3
+ var Component = class extends HTMLElement {
4
+ constructor() {
5
+ super();
6
+ this.player = null;
7
+ this.offs = [];
8
+ this.epoch = 0;
9
+ }
10
+ connectedCallback() {
11
+ const found = findPlayer(this);
12
+ if (found === null) return;
13
+ this.epoch += 1;
14
+ const epoch = this.epoch;
15
+ const attach = () => {
16
+ if (epoch !== this.epoch || !this.isConnected) return;
17
+ customElements.upgrade(found);
18
+ if (!("video" in found)) return;
19
+ this.player = found;
20
+ this.attach(this.player);
21
+ };
22
+ if ("video" in found) attach();
23
+ else customElements.whenDefined(PLAYER).then(attach);
24
+ }
25
+ disconnectedCallback() {
26
+ this.epoch += 1;
27
+ for (const off of this.offs) off();
28
+ this.offs = [];
29
+ this.player = null;
30
+ }
31
+ /** Adds `fn` for `name` on `target`, removed on detach. */
32
+ on(target, name, fn, options) {
33
+ target.addEventListener(name, fn, options);
34
+ this.offs.push(() => {
35
+ target.removeEventListener(name, fn, options);
36
+ });
37
+ }
38
+ };
39
+ //#endregion
40
+ export { Component };
@@ -0,0 +1,19 @@
1
+ //#region src/host.ts
2
+ const PLAYER = "mattebox-player";
3
+ /** The parent, or the host of the shadow root `node` sits in, or null at the document. */
4
+ function above(node) {
5
+ const parent = node.parentNode;
6
+ if (parent !== null) return parent;
7
+ return node instanceof ShadowRoot ? node.host : null;
8
+ }
9
+ /** The nearest player above `node`, or null outside one. It may not be upgraded yet. */
10
+ function findPlayer(node) {
11
+ let current = node;
12
+ while (current !== null) {
13
+ if (current instanceof HTMLElement && current.localName === "mattebox-player") return current;
14
+ current = above(current);
15
+ }
16
+ return null;
17
+ }
18
+ //#endregion
19
+ export { PLAYER, above, findPlayer };
@@ -0,0 +1,12 @@
1
+ import { MbxSpatialNav } from "./spatial-nav.js";
2
+ //#region src/index.ts
3
+ /**
4
+ * @mattebox/player-spatial-nav: <mbx-spatial-nav>, the remote control for the player
5
+ * on a TV. Importing this module registers the element; that is the
6
+ * package's one side effect. It is a package of its own because most pages
7
+ * never run on a TV, and a page that does not pays nothing for it.
8
+ */
9
+ const SPATIAL_NAV = "mbx-spatial-nav";
10
+ if (customElements.get("mbx-spatial-nav") === void 0) customElements.define(SPATIAL_NAV, MbxSpatialNav);
11
+ //#endregion
12
+ export { MbxSpatialNav, SPATIAL_NAV };
@@ -0,0 +1,42 @@
1
+ //#region src/keys.ts
2
+ /**
3
+ * The keys of a TV remote, as the standard `KeyboardEvent.key` names the
4
+ * player reads. Tizen and webOS report several remote keys by `keyCode`
5
+ * alone, with an empty, `Unidentified`, or vendor `key`. The arrows and
6
+ * Enter come the same way on older sets.
7
+ *
8
+ * Back: 10009 on Tizen, 461 on webOS. Media keys: Samsung's Tizen TV key
9
+ * list and LG's webOS TV key codes. The codes shared with a desktop
10
+ * keyboard (the arrows, Enter) map only when `key` names nothing.
11
+ */
12
+ const BY_CODE = {
13
+ 10009: "GoBack",
14
+ 461: "GoBack",
15
+ 10252: "MediaPlayPause",
16
+ 415: "MediaPlay",
17
+ 19: "MediaPause",
18
+ 413: "MediaStop",
19
+ 417: "MediaFastForward",
20
+ 412: "MediaRewind"
21
+ };
22
+ /** Keys a desktop keyboard also sends, by code, for sets that leave `key` empty. */
23
+ const PLAIN = {
24
+ 13: "Enter",
25
+ 37: "ArrowLeft",
26
+ 38: "ArrowUp",
27
+ 39: "ArrowRight",
28
+ 40: "ArrowDown"
29
+ };
30
+ /** The standard name of the key `event` reports: its own `key` when it has one the player reads. */
31
+ function keyOf(event) {
32
+ var _PLAIN$event$keyCode;
33
+ const remote = BY_CODE[event.keyCode];
34
+ if (remote !== void 0) return remote;
35
+ return event.key !== "" && event.key !== "Unidentified" ? event.key : (_PLAIN$event$keyCode = PLAIN[event.keyCode]) !== null && _PLAIN$event$keyCode !== void 0 ? _PLAIN$event$keyCode : event.key;
36
+ }
37
+ /** The keys that go back: the remote's Back, the browser's, and Escape. */
38
+ function isBack(key) {
39
+ return key === "GoBack" || key === "BrowserBack" || key === "Escape";
40
+ }
41
+ //#endregion
42
+ export { isBack, keyOf };
@@ -0,0 +1,94 @@
1
+ //#region src/navigate.ts
2
+ const DIRECTIONS = {
3
+ ArrowUp: "up",
4
+ ArrowDown: "down",
5
+ ArrowLeft: "left",
6
+ ArrowRight: "right"
7
+ };
8
+ const OPPOSITE = {
9
+ up: "down",
10
+ down: "up",
11
+ left: "right",
12
+ right: "left"
13
+ };
14
+ function focusable(element) {
15
+ if (!(element instanceof HTMLElement)) return false;
16
+ if (element instanceof HTMLButtonElement) return !element.disabled;
17
+ if (element.getAttribute("role") === "slider") return true;
18
+ return element.hasAttribute("tabindex") && element.tabIndex >= 0;
19
+ }
20
+ /** The focusable elements under `root`, in composed tree order, `root` itself left out. */
21
+ function candidates(root) {
22
+ const out = [];
23
+ const visit = (node) => {
24
+ var _node$shadowRoot$chil, _node$shadowRoot;
25
+ if (node !== root && focusable(node) && node.getClientRects().length > 0) out.push(node);
26
+ const children = node instanceof HTMLSlotElement ? node.assignedElements({ flatten: true }) : [...(_node$shadowRoot$chil = (_node$shadowRoot = node.shadowRoot) === null || _node$shadowRoot === void 0 ? void 0 : _node$shadowRoot.children) !== null && _node$shadowRoot$chil !== void 0 ? _node$shadowRoot$chil : [], ...node.shadowRoot ? [] : node.children];
27
+ for (const child of children) visit(child);
28
+ };
29
+ visit(root);
30
+ return out;
31
+ }
32
+ /** The gap between two spans on one axis: zero when they overlap. */
33
+ function gap(a0, a1, b0, b1) {
34
+ return Math.max(0, b0 - a1, a0 - b1);
35
+ }
36
+ /**
37
+ * The candidate nearest `from` on the side of `direction`, or null. A
38
+ * candidate is on that side when it lies wholly beyond `from`'s edge, as in
39
+ * the W3C CSS Spatial Navigation draft: the seek bar above a row of buttons
40
+ * spans the row, and is up from each button, never right of one.
41
+ *
42
+ * Left and right stay in the row: only a candidate that overlaps `from`
43
+ * vertically counts, so the arrows never jump to the row above because a
44
+ * control there happens to start past this one's edge. Up and down prefer a
45
+ * candidate in line, overlapping `from` horizontally, and take the nearest
46
+ * other one when there is none, so a button at the end of a row still
47
+ * reaches the bar above it.
48
+ *
49
+ * Among the candidates, the nearest along the arrow wins, then the nearest
50
+ * across it; the earlier in the tree breaks a tie.
51
+ */
52
+ function nearest(from, list, direction) {
53
+ const row = direction === "left" || direction === "right";
54
+ let best = null;
55
+ let bestKey = [
56
+ 2,
57
+ 0,
58
+ 0
59
+ ];
60
+ for (const element of list) {
61
+ const to = element.getBoundingClientRect();
62
+ let along;
63
+ if (direction === "right") along = to.left - from.right;
64
+ else if (direction === "left") along = from.left - to.right;
65
+ else if (direction === "down") along = to.top - from.bottom;
66
+ else along = from.top - to.bottom;
67
+ if (along < -1) continue;
68
+ const side = row ? gap(from.top, from.bottom, to.top, to.bottom) : gap(from.left, from.right, to.left, to.right);
69
+ const inLine = side === 0 ? 0 : 1;
70
+ if (row && inLine === 1) continue;
71
+ const key = [
72
+ inLine,
73
+ Math.max(0, along),
74
+ side
75
+ ];
76
+ if (before(key, bestKey)) {
77
+ best = element;
78
+ bestKey = key;
79
+ }
80
+ }
81
+ return best;
82
+ }
83
+ /** Whether `a` ranks before `b`: compared item by item. */
84
+ function before(a, b) {
85
+ for (let i = 0; i < a.length; i += 1) {
86
+ var _a$i, _b$i;
87
+ const x = (_a$i = a[i]) !== null && _a$i !== void 0 ? _a$i : 0;
88
+ const y = (_b$i = b[i]) !== null && _b$i !== void 0 ? _b$i : 0;
89
+ if (x !== y) return x < y;
90
+ }
91
+ return false;
92
+ }
93
+ //#endregion
94
+ export { DIRECTIONS, OPPOSITE, candidates, nearest };
@@ -0,0 +1,183 @@
1
+ import { above } from "./host.js";
2
+ import { Component } from "./component.js";
3
+ import { isBack, keyOf } from "./keys.js";
4
+ import { DIRECTIONS, OPPOSITE, candidates, nearest } from "./navigate.js";
5
+ //#region src/spatial-nav.ts
6
+ /**
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
+ var _active$shadowRoot;
70
+ let active = document.activeElement;
71
+ while ((active === null || active === void 0 || (_active$shadowRoot = active.shadowRoot) === null || _active$shadowRoot === void 0 ? void 0 : _active$shadowRoot.activeElement) != null) active = active.shadowRoot.activeElement;
72
+ if (!(active instanceof HTMLElement) || active === player) return null;
73
+ for (let node = active; node !== null; node = above(node)) if (node === player) return active;
74
+ return null;
75
+ }
76
+ /** Whether `element` sits inside something that carries `open`, below the player. */
77
+ inOpen(player, element) {
78
+ for (let node = element; node !== null && node !== player; node = above(node)) if (node instanceof HTMLElement && node.hasAttribute("open")) return true;
79
+ return false;
80
+ }
81
+ /** Where the first key puts focus: the pointer's control, the last one, or `initial`. */
82
+ start(player) {
83
+ var _this$getAttribute, _ref, _list$find;
84
+ const list = candidates(player);
85
+ for (const known of [this.pointed, this.last]) if (known !== null && list.includes(known)) return known;
86
+ const named = player.querySelector((_this$getAttribute = this.getAttribute("initial")) !== null && _this$getAttribute !== void 0 ? _this$getAttribute : INITIAL);
87
+ return (_ref = (_list$find = list.find((element) => {
88
+ var _named$shadowRoot;
89
+ return (named === null || named === void 0 ? void 0 : named.contains(element)) || (named === null || named === void 0 || (_named$shadowRoot = named.shadowRoot) === null || _named$shadowRoot === void 0 ? void 0 : _named$shadowRoot.contains(element));
90
+ })) !== null && _list$find !== void 0 ? _list$find : list[0]) !== null && _ref !== void 0 ? _ref : null;
91
+ }
92
+ key(player, event) {
93
+ if (event === this.sending) return;
94
+ if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey) return;
95
+ const name = keyOf(event);
96
+ const direction = DIRECTIONS[name];
97
+ if (direction === void 0 && name !== "Enter") {
98
+ if (isBack(name)) this.back(player, event);
99
+ else if (name !== event.key) this.again(event, name);
100
+ return;
101
+ }
102
+ const current = this.focused(player);
103
+ if (current === null || this.resting || player.hasAttribute("idle")) {
104
+ this.resting = false;
105
+ const start = this.start(player);
106
+ if (start === null) return;
107
+ event.preventDefault();
108
+ start.focus();
109
+ return;
110
+ }
111
+ if (this.inOpen(player, current)) return;
112
+ if (direction === void 0) {
113
+ if (current instanceof HTMLButtonElement && name === event.key || current.getAttribute("role") === "slider") return;
114
+ event.preventDefault();
115
+ current.click();
116
+ return;
117
+ }
118
+ if (current.getAttribute("role") === "slider" && (direction === "left" || direction === "right")) {
119
+ if (name !== event.key) this.again(event, name);
120
+ return;
121
+ }
122
+ const list = candidates(player).filter((element) => element !== current);
123
+ const back = this.move;
124
+ const next = back !== null && back.to === current && back.direction === OPPOSITE[direction] && list.includes(back.from) ? back.from : nearest(current.getBoundingClientRect(), list, direction);
125
+ event.preventDefault();
126
+ if (next === null) {
127
+ this.dispatchEvent(new CustomEvent("navigateout", {
128
+ detail: direction,
129
+ bubbles: true,
130
+ composed: true
131
+ }));
132
+ return;
133
+ }
134
+ this.move = {
135
+ from: current,
136
+ to: next,
137
+ direction
138
+ };
139
+ next.focus();
140
+ }
141
+ /**
142
+ * Back: Escape for the focused control first. Nothing took it and focus
143
+ * is on a control: focus leaves, and the bar fades. Otherwise it is the
144
+ * application's.
145
+ */
146
+ back(player, event) {
147
+ const current = this.focused(player);
148
+ if (current === null) return;
149
+ const cancel = new KeyboardEvent("keydown", {
150
+ key: "Escape",
151
+ bubbles: true,
152
+ composed: true,
153
+ cancelable: true
154
+ });
155
+ event.stopPropagation();
156
+ event.preventDefault();
157
+ if (this.send(current, cancel)) current.blur();
158
+ }
159
+ /** Sends `event` on again under the standard `name`, in place of the original. */
160
+ again(event, name) {
161
+ const target = event.composedPath()[0];
162
+ if (target === void 0) return;
163
+ event.stopPropagation();
164
+ event.preventDefault();
165
+ this.send(target, new KeyboardEvent("keydown", {
166
+ key: name,
167
+ bubbles: true,
168
+ composed: true,
169
+ cancelable: true
170
+ }));
171
+ }
172
+ /** Dispatches `event` on `target` past this element's own listener; false when a handler took it. */
173
+ send(target, event) {
174
+ this.sending = event;
175
+ try {
176
+ return target.dispatchEvent(event);
177
+ } finally {
178
+ this.sending = null;
179
+ }
180
+ }
181
+ };
182
+ //#endregion
183
+ export { MbxSpatialNav };
package/dist/host.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The contract of a control of the page's own, as guide chapter 03 of the
3
+ * player states it: the nearest <mattebox-player> above, through shadow
4
+ * hosts. This package implements the contract from outside, as the cast
5
+ * package does: the player exports no base class.
6
+ */
7
+ import type { PlayerHost } from '@mattebox/player';
8
+ export type { PlayerHost };
9
+ export declare const PLAYER = "mattebox-player";
10
+ /** The parent, or the host of the shadow root `node` sits in, or null at the document. */
11
+ export declare function above(node: Node): Node | null;
12
+ /** The nearest player above `node`, or null outside one. It may not be upgraded yet. */
13
+ export declare function findPlayer(node: Node): HTMLElement | null;
14
+ //# sourceMappingURL=host.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.d.ts","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,YAAY,EAAE,UAAU,EAAE,CAAC;AAE3B,eAAO,MAAM,MAAM,oBAAoB,CAAC;AAExC,0FAA0F;AAC1F,wBAAgB,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,CAI7C;AAED,wFAAwF;AACxF,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,WAAW,GAAG,IAAI,CAOzD"}
package/dist/host.js ADDED
@@ -0,0 +1,19 @@
1
+ export const PLAYER = 'mattebox-player';
2
+ /** The parent, or the host of the shadow root `node` sits in, or null at the document. */
3
+ export function above(node) {
4
+ const parent = node.parentNode;
5
+ if (parent !== null)
6
+ return parent;
7
+ return node instanceof ShadowRoot ? node.host : null;
8
+ }
9
+ /** The nearest player above `node`, or null outside one. It may not be upgraded yet. */
10
+ export function findPlayer(node) {
11
+ let current = node;
12
+ while (current !== null) {
13
+ if (current instanceof HTMLElement && current.localName === PLAYER)
14
+ return current;
15
+ current = above(current);
16
+ }
17
+ return null;
18
+ }
19
+ //# sourceMappingURL=host.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.js","sourceRoot":"","sources":["../src/host.ts"],"names":[],"mappings":"AAUA,MAAM,CAAC,MAAM,MAAM,GAAG,iBAAiB,CAAC;AAExC,0FAA0F;AAC1F,MAAM,UAAU,KAAK,CAAC,IAAU;IAC9B,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC;IAC/B,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO,MAAM,CAAC;IACnC,OAAO,IAAI,YAAY,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;AACvD,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,UAAU,CAAC,IAAU;IACnC,IAAI,OAAO,GAAgB,IAAI,CAAC;IAChC,OAAO,OAAO,KAAK,IAAI,EAAE,CAAC;QACxB,IAAI,OAAO,YAAY,WAAW,IAAI,OAAO,CAAC,SAAS,KAAK,MAAM;YAAE,OAAO,OAAO,CAAC;QACnF,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @mattebox/player-spatial-nav: <mbx-spatial-nav>, the remote control for the player
3
+ * on a TV. Importing this module registers the element; that is the
4
+ * package's one side effect. It is a package of its own because most pages
5
+ * never run on a TV, and a page that does not pays nothing for it.
6
+ */
7
+ import { MbxSpatialNav } from './spatial-nav.js';
8
+ export { MbxSpatialNav } from './spatial-nav.js';
9
+ export declare const SPATIAL_NAV = "mbx-spatial-nav";
10
+ declare global {
11
+ interface HTMLElementTagNameMap {
12
+ 'mbx-spatial-nav': MbxSpatialNav;
13
+ }
14
+ }
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,eAAO,MAAM,WAAW,oBAAoB,CAAC;AAM7C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,iBAAiB,EAAE,aAAa,CAAC;KAClC;CACF"}
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @mattebox/player-spatial-nav: <mbx-spatial-nav>, the remote control for the player
3
+ * on a TV. Importing this module registers the element; that is the
4
+ * package's one side effect. It is a package of its own because most pages
5
+ * never run on a TV, and a page that does not pays nothing for it.
6
+ */
7
+ import { MbxSpatialNav } from './spatial-nav.js';
8
+ export { MbxSpatialNav } from './spatial-nav.js';
9
+ export const SPATIAL_NAV = 'mbx-spatial-nav';
10
+ if (customElements.get(SPATIAL_NAV) === undefined) {
11
+ customElements.define(SPATIAL_NAV, MbxSpatialNav);
12
+ }
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,MAAM,CAAC,MAAM,WAAW,GAAG,iBAAiB,CAAC;AAE7C,IAAI,cAAc,CAAC,GAAG,CAAC,WAAW,CAAC,KAAK,SAAS,EAAE,CAAC;IAClD,cAAc,CAAC,MAAM,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC;AACpD,CAAC"}
package/dist/keys.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /** The standard name of the key `event` reports: its own `key` when it has one the player reads. */
2
+ export declare function keyOf(event: KeyboardEvent): string;
3
+ /** The keys that go back: the remote's Back, the browser's, and Escape. */
4
+ export declare function isBack(key: string): boolean;
5
+ //# sourceMappingURL=keys.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AA8BA,oGAAoG;AACpG,wBAAgB,KAAK,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,CAKlD;AAED,2EAA2E;AAC3E,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE3C"}
package/dist/keys.js ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The keys of a TV remote, as the standard `KeyboardEvent.key` names the
3
+ * player reads. Tizen and webOS report several remote keys by `keyCode`
4
+ * alone, with an empty, `Unidentified`, or vendor `key`. The arrows and
5
+ * Enter come the same way on older sets.
6
+ *
7
+ * Back: 10009 on Tizen, 461 on webOS. Media keys: Samsung's Tizen TV key
8
+ * list and LG's webOS TV key codes. The codes shared with a desktop
9
+ * keyboard (the arrows, Enter) map only when `key` names nothing.
10
+ */
11
+ const BY_CODE = {
12
+ 10009: 'GoBack',
13
+ 461: 'GoBack',
14
+ 10252: 'MediaPlayPause',
15
+ 415: 'MediaPlay',
16
+ 19: 'MediaPause',
17
+ 413: 'MediaStop',
18
+ 417: 'MediaFastForward',
19
+ 412: 'MediaRewind',
20
+ };
21
+ /** Keys a desktop keyboard also sends, by code, for sets that leave `key` empty. */
22
+ const PLAIN = {
23
+ 13: 'Enter',
24
+ 37: 'ArrowLeft',
25
+ 38: 'ArrowUp',
26
+ 39: 'ArrowRight',
27
+ 40: 'ArrowDown',
28
+ };
29
+ /** The standard name of the key `event` reports: its own `key` when it has one the player reads. */
30
+ export function keyOf(event) {
31
+ const remote = BY_CODE[event.keyCode];
32
+ if (remote !== undefined)
33
+ return remote;
34
+ const named = event.key !== '' && event.key !== 'Unidentified';
35
+ return named ? event.key : (PLAIN[event.keyCode] ?? event.key);
36
+ }
37
+ /** The keys that go back: the remote's Back, the browser's, and Escape. */
38
+ export function isBack(key) {
39
+ return key === 'GoBack' || key === 'BrowserBack' || key === 'Escape';
40
+ }
41
+ //# sourceMappingURL=keys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.js","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,OAAO,GAAqC;IAChD,KAAK,EAAE,QAAQ;IACf,GAAG,EAAE,QAAQ;IACb,KAAK,EAAE,gBAAgB;IACvB,GAAG,EAAE,WAAW;IAChB,EAAE,EAAE,YAAY;IAChB,GAAG,EAAE,WAAW;IAChB,GAAG,EAAE,kBAAkB;IACvB,GAAG,EAAE,aAAa;CACnB,CAAC;AAEF,oFAAoF;AACpF,MAAM,KAAK,GAAqC;IAC9C,EAAE,EAAE,OAAO;IACX,EAAE,EAAE,WAAW;IACf,EAAE,EAAE,SAAS;IACb,EAAE,EAAE,YAAY;IAChB,EAAE,EAAE,WAAW;CAChB,CAAC;AAEF,oGAAoG;AACpG,MAAM,UAAU,KAAK,CAAC,KAAoB;IACxC,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACtC,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC;IACxC,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,KAAK,EAAE,IAAI,KAAK,CAAC,GAAG,KAAK,cAAc,CAAC;IAC/D,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;AACjE,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,MAAM,CAAC,GAAW;IAChC,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,aAAa,IAAI,GAAG,KAAK,QAAQ,CAAC;AACvE,CAAC"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Where focus goes on an arrow: the candidates, and the nearest one on the
3
+ * side of the arrow.
4
+ *
5
+ * The candidates are every focusable element in the player's composed tree:
6
+ * through open shadow roots, which hold each control's button, and through
7
+ * slot assignment, which is how the controls are placed. Focusable means a
8
+ * `button`, a `role="slider"`, or a `tabindex` of zero or more; disabled,
9
+ * and not laid out, which covers `hidden`, `display: none`, and a collapsed
10
+ * control, rules one out.
11
+ */
12
+ export type Direction = 'up' | 'down' | 'left' | 'right';
13
+ export declare const DIRECTIONS: Readonly<Record<string, Direction>>;
14
+ export declare const OPPOSITE: Readonly<Record<Direction, Direction>>;
15
+ /** The focusable elements under `root`, in composed tree order, `root` itself left out. */
16
+ export declare function candidates(root: Element): HTMLElement[];
17
+ /**
18
+ * The candidate nearest `from` on the side of `direction`, or null. A
19
+ * candidate is on that side when it lies wholly beyond `from`'s edge, as in
20
+ * the W3C CSS Spatial Navigation draft: the seek bar above a row of buttons
21
+ * spans the row, and is up from each button, never right of one.
22
+ *
23
+ * Left and right stay in the row: only a candidate that overlaps `from`
24
+ * vertically counts, so the arrows never jump to the row above because a
25
+ * control there happens to start past this one's edge. Up and down prefer a
26
+ * candidate in line, overlapping `from` horizontally, and take the nearest
27
+ * other one when there is none, so a button at the end of a row still
28
+ * reaches the bar above it.
29
+ *
30
+ * Among the candidates, the nearest along the arrow wins, then the nearest
31
+ * across it; the earlier in the tree breaks a tie.
32
+ */
33
+ export declare function nearest(from: DOMRect, list: readonly HTMLElement[], direction: Direction): HTMLElement | null;
34
+ //# sourceMappingURL=navigate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"navigate.d.ts","sourceRoot":"","sources":["../src/navigate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAEzD,eAAO,MAAM,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAK1D,CAAC;AAEF,eAAO,MAAM,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAK3D,CAAC;AASF,2FAA2F;AAC3F,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,WAAW,EAAE,CAYvD;AAUD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,OAAO,CACrB,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,SAAS,EAAE,SAAS,GACnB,WAAW,GAAG,IAAI,CAwBpB"}
@@ -0,0 +1,100 @@
1
+ export const DIRECTIONS = {
2
+ ArrowUp: 'up',
3
+ ArrowDown: 'down',
4
+ ArrowLeft: 'left',
5
+ ArrowRight: 'right',
6
+ };
7
+ export const OPPOSITE = {
8
+ up: 'down',
9
+ down: 'up',
10
+ left: 'right',
11
+ right: 'left',
12
+ };
13
+ function focusable(element) {
14
+ if (!(element instanceof HTMLElement))
15
+ return false;
16
+ if (element instanceof HTMLButtonElement)
17
+ return !element.disabled;
18
+ if (element.getAttribute('role') === 'slider')
19
+ return true;
20
+ return element.hasAttribute('tabindex') && element.tabIndex >= 0;
21
+ }
22
+ /** The focusable elements under `root`, in composed tree order, `root` itself left out. */
23
+ export function candidates(root) {
24
+ const out = [];
25
+ const visit = (node) => {
26
+ if (node !== root && focusable(node) && node.getClientRects().length > 0)
27
+ out.push(node);
28
+ const children = node instanceof HTMLSlotElement
29
+ ? node.assignedElements({ flatten: true })
30
+ : [...(node.shadowRoot?.children ?? []), ...(node.shadowRoot ? [] : node.children)];
31
+ for (const child of children)
32
+ visit(child);
33
+ };
34
+ visit(root);
35
+ return out;
36
+ }
37
+ /** The gap between two spans on one axis: zero when they overlap. */
38
+ function gap(a0, a1, b0, b1) {
39
+ return Math.max(0, b0 - a1, a0 - b1);
40
+ }
41
+ /** How far a control may overlap the edge it lies beyond, for rounding in layout. */
42
+ const SLACK = 1;
43
+ /**
44
+ * The candidate nearest `from` on the side of `direction`, or null. A
45
+ * candidate is on that side when it lies wholly beyond `from`'s edge, as in
46
+ * the W3C CSS Spatial Navigation draft: the seek bar above a row of buttons
47
+ * spans the row, and is up from each button, never right of one.
48
+ *
49
+ * Left and right stay in the row: only a candidate that overlaps `from`
50
+ * vertically counts, so the arrows never jump to the row above because a
51
+ * control there happens to start past this one's edge. Up and down prefer a
52
+ * candidate in line, overlapping `from` horizontally, and take the nearest
53
+ * other one when there is none, so a button at the end of a row still
54
+ * reaches the bar above it.
55
+ *
56
+ * Among the candidates, the nearest along the arrow wins, then the nearest
57
+ * across it; the earlier in the tree breaks a tie.
58
+ */
59
+ export function nearest(from, list, direction) {
60
+ const row = direction === 'left' || direction === 'right';
61
+ let best = null;
62
+ let bestKey = [2, 0, 0];
63
+ for (const element of list) {
64
+ const to = element.getBoundingClientRect();
65
+ let along;
66
+ if (direction === 'right')
67
+ along = to.left - from.right;
68
+ else if (direction === 'left')
69
+ along = from.left - to.right;
70
+ else if (direction === 'down')
71
+ along = to.top - from.bottom;
72
+ else
73
+ along = from.top - to.bottom;
74
+ if (along < -SLACK)
75
+ continue;
76
+ const side = row
77
+ ? gap(from.top, from.bottom, to.top, to.bottom)
78
+ : gap(from.left, from.right, to.left, to.right);
79
+ const inLine = side === 0 ? 0 : 1;
80
+ if (row && inLine === 1)
81
+ continue;
82
+ const key = [inLine, Math.max(0, along), side];
83
+ if (before(key, bestKey)) {
84
+ best = element;
85
+ bestKey = key;
86
+ }
87
+ }
88
+ return best;
89
+ }
90
+ /** Whether `a` ranks before `b`: compared item by item. */
91
+ function before(a, b) {
92
+ for (let i = 0; i < a.length; i += 1) {
93
+ const x = a[i] ?? 0;
94
+ const y = b[i] ?? 0;
95
+ if (x !== y)
96
+ return x < y;
97
+ }
98
+ return false;
99
+ }
100
+ //# sourceMappingURL=navigate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"navigate.js","sourceRoot":"","sources":["../src/navigate.ts"],"names":[],"mappings":"AAaA,MAAM,CAAC,MAAM,UAAU,GAAwC;IAC7D,OAAO,EAAE,IAAI;IACb,SAAS,EAAE,MAAM;IACjB,SAAS,EAAE,MAAM;IACjB,UAAU,EAAE,OAAO;CACpB,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAA2C;IAC9D,EAAE,EAAE,MAAM;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,OAAO;IACb,KAAK,EAAE,MAAM;CACd,CAAC;AAEF,SAAS,SAAS,CAAC,OAAgB;IACjC,IAAI,CAAC,CAAC,OAAO,YAAY,WAAW,CAAC;QAAE,OAAO,KAAK,CAAC;IACpD,IAAI,OAAO,YAAY,iBAAiB;QAAE,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC;IACnE,IAAI,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAC3D,OAAO,OAAO,CAAC,YAAY,CAAC,UAAU,CAAC,IAAI,O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AM,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC;YACzB,IAAI,GAAG,OAAO,CAAC;YACf,OAAO,GAAG,GAAG,CAAC;QAChB,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,2DAA2D;AAC3D,SAAS,MAAM,CAAC,CAAoB,EAAE,CAAoB;IACxD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACrC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACpB,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QACpB,IAAI,CAAC,KAAK,CAAC;YAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,72 @@
1
+ /**
2
+ * <mbx-spatial-nav>: the remote control for the player on a TV. Placed
3
+ * inside the player, it renders nothing, and it moves focus between the
4
+ * controls with the arrows of a five-way pad.
5
+ *
6
+ * It listens on the player in the capture phase, so it decides before the
7
+ * focused control and the control bar, and takes a key by preventing it,
8
+ * which they then leave alone. It is the one capture-phase listener in the
9
+ * player.
10
+ *
11
+ * - An arrow moves focus to the nearest control on that side (`navigate.ts`).
12
+ * The opposite arrow returns to where the last move came from, so down
13
+ * then up lands where it started.
14
+ * - On a slider, left and right stay the slider's: they seek or set the
15
+ * volume. Up and down move focus.
16
+ * - Inside an element that carries `open`, a menu, every arrow and Enter
17
+ * stay the menu's.
18
+ * - While the player carries `idle`, or after the pointer moved, the first
19
+ * arrow or Enter only places focus, on the control focused last, the one
20
+ * the pointer was over, or the one `initial` names, `mbx-play-button` by
21
+ * default. A stray arrow never seeks.
22
+ * - An arrow with no control on its side fires `navigateout` on this
23
+ * element, bubbling and composed, with the direction as `detail`, so the
24
+ * application's own navigation takes focus out of the player. The arrow
25
+ * is taken all the same: left alone, the control bar would seek on it.
26
+ * - Enter clicks a focused control that is not a native button, which
27
+ * clicks itself.
28
+ * - Back is Escape for the focused control first: a menu goes back a page
29
+ * or closes, the seek bar drops its target. Nothing took it: focus leaves
30
+ * the controls, and the bar fades as it does after any pause. Focus
31
+ * already out: Back is the application's, to leave the player.
32
+ *
33
+ * TV sets report some remote keys by code alone (`keys.ts`). The element
34
+ * sends each on again under the standard name the player reads, so the
35
+ * control bar plays, pauses and seeks on the remote's media keys. The
36
+ * application registers those keys with the platform itself, such as
37
+ * `tizen.tvinputdevice.registerKey`: the player never does.
38
+ */
39
+ import { Component } from './component.js';
40
+ import type { PlayerHost } from './host.js';
41
+ export declare class MbxSpatialNav extends Component {
42
+ /** The control focused last inside the player. */
43
+ private last;
44
+ /** The last move: from where, to where, which way. The opposite arrow goes back. */
45
+ private move;
46
+ /** The control the pointer was over last. */
47
+ private pointed;
48
+ /** The pointer moved: the next key places focus, from where the pointer was. */
49
+ private resting;
50
+ /** The key this element sends on, which its own listener lets pass. */
51
+ private sending;
52
+ constructor();
53
+ protected attach(player: PlayerHost): void;
54
+ /** The control focused inside the player, through shadow roots, or null. */
55
+ private focused;
56
+ /** Whether `element` sits inside something that carries `open`, below the player. */
57
+ private inOpen;
58
+ /** Where the first key puts focus: the pointer's control, the last one, or `initial`. */
59
+ private start;
60
+ private key;
61
+ /**
62
+ * Back: Escape for the focused control first. Nothing took it and focus
63
+ * is on a control: focus leaves, and the bar fades. Otherwise it is the
64
+ * application's.
65
+ */
66
+ private back;
67
+ /** Sends `event` on again under the standard `name`, in place of the original. */
68
+ private again;
69
+ /** Dispatches `event` on `target` past this element's own listener; false when a handler took it. */
70
+ private send;
71
+ }
72
+ //# sourceMappingURL=spatial-nav.d.ts.map
@@ -0,0 +1 @@
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;IAUf,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"}
@@ -0,0 +1,203 @@
1
+ /**
2
+ * <mbx-spatial-nav>: the remote control for the player on a TV. Placed
3
+ * inside the player, it renders nothing, and it moves focus between the
4
+ * controls with the arrows of a five-way pad.
5
+ *
6
+ * It listens on the player in the capture phase, so it decides before the
7
+ * focused control and the control bar, and takes a key by preventing it,
8
+ * which they then leave alone. It is the one capture-phase listener in the
9
+ * player.
10
+ *
11
+ * - An arrow moves focus to the nearest control on that side (`navigate.ts`).
12
+ * The opposite arrow returns to where the last move came from, so down
13
+ * then up lands where it started.
14
+ * - On a slider, left and right stay the slider's: they seek or set the
15
+ * volume. Up and down move focus.
16
+ * - Inside an element that carries `open`, a menu, every arrow and Enter
17
+ * stay the menu's.
18
+ * - While the player carries `idle`, or after the pointer moved, the first
19
+ * arrow or Enter only places focus, on the control focused last, the one
20
+ * the pointer was over, or the one `initial` names, `mbx-play-button` by
21
+ * default. A stray arrow never seeks.
22
+ * - An arrow with no control on its side fires `navigateout` on this
23
+ * element, bubbling and composed, with the direction as `detail`, so the
24
+ * application's own navigation takes focus out of the player. The arrow
25
+ * is taken all the same: left alone, the control bar would seek on it.
26
+ * - Enter clicks a focused control that is not a native button, which
27
+ * clicks itself.
28
+ * - Back is Escape for the focused control first: a menu goes back a page
29
+ * or closes, the seek bar drops its target. Nothing took it: focus leaves
30
+ * the controls, and the bar fades as it does after any pause. Focus
31
+ * already out: Back is the application's, to leave the player.
32
+ *
33
+ * TV sets report some remote keys by code alone (`keys.ts`). The element
34
+ * sends each on again under the standard name the player reads, so the
35
+ * control bar plays, pauses and seeks on the remote's media keys. The
36
+ * application registers those keys with the platform itself, such as
37
+ * `tizen.tvinputdevice.registerKey`: the player never does.
38
+ */
39
+ import { Component } from './component.js';
40
+ import { above } from './host.js';
41
+ import { isBack, keyOf } from './keys.js';
42
+ import { candidates, DIRECTIONS, nearest, OPPOSITE } from './navigate.js';
43
+ const INITIAL = 'mbx-play-button';
44
+ export class MbxSpatialNav extends Component {
45
+ constructor() {
46
+ super();
47
+ this.last = null;
48
+ this.move = null;
49
+ this.pointed = null;
50
+ this.resting = false;
51
+ this.sending = null;
52
+ }
53
+ attach(player) {
54
+ this.on(player, 'keydown', (event) => this.key(player, event), {
55
+ capture: true,
56
+ });
57
+ this.on(player, 'pointermove', (event) => {
58
+ this.resting = true;
59
+ const list = candidates(player);
60
+ const over = event.composedPath().find((node) => list.includes(node));
61
+ if (over !== undefined)
62
+ this.pointed = over;
63
+ });
64
+ this.on(player, 'focusin', () => {
65
+ const current = this.focused(player);
66
+ if (current !== null)
67
+ this.last = current;
68
+ });
69
+ }
70
+ /** The control focused inside the player, through shadow roots, or null. */
71
+ focused(player) {
72
+ let active = document.activeElement;
73
+ while (active?.shadowRoot?.activeElement != null)
74
+ active = active.shadowRoot.activeElement;
75
+ if (!(active instanceof HTMLElement) || active === player)
76
+ return null;
77
+ for (let node = active; node !== null; node = above(node)) {
78
+ if (node === player)
79
+ return active;
80
+ }
81
+ return null;
82
+ }
83
+ /** Whether `element` sits inside something that carries `open`, below the player. */
84
+ inOpen(player, element) {
85
+ for (let node = element; node !== null && node !== player; node = above(node)) {
86
+ if (node instanceof HTMLElement && node.hasAttribute('open'))
87
+ return true;
88
+ }
89
+ return false;
90
+ }
91
+ /** Where the first key puts focus: the pointer's control, the last one, or `initial`. */
92
+ start(player) {
93
+ const list = candidates(player);
94
+ for (const known of [this.pointed, this.last]) {
95
+ if (known !== null && list.includes(known))
96
+ return known;
97
+ }
98
+ const named = player.querySelector(this.getAttribute('initial') ?? INITIAL);
99
+ return (list.find((element) => named?.contains(element) || named?.shadowRoot?.contains(element)) ??
100
+ list[0] ??
101
+ null);
102
+ }
103
+ key(player, event) {
104
+ if (event === this.sending)
105
+ return;
106
+ if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey)
107
+ return;
108
+ const name = keyOf(event);
109
+ const direction = DIRECTIONS[name];
110
+ if (direction === undefined && name !== 'Enter') {
111
+ if (isBack(name))
112
+ this.back(player, event);
113
+ else if (name !== event.key)
114
+ this.again(event, name);
115
+ return;
116
+ }
117
+ const current = this.focused(player);
118
+ // The first key while the bar sleeps, or after the pointer, only places
119
+ // focus, so a stray arrow does not seek.
120
+ if (current === null || this.resting || player.hasAttribute('idle')) {
121
+ this.resting = false;
122
+ const start = this.start(player);
123
+ if (start === null)
124
+ return;
125
+ event.preventDefault();
126
+ start.focus();
127
+ return;
128
+ }
129
+ if (this.inOpen(player, current))
130
+ return;
131
+ if (direction === undefined) {
132
+ // Enter. A native button clicks itself on a real Enter; a code a TV
133
+ // set sent without a name is not one, so the click comes from here.
134
+ const native = current instanceof HTMLButtonElement && name === event.key;
135
+ if (native || current.getAttribute('role') === 'slider')
136
+ return;
137
+ event.preventDefault();
138
+ current.click();
139
+ return;
140
+ }
141
+ const slider = current.getAttribute('role') === 'slider';
142
+ if (slider && (direction === 'left' || direction === 'right')) {
143
+ if (name !== event.key)
144
+ this.again(event, name);
145
+ return;
146
+ }
147
+ const list = candidates(player).filter((element) => element !== current);
148
+ const back = this.move;
149
+ const next = back !== null &&
150
+ back.to === current &&
151
+ back.direction === OPPOSITE[direction] &&
152
+ list.includes(back.from)
153
+ ? back.from
154
+ : nearest(current.getBoundingClientRect(), list, direction);
155
+ event.preventDefault();
156
+ if (next === null) {
157
+ this.dispatchEvent(new CustomEvent('navigateout', { detail: direction, bubbles: true, composed: true }));
158
+ return;
159
+ }
160
+ this.move = { from: current, to: next, direction };
161
+ next.focus();
162
+ }
163
+ /**
164
+ * Back: Escape for the focused control first. Nothing took it and focus
165
+ * is on a control: focus leaves, and the bar fades. Otherwise it is the
166
+ * application's.
167
+ */
168
+ back(player, event) {
169
+ const current = this.focused(player);
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
+ }
203
+ //# sourceMappingURL=spatial-nav.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spatial-nav.js","sourceRoot":"","sources":["../src/spatial-nav.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,EAAE,SAAS,EAAE,MAA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AC,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"}
package/package.json CHANGED
@@ -1,12 +1,58 @@
1
1
  {
2
2
  "name": "@mattebox/player-spatial-nav",
3
- "version": "0.0.0",
4
- "description": "Placeholder to set up trusted publishing. Use 0.1.0 or later.",
5
- "license": "MIT",
3
+ "version": "0.1.1",
4
+ "description": "<mbx-spatial-nav>: remote control navigation for <mattebox-player> on Tizen, webOS and other TV browsers.",
5
+ "author": "Josep Boix Requesens",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/jboix/mattebox-player.git",
9
9
  "directory": "packages/spatial-nav"
10
10
  },
11
- "files": []
11
+ "homepage": "https://github.com/jboix/mattebox-player#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/jboix/mattebox-player/issues"
14
+ },
15
+ "keywords": [
16
+ "mattebox",
17
+ "player",
18
+ "custom-element",
19
+ "tv",
20
+ "spatial-navigation",
21
+ "tizen",
22
+ "webos"
23
+ ],
24
+ "license": "MIT",
25
+ "type": "module",
26
+ "sideEffects": true,
27
+ "engines": {
28
+ "node": ">=20.19"
29
+ },
30
+ "main": "./dist/es2015/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "modern": "./dist/index.js",
36
+ "default": "./dist/es2015/index.js"
37
+ },
38
+ "./package.json": "./package.json"
39
+ },
40
+ "files": [
41
+ "dist"
42
+ ],
43
+ "scripts": {
44
+ "clean": "rm -rf dist",
45
+ "build": "npm run clean && npm run build:modern && npm run build:es2015 && npm run build:cdn",
46
+ "build:modern": "tsc -p tsconfig.build.json",
47
+ "build:es2015": "rolldown -c rolldown.dist.config.mjs",
48
+ "build:cdn": "rolldown -c rolldown.cdn.config.mjs",
49
+ "check:package": "publint && attw --pack . --format table --no-emoji",
50
+ "release:ci": "semantic-release"
51
+ },
52
+ "peerDependencies": {
53
+ "@mattebox/player": ">=0.9.0"
54
+ },
55
+ "devDependencies": {
56
+ "@mattebox/player-core": "^0.6.0"
57
+ }
12
58
  }