Skip to content
Merged
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
4 changes: 2 additions & 2 deletions bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
}
],
"warnings": {
"number": "+A-42-48-105-109",
"error": "+A-3-44-102"
"number": "+A-105",
"error": "+A"
},
"bs-dependencies": [
"bs-fetch"
Expand Down
131 changes: 61 additions & 70 deletions examples/dom_example.re
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
open Webapi.Dom;
open Belt.Option;

/* Adapted from https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Examples#Example_7:_Displaying_Event_Object_Properties */
/* Option.map */
let map = (f) =>
fun
| Some(v) => Some(f(v))
| None => None;

let andThen = (f: 'a => option('b)) =>
fun
| Some(v) => f(v)
| None => None;

let unwrapUnsafely =
fun
Expand All @@ -20,79 +11,79 @@ let unwrapUnsafely =
/*
* These SHOULD type check
*/
document |> Document.createElement("div")
|> Element.className;
let _ = document->Document.createElement("div")->Element.className;

document |> Document.createElement("div")
|> Element.nextElementSibling
|> map(Element.innerText); /* innerText is a function that accepts a Node */
let _ =
document->Document.createElement("div")->Element.nextElementSibling->map(Element.innerText); /* innerText is a function that accepts a Node */

/* Before subtyping:
document |> Document.createElement("div")
|> Element.asNode
|> Node.parentElement /* inherited from Node, returns DomRe.element */
|> map(Element.innerText); /* inherited from Node */
*/
document->Document.createElement("div")
->Element.asNode
->Node.parentElement /* inherited from Node, returns DomRe.element */
->map(Element.innerText); /* inherited from Node */
*/

/* After subtyping: */
document |> Document.createElement("div")
|> Element.parentElement /* inherited from Node, returns DomRe.element */
|> map(Element.innerText); /* inherited from Node */

let el =
document |> Document.createElement("div")
|> Element.asHtmlElement
|> unwrapUnsafely;

/*
document |> Document.asHtmlDocument
|> andThen(HtmlDocument.body)
|> map(Element.appendChild(el));
*/
let _ =
document
->Document.createElement("div")
->Element.parentElement /* inherited from Node, returns DomRe.element */
->map(Element.innerText); /* inherited from Node */

let el = document->Document.createElement("div")->Element.asHtmlElement->unwrapUnsafely;

/*
document->Document.asHtmlDocument
->flatMap(HtmlDocument.body)
->map(Element.appendChild(el));
*/

/* Before subtyping:
document |> Document.asHtmlDocument
|> andThen(HtmlDocument.body)
|> map(Element.appendChild (el |> HtmlElement.asNode));
*/
document->Document.asHtmlDocument
->flatMap(HtmlDocument.body)
->map(Element.appendChild (el->HtmlElement.asNode));
*/

/* After subtyping: */
document |> Document.asHtmlDocument
|> andThen(HtmlDocument.body)
|> map(Element.appendChild(el));
let _ =
document
->Document.asHtmlDocument
->flatMap(HtmlDocument.body)
->map(body => body->Element.appendChild(el));

/*
/*
* These MAY fail type check
*/
document |> Document.createElement("div")
|> Element.nextElementSibling
|> map(Node.innerText);

/*
* These SHOULD NOT type check
*/
document |> Document.createElement("div")
|> Element.asNode
|> Element.parentElement; /* inherited from Node, returns DomRe.element */
*/
/*
* These MAY fail type check
*/
document->Document.createElement("div")
->Element.nextElementSibling
->map(Node.innerText);

/*
* These SHOULD NOT type check
*/
document->Document.createElement("div")
->Element.asNode
->Element.parentElement; /* inherited from Node, returns DomRe.element */
*/

/*
/* ideal, but requires piped setters */
switch (document |> body) {
| Some body =>
document |> createElement("div")
|> setInnerText("</>")
|> setClassName("reason_tools_button")
|> setOnClick(swap)
|> (body |> appendChild);
| None =>
...
}
*/

/* ideal, but requires piped setters */
switch (document->body) {
| Some body =>
document->createElement("div")
->setInnerText("</>")
->setClassName("reason_tools_button")
->setOnClick(swap)
->(body->appendChild);
| None =>
...
}
*/

/* non-standard event-specific listener API - log screen coordinates of mouse cursor when moved */
document |> Document.createElement("div")
|> Element.addMouseMoveEventListener(e => (MouseEvent.screenX(e), MouseEvent.screenY(e)) |> Js.log);
document
->Document.createElement("div")
->Element.addMouseMoveEventListener(e =>
(MouseEvent.screenX(e), MouseEvent.screenY(e))->Js.log
);
21 changes: 5 additions & 16 deletions lib/js/tests/Webapi/Canvas/Webapi__Canvas__Canvas2d__test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var List = require("bs-platform/lib/js/list.js");
var Caml_option = require("bs-platform/lib/js/caml_option.js");
var Webapi__Canvas__Canvas2d = require("../../../src/Webapi/Canvas/Webapi__Canvas__Canvas2d.js");

var canvasEl = document.createElement("canvas");
Expand Down Expand Up @@ -32,9 +31,9 @@ ctx.lineJoin = Webapi__Canvas__Canvas2d.LineJoin.round;

ctx.miterLimit = 10;

Webapi__Canvas__Canvas2d.setStrokeStyle(ctx, /* String */0, "red");
ctx.strokeStyle = "red";

Webapi__Canvas__Canvas2d.setFillStyle(ctx, /* String */0, "red");
ctx.fillStyle = "red";

var match = Webapi__Canvas__Canvas2d.fillStyle(ctx);

Expand Down Expand Up @@ -100,7 +99,7 @@ ctx.isPointInPath(0, 0);

var linearGradient = ctx.createLinearGradient(0.0, 0.0, 0.0, 0.0);

Webapi__Canvas__Canvas2d.setStrokeStyle(ctx, /* Gradient */1, linearGradient);
ctx.strokeStyle = linearGradient;

ctx.createRadialGradient(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);

Expand Down Expand Up @@ -160,19 +159,9 @@ ctx.textAlign = "left";

ctx.textBaseline = "top";

function arg(param, param$1) {
param$1.fillText("hi", 1, 0, param !== undefined ? Caml_option.valFromOption(param) : undefined);

}

arg(undefined, ctx);

function arg$1(param, param$1) {
param$1.strokeText("hi", 1, 0, param !== undefined ? Caml_option.valFromOption(param) : undefined);

}
ctx.fillText("hi", 1, 0, undefined);

arg$1(undefined, ctx);
ctx.strokeText("hi", 1, 0, undefined);

ctx.fillRect(1, 0, 10, 10);

Expand Down
5 changes: 2 additions & 3 deletions lib/js/tests/Webapi/Dom/Webapi__Dom__DomStringMap__test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ var dataset = Belt_Option.map(__x, (function (prim) {

if (dataset !== undefined) {
var dataset$1 = Caml_option.valFromOption(dataset);
Webapi__Dom__DomStringMap.set("fooKey", "barValue", dataset$1);
Webapi__Dom__DomStringMap.get("fooKey", dataset$1);
Webapi__Dom__DomStringMap.unsafeDeleteKey("fooKey", dataset$1);
dataset$1["fooKey"] = "barValue";
Webapi__Dom__DomStringMap.unsafeDeleteKey(dataset$1, "fooKey");
}

exports.dataset = dataset;
Expand Down
2 changes: 1 addition & 1 deletion lib/js/tests/Webapi/Dom/Webapi__Dom__DragEvent__test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $$event.stopImmediatePropagation();

$$event.stopPropagation();

Curry._2(Webapi__Dom__DragEvent.getModifierState, /* Alt */0, $$event);
Curry._2(Webapi__Dom__DragEvent.getModifierState, $$event, /* Alt */0);

exports.$$event = $$event;
/* event Not a pure module */
6 changes: 3 additions & 3 deletions lib/js/tests/Webapi/Dom/Webapi__Dom__Element__test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ el.hasAttributeNS("http://...", "foo");

el.hasAttributes();

Webapi__Dom__Element.insertAdjacentElement(/* BeforeBegin */0, el2, el);
Webapi__Dom__Element.insertAdjacentElement(el, /* BeforeBegin */0, el2);

Webapi__Dom__Element.insertAdjacentHTML(/* AfterBegin */1, "<strong>text</strong>", el);
Webapi__Dom__Element.insertAdjacentHTML(el, /* AfterBegin */1, "<strong>text</strong>");

Webapi__Dom__Element.insertAdjacentText(/* AfterEnd */3, "text", el);
Webapi__Dom__Element.insertAdjacentText(el, /* AfterEnd */3, "text");

el.matches("input");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ htmlDocument.title = "Reason: Rapid Expressive Systems Programming.";

htmlDocument.close();

Webapi__Dom__HtmlDocument.execCommand("copy", false, undefined, htmlDocument);
Webapi__Dom__HtmlDocument.execCommand(htmlDocument, "copy", false, undefined);

htmlDocument.getElementsByName("angry-joe");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $$event.stopImmediatePropagation();

$$event.stopPropagation();

Webapi__Dom__KeyboardEvent.getModifierState(/* Alt */0, $$event);
Webapi__Dom__KeyboardEvent.getModifierState($$event, /* Alt */0);

exports.$$event = $$event;
/* event Not a pure module */
2 changes: 1 addition & 1 deletion lib/js/tests/Webapi/Dom/Webapi__Dom__MouseEvent__test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $$event.stopImmediatePropagation();

$$event.stopPropagation();

Webapi__Dom__MouseEvent.getModifierState(/* Alt */0, $$event);
Webapi__Dom__MouseEvent.getModifierState($$event, /* Alt */0);

exports.$$event = $$event;
/* event Not a pure module */
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $$event.stopImmediatePropagation();

$$event.stopPropagation();

Curry._2(Webapi__Dom__PointerEvent.getModifierState, /* Alt */0, $$event);
Curry._2(Webapi__Dom__PointerEvent.getModifierState, $$event, /* Alt */0);

Webapi__Dom__PointerEvent.pointerType($$event);

Expand Down
2 changes: 1 addition & 1 deletion lib/js/tests/Webapi/Dom/Webapi__Dom__Range__test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ range.detach();

range.toString();

Webapi__Dom__Range.comparePoint(node, 0, range);
Webapi__Dom__Range.comparePoint(range, node, 0);

range.createContextualFragment("<strong>stuff</strong>");

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rescript-webapi",
"version": "0.0.1",
"version": "0.1.0",
"description": "Reason / ReScript bindings to DOM",
"repository": {
"type": "git",
Expand All @@ -19,7 +19,8 @@
"start": "bsb -make-world -w",
"clean": "bsb -clean-world",
"doc-install": "npm install --no-save bsdoc",
"test": "bsb -make-world && bsdoc build api && bsdoc support-files"
"test": "bsb -make-world && bsdoc build api && bsdoc support-files",
"refmt-all": "find src tests \\( -name '*.re' -o -name '*.rei' \\) | xargs bsrefmt -w 100 --in-place"
},
"files": [
"src",
Expand Down
7 changes: 4 additions & 3 deletions src/Webapi.re
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Url = Webapi__Url;

type rafId;

[@bs.val] external requestAnimationFrame : (float => unit) => unit = "";
[@bs.val] external requestCancellableAnimationFrame : (float => unit) => rafId = "requestAnimationFrame";
[@bs.val] external cancelAnimationFrame : rafId => unit = "";
[@val] external requestAnimationFrame: (float => unit) => unit;
[@val]
external requestCancellableAnimationFrame: (float => unit) => rafId = "requestAnimationFrame";
[@val] external cancelAnimationFrame: rafId => unit;
Loading