aboutsummaryrefslogtreecommitdiff
path: root/editor/editor.ml
blob: c3cad1e601c11944b58f53c7900bc8f28b51e090 (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
102
103
104
105
106
107
108
open StdLabels
open Brr
module PM = Prosemirror
module Js = Js_of_ocaml.Js

let populate_menu () =
  match Blog.Sidebar.get () with
  | None -> ()
  | Some element ->
    let () = Blog.Sidebar.clean element in
    let uri = Brr.Window.location Brr.G.window in

    let pages =

      List.map (Storage.get_ids ())
        ~f:(fun name ->
            let target =
              Jstr.( (Brr.Uri.path uri)
                     + (Jstr.v "?page=")
                     + name) in
            El.li
              [ El.a
                  ~at:[At.href target]
                  [ El.txt name ] ]
          ) in

    let childs =
      [ El.button
          ~at:At.[class' (Jstr.v "action-button")]
          [ El.i
              []
              ~at:At.[ class' (Jstr.v "fa")
                     ; class' (Jstr.v "fa-2x")
                     ; class' (Jstr.v "fa-times-circle")
                     ]
          ]
      ; El.hr ()
      ; El.ul
          pages
      ] in

    El.append_children element childs


let prosemirror id content =
  begin match (Jv.is_none id), (Jv.is_none content) with
    | false, false ->

      let module PM = Prosemirror in
      let pm = PM.v () in

      let schema = (PM.SchemaBasic.schema pm) in
      let schema = Footnotes.footnote_schema pm schema in

      let specs = PM.Model.schema_spec
          (PM.SchemaList.add_list_nodes
             pm
             (schema##.spec##.nodes)
             (Jstr.v "paragraph block*")
             (Some (Jstr.v "block")))
          (Some schema##.spec##.marks)
          None in
      let mySchema = PM.Model.schema pm specs in

      populate_menu ();

      (* Create the initial state *)
      let state = Storage.load pm mySchema (Jv.Id.of_jv content) Storage.page_id in

      let props = PM.View.direct_editor_props () in
      props##.state := state;

      (* Each time the state is update, handle the copy *)
      props##.dispatchTransaction := Js.wrap_meth_callback @@ (fun view tr ->
          let state = view##.state##apply tr in
          view##updateState state
        );

      let view' = (Footnotes.footnote_view pm) in

      let nodes = PM.O.init
          [| ("footnote", view')  |] in
      props##.nodeViews := nodes;
      let view = PM.View.editor_view
          pm
          (Jv.Id.of_jv id)
          props in

      (* Attach an event on focus out *)
      let _ = Brr_note.Evr.on_el
          (Ev.focusout)
          (fun _ -> Storage.save view Storage.page_id)
          (Jv.Id.of_jv id) in

      ()

    | _, _ -> Console.(error [str "No element with id '%s' '%s' found"; id ; content])

  end

let () =

  let open Jv in
  let editor = obj
      [| "attach_prosemirror", (repr prosemirror)
      |] in

  set global "editor" editor