From d17d17261faccb3eb42e91f88ca035e5b1730c66 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sun, 31 Jan 2021 04:21:01 +0100 Subject: Bindings to prosemirror --- editor/quill.ml | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 editor/quill.ml (limited to 'editor/quill.ml') diff --git a/editor/quill.ml b/editor/quill.ml new file mode 100755 index 0000000..8069d90 --- /dev/null +++ b/editor/quill.ml @@ -0,0 +1,101 @@ +open Brr + +type t = Jv.t + +type options + +let bounds + : (options, El.t) J.prop + = J.prop "bounds" + +let debug + : (options, Jstr.t) J.prop + = J.prop "debug" + +let placeholder + : (options, Jstr.t) J.prop + = J.prop "placeholder" + +let readonly + : (options, Jstr.t) J.prop + = J.prop "readonly" + +let theme + : (options, Jstr.t) J.prop + = J.prop "theme" + +let scrollingContainer + : (options, El.t) J.prop + = J.prop "scrollingContainer" + +let options + : unit -> options + = Jv.Id.of_jv @@ Jv.obj' [||] + +(** Constructor. + + [quill element] will create the editor inside the given element + +*) +let quill + : ?options:options -> El.t -> (t, Jv.Error.t) Result.t + = fun ?options element -> + let quill = Jv.get Jv.global "Quill" in + + let options = Jv.of_option ~none:Jv.undefined Jv.Id.to_jv options in + + match Jv.new' quill Jv.Id.[| to_jv element; options |] with + | exception Jv.Error e -> Error e + | v -> Ok v + + +type delta = Jv.t + +let delta_to_json + : delta -> Brr.Json.t + = Jv.Id.to_jv + +let delta_of_json + : Brr.Json.t -> delta + = Jv.Id.of_jv + +(* Operations is an array *) +type operations = Jv.t + +let ops + : (delta, operations) J.prop + = J.prop "ops" + + +(** Return the editor content *) +let get_contents + : t -> delta + = fun t -> + Jv.call t "getContents" [||] + +let set_contents + : t -> delta -> unit + = fun t contents -> + ignore @@ Jv.call t "setContents" [|contents|] + +(** [extract_content t index length] return the content starting from index, + with length elements *) +let extract_contents + : t -> int -> int -> delta + = fun t index length -> + Jv.call t "getContents" [|Jv.of_int index; Jv.of_int length|] + +let on_text_change + : t -> (string -> string -> string -> unit) -> unit + = fun t callback -> + ignore @@ Jv.call t "on" [|Jv.Id.to_jv @@ Jstr.v "text-change" ; Jv.repr callback|] + +(* [update_contents t delta] replace the content with the commands given + by delta. +*) +let update_contents + : t -> delta -> delta + = fun t delta -> + Jv.call t "updateContents" [|delta|] + + -- cgit v1.2.3