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

Add onwheel to vdom for the Wheel event #53

Open
wants to merge 2 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
22 changes: 14 additions & 8 deletions lib/vdom.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type key_event = {which: int; alt_key: bool; ctrl_key: bool; shift_key: bool}

type paste_event = {text: string; selection_start: int; selection_end: int}

type wheel_event = {delta_x: float; delta_y: float; delta_z: float; delta_mode: int}

type js_object = .. (* forward declaration in Vdom_blit, to avoid depending to DOM API here *)

module Decoder = struct
Expand Down Expand Up @@ -152,6 +154,12 @@ module Decoder = struct
and+ selection_end = field "currentTarget.selectionEnd" int in
{text; selection_start; selection_end}

let wheel_event =
let+ delta_x = field "deltaX" float
and+ delta_y = field "deltaY" float
and+ delta_z = field "deltaZ" float
and+ delta_mode = field "deltaMode" int in
{delta_x; delta_y; delta_z; delta_mode}
end

type 'msg msg_options = {msg: 'msg option; stop_propagation: bool; prevent_default: bool}
Expand Down Expand Up @@ -249,8 +257,13 @@ let onkeyup_cancel ?stop_propagation msg = onkeyevent_cancel ?stop_propagation "

let onpaste ?prevent_default ?stop_propagation msg = on ?prevent_default ?stop_propagation "paste" (Decoder.map msg Decoder.paste_event)

let onwheel ?prevent_default ?stop_propagation msg = on ?prevent_default ?stop_propagation "wheel" (Decoder.map_some msg Decoder.wheel_event)

let oncustomevent msg = Handler (CustomEvent msg)

let trim_end c s =
let l = String.length s in
if l > 0 && s.[l - 1] = c then String.sub s 0 (l - 1) else s

let str_prop k v = Property (k, String v)
let int_prop k v = Property (k, Int v)
Expand All @@ -259,7 +272,7 @@ let float_prop k v = Property (k, Float v)
let style k v = Style (k, v)
let attr k v = Attribute (k, v)
let int_attr k v = Attribute (k, string_of_int v)
let float_attr k v = Attribute (k, string_of_float v)
let float_attr k v = Attribute (k, trim_end '.' (string_of_float v))
let scroll_to_show ~align_top = bool_prop "scroll-to-show" align_top
let autofocus = bool_prop "autofocus" true
let autofocus_counter x = int_prop "autofocus" x
Expand Down Expand Up @@ -384,13 +397,6 @@ let simple_app ~init ~update ~view () =
()


let trim_end c s =
let l = ref (String.length s) in
while !l > 0 && s.[!l - 1] = c do
decr l
done;
if !l < String.length s then String.sub s 0 !l else s

let replace_char s c x =
match String.index_opt s c with
| None -> s
Expand Down
3 changes: 3 additions & 0 deletions lib/vdom.mli
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ type key_event = {which: int; alt_key: bool; ctrl_key: bool; shift_key: bool}

type paste_event = {text: string; selection_start: int; selection_end: int}

type wheel_event = {delta_x: float; delta_y: float; delta_z: float; delta_mode: int}

type 'msg msg_options = {msg: 'msg option; stop_propagation: bool; prevent_default: bool}

type +'msg event_handler =
Expand Down Expand Up @@ -218,6 +220,7 @@ val onkeydown_cancel: ?stop_propagation:unit -> (key_event -> 'msg option) -> 'm
val onkeyup: ?prevent_default:unit -> ?stop_propagation:unit -> (key_event -> 'msg) -> 'msg attribute
val onkeyup_cancel: ?stop_propagation:unit -> (key_event -> 'msg option) -> 'msg attribute
val onpaste: ?prevent_default:unit -> ?stop_propagation:unit -> (paste_event -> 'msg option) -> 'msg attribute
val onwheel: ?prevent_default:unit -> ?stop_propagation:unit -> (wheel_event -> 'msg) -> 'msg attribute
val oncustomevent: (Custom.event -> 'msg option) -> 'msg attribute


Expand Down