geochan/src/js/js_post_form.ml
2022-12-31 06:11:41 +01:00

55 lines
1.8 KiB
OCaml

let log = Format.printf
(* called by clicking post_id *)
(* insert emojid into reply form *)
let insert_quote emojid _event =
log "quote@\n";
Option.iter
(fun textarea ->
let content = Jv.to_string @@ Jv.get textarea "value" in
let new_content =
if String.ends_with ~suffix:"\n" content || String.length content = 0
then (* don't skip a line *)
Format.sprintf "%s[%s] " content emojid
else Format.sprintf "%s@\n[%s] " content emojid
in
ignore @@ Jv.set textarea "value" (Jv.of_string new_content) )
Jv.(find global "reply-comment")
let () =
log "add inser_quote event on post links@\n";
let document = Jv.get Jv.global "document" in
let quote_links =
Jv.to_jv_list
@@ Jv.call document "getElementsByClassName" [| Jv.of_string "quote-link" |]
in
log "quote_links leng %d@\n" (List.length quote_links);
let add_click quote_link =
let emojid =
Jv.to_string
@@ Jv.call quote_link "getAttribute" [| Jv.of_string "data-emojid" |]
in
ignore
@@ Jv.call quote_link "addEventListener"
[| Jv.of_string "click"; Jv.repr (insert_quote emojid) |]
in
List.iter add_click quote_links
(* make image description field visible when a file is selected*)
let make_visible el _event =
let el_style = Jv.get el "style" in
ignore @@ Jv.set el_style "display" (Jv.of_string "block")
let () =
log "change image description visibility@\n";
Option.iter
(fun file_input ->
let alt_input = Jv.get Jv.global "alt" in
let alt_label = Jv.get Jv.global "alt-label" in
ignore
@@ Jv.call file_input "addEventListener"
[| Jv.of_string "change"; Jv.repr (make_visible alt_input) |];
ignore
@@ Jv.call file_input "addEventListener"
[| Jv.of_string "change"; Jv.repr (make_visible alt_label) |] )
Jv.(find global "file")