text stringlengths 12 786k |
|---|
module Custom = struct type t = { dom : Js_browser . Element . t ; sync : ( Vdom . Custom . t -> bool ) bool ; dispose : ( unit -> unit ) unit ; } type ctx = { parent : Js_browser . Element . t ; send_event : ( Vdom . event -> unit ) unit ; after_redraw : ( ( un... |
let rec scroll_parent node = if node = Element . null then Document . body document else let overflow_y = Style . get ( Window . get_computed_style window node ) node " overflowY " in let is_scrollable = overflow_y <> " visible " && overflow_y <> " hidden " in if is_scrollable && Element . ... |
let scroll_to_make_visible child = let open Js_browser in let parent = scroll_parent child in let r_parent = Element . get_bounding_client_rect parent in let r_child = Element . get_bounding_client_rect child in let y1 = Rect . bottom r_parent and y2 = Rect . bottom r_child in if y2 > y1 then Element . set_s... |
type ' msg ctrl = | BText of { vdom : ' msg vdom ; dom : Element . t } t | BElement of { vdom : ' msg vdom ; dom : Element . t ; children : ' msg ctrl list } list | BMap : { vdom : ' msg vdom ; dom : Element . t ; f : ( ' submsg -> ' msg ) ' msg ; child : ' submsg ... |
let get_dom = function | BText x -> x . dom | BElement x -> x . dom | BMap x -> x . dom | BMemo x -> x . dom | BCustom x -> x . elt . dom |
let get_vdom = function | BText x -> x . vdom | BElement x -> x . vdom | BMap x -> x . vdom | BMemo x -> x . vdom | BCustom x -> x . vdom |
let key_of_vdom = function | Text { key ; _ } _ | Element { key ; _ } _ | Map { key ; _ } _ | Memo { key ; _ } _ | Custom { key ; _ } _ -> key |
let eval_prop = function | String x -> Ojs . string_to_js x | Int x -> Ojs . int_to_js x | Bool x -> Ojs . bool_to_js x | Float x -> Ojs . float_to_js x |
let string_of_prop = function | String s -> s | Int x -> string_of_int x | Bool x -> string_of_bool x | Float x -> string_of_float x |
let same_prop v1 v2 = v1 == v2 || match v1 , v2 with | String x1 , String x2 -> x1 = x2 | Int x1 , Int x2 -> x1 = x2 | Bool x1 , Bool x2 -> x1 = x2 | Float x1 , Float x2 -> x1 = x2 | _ -> false |
let bmemo vdom child = BMemo { vdom ; dom = get_dom child ; child } child |
let async f = ignore ( Window . set_timeout window f 0 ) 0 |
let custom_attribute dom = function | " scroll - to - show " -> async ( fun ( ) -> try scroll_to_make_visible dom with exn -> Printf . printf " scroll : % s \ n " %! ( Printexc . to_string exn ) exn ) ; true | " autofocus " -> async ( fun ( ) -> Element . focus dom ) do... |
let apply_attributes dom attributes = List . iter ( function | Property ( k , v ) v -> if not ( custom_attribute dom k ) k then Ojs . set ( Element . t_to_js dom ) dom k ( eval_prop v ) v | Style ( k , v ) v -> Ojs . set ( Ojs . get ( Element . t_to_js dom ) dom " style )... |
type ctx = { process_custom : ( Element . t -> event -> unit ) unit ; custom_handlers : Custom . handler list ; after_redraw : ( ( unit -> unit ) unit -> unit ) unit ; } |
let rec blit : ' msg . parent : _ -> ctx -> ' msg vdom -> ' msg ctrl = fun ~ parent ctx vdom -> match vdom with | Text { txt ; key = _ } _ -> BText { vdom ; dom = Document . create_text_node document txt } txt | Map { f ; child ; key = _ } _ -> let child = blit ~ parent ct... |
let blit ctx vdom = try blit ctx vdom with exn -> Printf . printf " Error during vdom blit : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn |
let sync_props to_string same set clear l1 l2 = let sort = List . sort ( fun ( k1 , _ ) _ ( k2 , _ ) _ -> compare ( k1 : string ) string k2 ) k2 in let l1 = sort l1 and l2 = sort l2 in let rec loop l1 l2 = match l1 , l2 with | [ ] , [ ] -> ( ) | ( k1 , v1 ) v1 :... |
let rec choose f = function | [ ] -> [ ] | hd :: tl -> match f hd with | None -> choose f tl | Some x -> x :: choose f tl |
let js_empty_string = Ojs . string_to_js " " |
let sync_attributes dom a1 a2 = let props = function Property ( k , v ) v -> Some ( k , v ) v | Style _ | Handler _ | Attribute _ -> None in let set k v = match k , v with | " value " , String s when s = Element . value dom -> ( ) | _ -> if not ( custom_attribute dom k ) k th... |
let rec dispose : type msg . msg ctrl -> unit = fun ctrl -> match ctrl with | BText _ -> ( ) | BCustom { elt ; _ } _ -> elt . dispose ( ) | BElement { children ; _ } _ -> List . iter dispose children | BMap { child ; _ } _ -> dispose child | BMemo { child ; _ } _... |
let rec sync : type old_msg msg . ctx -> Element . t -> old_msg ctrl -> msg vdom -> msg ctrl = fun ctx parent old vdom -> match old , vdom with | _ when ( vdom : msg vdom ) vdom == ( Obj . magic ( get_vdom old : old_msg vdom ) vdom ) vdom -> ( Obj . magic ( old : old_msg ctrl ) ... |
let sync ctx parent old vdom = try sync ctx parent old vdom with exn -> Printf . printf " Error during vdom sync : % s \ n " %! ( Printexc . to_string exn ) exn ; raise exn |
type ' msg find = | NotFound | Found : { mapper : ( ' inner_msg -> ' msg ) ' msg ; inner : ' inner_msg ctrl ; parent : ' msg find } find -> ' msg find |
let rec found : type inner_msg msg . ( inner_msg -> msg ) msg -> msg find -> inner_msg ctrl -> msg find = fun mapper parent -> function | BElement _ | BText _ | BCustom _ as inner -> Found { mapper ; inner ; parent } parent | BMap { f ; child ; _ } _ -> found ( fun x -> mapper... |
let rec vdom_of_dom : type msg . msg ctrl -> Element . t -> msg find = fun root dom -> match Ojs . option_of_js Element . t_of_js ( Element . t_to_js dom ) dom with | None -> NotFound | Some dom when dom == get_dom root -> found Fun . id NotFound root | Some dom -> begin match vdom_of_dom root (... |
let mouse_event e = { x = Event . client_x e ; y = Event . client_y e ; page_x = Event . page_x e ; page_y = Event . page_y e ; buttons = Event . buttons e ; alt_key = Event . alt_key e ; ctrl_key = Event . ctrl_key e ; shift_key = Event . shift_key e ; } |
let key_event e = { which = Event . which e ; alt_key = Event . alt_key e ; ctrl_key = Event . ctrl_key e ; shift_key = Event . shift_key e ; } |
type ( ' model , ' msg ) ' msg app = { dom : Js_browser . Element . t ; process : ( ' msg -> unit ) unit ; get : ( unit -> ' model ) ' model ; after_redraw : ( unit -> unit ) unit -> unit ; dispose : ( unit -> unit ) unit ; } |
let dom x = x . dom |
let process x = x . process |
let get x = x . get ( ) |
let after_redraw x = x . after_redraw |
type env = { cmds : Cmd . handler list ; customs : Custom . handler list ; } |
let empty = { cmds = [ ] ; customs = [ ] } |
let cmd h = { empty with cmds = [ h ] h } h |
let custom h = { empty with customs = [ h ] h } h |
let merge envs = { cmds = List . concat ( List . map ( fun e -> e . cmds ) cmds envs ) envs ; customs = List . concat ( List . map ( fun e -> e . customs ) customs envs ) envs ; } |
let global = ref empty |
let register e = global := merge [ e ; ! global ] global |
let run ( type msg model ) model ( ? env = empty ) empty ? container ( { init = ( model0 , cmd0 ) cmd0 ; update ; view } view : ( model , msg ) msg Vdom . app ) app = let env = merge [ env ; ! global ] global in let container_created , container = match container with ... |
let dispose { dispose ; _ } _ = dispose ( ) |
let hello_world : Vdom . Node . t = Vdom . Node . text " hello world " ! |
let ( ) = Util . run_vdom hello_world ~ id " : hello_world " |
let bulleted_list : Vdom . Node . t = let open Vdom . Node in div [ h3 [ text " Norwegian Pancakes " ] ; ul [ li [ text " 3 eggs " ] ; li [ text " 2 cups of milk " ] ; li [ text " 1 cup of flour " ] ] ] ; ; |
let ( ) = Util . run_vdom bulleted_list ~ id " : bulleted_list " |
let alert s = Js_of_ocaml . Dom_html . window ## alert ( Js_of_ocaml . Js . string s ) |
let input_placeholder : Vdom . Node . t = Vdom . Node . input ~ attr ( : Vdom . Attr . placeholder " placeholder text here " ) [ ] ; ; |
let ( ) = Util . run_vdom input_placeholder ~ id " : input_placeholder " |
let css_gen : Vdom . Node . t = Vdom . Node . span ~ attr ( : Vdom . Attr . style ( Css_gen . color ( ` Name " red " ) ) ) [ Vdom . Node . text " this text is red " ] ; ; |
let ( ) = Util . run_vdom css_gen ~ id " : css_gen " |
type mouse_event = Js_of_ocaml . Dom_html . mouseEvent Js_of_ocaml . Js . t |
let clicky : Vdom . Node . t = Vdom . Node . button ~ attr : ( Vdom . Attr . on_click ( fun ( _evt : mouse_event ) -> alert " hello there " ; ! Ui_effect . Ignore ) ) [ Vdom . Node . text " click me " ! ] ; ; |
let ( ) = Util . run_vdom clicky ~ id " : clicky_button " |
let contains ~ pattern s = let rec loop i = if i < 0 then false else String . sub s i ( String . length pattern ) pattern = pattern || loop ( i - 1 ) 1 in loop ( String . length s - String . length pattern ) pattern |
module SelectionList = struct open Vdom type ' a model = { list : ' a list ; cursor : int ; filter : string ; } type msg = [ ` Cursor of int | ` Filter of string | ` Nop ] Nop let view show msg select { list ; cursor ; filter } filter = let keep = contains ~ pattern : filter in... |
module Initializable = struct type ' a model = | Initializing | Ready of ' a type ( ' a , ' msg ) ' msg msg = [ ` Got of ' a | ` Internal of ' msg ] ' msg let internal msg = ` Internal msg let got x = ` Got x let update f model msg = match model , msg with | Initializing , ` Go... |
let [ @ inline ] min ( x : int ) y = if x < y then x else y ' a array -> int -> ' a array -> int -> int -> unit = " caml_array_blit " |
module Make ( Resize : Vec_gen . ResizeType ) = struct type elt = Resize . t let null = Resize . null |
let unsafe_blit = Bs_hash_stubs . int_unsafe_blit # endif |
type t = { mutable arr : elt array ; mutable len : int ; } |
let length d = d . len |
let compact d = let d_arr = d . arr in if d . len <> Array . length d_arr then begin let newarr = unsafe_sub d_arr 0 d . len in d . arr <- newarr end |
let singleton v = { len = 1 ; arr = [ | v ] | } |
let empty ( ) = { len = 0 ; arr = [ ] ; || } |
let is_empty d = d . len = 0 |
let reset d = d . len <- 0 ; d . arr <- [ ] || |
let to_list d = let rec loop ( d_arr : elt array ) idx accum = if idx < 0 then accum else loop d_arr ( idx - 1 ) ( Array . unsafe_get d_arr idx :: accum ) in loop d . arr ( d . len - 1 ) [ ] |
let of_list lst = let arr = Array . of_list lst in { arr ; len = Array . length arr } |
let to_array d = unsafe_sub d . arr 0 d . len |
let of_array src = { len = Array . length src ; arr = Array . copy src ; } |
let of_sub_array arr off len = { len = len ; arr = Array . sub arr off len } |
let unsafe_internal_array v = v . arr |
let copy src = let len = src . len in { len ; arr = unsafe_sub src . arr 0 len ; } |
let reverse_in_place src = Ext_array . reverse_range src . arr 0 src . len |
let sub ( src : t ) start len = let src_len = src . len in if len < 0 || start > src_len - len then invalid_arg " Vec . sub " else { len ; arr = unsafe_sub src . arr start len } |
let iter d f = let arr = d . arr in for i = 0 to d . len - 1 do f ( Array . unsafe_get arr i ) done |
let iteri d f = let arr = d . arr in for i = 0 to d . len - 1 do f i ( Array . unsafe_get arr i ) done |
let iter_range d ~ from ~ to_ f = if from < 0 || to_ >= d . len then invalid_arg " Vec . iter_range " else let d_arr = d . arr in for i = from to to_ do f ( Array . unsafe_get d_arr i ) done |
let iteri_range d ~ from ~ to_ f = if from < 0 || to_ >= d . len then invalid_arg " Vec . iteri_range " else let d_arr = d . arr in for i = from to to_ do f i ( Array . unsafe_get d_arr i ) done |
let map_into_array f src = let src_len = src . len in let src_arr = src . arr in if src_len = 0 then [ ] || else let first_one = f ( Array . unsafe_get src_arr 0 ) in let arr = Array . make src_len first_one in for i = 1 to src_len - 1 do Array . unsafe_set arr i ( f ( Array . unsaf... |
let map_into_list f src = let src_len = src . len in let src_arr = src . arr in if src_len = 0 then [ ] else let acc = ref [ ] in for i = src_len - 1 downto 0 do acc := f ( Array . unsafe_get src_arr i ) :: ! acc done ; ! acc |
let mapi f src = let len = src . len in if len = 0 then { len ; arr = [ | ] | } else let src_arr = src . arr in let arr = Array . make len ( Array . unsafe_get src_arr 0 ) in for i = 1 to len - 1 do Array . unsafe_set arr i ( f i ( Array . unsafe_get src_arr i ) ) done ; ... |
let fold_left f x a = let rec loop a_len ( a_arr : elt array ) idx x = if idx >= a_len then x else loop a_len a_arr ( idx + 1 ) ( f x ( Array . unsafe_get a_arr idx ) ) in loop a . len a . arr 0 x |
let fold_right f a x = let rec loop ( a_arr : elt array ) idx x = if idx < 0 then x else loop a_arr ( idx - 1 ) ( f ( Array . unsafe_get a_arr idx ) x ) in loop a . arr ( a . len - 1 ) x |
let filter f d = let new_d = copy d in let new_d_arr = new_d . arr in let d_arr = d . arr in let p = ref 0 in for i = 0 to d . len - 1 do let x = Array . unsafe_get d_arr i in if f x then begin Array . unsafe_set new_d_arr ! p x ; incr p ; end ; done ; new_d . len <- ! p ; new_d |
let equal eq x y : bool = if x . len <> y . len then false else let rec aux x_arr y_arr i = if i < 0 then true else if eq ( Array . unsafe_get x_arr i ) ( Array . unsafe_get y_arr i ) then aux x_arr y_arr ( i - 1 ) else false in aux x . arr y . arr ( x . len - 1 ) |
let get d i = if i < 0 || i >= d . len then invalid_arg " Vec . get " else Array . unsafe_get d . arr i |
let unsafe_get d i = Array . unsafe_get d . arr i |
let last d = if d . len <= 0 then invalid_arg " Vec . last " else Array . unsafe_get d . arr ( d . len - 1 ) |
let capacity d = Array . length d . arr |
let exists p d = let a = d . arr in let n = d . len in let rec loop i = if i = n then false else if p ( Array . unsafe_get a i ) then true else loop ( succ i ) in loop 0 |
let map f src = let src_len = src . len in if src_len = 0 then { len = 0 ; arr = [ ] } || ] } ) * else let src_arr = src . arr in let first = f ( Array . unsafe_get src_arr 0 ) in let arr = Array . make src_len first in for i = 1 to src_len - 1 do Array . unsafe_set arr i ... |
let init len f = if len < 0 then invalid_arg " Vec . init " else if len = 0 then { len = 0 ; arr = [ ] || } else let first = f 0 in let arr = Array . make len first in for i = 1 to len - 1 do Array . unsafe_set arr i ( f i ) done ; { len ; arr } let make initsize : t = ... |
type ( ' a , ' - p ) t = { mutable growth_rate : float ; mutable length : int ; mutable data : ' a array } |
let make ? growth_rate ( : gr = default_growth_rate ) ? capacity ( : c = 0 ) ( ) = if gr <= 1 . then raise ( Invalid_argument " growth_rate <= 1 " ) else if c < 0 then raise ( Invalid_argument " capacity < 0 " ) else { growth_rate = gr ; length = 0 ; data = array_... |
let as_read_only v = ( v :> ( ' a , [ ` R ] ) t ) |
let as_write_only v = ( v :> ( ' a , [ ` W ] ) t ) |
let set_growth_rate gr v = if gr <= 1 . then raise ( Invalid_argument " growth_rate <= 1 " ) else v . growth_rate <- gr |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.