diff options
author | Sébastien Dailly <sebastien@chimrod.com> | 2021-09-10 20:50:40 +0200 |
---|---|---|
committer | Sébastien Dailly <sebastien@chimrod.com> | 2021-09-10 20:50:40 +0200 |
commit | 040c74ea186f195b8579960d2a74418c38cd9b76 (patch) | |
tree | 2bb882c2dc7c18b39bf27187119534e2bd96d302 /src/js | |
parent | 1ba97f613c25926f4007fda9e38131fbb8961173 (diff) |
Added js code
Diffstat (limited to 'src/js')
-rwxr-xr-x | src/js/dune | 18 | ||||
-rw-r--r-- | src/js/tengwar.ml | 58 |
2 files changed, 76 insertions, 0 deletions
diff --git a/src/js/dune b/src/js/dune new file mode 100755 index 0000000..9387c5f --- /dev/null +++ b/src/js/dune @@ -0,0 +1,18 @@ +(executable + (name tengwar) + (libraries + brr + brr.note + application + translator + sounds + ) + (modes js) + (preprocess (pps js_of_ocaml-ppx)) + (link_flags (:standard -no-check-prims)) + ) + +(rule + (targets tengwar.js) + (deps tengwar.bc.js) + (action (copy %{deps} %{targets}))) diff --git a/src/js/tengwar.ml b/src/js/tengwar.ml new file mode 100644 index 0000000..dd37c2d --- /dev/null +++ b/src/js/tengwar.ml @@ -0,0 +1,58 @@ +open Brr +open Note +open Brr_note + +let get_element_by_id id = + id + |> Jv.Id.of_jv + |> Jv.to_jstr + |> Brr.Document.find_el_by_id Brr.G.document + +let (let=?) : 'a option -> ('a -> unit) -> unit + = fun f opt -> Option.iter opt f + +let main id phon tengwar = + match (Jv.is_none id) with + | true -> Console.(error [str "No element with id '%s' found"; id]) + | false -> + + let=? source = get_element_by_id id in + let=? phon = get_element_by_id phon in + let=? tengwar = get_element_by_id tengwar in + + let ev = Evr.on_el + Ev.input + (fun _ -> + let value = El.prop El.Prop.value source in + let str = Jstr.to_string value in + let transcription = Translator.Reader.process str in + + let res1 = Result.map + (fun t-> Sounds.repr (module Repr.Default) t) + transcription in + let () = match res1 with + | Ok response -> + El.set_prop El.Prop.value (Jstr.of_string response) phon + | Error _err -> () in + let res2 = Result.map + (fun t-> Sounds.repr (module Repr.Tengwar) t) + transcription in + let () = match res2 with + | Ok response -> + El.set_prop El.Prop.value (Jstr.of_string response) tengwar + | Error _err -> () in + () + ) + source in + + match (E.log ev (fun _ -> ())) with + | None -> () + | Some v -> Logr.hold v + +let () = + + let open Jv in + let main = obj + [| "run", (repr main) |] in + + set global "lib" main |