···1010 cm : Editor.t;
1111 worker : Client.t;
1212 merlin_worker : Merlin_ext.Client.worker;
1313+ run_on : [ `Click | `Load ];
1314}
14151516let id t = t.id
···6970 match prev with
7071 | None ->
7172 Editor.set_previous_lines t.cm 0;
7272- refresh_lines_from ~editor:t;
7373- run t
7373+ refresh_lines_from ~editor:t
7474 | Some p ->
7575 assert (p.next = None);
7676 p.next <- Some t;
7777- refresh_lines_from ~editor:p;
7878- run t
7777+ refresh_lines_from ~editor:p
79788079let set_source_from_html editor this =
8180 let doc = Webcomponent.text_content this in
···113112 ();
114113 ]
115114116116-let init ~id ?extra_style ?inline_style worker this =
115115+let init ~id ~run_on ?extra_style ?inline_style worker this =
117116 let shadow = Webcomponent.attach_shadow this in
118117 init_css shadow ~extra_style ~inline_style;
119118···134133 next = None;
135134 worker;
136135 merlin_worker;
136136+ run_on;
137137 }
138138 in
139139 Editor.on_change cm (fun () -> invalidate_after ~editor);
···186186let receive_merlin t msg =
187187 Merlin_ext.Client.on_message t.merlin_worker
188188 (Merlin_ext.fix_answer ~pre:(pre_source t) ~doc:(Editor.source t.cm) msg)
189189+190190+let loadable t = t.run_on = `Load
+3
src/cell.mli
···2233val init :
44 id:int ->
55+ run_on:[ `Click | `Load ] ->
56 ?extra_style:Jstr.t ->
67 ?inline_style:Jstr.t ->
78 Client.t ->
···1415val completed_run : t -> X_protocol.output list -> unit
1516val set_prev : prev:t option -> t -> unit
1617val receive_merlin : t -> Protocol.answer -> unit
1818+val loadable : t -> bool
1919+val run : t -> unit
+6-1
src/webcomponent.ml
···1818 Jv.set test "prototype" (Jv.get html_element "prototype");
1919 Jv.set Jv.global "__xocaml_exported" (Jv.callback ~arity:1 fn);
2020 Jv.set (Jv.get test "prototype") "connectedCallback"
2121- (jv_pure_js_expr "(function() { return __xocaml_exported(this) })");
2121+ (jv_pure_js_expr
2222+ "(function() { setTimeout(() => __xocaml_exported(this), 0) })");
2223 let _ : Jv.t = Jv.call custom_elements "define" [| Jv.of_jstr name; test |] in
2324 ()
24252526let text_content t = Jstr.to_string @@ Jv.to_jstr @@ Jv.get t "textContent"
2627let as_target t = Brr.El.of_jv t
2828+2929+let get_attribute t name =
3030+ let attr = Jv.call t "getAttribute" [| Jv.of_string name |] in
3131+ Jv.to_option Jv.to_string attr
27322833let attach_shadow t =
2934 Brr.El.of_jv
+1
src/webcomponent.mli
···2233val define : Jstr.t -> (t -> unit) -> unit
44val text_content : t -> string
55+val get_attribute : t -> string -> string option
56val as_target : t -> Brr.El.t
67val attach_shadow : t -> Brr.El.t
+11-1
src/x_ocaml.ml
···39394040let extra_style = current_attribute "src-style"
4141let inline_style = current_attribute "inline-style"
4242+let run_on = current_attribute "run-on" |> Option.map Jstr.to_string
4343+let run_on_of_string = function "click" -> `Click | "load" | _ -> `Load
42444345let _ =
4446 Webcomponent.define elt_name @@ fun this ->
4547 let prev = match !all with [] -> None | e :: _ -> Some e in
4848+ let run_on =
4949+ run_on_of_string
5050+ @@
5151+ match Webcomponent.get_attribute this "run-on" with
5252+ | Some s -> s
5353+ | None -> Option.value ~default:"load" run_on
5454+ in
4655 let id = List.length !all in
4747- let editor = Cell.init ~id ?extra_style ?inline_style worker this in
5656+ let editor = Cell.init ~id ~run_on ?extra_style ?inline_style worker this in
4857 all := editor :: !all;
4958 Cell.set_prev ~prev editor;
5959+ if List.for_all Cell.loadable !all then Cell.run editor;
5060 ()