aboutsummaryrefslogtreecommitdiff
path: root/editor/quill.ml
blob: 8069d9023fd46851cec36b84c3a5c20e2fc95c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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|]