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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
open Brr
open Note
module Prop
: sig
type ('a, 'b) prop
val prop
: string -> ('a, 'b) prop
val get
: 'a -> ('a, 'b) prop -> 'b option
val set
: 'a -> ('a, 'b) prop -> 'b -> unit
end
= struct
type ('a, 'b) prop = Jv.prop'
let prop
: string -> ('a, 'b) prop
= Jstr.of_string
let get
: 'a -> ('a, 'b) prop -> 'b option
= fun obj prop ->
Jv.get' (Jv.Id.to_jv obj) prop
|> Jv.to_option Jv.Id.of_jv
let set
: 'a -> ('a, 'b) prop -> 'b -> unit
= fun obj prop value ->
Jv.set'
(Jv.Id.to_jv obj)
prop
(Jv.Id.to_jv value)
end
module Quill = struct
type t = Jv.t
type options
let bounds
: (options, El.t) Prop.prop
= Prop.prop "bounds"
let debug
: (options, Jstr.t) Prop.prop
= Prop.prop "debug"
let placeholder
: (options, Jstr.t) Prop.prop
= Prop.prop "placeholder"
let readonly
: (options, Jstr.t) Prop.prop
= Prop.prop "readonly"
let theme
: (options, Jstr.t) Prop.prop
= Prop.prop "theme"
let scrollingContainer
: (options, El.t) Prop.prop
= Prop.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
(* Operations is an array *)
type operations = Jv.t
let ops
: (delta, operations) Prop.prop
= Prop.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 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|]
end
let storage_key = (Jstr.v "content")
let save_contents
: Quill.t -> unit
= fun editor ->
let storage = Brr_io.Storage.local G.window in
let contents = Quill.get_contents editor in
Brr_io.Storage.set_item
storage
storage_key
(Json.encode @@ Jv.Id.of_jv @@ contents)
|> Console.log_if_error ~use:()
let load_contents
: Quill.t -> unit
= fun editor ->
let storage = Brr_io.Storage.local G.window in
Brr_io.Storage.get_item
storage
storage_key
|> Option.iter (fun contents ->
Json.decode contents
|> Result.map (fun json ->
Quill.set_contents editor json
)
|> Console.log_if_error ~use:()
)
let page_main id =
begin match (Jv.is_none id) with
| true -> Console.(error [str "No element with id '%s' found"; id])
| false ->
let options = Quill.options () in
Prop.set options Quill.placeholder (Jstr.v "Nouvelle note…");
Prop.set options Quill.theme (Jstr.v "snow");
(* Create the editor with the configuration *)
Quill.quill ~options (Jv.Id.of_jv id)
|> Result.iter (fun editor ->
load_contents editor;
let () = Quill.on_text_change editor (fun delta old source ->
let _ = delta
and _ = old
and _ = source
in
()
)
in
(* Attach an event on focus out *)
let out_event = Brr_note.Evr.on_el
(Ev.focusout)
(fun _ -> save_contents editor)
(Jv.Id.of_jv id) in
(* Prevent the event to be garbage collected *)
E.log out_event (fun _ -> ())
|> Option.iter Logr.hold
)
end
let () =
let open Jv in
let editor = obj
[| "attach", (repr page_main)
|] in
set global "editor" editor
|