@mattebox/player 0.5.0 → 0.7.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.
Files changed (264) hide show
  1. package/README.md +9 -6
  2. package/dist/cdn/mattebox-player.min.js +3 -444
  3. package/dist/controls/airplay.js +28 -44
  4. package/dist/controls/airplay.js.map +1 -1
  5. package/dist/controls/chapters.d.ts +22 -11
  6. package/dist/controls/chapters.d.ts.map +1 -1
  7. package/dist/controls/chapters.js +87 -73
  8. package/dist/controls/chapters.js.map +1 -1
  9. package/dist/controls/cues.d.ts +0 -22
  10. package/dist/controls/cues.d.ts.map +1 -1
  11. package/dist/controls/cues.js +161 -191
  12. package/dist/controls/cues.js.map +1 -1
  13. package/dist/controls/fullscreen.js +52 -70
  14. package/dist/controls/fullscreen.js.map +1 -1
  15. package/dist/controls/icons.d.ts +1 -1
  16. package/dist/controls/icons.d.ts.map +1 -1
  17. package/dist/controls/icons.js +46 -70
  18. package/dist/controls/icons.js.map +1 -1
  19. package/dist/controls/menu.d.ts +7 -4
  20. package/dist/controls/menu.d.ts.map +1 -1
  21. package/dist/controls/menu.js +241 -223
  22. package/dist/controls/menu.js.map +1 -1
  23. package/dist/controls/pip.js +35 -49
  24. package/dist/controls/pip.js.map +1 -1
  25. package/dist/controls/ranges.js +42 -37
  26. package/dist/controls/ranges.js.map +1 -1
  27. package/dist/controls/session.d.ts +6 -1
  28. package/dist/controls/session.d.ts.map +1 -1
  29. package/dist/controls/session.js +41 -26
  30. package/dist/controls/session.js.map +1 -1
  31. package/dist/controls/slider.d.ts +2 -2
  32. package/dist/controls/slider.d.ts.map +1 -1
  33. package/dist/controls/slider.js +151 -159
  34. package/dist/controls/slider.js.map +1 -1
  35. package/dist/controls/time.js +17 -17
  36. package/dist/controls/time.js.map +1 -1
  37. package/dist/dom.d.ts +6 -0
  38. package/dist/dom.d.ts.map +1 -1
  39. package/dist/dom.js +28 -19
  40. package/dist/dom.js.map +1 -1
  41. package/dist/element-entry.js +9 -6
  42. package/dist/element-entry.js.map +1 -1
  43. package/dist/element.d.ts +15 -24
  44. package/dist/element.d.ts.map +1 -1
  45. package/dist/element.js +385 -454
  46. package/dist/element.js.map +1 -1
  47. package/dist/elements/airplay-button.js +51 -53
  48. package/dist/elements/airplay-button.js.map +1 -1
  49. package/dist/elements/audio-menu.js +62 -63
  50. package/dist/elements/audio-menu.js.map +1 -1
  51. package/dist/elements/chapters-menu.d.ts.map +1 -1
  52. package/dist/elements/chapters-menu.js +75 -65
  53. package/dist/elements/chapters-menu.js.map +1 -1
  54. package/dist/elements/component.d.ts +1 -1
  55. package/dist/elements/component.d.ts.map +1 -1
  56. package/dist/elements/component.js +86 -89
  57. package/dist/elements/component.js.map +1 -1
  58. package/dist/elements/control-bar.d.ts +18 -5
  59. package/dist/elements/control-bar.d.ts.map +1 -1
  60. package/dist/elements/control-bar.js +282 -319
  61. package/dist/elements/control-bar.js.map +1 -1
  62. package/dist/elements/current-time.js +26 -25
  63. package/dist/elements/current-time.js.map +1 -1
  64. package/dist/elements/drm-badge.d.ts.map +1 -1
  65. package/dist/elements/drm-badge.js +107 -141
  66. package/dist/elements/drm-badge.js.map +1 -1
  67. package/dist/elements/duration.d.ts.map +1 -1
  68. package/dist/elements/duration.js +24 -21
  69. package/dist/elements/duration.js.map +1 -1
  70. package/dist/elements/error-screen.d.ts.map +1 -1
  71. package/dist/elements/error-screen.js +68 -102
  72. package/dist/elements/error-screen.js.map +1 -1
  73. package/dist/elements/fullscreen-button.js +48 -54
  74. package/dist/elements/fullscreen-button.js.map +1 -1
  75. package/dist/elements/live-button.d.ts.map +1 -1
  76. package/dist/elements/live-button.js +67 -94
  77. package/dist/elements/live-button.js.map +1 -1
  78. package/dist/elements/menu-element.js +31 -28
  79. package/dist/elements/menu-element.js.map +1 -1
  80. package/dist/elements/mute-button.js +58 -59
  81. package/dist/elements/mute-button.js.map +1 -1
  82. package/dist/elements/pip-button.js +54 -54
  83. package/dist/elements/pip-button.js.map +1 -1
  84. package/dist/elements/play-button.d.ts.map +1 -1
  85. package/dist/elements/play-button.js +73 -58
  86. package/dist/elements/play-button.js.map +1 -1
  87. package/dist/elements/quality-menu.js +66 -69
  88. package/dist/elements/quality-menu.js.map +1 -1
  89. package/dist/elements/remaining-time.d.ts.map +1 -1
  90. package/dist/elements/remaining-time.js +27 -24
  91. package/dist/elements/remaining-time.js.map +1 -1
  92. package/dist/elements/scan-button.d.ts +17 -0
  93. package/dist/elements/scan-button.d.ts.map +1 -0
  94. package/dist/elements/scan-button.js +117 -0
  95. package/dist/elements/scan-button.js.map +1 -0
  96. package/dist/elements/seek-bar.d.ts +93 -14
  97. package/dist/elements/seek-bar.d.ts.map +1 -1
  98. package/dist/elements/seek-bar.js +475 -335
  99. package/dist/elements/seek-bar.js.map +1 -1
  100. package/dist/elements/shared.d.ts +3 -11
  101. package/dist/elements/shared.d.ts.map +1 -1
  102. package/dist/elements/shared.js +44 -144
  103. package/dist/elements/shared.js.map +1 -1
  104. package/dist/elements/skip-button.d.ts.map +1 -1
  105. package/dist/elements/skip-button.js +54 -55
  106. package/dist/elements/skip-button.js.map +1 -1
  107. package/dist/elements/spacer.d.ts.map +1 -1
  108. package/dist/elements/spacer.js +12 -7
  109. package/dist/elements/spacer.js.map +1 -1
  110. package/dist/elements/speed-menu.js +60 -55
  111. package/dist/elements/speed-menu.js.map +1 -1
  112. package/dist/elements/spinner.d.ts.map +1 -1
  113. package/dist/elements/spinner.js +52 -78
  114. package/dist/elements/spinner.js.map +1 -1
  115. package/dist/elements/start-button.d.ts.map +1 -1
  116. package/dist/elements/start-button.js +107 -120
  117. package/dist/elements/start-button.js.map +1 -1
  118. package/dist/elements/subtitles-menu.js +95 -103
  119. package/dist/elements/subtitles-menu.js.map +1 -1
  120. package/dist/elements/time-element.d.ts.map +1 -1
  121. package/dist/elements/time-element.js +36 -28
  122. package/dist/elements/time-element.js.map +1 -1
  123. package/dist/elements/title.d.ts.map +1 -1
  124. package/dist/elements/title.js +105 -137
  125. package/dist/elements/title.js.map +1 -1
  126. package/dist/elements/volume-slider.d.ts.map +1 -1
  127. package/dist/elements/volume-slider.js +47 -45
  128. package/dist/elements/volume-slider.js.map +1 -1
  129. package/dist/elements/volume.d.ts.map +1 -1
  130. package/dist/elements/volume.js +42 -50
  131. package/dist/elements/volume.js.map +1 -1
  132. package/dist/entries/airplay-button.js +7 -7
  133. package/dist/entries/airplay-button.js.map +1 -1
  134. package/dist/entries/audio-menu.js +7 -6
  135. package/dist/entries/audio-menu.js.map +1 -1
  136. package/dist/entries/chapters-menu.js +7 -7
  137. package/dist/entries/chapters-menu.js.map +1 -1
  138. package/dist/entries/control-bar.js +7 -6
  139. package/dist/entries/control-bar.js.map +1 -1
  140. package/dist/entries/current-time.js +7 -6
  141. package/dist/entries/current-time.js.map +1 -1
  142. package/dist/entries/drm-badge.js +7 -6
  143. package/dist/entries/drm-badge.js.map +1 -1
  144. package/dist/entries/duration.js +7 -6
  145. package/dist/entries/duration.js.map +1 -1
  146. package/dist/entries/error-screen.js +7 -6
  147. package/dist/entries/error-screen.js.map +1 -1
  148. package/dist/entries/fullscreen-button.js +7 -6
  149. package/dist/entries/fullscreen-button.js.map +1 -1
  150. package/dist/entries/live-button.js +7 -6
  151. package/dist/entries/live-button.js.map +1 -1
  152. package/dist/entries/mute-button.js +7 -6
  153. package/dist/entries/mute-button.js.map +1 -1
  154. package/dist/entries/pip-button.js +7 -6
  155. package/dist/entries/pip-button.js.map +1 -1
  156. package/dist/entries/play-button.js +7 -6
  157. package/dist/entries/play-button.js.map +1 -1
  158. package/dist/entries/quality-menu.js +7 -6
  159. package/dist/entries/quality-menu.js.map +1 -1
  160. package/dist/entries/remaining-time.js +7 -7
  161. package/dist/entries/remaining-time.js.map +1 -1
  162. package/dist/entries/scan-button.d.ts +5 -0
  163. package/dist/entries/scan-button.d.ts.map +1 -0
  164. package/dist/entries/scan-button.js +9 -0
  165. package/dist/entries/scan-button.js.map +1 -0
  166. package/dist/entries/seek-bar.js +7 -6
  167. package/dist/entries/seek-bar.js.map +1 -1
  168. package/dist/entries/skip-button.js +7 -6
  169. package/dist/entries/skip-button.js.map +1 -1
  170. package/dist/entries/spacer.js +7 -6
  171. package/dist/entries/spacer.js.map +1 -1
  172. package/dist/entries/speed-menu.js +7 -6
  173. package/dist/entries/speed-menu.js.map +1 -1
  174. package/dist/entries/spinner.js +7 -7
  175. package/dist/entries/spinner.js.map +1 -1
  176. package/dist/entries/start-button.js +7 -6
  177. package/dist/entries/start-button.js.map +1 -1
  178. package/dist/entries/subtitles-menu.js +7 -6
  179. package/dist/entries/subtitles-menu.js.map +1 -1
  180. package/dist/entries/title.js +7 -7
  181. package/dist/entries/title.js.map +1 -1
  182. package/dist/entries/volume-slider.js +7 -6
  183. package/dist/entries/volume-slider.js.map +1 -1
  184. package/dist/entries/volume.js +9 -8
  185. package/dist/entries/volume.js.map +1 -1
  186. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/asyncToGenerator.js +1 -1
  187. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/defineProperty.js +1 -1
  188. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/objectSpread2.js +1 -1
  189. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/toPrimitive.js +1 -1
  190. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/toPropertyKey.js +1 -1
  191. package/dist/es2015/_virtual/{_@oxc-project_runtime@0.149.0 → _@oxc-project_runtime@0.150.0}/helpers/esm/typeof.js +1 -1
  192. package/dist/es2015/controls/chapters.js +40 -7
  193. package/dist/es2015/controls/cues.js +1 -8
  194. package/dist/es2015/controls/icons.js +3 -1
  195. package/dist/es2015/controls/menu.js +28 -9
  196. package/dist/es2015/controls/session.js +16 -4
  197. package/dist/es2015/controls/slider.js +5 -4
  198. package/dist/es2015/dom.js +10 -1
  199. package/dist/es2015/element.js +49 -40
  200. package/dist/es2015/elements/chapters-menu.js +18 -10
  201. package/dist/es2015/elements/control-bar.js +41 -35
  202. package/dist/es2015/elements/drm-badge.js +1 -25
  203. package/dist/es2015/elements/duration.js +2 -1
  204. package/dist/es2015/elements/error-screen.js +1 -30
  205. package/dist/es2015/elements/live-button.js +1 -29
  206. package/dist/es2015/elements/menu-element.js +2 -2
  207. package/dist/es2015/elements/play-button.js +10 -3
  208. package/dist/es2015/elements/remaining-time.js +2 -1
  209. package/dist/es2015/elements/scan-button.js +118 -0
  210. package/dist/es2015/elements/seek-bar.js +264 -71
  211. package/dist/es2015/elements/shared.js +5 -106
  212. package/dist/es2015/elements/skip-button.js +2 -1
  213. package/dist/es2015/elements/spacer.js +2 -1
  214. package/dist/es2015/elements/spinner.js +1 -32
  215. package/dist/es2015/elements/start-button.js +8 -26
  216. package/dist/es2015/elements/time-element.js +1 -4
  217. package/dist/es2015/elements/title.js +1 -45
  218. package/dist/es2015/elements/volume-slider.js +2 -1
  219. package/dist/es2015/elements/volume.js +1 -11
  220. package/dist/es2015/entries/scan-button.js +7 -0
  221. package/dist/es2015/index.js +3 -3
  222. package/dist/es2015/presets.js +1 -1
  223. package/dist/es2015/style.js +1 -98
  224. package/dist/es2015/tags.js +2 -2
  225. package/dist/host.js +19 -17
  226. package/dist/host.js.map +1 -1
  227. package/dist/index.d.ts +3 -3
  228. package/dist/index.d.ts.map +1 -1
  229. package/dist/index.js +55 -28
  230. package/dist/labels.js +13 -9
  231. package/dist/labels.js.map +1 -1
  232. package/dist/namespaces.d.ts +9 -26
  233. package/dist/namespaces.d.ts.map +1 -1
  234. package/dist/namespaces.js +6 -2
  235. package/dist/namespaces.js.map +1 -1
  236. package/dist/presets.js +33 -28
  237. package/dist/presets.js.map +1 -1
  238. package/dist/style.d.ts +1 -20
  239. package/dist/style.d.ts.map +1 -1
  240. package/dist/style.js +4 -97
  241. package/dist/style.js.map +1 -1
  242. package/dist/surface.js +25 -21
  243. package/dist/surface.js.map +1 -1
  244. package/dist/tags.d.ts +1 -1
  245. package/dist/tags.d.ts.map +1 -1
  246. package/dist/tags.js +31 -27
  247. package/dist/tags.js.map +1 -1
  248. package/package.json +4 -4
  249. package/dist/compose.d.ts +0 -5
  250. package/dist/compose.d.ts.map +0 -1
  251. package/dist/compose.js +0 -31
  252. package/dist/compose.js.map +0 -1
  253. package/dist/elements/panels.d.ts +0 -15
  254. package/dist/elements/panels.d.ts.map +0 -1
  255. package/dist/elements/panels.js +0 -55
  256. package/dist/elements/panels.js.map +0 -1
  257. package/dist/entries/panels.d.ts +0 -5
  258. package/dist/entries/panels.d.ts.map +0 -1
  259. package/dist/entries/panels.js +0 -8
  260. package/dist/entries/panels.js.map +0 -1
  261. package/dist/es2015/compose.js +0 -33
  262. package/dist/es2015/elements/panels.js +0 -52
  263. package/dist/es2015/entries/panels.js +0 -7
  264. package/dist/index.js.map +0 -1
package/dist/element.js CHANGED
@@ -1,466 +1,397 @@
1
- /**
2
- * <mattebox-player>: a real <video> as a light-DOM child, so the integrator
3
- * can reach it, and the error surface in shadow DOM. The element never
4
- * forwards or wraps a media element member: play, pause, seek, volume and
5
- * the rest live on the video, where the browser, Picture-in-Picture and the
6
- * OS media session already find them.
7
- *
8
- * The controls are elements the page places inside, beside the video, and
9
- * the stage slots them: over the picture for the bar and the screens, in
10
- * flow under it for the panels row. A page that places none gets the
11
- * default composition for its mode appended to its light DOM. The element
12
- * reflects the video's state as attributes on itself, `paused`, `playing`,
13
- * `ended`, `muted`, `audio`, `started` and `waiting`, for the page's
14
- * stylesheet and the controls alike.
15
- *
16
- * No class fields, `#private` or otherwise: the ES2015 build would lower
17
- * them into runtime helpers, and the emit check bans helpers. Statics are
18
- * getters and instance state is declared, then assigned in the constructor.
19
- */
20
- import { createPlayer, inferType, matteboxHandler, nativeHandler } from '@mattebox/player-core';
21
- import { composeBar, composePanels } from './compose.js';
22
- import { namespaces } from './namespaces.js';
23
- import { drmGuard, resolvePreset } from './presets.js';
24
- import { STYLE } from './style.js';
25
- import { errorSurface } from './surface.js';
26
- import { CONTROL_BAR, PANELS, PLAYER } from './tags.js';
1
+ import { namespaces } from "./namespaces.js";
2
+ import { drmGuard, resolvePreset } from "./presets.js";
3
+ import { STYLE } from "./style.js";
4
+ import { errorSurface } from "./surface.js";
5
+ import { PLAYER } from "./tags.js";
6
+ import { createPlayer, inferType, matteboxHandler, nativeHandler } from "@mattebox/player-core";
7
+ //#region src/element.ts
27
8
  /** Attributes forwarded onto the video as attributes, never as properties. */
28
- const FORWARDED = ['autoplay', 'muted', 'poster', 'crossorigin'];
9
+ const FORWARDED = [
10
+ "autoplay",
11
+ "muted",
12
+ "poster",
13
+ "crossorigin"
14
+ ];
29
15
  /** Everything else the element watches. Changing any of them reloads. */
30
- const OWN = ['src', 'type', 'preset', 'license-url', 'thumbnails'];
16
+ const OWN = [
17
+ "src",
18
+ "type",
19
+ "preset",
20
+ "license-url",
21
+ "thumbnails"
22
+ ];
23
+ /**
24
+ * The chapters file URL. A session with the engine's `chapters` namespace
25
+ * loads it there: through the engine's transport, so request hooks apply,
26
+ * and in every format the engine reads. Any other session gets a
27
+ * `<track kind="chapters">` on the video, hidden so its cues load and
28
+ * nothing is drawn. The seek bar and the chapters menu read either.
29
+ * Changing it never reloads the source.
30
+ */
31
+ const CHAPTERS = "chapters";
31
32
  /**
32
- * The chapters track URL. A `<track kind="chapters">` on the video, hidden
33
- * so its cues load and nothing is drawn: chapters are the browser's own
34
- * text track, which the seek bar and the chapters menu read, and which a
35
- * page can also put there itself. Changing it swaps the track and never
36
- * reloads the source.
37
- */
38
- const CHAPTERS = 'chapters';
33
+ * The one attribute whose meaning the element owns: `native` keeps the
34
+ * browser's controls on the video, `custom` swaps them for the controls the
35
+ * page placed inside, `none` takes the browser's off and hides every child
36
+ * but the video. Anything else is `native`. Changing it never touches the
37
+ * session.
38
+ */
39
+ const CONTROLS = "controls";
39
40
  /**
40
- * The one attribute whose meaning the element owns: `native` keeps the
41
- * browser's controls on the video, `custom` swaps them for the bar, `none`
42
- * leaves the video bare for a page that draws its own. Anything else is
43
- * `native`. Changing it swaps the bar without touching the session.
44
- */
45
- const CONTROLS = 'controls';
41
+ * Inline playback, on until the page writes `playsinline="false"`, the way
42
+ * `spellcheck` and `draggable` are on until told otherwise. An iPhone takes
43
+ * a video without `playsinline` to its own fullscreen on play, where no
44
+ * control the page placed shows. Changing it never reloads.
45
+ */
46
+ const PLAYSINLINE = "playsinline";
46
47
  /** The video's state, as attributes on the element, read on the events the video fires for it. */
47
48
  const STATE_EVENTS = [
48
- 'play',
49
- 'pause',
50
- 'ended',
51
- 'emptied',
52
- 'volumechange',
53
- 'loadedmetadata',
54
- 'resize',
55
- 'playing',
56
- 'waiting',
57
- 'stalled',
58
- 'canplay',
49
+ "play",
50
+ "pause",
51
+ "ended",
52
+ "emptied",
53
+ "volumechange",
54
+ "loadedmetadata",
55
+ "resize",
56
+ "playing",
57
+ "waiting",
58
+ "stalled",
59
+ "canplay"
59
60
  ];
60
61
  /** Every stage the engine ships. A narrower preset is optimization. */
61
- const DEFAULT_PRESET = 'full';
62
+ const DEFAULT_PRESET = "full";
62
63
  /**
63
- * What `define()` recorded, for the elements the parser creates. It is the
64
- * one value shared across elements, because a page that writes only markup
65
- * still has to be able to say what the engine carries.
66
- */
64
+ * What `define()` recorded, for the elements the parser creates. It is the
65
+ * one value shared across elements, because a page that writes only markup
66
+ * still has to be able to say what the engine carries.
67
+ */
67
68
  let defaults = {};
68
69
  /**
69
- * The interface above merges only `addEventListener` and `removeEventListener`
70
- * overloads, which the class inherits from HTMLElement. The rule it suppresses
71
- * guards against properties an interface declares and a constructor never
72
- * assigns, and there are none.
73
- */
74
- // biome-ignore lint/suspicious/noUnsafeDeclarationMerging: method overloads only, no properties
75
- export class MatteboxPlayerElement extends HTMLElement {
76
- static get observedAttributes() {
77
- return [...FORWARDED, ...OWN, CONTROLS, CHAPTERS];
78
- }
79
- /**
80
- * Registers the element once, and records the stages markup-only pages
81
- * get. The controls register through their own entries, the package's
82
- * root entry importing every one; a control connected before its player
83
- * is defined waits for the definition.
84
- */
85
- static define(options) {
86
- if (options !== undefined)
87
- defaults = options;
88
- if (customElements.get(PLAYER) === undefined) {
89
- customElements.define(PLAYER, MatteboxPlayerElement);
90
- }
91
- }
92
- constructor(options) {
93
- super();
94
- this.options = options ?? defaults;
95
- this.composed = [];
96
- this.settled = false;
97
- this.focusable = false;
98
- this.track = null;
99
- this.core = null;
100
- this.current = null;
101
- this.failure = null;
102
- this.reflecting = false;
103
- this.started = false;
104
- this.waiting = false;
105
- this.offs = [];
106
- this.queue = Promise.resolve();
107
- this.pending = false;
108
- this.media = document.createElement('video');
109
- // Native until the `controls` attribute says otherwise, which arrives
110
- // through attributeChangedCallback: a constructor must not read attributes.
111
- this.media.controls = true;
112
- for (const name of STATE_EVENTS) {
113
- this.media.addEventListener(name, () => {
114
- this.note(name);
115
- this.reflect();
116
- });
117
- }
118
- // An error over a picture that then plays is wrong by definition.
119
- this.media.addEventListener('playing', () => {
120
- this.failure = null;
121
- });
122
- this.stage = document.createElement('div');
123
- this.stage.setAttribute('part', 'stage');
124
- this.stage.append(document.createElement('slot'));
125
- this.surface = errorSurface(() => {
126
- this.reload();
127
- });
128
- const root = this.attachShadow({ mode: 'open' });
129
- const style = document.createElement('style');
130
- style.textContent = STYLE;
131
- root.append(style, this.stage, this.surface.root);
132
- }
133
- /** The media element. Everything the browser already does is here. */
134
- get video() {
135
- return this.media;
136
- }
137
- /** The core, or null until the first source has been set up: resolving a preset is asynchronous. */
138
- get player() {
139
- return this.core;
140
- }
141
- /** The engine feeding the video, or null for a native session and before the first load. */
142
- get engine() {
143
- return this.current?.engine ?? null;
144
- }
145
- /**
146
- * The fatal error of the current load, or null once a load starts or
147
- * playback resumes. The `error` event says when; this says what, for a
148
- * control that attaches after the event, such as the default composition
149
- * behind a source that fails while the page is still parsing.
150
- */
151
- get error() {
152
- return this.failure;
153
- }
154
- connectedCallback() {
155
- if (this.media.parentNode !== this)
156
- this.append(this.media);
157
- for (const name of FORWARDED)
158
- this.forward(name);
159
- this.reflect();
160
- if (!this.settled)
161
- this.settle();
162
- this.reload();
163
- }
164
- /** Decides on the default composition once the children are all in, then on every mode change. */
165
- settle() {
166
- const done = () => {
167
- this.settled = true;
168
- this.mode();
169
- };
170
- if (document.readyState === 'loading') {
171
- document.addEventListener('DOMContentLoaded', done, { once: true });
172
- }
173
- else {
174
- queueMicrotask(done);
175
- }
176
- }
177
- /** The two states the video does not hold: whether it has played once, and whether it waits for data. */
178
- note(event) {
179
- if (event === 'playing') {
180
- this.started = true;
181
- this.waiting = false;
182
- }
183
- else if (event === 'waiting' || event === 'stalled') {
184
- this.waiting = true;
185
- }
186
- else if (event === 'canplay') {
187
- this.waiting = false;
188
- }
189
- else if (event === 'emptied') {
190
- // A new source starts over: the title shows again before its first play.
191
- this.started = false;
192
- this.waiting = false;
193
- }
194
- }
195
- /** The video's state on the element, for `mattebox-player[paused]` and the like. */
196
- reflect() {
197
- const video = this.media;
198
- this.reflecting = true;
199
- this.toggleAttribute('paused', video.paused);
200
- this.toggleAttribute('playing', !video.paused);
201
- this.toggleAttribute('ended', video.ended);
202
- // `muted` is also forwarded: the page's attribute sets the video, and
203
- // the video's state sets the attribute. Reflection never forwards, so
204
- // a read can only ever change the attribute, never the video.
205
- this.toggleAttribute('muted', video.muted);
206
- this.toggleAttribute('audio', this.audio());
207
- this.toggleAttribute('started', this.started);
208
- this.toggleAttribute('waiting', this.waiting);
209
- this.reflecting = false;
210
- }
211
- /**
212
- * Whether the source has no picture. Once the metadata is in, the video's
213
- * size decides: zero by zero is sound alone, and `resize` catches a stream
214
- * that moves to or from an audio-only rendition. Before that, the type the
215
- * page wrote or the URL implies is the hint, so an mp3 is `audio` from
216
- * the start rather than from its metadata.
217
- */
218
- audio() {
219
- const video = this.media;
220
- if (video.readyState >= HTMLMediaElement.HAVE_METADATA) {
221
- return video.videoWidth === 0 && video.videoHeight === 0;
222
- }
223
- const src = this.getAttribute('src');
224
- const type = this.getAttribute('type') ?? (src === null ? undefined : inferType(src));
225
- return type?.startsWith('audio/') === true;
226
- }
227
- disconnectedCallback() {
228
- // A single-page app must not leave a pipeline attached to a video it
229
- // navigated away from. Reconnecting loads the `src` again.
230
- this.enqueue(() => this.teardown());
231
- }
232
- attributeChangedCallback(name) {
233
- if (FORWARDED.includes(name)) {
234
- if (!this.reflecting)
235
- this.forward(name);
236
- return;
237
- }
238
- if (name === CONTROLS) {
239
- this.mode();
240
- return;
241
- }
242
- if (name === CHAPTERS) {
243
- this.chapters();
244
- return;
245
- }
246
- // The preset decides the chain, so it is the one attribute that rebuilds it.
247
- if (name === 'preset')
248
- this.enqueue(() => this.discard());
249
- // The source's type is the hint for `audio` until its metadata arrives.
250
- // Only `audio`: a full reflection here would drop a `muted` the page
251
- // wrote before its own callback forwards it.
252
- if (name === 'src' || name === 'type')
253
- this.toggleAttribute('audio', this.audio());
254
- this.reload();
255
- }
256
- /**
257
- * Applies the `controls` attribute: the video's own controls, the
258
- * tabindex, and the default composition for the mode where the page
259
- * wrote none. The controls themselves are elements and attach on their own.
260
- */
261
- mode() {
262
- const value = this.getAttribute(CONTROLS);
263
- const custom = value === 'custom';
264
- this.media.controls = !custom && value !== 'none';
265
- // Under custom controls the error screen element carries a fatal error.
266
- if (custom)
267
- this.surface.clear();
268
- if (!custom) {
269
- if (this.focusable)
270
- this.removeAttribute('tabindex');
271
- this.focusable = false;
272
- }
273
- else if (!this.hasAttribute('tabindex')) {
274
- // A bare video is not focusable, so without this a click on it would
275
- // leave the shortcuts nowhere to listen. A page that set its own
276
- // tabindex keeps it.
277
- this.tabIndex = 0;
278
- this.focusable = true;
279
- }
280
- if (!this.settled)
281
- return;
282
- for (const node of this.composed)
283
- node.remove();
284
- this.composed = [];
285
- const own = custom ? CONTROL_BAR : PANELS;
286
- if (this.querySelector(`:scope > ${own}`) === null) {
287
- this.composed = custom ? composeBar() : composePanels();
288
- this.append(...this.composed);
289
- }
290
- }
291
- /** Keeps a hidden chapters track on the video for the `chapters` attribute, or none. */
292
- chapters() {
293
- const url = this.getAttribute(CHAPTERS);
294
- if (url === null) {
295
- this.track?.remove();
296
- this.track = null;
297
- return;
298
- }
299
- const held = this.track;
300
- if (held !== null && held.parentNode === this.media && held.getAttribute('src') === url)
301
- return;
302
- // A new element each time: one removed mid-load, as the engine's attach
303
- // does, stays in its error state, and neither a new `src` nor a second
304
- // insertion starts it loading again.
305
- held?.remove();
306
- const track = document.createElement('track');
307
- track.kind = 'chapters';
308
- track.src = url;
309
- this.track = track;
310
- this.media.append(track);
311
- // Set after the insertion: a track element's mode exists once it is in
312
- // a media element, and `hidden` is what makes its cues load unseen.
313
- track.track.mode = 'hidden';
314
- }
315
- forward(name) {
316
- const value = this.getAttribute(name);
317
- if (value === null)
318
- this.media.removeAttribute(name);
319
- else
320
- this.media.setAttribute(name, value);
321
- // A video reads `muted` into its state only when it is created, and this
322
- // one was created before any attribute reached it. Muted autoplay and
323
- // the autoplay policy read the state, so the attribute alone would not do.
324
- if (name === 'muted')
325
- this.media.muted = value !== null;
326
- }
327
- enqueue(task) {
328
- this.queue = this.queue.then(task, task).catch(() => undefined);
329
- }
330
- /**
331
- * One run per burst. The parser sets every attribute and then connects the
332
- * element before any queued task starts, and each of those would load on
333
- * its own; the flag folds them into the one run that sees the final state.
334
- */
335
- reload() {
336
- if (this.pending)
337
- return;
338
- this.pending = true;
339
- this.enqueue(() => {
340
- this.pending = false;
341
- return this.run();
342
- });
343
- }
344
- emit(name, detail) {
345
- this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));
346
- }
347
- report(error) {
348
- if (error.fatal) {
349
- this.failure = error;
350
- // Under custom controls the error screen element is the surface, and
351
- // the row under the video stays quiet, as the panels do.
352
- if (this.getAttribute(CONTROLS) !== 'custom')
353
- this.surface.show(error);
354
- }
355
- this.emit('error', error);
356
- }
357
- async run() {
358
- this.unmount();
359
- if (!this.isConnected)
360
- return;
361
- const url = this.getAttribute('src');
362
- if (url === null)
363
- return;
364
- const core = await this.ensure();
365
- const type = this.getAttribute('type');
366
- const source = type === null ? { url } : { url, type };
367
- this.failure = null;
368
- this.surface.clear();
369
- let session;
370
- try {
371
- session = await core.load(source);
372
- }
373
- catch {
374
- // The core already reported it on the error event, which the surface
375
- // and the element's own event both carry. Nothing to add here.
376
- return;
377
- }
378
- if (!this.isConnected) {
379
- await core.unload();
380
- return;
381
- }
382
- this.current = session;
383
- this.configure(session);
384
- // The engine's attach empties the media element to reset its resource
385
- // selection (`kernel/mse.ts`), and the chapters track goes with the
386
- // `<source>` children it means. Wanted: the track to survive attach.
387
- // Had to: put it back once the session is in. The surface that would
388
- // make it one call: an attach that removes `<source>` children alone.
389
- this.chapters();
390
- }
391
- async ensure() {
392
- if (this.core !== null)
393
- return this.core;
394
- const core = createPlayer(this.media, { handlers: await this.chain() });
395
- this.offs.push(core.on('sourcechange', (session) => {
396
- // Before the event goes out, so a control that reads `engine` on
397
- // `sourcechange` sees the session the event is about.
398
- this.current = session;
399
- this.emit('sourcechange', session);
400
- }), core.on('error', (error) => {
401
- this.report(error);
402
- }));
403
- this.core = core;
404
- return core;
405
- }
406
- async chain() {
407
- if (this.options.handlers !== undefined)
408
- return this.options.handlers;
409
- const config = this.options.config === undefined ? {} : { config: this.options.config };
410
- if (this.options.stages !== undefined) {
411
- return [matteboxHandler({ stages: this.options.stages, ...config }), nativeHandler()];
412
- }
413
- const name = this.getAttribute('preset') ?? DEFAULT_PRESET;
414
- const preset = await resolvePreset(name);
415
- if (preset === null) {
416
- this.report({
417
- category: 'config',
418
- code: 'CONFIG_INVALID',
419
- fatal: true,
420
- recoverable: false,
421
- handler: null,
422
- context: { preset: name },
423
- });
424
- return [nativeHandler()];
425
- }
426
- return [matteboxHandler({ preset, ...config, ...drmGuard() }), nativeHandler()];
427
- }
428
- /** What the attributes ask of the session's namespaces, once there is one. */
429
- configure(session) {
430
- const engine = session.engine;
431
- if (engine === null)
432
- return;
433
- const optional = namespaces(engine);
434
- const license = this.getAttribute('license-url');
435
- // After the session exists, not before: passing a license URL up front
436
- // means constructing eme-core, and importing a stage to configure it
437
- // would bundle it.
438
- if (license !== null && optional.drm !== undefined)
439
- optional.drm.setLicenseUrl(license);
440
- const track = this.getAttribute('thumbnails');
441
- if (track !== null && optional.thumbnails !== undefined) {
442
- // Loaded here; drawn by the seek bar's preview under `controls="custom"`.
443
- // Native controls expose no scrub position to anchor a preview to, so
444
- // under `native` the track only answers `engine.thumbnails.at(time)` for
445
- // the app. See docs/guide/03-the-element.md.
446
- void optional.thumbnails.load(track).catch(() => undefined);
447
- }
448
- }
449
- unmount() {
450
- this.current = null;
451
- }
452
- async teardown() {
453
- this.unmount();
454
- if (this.core !== null)
455
- await this.core.unload();
456
- }
457
- /** Drops the player itself, so the next run builds a chain from the new preset. */
458
- async discard() {
459
- await this.teardown();
460
- for (const off of this.offs)
461
- off();
462
- this.offs = [];
463
- this.core = null;
464
- }
465
- }
70
+ * The interface above merges only `addEventListener` and `removeEventListener`
71
+ * overloads, which the class inherits from HTMLElement. The rule it suppresses
72
+ * guards against properties an interface declares and a constructor never
73
+ * assigns, and there are none.
74
+ */
75
+ var MatteboxPlayerElement = class MatteboxPlayerElement extends HTMLElement {
76
+ static get observedAttributes() {
77
+ return [
78
+ ...FORWARDED,
79
+ ...OWN,
80
+ CONTROLS,
81
+ CHAPTERS,
82
+ PLAYSINLINE
83
+ ];
84
+ }
85
+ /**
86
+ * Registers the element once, and records the stages markup-only pages
87
+ * get. The controls register through their own entries, the package's
88
+ * root entry importing every one; a control connected before its player
89
+ * is defined waits for the definition.
90
+ */
91
+ static define(options) {
92
+ if (options !== void 0) defaults = options;
93
+ if (customElements.get("mattebox-player") === void 0) customElements.define(PLAYER, MatteboxPlayerElement);
94
+ }
95
+ constructor(options) {
96
+ super();
97
+ this.options = options ?? defaults;
98
+ this.focusable = false;
99
+ this.track = null;
100
+ this.given = null;
101
+ this.core = null;
102
+ this.current = null;
103
+ this.failure = null;
104
+ this.reflecting = false;
105
+ this.started = false;
106
+ this.waiting = false;
107
+ this.offs = [];
108
+ this.queue = Promise.resolve();
109
+ this.pending = false;
110
+ this.media = document.createElement("video");
111
+ this.media.controls = true;
112
+ this.media.setAttribute(PLAYSINLINE, "");
113
+ for (const name of STATE_EVENTS) this.media.addEventListener(name, () => {
114
+ this.note(name);
115
+ this.reflect();
116
+ });
117
+ this.media.addEventListener("playing", () => {
118
+ this.failure = null;
119
+ });
120
+ this.stage = document.createElement("div");
121
+ this.stage.setAttribute("part", "stage");
122
+ this.stage.append(document.createElement("slot"));
123
+ this.surface = errorSurface(() => {
124
+ this.reload();
125
+ });
126
+ const root = this.attachShadow({ mode: "open" });
127
+ const style = document.createElement("style");
128
+ style.textContent = STYLE;
129
+ root.append(style, this.stage, this.surface.root);
130
+ }
131
+ /** The media element. Everything the browser already does is here. */
132
+ get video() {
133
+ return this.media;
134
+ }
135
+ /** The core, or null until the first source has been set up: resolving a preset is asynchronous. */
136
+ get player() {
137
+ return this.core;
138
+ }
139
+ /** The engine feeding the video, or null for a native session and before the first load. */
140
+ get engine() {
141
+ return this.current?.engine ?? null;
142
+ }
143
+ /**
144
+ * The fatal error of the current load, or null once a load starts or
145
+ * playback resumes. The `error` event says when; this says what, for a
146
+ * control that attaches after the event, such as an error screen parsed
147
+ * after a source that failed while the page was still loading.
148
+ */
149
+ get error() {
150
+ return this.failure;
151
+ }
152
+ connectedCallback() {
153
+ if (this.media.parentNode !== this) this.append(this.media);
154
+ for (const name of FORWARDED) this.forward(name);
155
+ this.reflect();
156
+ this.reload();
157
+ }
158
+ /** The two states the video does not hold: whether it has played once, and whether it waits for data. */
159
+ note(event) {
160
+ if (event === "playing") {
161
+ this.started = true;
162
+ this.waiting = false;
163
+ } else if (event === "waiting" || event === "stalled") this.waiting = true;
164
+ else if (event === "canplay") this.waiting = false;
165
+ else if (event === "emptied") {
166
+ this.started = false;
167
+ this.waiting = false;
168
+ }
169
+ }
170
+ /** The video's state on the element, for `mattebox-player[paused]` and the like. */
171
+ reflect() {
172
+ const video = this.media;
173
+ this.reflecting = true;
174
+ this.toggleAttribute("paused", video.paused);
175
+ this.toggleAttribute("playing", !video.paused);
176
+ this.toggleAttribute("ended", video.ended);
177
+ this.toggleAttribute("muted", video.muted);
178
+ this.toggleAttribute("audio", this.audio());
179
+ this.toggleAttribute("started", this.started);
180
+ this.toggleAttribute("waiting", this.waiting);
181
+ this.reflecting = false;
182
+ }
183
+ /**
184
+ * Whether the source has no picture. Once the metadata is in, the video's
185
+ * size decides: zero by zero is sound alone, and `resize` catches a stream
186
+ * that moves to or from an audio-only rendition. Before that, the type the
187
+ * page wrote or the URL implies is the hint, so an mp3 is `audio` from
188
+ * the start rather than from its metadata.
189
+ */
190
+ audio() {
191
+ const video = this.media;
192
+ if (video.readyState >= HTMLMediaElement.HAVE_METADATA) return video.videoWidth === 0 && video.videoHeight === 0;
193
+ const src = this.getAttribute("src");
194
+ return (this.getAttribute("type") ?? (src === null ? void 0 : inferType(src)))?.startsWith("audio/") === true;
195
+ }
196
+ disconnectedCallback() {
197
+ this.enqueue(() => this.teardown());
198
+ }
199
+ attributeChangedCallback(name) {
200
+ if (FORWARDED.includes(name)) {
201
+ if (!this.reflecting) this.forward(name);
202
+ return;
203
+ }
204
+ if (name === CONTROLS) {
205
+ this.mode();
206
+ return;
207
+ }
208
+ if (name === CHAPTERS) {
209
+ this.chapters();
210
+ return;
211
+ }
212
+ if (name === PLAYSINLINE) {
213
+ this.media.toggleAttribute(PLAYSINLINE, this.getAttribute(PLAYSINLINE) !== "false");
214
+ return;
215
+ }
216
+ if (name === "preset") this.enqueue(() => this.discard());
217
+ if (name === "src" || name === "type") this.toggleAttribute("audio", this.audio());
218
+ this.reload();
219
+ }
220
+ /**
221
+ * Applies the `controls` attribute: the video's own controls and the
222
+ * tabindex. The controls themselves are elements the page placed, and
223
+ * they attach on their own; the element appends none.
224
+ */
225
+ mode() {
226
+ const value = this.getAttribute(CONTROLS);
227
+ const custom = value === "custom";
228
+ this.media.controls = !custom && value !== "none";
229
+ if (custom) this.surface.clear();
230
+ if (!custom) {
231
+ if (this.focusable) this.removeAttribute("tabindex");
232
+ this.focusable = false;
233
+ } else if (!this.hasAttribute("tabindex")) {
234
+ this.tabIndex = 0;
235
+ this.focusable = true;
236
+ }
237
+ }
238
+ /** Hands the `chapters` attribute to the engine, or keeps a hidden chapters track on the video for it. */
239
+ chapters() {
240
+ const url = this.getAttribute(CHAPTERS);
241
+ const engine = this.current?.engine ?? null;
242
+ const api = engine === null ? void 0 : namespaces(engine).chapters;
243
+ if (api !== void 0) {
244
+ this.track?.remove();
245
+ this.track = null;
246
+ if (url === this.given) return;
247
+ this.given = url;
248
+ if (url === null) api.set([]);
249
+ else api.load(url).catch(() => void 0);
250
+ return;
251
+ }
252
+ if (url === null) {
253
+ this.track?.remove();
254
+ this.track = null;
255
+ return;
256
+ }
257
+ const held = this.track;
258
+ if (held !== null && held.parentNode === this.media && held.getAttribute("src") === url) return;
259
+ held?.remove();
260
+ const track = document.createElement("track");
261
+ track.kind = "chapters";
262
+ track.src = url;
263
+ this.track = track;
264
+ this.media.append(track);
265
+ track.track.mode = "hidden";
266
+ }
267
+ forward(name) {
268
+ const value = this.getAttribute(name);
269
+ if (value === null) this.media.removeAttribute(name);
270
+ else this.media.setAttribute(name, value);
271
+ if (name === "muted") this.media.muted = value !== null;
272
+ }
273
+ enqueue(task) {
274
+ this.queue = this.queue.then(task, task).catch(() => void 0);
275
+ }
276
+ /**
277
+ * One run per burst. The parser sets every attribute and then connects the
278
+ * element before any queued task starts, and each of those would load on
279
+ * its own; the flag folds them into the one run that sees the final state.
280
+ */
281
+ reload() {
282
+ if (this.pending) return;
283
+ this.pending = true;
284
+ this.enqueue(() => {
285
+ this.pending = false;
286
+ return this.run();
287
+ });
288
+ }
289
+ emit(name, detail) {
290
+ this.dispatchEvent(new CustomEvent(name, {
291
+ detail,
292
+ bubbles: true,
293
+ composed: true
294
+ }));
295
+ }
296
+ report(error) {
297
+ if (error.fatal) {
298
+ this.failure = error;
299
+ if (this.getAttribute(CONTROLS) !== "custom") this.surface.show(error);
300
+ }
301
+ this.emit("error", error);
302
+ }
303
+ async run() {
304
+ this.unmount();
305
+ if (!this.isConnected) return;
306
+ const url = this.getAttribute("src");
307
+ if (url === null) return;
308
+ const core = await this.ensure();
309
+ const type = this.getAttribute("type");
310
+ const source = type === null ? { url } : {
311
+ url,
312
+ type
313
+ };
314
+ this.failure = null;
315
+ this.surface.clear();
316
+ let session;
317
+ try {
318
+ session = await core.load(source);
319
+ } catch {
320
+ return;
321
+ }
322
+ if (!this.isConnected) {
323
+ await core.unload();
324
+ return;
325
+ }
326
+ this.current = session;
327
+ this.given = null;
328
+ this.configure(session);
329
+ this.chapters();
330
+ }
331
+ async ensure() {
332
+ if (this.core !== null) return this.core;
333
+ const core = createPlayer(this.media, { handlers: await this.chain() });
334
+ this.offs.push(core.on("sourcechange", (session) => {
335
+ this.current = session;
336
+ this.emit("sourcechange", session);
337
+ }), core.on("error", (error) => {
338
+ this.report(error);
339
+ }));
340
+ this.core = core;
341
+ return core;
342
+ }
343
+ async chain() {
344
+ if (this.options.handlers !== void 0) return this.options.handlers;
345
+ const config = this.options.config === void 0 ? {} : { config: this.options.config };
346
+ if (this.options.stages !== void 0) return [matteboxHandler({
347
+ stages: this.options.stages,
348
+ ...config
349
+ }), nativeHandler()];
350
+ const name = this.getAttribute("preset") ?? DEFAULT_PRESET;
351
+ const preset = await resolvePreset(name);
352
+ if (preset === null) {
353
+ this.report({
354
+ category: "config",
355
+ code: "CONFIG_INVALID",
356
+ fatal: true,
357
+ recoverable: false,
358
+ handler: null,
359
+ context: { preset: name }
360
+ });
361
+ return [nativeHandler()];
362
+ }
363
+ return [matteboxHandler({
364
+ preset,
365
+ ...config,
366
+ ...drmGuard()
367
+ }), nativeHandler()];
368
+ }
369
+ /** What the attributes ask of the session's namespaces, once there is one. */
370
+ configure(session) {
371
+ const engine = session.engine;
372
+ if (engine === null) return;
373
+ const optional = namespaces(engine);
374
+ const license = this.getAttribute("license-url");
375
+ if (license !== null && optional.drm !== void 0) optional.drm.setLicenseUrl(license);
376
+ const track = this.getAttribute("thumbnails");
377
+ if (track !== null && optional.thumbnails !== void 0) optional.thumbnails.load(track).catch(() => void 0);
378
+ }
379
+ unmount() {
380
+ this.current = null;
381
+ }
382
+ async teardown() {
383
+ this.unmount();
384
+ if (this.core !== null) await this.core.unload();
385
+ }
386
+ /** Drops the player itself, so the next run builds a chain from the new preset. */
387
+ async discard() {
388
+ await this.teardown();
389
+ for (const off of this.offs) off();
390
+ this.offs = [];
391
+ this.core = null;
392
+ }
393
+ };
394
+ //#endregion
395
+ export { MatteboxPlayerElement };
396
+
466
397
  //# sourceMappingURL=element.js.map