Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added DataTransfer bindings #51

Merged
merged 9 commits into from
Dec 7, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* `StaticRange` bindings (#48)
* Added bindings for the File constructor (#52)
* Added bindings for the Blob constructor (#52)
* `DataTransfer` bindings (#51)

### Miscellaneous
* Converted project to rescript syntax (#18)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';


var items = new DataTransfer().items;

var length = items.length;

var item = items[1];

items.add("text", "text/plain");

items.remove(10);

items.clear();

exports.items = items;
exports.length = length;
exports.item = item;
/* items Not a pure module */
27 changes: 27 additions & 0 deletions lib/js/tests/Webapi/Dom/Webapi__Dom__DataTransferItem__test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var Js_option = require("rescript/lib/js/js_option.js");
var Caml_option = require("rescript/lib/js/caml_option.js");

var item = Js_option.getExn(new DataTransfer().items[0]);

var kind = item.kind;

var itemType = item.type;

item.getAsString(function (_str) {

});

var file = item.getFile();

var file$1 = (file == null) ? undefined : Caml_option.some(file);

var kindString = "file";

exports.item = item;
exports.kind = kind;
exports.itemType = itemType;
exports.file = file$1;
exports.kindString = kindString;
/* item Not a pure module */
43 changes: 43 additions & 0 deletions lib/js/tests/Webapi/Dom/Webapi__Dom__DataTransfer__test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';


var dataTransfer = new DataTransfer();

var dropEffect = dataTransfer.dropEffect;

dataTransfer.dropEffect = "copy";

var effectAllowed = dataTransfer.effectAllowed;

dataTransfer.effectAllowed = "linkMove";

var items = dataTransfer.items;

dataTransfer.setDragImage(document.createElement("div"), 10, 10);

var types = dataTransfer.types;

var data = dataTransfer.getData("text/plain");

dataTransfer.setData("text/plain", "text");

dataTransfer.clearData("text/plain");

dataTransfer.clearData();

var files = dataTransfer.files;

var dropEffectString = "copy";

var effectAllowedString = "linkMove";

exports.dataTransfer = dataTransfer;
exports.dropEffect = dropEffect;
exports.effectAllowed = effectAllowed;
exports.items = items;
exports.types = types;
exports.data = data;
exports.files = files;
exports.dropEffectString = dropEffectString;
exports.effectAllowedString = effectAllowedString;
/* dataTransfer Not a pure module */
1 change: 1 addition & 0 deletions src/Webapi.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Blob = Webapi__Blob
module Canvas = Webapi__Canvas
module Dom = Webapi__Dom
module File = Webapi__File
module FileList = Webapi__FileList
module Fetch = Webapi__Fetch
module FormData = Webapi__FormData

Expand Down
35 changes: 35 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__DataTransfer.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
type t = Dom.dataTransfer

type dropEffect = [#none | #copy | #link | #move]

type effectAllowed = [
spocke marked this conversation as resolved.
Show resolved Hide resolved
| #none
| #copy
| #copyLink
| #copyMove
| #link
| #linkMove
| #move
| #all
| #uninitialized
]

@new external make: unit => t = "DataTransfer"

@get external getDropEffect: t => dropEffect = "dropEffect"
@set external setDropEffect: (t, dropEffect) => unit = "dropEffect"
@get external getEffectAllowed: t => effectAllowed = "effectAllowed"
@set external setEffectAllowed: (t, effectAllowed) => unit = "effectAllowed"
@get external items: t => Webapi__Dom__DataTransferItemList.t = "items"
@send external setDragImage: (t, Dom.element_like<'a>, int, int) => unit = "setDragImage"

// old interface
TheSpyder marked this conversation as resolved.
Show resolved Hide resolved
@get external types: t => array<string> = "types"
@send external getData: (t, string) => string = "getData"
@send external setData: (t, ~format: string, string) => unit = "setData"
@send external clearData: (t, string) => unit = "clearData"
@send external clearAllData: t => unit = "clearData"
@get external files: t => Webapi__FileList.t = "files"

external dropEffectToString: dropEffect => string = "%identity"
external effectAllowedToString: effectAllowed => string = "%identity"
12 changes: 12 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__DataTransferItem.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type t

type kind = [#string | #file]

type functionStringCallback = string => unit

@get external kind: t => kind = "kind"
@get external itemType: t => string = "type"
@send external getAsString: (t, functionStringCallback) => unit = "getAsString"
@send @return(nullable) external getFile: t => option<Webapi__File.t> = "getFile"

external kindToString: kind => string = "%identity"
8 changes: 8 additions & 0 deletions src/Webapi/Dom/Webapi__Dom__DataTransferItemList.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type t

@get external length: t => int = "length"
@get_index external get: (t, int) => option<Webapi__Dom__DataTransferItem.t> = ""
@send @return(nullable) external addString: (t, string, ~format: string) => option<Webapi__Dom__DataTransferItem.t> = "add"
@send @return(nullable) external addFile: (t, Webapi__File.t) => option<Webapi__Dom__DataTransferItem.t> = "add"
@send external remove: (t, int) => unit = "remove"
@send external clear: t => unit = "clear"
3 changes: 3 additions & 0 deletions src/Webapi/Webapi__Dom.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module ClipboardEvent = Webapi__Dom__ClipboardEvent
module CloseEvent = Webapi__Dom__CloseEvent
module CompositionEvent = Webapi__Dom__CompositionEvent
module CustomEvent = Webapi__Dom__CustomEvent
module DataTransfer = Webapi__Dom__DataTransfer
module DataTransferItem = Webapi__Dom__DataTransferItem
module DataTransferItemList = Webapi__Dom__DataTransferItemList
module Document = Webapi__Dom__Document
module DocumentFragment = Webapi__Dom__DocumentFragment
module DocumentType = Webapi__Dom__DocumentType
Expand Down
9 changes: 9 additions & 0 deletions src/Webapi/Webapi__FileList.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type t = {
length: int
}

@val external toArray: t => array<Webapi__File.t> = "Array.prototype.slice.call"
spocke marked this conversation as resolved.
Show resolved Hide resolved

@get external length: t => int = "length"

@send @return(nullable) external item: (t, int) => option<Webapi__File.t> = "item"
12 changes: 12 additions & 0 deletions tests/Webapi/Dom/Webapi__Dom__DataTransferItemList__test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
open Webapi.Dom
open DataTransferItemList

let items = DataTransfer.make()->DataTransfer.items

let length: int = items->length
let item: option<Webapi__Dom__DataTransferItem.t> = items->get(1)

items->addString("text", ~format="text/plain")->ignore
// items->addFile(file) - we can't really make a file see #52
items->remove(10)
items->clear
11 changes: 11 additions & 0 deletions tests/Webapi/Dom/Webapi__Dom__DataTransferItem__test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
open Webapi.Dom
open DataTransferItem

let item = DataTransfer.make()->DataTransfer.items->DataTransferItemList.get(0)->Js.Option.getExn

let kind: kind = item->kind
let itemType: string = item->itemType
item->getAsString(_str => ())
let file: option<Webapi__File.t> = item->getFile

let kindString: string = kindToString(#file)
21 changes: 21 additions & 0 deletions tests/Webapi/Dom/Webapi__Dom__DataTransfer__test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open Webapi.Dom
open DataTransfer

let dataTransfer = make()

let dropEffect: dropEffect = dataTransfer->getDropEffect
dataTransfer->setDropEffect(#copy)
let effectAllowed: effectAllowed = dataTransfer->getEffectAllowed
dataTransfer->setEffectAllowed(#linkMove)
let items: Webapi__Dom__DataTransferItemList.t = dataTransfer->items
dataTransfer->setDragImage(document->Document.createElement("div"), 10, 10)

let types: array<string> = dataTransfer->types
let data: string = dataTransfer->getData("text/plain")
dataTransfer->setData(~format="text/plain", "text")
dataTransfer->clearData("text/plain")
dataTransfer->clearAllData
let files: Webapi__FileList.t = dataTransfer->files

let dropEffectString: string = dropEffectToString(#copy)
let effectAllowedString: string = effectAllowedToString(#linkMove)