Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

feat: HtmlVideoElement, HtmlMediaElement, TextTrack, VideoTrack #196

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
### 0.20.0

* Added binding for `HTMLVideoElement`
* Added binding for `HTMLMediaElement`
* Added binding for `MediaError`
* Added binding for `AudioTrack`
* Added binding for `AudioTrackList`
* Added binding for `TextTrack`
* Added binding for `TextTrackList`
* Added binding for `TextTrackCue`
* Added binding for `TextTrackCueList`
* Added binding for `VideoTrack`
* Added binding for `VideoTrackList`
* Added binding for `TimeRanges`

### 0.19.2

* Removed peer dependency on `bsdoc`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bs-webapi",
"version": "0.19.2",
"version": "0.20.0",
"description": "Reason + BuckleScript bindings to DOM",
"repository": {
"type": "git",
Expand Down
18 changes: 18 additions & 0 deletions src/Webapi.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ module Iterator = FormData.Iterator;

module Performance = Webapi__Performance;

/** @since 0.20.0 */
module AudioTrack = Webapi__AudioTrack;
/** @since 0.20.0 */
module AudioTrackList = Webapi__AudioTrackList;

/** @since 0.20.0 */
module TextTrack = Webapi__TextTrack;
/** @since 0.20.0 */
module TextTrackList = Webapi__TextTrackList;

/** @since 0.20.0 */
module VideoTrack = Webapi__VideoTrack;
/** @since 0.20.0 */
module VideoTrackList = Webapi__VideoTrackList;

/** @since 0.20.0 */
module TimeRanges = Webapi__TimeRanges;

/** @since 0.19.0 */
module ReadableStream = Webapi__ReadableStream;

Expand Down
139 changes: 139 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__HtmlMediaElement.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#htmlmediaelement
*/
module Impl = (T: {type t;}) => {
type t_htmlMediaElement = T.t;

type mediaProvider; // TODO: one of MediaStream, MediaSource, or Blob
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking this might be a Webapi__MediaProvider.t instead of a local phantom type


/** Properties */
[@bs.get] external buffered: t_htmlMediaElement => Webapi__TimeRanges.t = "buffered";

// This is part of the recommendation spec
// type mediaController; // TODO
// [@bs.get] [@bs.return nullable] external controller: t_htmlMediaElement => option(mediaController) = "controller";
// [@bs.set] external setController: (t_htmlMediaElement, mediaController) => unit = "controller";

[@bs.get] external controls: t_htmlMediaElement => bool = "controls";
[@bs.set] external setControls: (t_htmlMediaElement, bool) => unit = "controls";

/** One or more of "nodownload", "nofullscreen", and "noremoteplayback" */
[@bs.get] external controlsList: t_htmlMediaElement => Dom.domTokenList = "controlsList";

[@bs.get] [@bs.return nullable] external crossOrigin: t_htmlMediaElement => option(string) = "crossOrigin";
[@bs.set]
external setCrossOrigin:
(
t_htmlMediaElement,
[@bs.string] [ | `anonymous | [@bs.as "use-credentials"] `useCredentials | [@bs.as ""] `empty]
) =>
unit =
"crossOrigin";

[@bs.get] external currentSrc: t_htmlMediaElement => string = "currentSrc";

[@bs.get] external currentTime: t_htmlMediaElement => float = "currentTime";
[@bs.set] external setCurrentTime: (t_htmlMediaElement, float) => unit = "currentTime";

[@bs.get] external defaultMuted: t_htmlMediaElement => bool = "defaultMuted";
[@bs.set] external setDefaultMuted: (t_htmlMediaElement, bool) => unit = "defaultMuted";

[@bs.get] external defaultPlaybackRate: t_htmlMediaElement => float = "defaultPlaybackRate";
[@bs.set] external setDefaultPlaybackRate: (t_htmlMediaElement, float) => unit = "defaultPlaybackRate";

[@bs.get] external disableRemotePlayback: t_htmlMediaElement => bool = "disableRemotePlayback";
[@bs.set] external setDisableRemotePlayback: (t_htmlMediaElement, bool) => unit = "disableRemotePlayback";

[@bs.get] external duration: t_htmlMediaElement => float = "duration";
[@bs.get] external ended: t_htmlMediaElement => bool = "ended";
[@bs.get] [@bs.return nullable] external error: t_htmlMediaElement => option(Webapi__Dom__MediaError.t) = "error";

[@bs.get] external loop: t_htmlMediaElement => bool = "loop";
[@bs.set] external setLoop: (t_htmlMediaElement, bool) => unit = "loop";

// [@bs.get] external mediaGroup: t_htmlMediaElement => string = "mediaGroup"; // Not well supported
// [@bs.set] external setMediaGroup: (t_htmlMediaElement, string) => unit = "mediaGroup"; // Not well supported

[@bs.get] external muted: t_htmlMediaElement => bool = "muted";
[@bs.set] external setMuted: (t_htmlMediaElement, bool) => unit = "muted";

type mediaNetworkState =
| NetworkEmpty // 0
| NetworkIdle // 1
| NetworkLoading // 2
| NetworkNoSource; // 3

let decodeNetworkState = fun
| 0 => Some(NetworkEmpty)
| 1 => Some(NetworkIdle)
| 2 => Some(NetworkLoading)
| 3 => Some(NetworkNoSource)
| _ => None;

[@bs.get] external _networkState: t_htmlMediaElement => int = "networkState";
let networkState = t_htmlMediaElement => _networkState(t_htmlMediaElement) |> decodeNetworkState;

[@bs.get] external paused: t_htmlMediaElement => bool = "paused";

[@bs.get] external playbackRate: t_htmlMediaElement => float = "playbackRate";
[@bs.set] external setPlaybackRate: (t_htmlMediaElement, float) => unit = "playbackRate";

[@bs.get] external played: t_htmlMediaElement => Webapi__TimeRanges.t = "played";
[@bs.get] external preload: t_htmlMediaElement => string = "preload";

type readyStateEnum =
| HaveNothing
| HaveMetadata
| HaveCurrentData
| HaveFutureData
| HaveEnoughData;

let decodeReadyState = fun
| 0 => Some(HaveNothing)
| 1 => Some(HaveMetadata)
| 2 => Some(HaveCurrentData)
| 3 => Some(HaveFutureData)
| 4 => Some(HaveEnoughData)
| _ => None;

[@bs.get] external _readyState: t_htmlMediaElement => int = "readyState";
let readyState = t => _readyState(t) |> decodeReadyState;

[@bs.get] external seekable: t_htmlMediaElement => Webapi__TimeRanges.t = "seekable";
[@bs.get] external seeking: t_htmlMediaElement => bool = "seeking";
// [@bs.get] external sinkId: t_htmlMediaElement => string = "sinkId"; // experimental

[@bs.get] external src: t_htmlMediaElement => string = "src";
[@bs.set] external setSrc: (t_htmlMediaElement, string) => unit = "src";

[@bs.get] [@bs.return nullable] external srcObject: t_htmlMediaElement => option(mediaProvider) = "srcObject";

[@bs.get] external audioTracks: t_htmlMediaElement => Webapi__AudioTrackList.t = "textTracks";
[@bs.get] external textTracks: t_htmlMediaElement => Webapi__TextTrackList.t = "textTracks";

// [@bs.get] external videoTracks: t_htmlMediaElement => Webapi__VideoTrackList.t = "videoTracks"; // Not widely available

[@bs.get] external volume: t_htmlMediaElement => float = "volume";
[@bs.set] external setVolume: (t_htmlMediaElement, float) => unit = "volume";

/** Methods */

[@bs.send] external addTextTrack: (t_htmlMediaElement, Webapi__TextTrack.textTrackKind, ~label: string, ~language: string, unit) => Webapi__TextTrack.t = "addTextTrack";

// return is one of "probably", "maybe", or ""
[@bs.send] external canPlayType: (t_htmlMediaElement, string) => string = "canPlayType";
// [@bs.send] external fastSeek: (t_htmlMediaElement, float) => unit = "fastSeek"; // experimental
[@bs.send] external load: t_htmlMediaElement => unit = "load";
[@bs.send] external pause: t_htmlMediaElement => unit = "pause";
[@bs.send] external play: t_htmlMediaElement => Js.Promise.t(unit) = "play";

// [@bs.send] external getStartDate: t_htmlMediaElement => Js.Date.t = "getStartDate"; // not supported?
};

type t; // TODO: Dom.htmlMediaElement

include Webapi__Dom__EventTarget.Impl({type nonrec t = t;});
include Webapi__Dom__Node.Impl({type nonrec t = t;});
include Webapi__Dom__Element.Impl({type nonrec t = t;});
include Webapi__Dom__HtmlElement.Impl({type nonrec t = t;});
include Impl({type nonrec t = t;});
32 changes: 32 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__HtmlVideoElement.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#htmlvideoelement
*/
module Impl = (T: {type t;}) => {
type t_htmlVideoElement = T.t;

external unsafeOfElement: Dom.element => t_htmlVideoElement = "%identity";

let ofElement = (el): option(t_htmlVideoElement) => switch(Webapi__Dom__Element.tagName(el)) {
| "VIDEO" => el->unsafeOfElement->Some
| _ => None
};

/** Properties */
[@bs.get] external height: t_htmlVideoElement => int = "height";
[@bs.set] external setHeight: (t_htmlVideoElement, int) => unit = "height";
[@bs.get] external poster: t_htmlVideoElement => string = "poster";
[@bs.set] external setPoster: (t_htmlVideoElement, string) => unit = "poster";
[@bs.get] external videoHeight: t_htmlVideoElement => int = "videoHeight";
[@bs.get] external videoWidth: t_htmlVideoElement => int = "videoWidth";
[@bs.get] external width: t_htmlVideoElement => int = "width";
[@bs.set] external setWidth: (t_htmlVideoElement, int) => unit = "width";
};

type t; // TODO: Dom.htmlVideoElement

include Webapi__Dom__EventTarget.Impl({ type nonrec t = t; });
include Webapi__Dom__Node.Impl({ type nonrec t = t; });
include Webapi__Dom__Element.Impl({ type nonrec t = t; });
include Webapi__Dom__HtmlElement.Impl({ type nonrec t = t; });
include Webapi__Dom__HtmlMediaElement.Impl({ type nonrec t = t; });
include Impl({ type nonrec t = t; });
23 changes: 23 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__MediaError.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#mediaerror
*/
type t;

type errorType =
| ERR_ABORTED
| ERR_NETWORK
| ERR_DECODE
| ERR_SRC_NOT_SUPPORTED;

let decodeErrorType = fun
| 1 => Some(ERR_ABORTED)
| 2 => Some(ERR_NETWORK)
| 3 => Some(ERR_DECODE)
| 4 => Some(ERR_SRC_NOT_SUPPORTED)
| _ => None;

/** Properties */

[@bs.get] external _code: t => int = "code";
let code = t => _code(t) |> decodeErrorType;
[@bs.get] external message: t => string = "message";
17 changes: 17 additions & 0 deletions src/Webapi/TextTrack/Webapi__TextTrack__TextTrackCue.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#texttrackcue
*/
type t;

include Webapi__Dom__EventTarget.Impl({ type nonrec t = t; });

// FIXME: circular dep
// [@bs.get] [@bs.return nullable] external track: t => option(Webapi__TextTrack.t) = "track";
[@bs.get] external id: t => string = "id";
[@bs.set] external setId: (t, string) => unit = "id";
[@bs.get] external startTime: t => float = "startTime";
[@bs.set] external setStartTime: (t, float) => unit = "startTime";
[@bs.get] external endTime: t => float = "endTime";
[@bs.set] external setEndTime: (t, float) => unit = "endTime";
[@bs.get] external pauseOnExit: t => bool = "pauseOnExit";
[@bs.set] external setPauseOnExit: (t, bool) => unit = "pauseOnExit";
11 changes: 11 additions & 0 deletions src/Webapi/TextTrack/Webapi__TextTrack__TextTrackCueList.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#texttrackcuelist
*/
type t;

/** Properties */
[@bs.get] external length: t => int = "length";
[@bs.get_index] [@bs.return nullable] external get: (t, int) => option(Webapi__TextTrack__TextTrackCue.t) = "";

/** Methods */
[@bs.send] [@bs.return nullable] external getCueById: (t, string) => option(Webapi__TextTrack__TextTrackCue.t) = "getCueById";
14 changes: 14 additions & 0 deletions src/Webapi/Webapi__AudioTrack.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#audiotrack
*/
type t;

/** Properties */

[@bs.get] external id: t => string = "id";
[@bs.get] external kind: t => string = "kind";
[@bs.get] external label: t => string = "label";
[@bs.get] external language: t => string = "language";

[@bs.get] external enabled: t => bool = "enabled";
[@bs.set] external setEnabled: (t, bool) => unit = "enabled";
14 changes: 14 additions & 0 deletions src/Webapi/Webapi__AudioTrackList.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#audiotracklist
*/
type t;
include Webapi__Dom__EventTarget.Impl({ type nonrec t = t; });

/** Properties */

[@bs.get] external length: t => float = "length";
[@bs.get_index] external get: (t, int) => option(Webapi__AudioTrack.t) = "";

/** Methods */

[@bs.send] [@bs.return nullable] external getTrackById: (t, string) => option(Webapi__AudioTrack.t) = "getTrackById";
5 changes: 3 additions & 2 deletions src/Webapi/Webapi__Dom.re
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ module HtmlElement = Webapi__Dom__HtmlElement;
module HtmlFormElement = Webapi__Dom__HtmlFormElement;
module HtmlImageElement = Webapi__Dom__HtmlImageElement;
module HtmlInputElement = Webapi__Dom__HtmlInputElement;
module HtmlMediaElement = Webapi__Dom__HtmlMediaElement;
module HtmlVideoElement = Webapi__Dom__HtmlVideoElement;
module IdbVersionChangeEvent = Webapi__Dom__IdbVersionChangeEvent;
module Image = Webapi__Dom__Image;
module InputEvent = Webapi__Dom__InputEvent;
module KeyboardEvent = Webapi__Dom__KeyboardEvent;
module Location = Webapi__Dom__Location;
module MediaError = Webapi__Dom__MediaError;
module MouseEvent = Webapi__Dom__MouseEvent;
module MutationObserver = Webapi__Dom__MutationObserver;
module MutationRecord = Webapi__Dom__MutationRecord;
Expand Down Expand Up @@ -130,7 +133,6 @@ include Webapi__Dom__Types;
HTMLLIElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMenuElement
HTMLMetaElement
HTMLMeterElement
Expand Down Expand Up @@ -164,7 +166,6 @@ include Webapi__Dom__Types;
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement

/* Other interfaces */
CanvasRenderingContext2D
Expand Down
33 changes: 33 additions & 0 deletions src/Webapi/Webapi__TextTrack.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#texttrack
*/
type t;
include Webapi__Dom__EventTarget.Impl({ type nonrec t = t; });

/** @since 0.20.0 */
module TextTrackCueList = Webapi__TextTrack__TextTrackCueList;
/** @since 0.20.0 */
module TextTrackCue = Webapi__TextTrack__TextTrackCue;

// TODO: string enum for
// enum TextTrackMode { "disabled", "hidden", "showing" };
// enum TextTrackKind { "subtitles", "captions", "descriptions", "chapters", "metadata" };
type textTrackMode = string;
type textTrackKind = string;

/** Properties */

[@bs.get] external activeCues: t => option(TextTrackCueList.t) = "activeCues";
[@bs.get] external cues: t => option(TextTrackCueList.t) = "cues";
[@bs.get] external id: t => string = "id";
[@bs.get] external inBandMetadataTrackDispatchType: t => option(string) = "inBandMetadataTrackDispatchType";
[@bs.get] external kind: t => textTrackKind = "kind";
[@bs.get] external label: t => string = "label";
[@bs.get] external language: t => string = "language";
[@bs.get] external mode: t => textTrackMode = "mode";
[@bs.set] external setMode: (t, textTrackMode) => unit = "mode";

/** Methods */

[@bs.send] external addCue: (t, TextTrackCue.t) => unit = "addCue";
[@bs.send] external removeCue: (t, TextTrackCue.t) => unit = "removeCue";
10 changes: 10 additions & 0 deletions src/Webapi/Webapi__TextTrackList.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Spec: https://html.spec.whatwg.org/multipage/media.html#texttracklist
*/

type t;
include Webapi__Dom__EventTarget.Impl({ type nonrec t = t; });

[@bs.get] external length: t => float = "length";
[@bs.get_index] external get: (t, int) => option(Webapi__TextTrack.t) = "";
[@bs.send] [@bs.return nullable] external getTrackById: (t, string) => option(Webapi__TextTrack.t) = "getTrackById";
Loading