forked from OvermindDL1/bucklescript-tea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tea_mouse.ml
43 lines (34 loc) · 1.02 KB
/
tea_mouse.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
type position = {
x : int;
y : int;
}
let position =
let open Tea_json.Decoder in
map2 (fun x y -> {x; y})
(field "pageX" int)
(field "pageY" int)
let registerGlobal name key tagger =
let open Vdom in
let enableCall callbacks_base =
let callbacks = ref callbacks_base in
let fn = fun ev ->
let open Tea_json.Decoder in
let open Tea_result in
match decodeEvent position ev with
| Error _ -> None
| Ok pos -> Some (tagger pos) in
let handler = EventHandlerCallback (key, fn) in
let elem = Web_node.document_node in
let cache = eventHandler_Register callbacks elem name handler in
fun () ->
let _ = eventHandler_Unregister elem name cache in
()
in Tea_sub.registration key enableCall
let clicks ?(key="") tagger =
registerGlobal "click" key tagger
let moves ?(key="") tagger =
registerGlobal "mousemove" key tagger
let downs ?(key="") tagger =
registerGlobal "mousedown" key tagger
let ups ?(key="") tagger =
registerGlobal "mouseup" key tagger