package slipshow

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file previewer.ml

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
open Brr

module Msg = struct
  type msg = Communication.t

  let of_jv m : msg option = m |> Jv.to_string |> Communication.of_string
end

type previewer = {
  mutable stage : int;
  mutable index : int;
  panels : Brr.El.t array;
  errors_el : Brr.El.t;
  preview_status : Brr.El.t;
  notifications_el : Brr.El.t;
  ids : string * string;
  include_speaker_view : bool;
  mutable should_refresh : bool;
}

let send_message panel payload =
  let content_window w =
    Jv.get (Brr.El.to_jv w) "contentWindow" |> Window.of_jv
  in
  let window = content_window panel in
  let msg =
    (* Currently, the ID does not matter... *)
    { payload; id = "TODO" } |> Communication.to_string |> Jv.of_string
  in
  Window.post_message window ~msg

let send_open_speaker_view panel =
  let payload = Communication.Open_speaker_notes in
  send_message panel payload

let send_can_save panel =
  let payload = Communication.Can_save in
  send_message panel payload

let send_next panel =
  let payload = Communication.Next in
  send_message panel payload

let send_previous panel =
  let payload = Communication.Previous in
  send_message panel payload

let () = Random.self_init ()

let css =
  {|
.right-panel1.active_panel, .right-panel2.active_panel {
  z-index: 1;
}
.right-panel1, .right-panel2 {
    z-index: 0;
    width:100%;
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    border:0;
    height:100%
}
.preview-status-elem {
    position: absolute;
    top: 20px;
    right: 132px;
    width: 50px;
    height: 50px;
    z-index: 10;
    display: block;

    border: 5px solid rgba(150, 150, 150);
    border-top: 5px solid #3498db;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

/* Define the rotation */
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Hide when active */
.preview-status-elem.preview-status {
    display: none;
}
.slipshow-notification-elem {
    position:absolute;
    top:0;
    right:0;
    z-index: 1000;
}
.slipshow-notification-elem > div {
    border: 2px solid black;
    border-radius: 10px;
    padding: 30px;
    margin: 10px;
    background-color: white;
}
|}

let preview_status_class = Jstr.v "preview-status"

let create_previewer ?(initial_stage = 0) ?(callback = fun _ -> ())
    ?(save_drawing = fun ~path:_ ~content:_ -> ()) ~include_speaker_view
    ~errors_el ~steal_focus ~can_save root =
  let ( !! ) = Jstr.v in
  let name1 = Random.int 1000000 |> string_of_int |> fun s -> "id" ^ s in
  let name2 = Random.int 1000000 |> string_of_int |> fun s -> "id" ^ s in
  let ids = [| name1; name2 |] in
  let panel1 =
    El.iframe ~at:[ At.name !!name1; At.class' !!"right-panel1" ] []
  in
  let panel2 =
    El.iframe ~at:[ At.name !!name2; At.class' !!"right-panel2" ] []
  in
  let preview_status =
    El.div ~at:[ At.class' !!"preview-status-elem preview-status" ] []
  in
  let notifications_el =
    El.div ~at:[ At.class' !!"slipshow-notification-elem" ] []
  in
  let css = El.style [ El.txt' css ] in
  let () =
    El.append_children root
      [ panel1; panel2; css; preview_status; notifications_el ]
  in
  let panels = [| panel1; panel2 |] in
  let is_speaker_view_open = ref false in
  let p =
    {
      stage = initial_stage;
      index = 0;
      panels;
      ids = (name1, name2);
      include_speaker_view;
      errors_el;
      preview_status;
      notifications_el;
      should_refresh = true;
    }
  in
  let _ =
    Ev.listen Brr_io.Message.Ev.message
      (fun event ->
        let ( let> ) x f = Option.iter f x in
        let> source = Brr_io.Message.Ev.source (Ev.as_type event) in
        let source_name = Jv.get source "name" |> Jv.to_string in
        let raw_data : Jv.t = Brr_io.Message.Ev.data (Ev.as_type event) in
        let msg = Msg.of_jv raw_data in
        let from_current = String.equal source_name ids.(p.index)
        and from_other = String.equal source_name ids.(1 - p.index) in
        match msg with
        | Some { payload = State (new_stage, _mode); id = _ } when from_current
          ->
            callback new_stage;
            p.stage <- new_stage
        | Some { payload = Open_recording_panel; id = _ } when from_current ->
            p.should_refresh <- false
        | Some { payload = Close_recording_panel; id = _ } when from_current ->
            p.should_refresh <- true
        | Some { payload = Open_speaker_notes; id = _ } when from_current ->
            is_speaker_view_open := true
        | Some { payload = Close_speaker_notes; id = _ } when from_current ->
            is_speaker_view_open := false
        | Some { payload = Ready; id = _ } when from_current ->
            if can_save then send_can_save panels.(p.index)
        | Some { payload = Ready; id = _ } when from_other ->
            Jv.set (El.to_jv panels.(p.index)) "srcdoc" (Jv.of_string "");
            let () = El.set_class preview_status_class true preview_status in
            if can_save then send_can_save panels.(1 - p.index);
            if !is_speaker_view_open then
              send_open_speaker_view panels.(1 - p.index);
            p.index <- 1 - p.index;
            El.set_class (Jstr.v "active_panel") true panels.(p.index);
            let () =
              if steal_focus then
                let contentDocument el =
                  Jv.get (El.to_jv el) "contentDocument" |> Document.of_jv
                in
                (* Depending on whether a speaker view is possible, the focus
                   target is not accessible the same way *)
                let focus_target =
                  let d = contentDocument panels.(p.index) in
                  match
                    Document.find_el_by_id d
                      (Jstr.v "slipshow__internal_iframe")
                  with
                  | Some iframe -> iframe
                  | None -> panels.(p.index)
                in
                El.set_has_focus true focus_target
            in
            El.set_class (Jstr.v "active_panel") false panels.(1 - p.index)
        | Some { payload = Save_drawing (path, content); id = _ }
          when from_current ->
            save_drawing ~path ~content
        | _ -> ())
      (Window.as_target G.window)
  in
  p

let set_errors errors_el warnings =
  let innerhtml el v =
    let _ = Jv.set (El.to_jv el) "innerHTML" (Jv.of_string v) in
    ()
  in
  innerhtml errors_el warnings

let set_srcdoc { index; panels; errors_el; preview_status; _ }
    (slipshow, warnings) =
  set_errors errors_el warnings;
  try Jv.set (El.to_jv panels.(1 - index)) "srcdoc" (Jv.of_string slipshow)
  with exn ->
    El.set_class preview_status_class true preview_status;
    Console.(log [ "exception"; Printexc.to_string exn ])

let notify (previewer : previewer) notif =
  let el = previewer.notifications_el in
  let open Brr in
  let n = El.div [ El.txt' notif ] in
  El.append_children el [ n ];
  let _cancel = G.set_timeout ~ms:8000 @@ fun () -> El.remove n in
  ()

let guard_open_recording_panel previewer f =
  if previewer.should_refresh then f ()
  else
    notify previewer
      "Refresh of the preview is disabled when the drawing record panel is open"

let preview ?options ?slipshow_js previewer source =
  guard_open_recording_panel previewer @@ fun () ->
  let () = El.set_class preview_status_class false previewer.preview_status in
  let starting_state = previewer.stage in
  let has_speaker_view = previewer.include_speaker_view in
  let this_file = Fpath.v "-" in
  let directory = Fpath.v "./" in
  let read_file f =
    if Fpath.equal this_file f then Ok (Some source) else Ok None
  in
  let slipshow, warnings =
    Slipshow.convert ~directory ~has_speaker_view ?slipshow_js ?options
      ~read_file ~autofocus:false ~starting_state this_file
  in
  let warnings =
    List.map
      (Format.asprintf "%a@.@."
         (Grace_ansi_renderer.pp_diagnostic ?config:None
            ~code_to_string:Diagnosis.to_code))
      warnings
    |> List.map (Ansi.process (Ansi.create ()))
    |> String.concat ""
  in
  set_srcdoc previewer (slipshow, warnings)

let preview_compiled previewer (delayed, warnings) =
  guard_open_recording_panel previewer @@ fun () ->
  let () = El.set_class preview_status_class false previewer.preview_status in
  let starting_state = Some previewer.stage in
  let slipshow = Slipshow.add_starting_state delayed starting_state in
  set_srcdoc previewer (slipshow, warnings)

let ids { ids; _ } = ids

let next (previewer : previewer) =
  let current_window = previewer.panels.(previewer.index) in
  send_next current_window

let previous (previewer : previewer) =
  let current_window = previewer.panels.(previewer.index) in
  send_previous current_window