this repo has no description

add attribute run-on=click|load (#12)

authored by

Cuihtlauac Alvarado and committed by
GitHub
020c7bbb 7030c88a

+28 -7
+7 -5
src/cell.ml
··· 10 10 cm : Editor.t; 11 11 worker : Client.t; 12 12 merlin_worker : Merlin_ext.Client.worker; 13 + run_on : [ `Click | `Load ]; 13 14 } 14 15 15 16 let id t = t.id ··· 69 70 match prev with 70 71 | None -> 71 72 Editor.set_previous_lines t.cm 0; 72 - refresh_lines_from ~editor:t; 73 - run t 73 + refresh_lines_from ~editor:t 74 74 | Some p -> 75 75 assert (p.next = None); 76 76 p.next <- Some t; 77 - refresh_lines_from ~editor:p; 78 - run t 77 + refresh_lines_from ~editor:p 79 78 80 79 let set_source_from_html editor this = 81 80 let doc = Webcomponent.text_content this in ··· 113 112 (); 114 113 ] 115 114 116 - let init ~id ?extra_style ?inline_style worker this = 115 + let init ~id ~run_on ?extra_style ?inline_style worker this = 117 116 let shadow = Webcomponent.attach_shadow this in 118 117 init_css shadow ~extra_style ~inline_style; 119 118 ··· 134 133 next = None; 135 134 worker; 136 135 merlin_worker; 136 + run_on; 137 137 } 138 138 in 139 139 Editor.on_change cm (fun () -> invalidate_after ~editor); ··· 186 186 let receive_merlin t msg = 187 187 Merlin_ext.Client.on_message t.merlin_worker 188 188 (Merlin_ext.fix_answer ~pre:(pre_source t) ~doc:(Editor.source t.cm) msg) 189 + 190 + let loadable t = t.run_on = `Load
+3
src/cell.mli
··· 2 2 3 3 val init : 4 4 id:int -> 5 + run_on:[ `Click | `Load ] -> 5 6 ?extra_style:Jstr.t -> 6 7 ?inline_style:Jstr.t -> 7 8 Client.t -> ··· 14 15 val completed_run : t -> X_protocol.output list -> unit 15 16 val set_prev : prev:t option -> t -> unit 16 17 val receive_merlin : t -> Protocol.answer -> unit 18 + val loadable : t -> bool 19 + val run : t -> unit
+6 -1
src/webcomponent.ml
··· 18 18 Jv.set test "prototype" (Jv.get html_element "prototype"); 19 19 Jv.set Jv.global "__xocaml_exported" (Jv.callback ~arity:1 fn); 20 20 Jv.set (Jv.get test "prototype") "connectedCallback" 21 - (jv_pure_js_expr "(function() { return __xocaml_exported(this) })"); 21 + (jv_pure_js_expr 22 + "(function() { setTimeout(() => __xocaml_exported(this), 0) })"); 22 23 let _ : Jv.t = Jv.call custom_elements "define" [| Jv.of_jstr name; test |] in 23 24 () 24 25 25 26 let text_content t = Jstr.to_string @@ Jv.to_jstr @@ Jv.get t "textContent" 26 27 let as_target t = Brr.El.of_jv t 28 + 29 + let get_attribute t name = 30 + let attr = Jv.call t "getAttribute" [| Jv.of_string name |] in 31 + Jv.to_option Jv.to_string attr 27 32 28 33 let attach_shadow t = 29 34 Brr.El.of_jv
+1
src/webcomponent.mli
··· 2 2 3 3 val define : Jstr.t -> (t -> unit) -> unit 4 4 val text_content : t -> string 5 + val get_attribute : t -> string -> string option 5 6 val as_target : t -> Brr.El.t 6 7 val attach_shadow : t -> Brr.El.t
+11 -1
src/x_ocaml.ml
··· 39 39 40 40 let extra_style = current_attribute "src-style" 41 41 let inline_style = current_attribute "inline-style" 42 + let run_on = current_attribute "run-on" |> Option.map Jstr.to_string 43 + let run_on_of_string = function "click" -> `Click | "load" | _ -> `Load 42 44 43 45 let _ = 44 46 Webcomponent.define elt_name @@ fun this -> 45 47 let prev = match !all with [] -> None | e :: _ -> Some e in 48 + let run_on = 49 + run_on_of_string 50 + @@ 51 + match Webcomponent.get_attribute this "run-on" with 52 + | Some s -> s 53 + | None -> Option.value ~default:"load" run_on 54 + in 46 55 let id = List.length !all in 47 - let editor = Cell.init ~id ?extra_style ?inline_style worker this in 56 + let editor = Cell.init ~id ~run_on ?extra_style ?inline_style worker this in 48 57 all := editor :: !all; 49 58 Cell.set_prev ~prev editor; 59 + if List.for_all Cell.loadable !all then Cell.run editor; 50 60 ()