summaryrefslogtreecommitdiff
path: root/editor/prosemirror/bindings.ml
blob: a6a09dcb0d68756aa6ebef5e9dd0cbbb56eb76b0 (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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
open Js_of_ocaml.Js

module TypedObject : sig
  type 'a t

  val get : 'a t -> Jv.prop -> 'a option

  val get' : 'a t -> Jv.prop' -> 'a option

  val set : 'a t -> Jv.prop -> 'a -> unit

  val set' : 'a t -> Jv.prop' -> 'a -> unit

  val create : unit -> 'a t

  val init : (Jv.prop * 'a) array -> 'a t
end = struct
  type 'a t = Jv.t

  let get : 'a t -> Jv.prop -> 'a =
   fun t prop -> Jv.to_option Jv.Id.of_jv (Jv.get t prop)


  let get' : 'a t -> Jv.prop' -> 'a =
   fun t prop -> Jv.to_option Jv.Id.of_jv (Jv.get' t prop)


  let set : 'a t -> Jv.prop -> 'a -> unit =
   fun o prop v -> Jv.set o prop (Jv.Id.to_jv v)


  let set' : 'a t -> Jv.prop' -> 'a -> unit =
   fun o prop v -> Jv.set' o prop (Jv.Id.to_jv v)


  let create : unit -> 'a t = fun () -> Jv.obj [||]

  let init : (Jv.prop * 'a) array -> 'a t =
   fun param -> Jv.obj (Obj.magic param)
end

class type ['a] ordered_map =
  object ('this)
    method get : Jstr.t -> 'a t opt meth

    method update : Jstr.t -> 'a t -> Jstr.t opt -> 'this meth

    method remove : Jstr.t -> 'this meth

    method addToStart : Jstr.t -> 'a t -> 'this t meth

    method addToEnd : Jstr.t -> 'a t -> 'this t meth
  end

module Classes = struct
  type 'a meta_data

  type domOutputSpec

  type parse_rule

  type content_match

  type slice

  class type _node_props =
    object ('this)
      method inlineContent : bool t readonly_prop
      (** True if this node type has inline content. *)

      method isBlock : bool t readonly_prop

      method isText : bool t readonly_prop

      method isInline : bool t readonly_prop

      method isTextblock : bool t readonly_prop

      method isLeaf : bool t readonly_prop

      method isAtom : bool t readonly_prop
    end

  type depth = int opt

  class type mark =
    object ('this)
      method _type : mark_type t readonly_prop

      method attrs : 'a TypedObject.t prop

      method isInSet : mark t js_array t -> bool t meth

      method eq : 'this t -> bool t meth
    end

  and node_spec =
    object ('this)
      method content : Jstr.t opt prop

      method marks : Jstr.t opt prop

      method group : Jstr.t opt prop

      method inline : bool t opt prop

      method atom : bool t opt prop

      method attrs : 'a TypedObject.t prop

      method selectable : bool t opt prop

      method draggable : bool t opt prop

      method code : bool t opt prop

      method defining : bool t opt prop

      method isolating : bool t opt prop

      method toDOM : (node t -> domOutputSpec t) callback prop

      method parseDom : parse_rule t js_array t opt prop
    end

  and resolved_pos =
    object ('this)
      method pos : int readonly_prop

      method depth : int readonly_prop

      method parentOffset : int readonly_prop

      method parent : node t readonly_prop

      method doc : node t readonly_prop

      method node : depth -> node t meth

      method index : depth -> int meth

      method start : depth -> int meth

      method _end : depth -> int meth

      method after : depth -> int meth

      method nodeAfter : node t opt readonly_prop

      method nodeBefore : node t opt readonly_prop

      method marks : unit -> mark t js_array t meth

      method sameParent : 'this t -> bool t meth

      method max : 'this t -> 'this t meth

      method min : 'this t -> 'this t meth
    end

  and mark_spec =
    object ('this)
      method toDOM : (node t -> domOutputSpec t) callback prop

      method inclusive : bool t prop

      method spanning : bool t prop
    end

  and schema_spec =
    object ('this)
      method nodes : node_spec ordered_map t readonly_prop

      method marks : mark_spec ordered_map t readonly_prop

      method topNode : Jstr.t opt readonly_prop
    end

  and schema =
    object ('this)
      method spec : schema_spec t prop

      method nodes : node_type t TypedObject.t readonly_prop

      method marks : mark_type t TypedObject.t readonly_prop

      method topNodeType : node_type t readonly_prop

      method text : Jstr.t -> mark t js_array t opt -> node t meth

      method node :
           Jstr.t
        -> < .. > t opt
        -> fragment t opt
        -> mark t js_array t opt
        -> node t meth
      (** [node t attrs fragment ] Will create a node with the type [t] and
        attributes [attrs]. The content will always be a fragment.

        You can create a fragment from an array on node with the function
        [Model.Fragment.from_array]
    *)

      method mark_fromType : mark_type t -> 'a TypedObject.t opt -> mark t meth
    end

  and node_type =
    object ('this)
      inherit _node_props

      method name : Jstr.t readonly_prop

      method schema : schema t readonly_prop

      method spec : node_spec t readonly_prop

      method contentMatch : content_match t readonly_prop

      method hasRequiredAttrs : unit -> bool t meth

      method create_withFragmentContent :
        < .. > t opt -> fragment t opt -> mark t opt -> node t meth
    end

  (** Signature for MarkType class

      https://prosemirror.net/docs/ref/#model.MarkType
  *)
  and mark_type =
    object ('this)
      method name : Jstr.t readonly_prop

      method schema : schema t readonly_prop

      method spec : mark_spec t readonly_prop

      method isInSet : mark t js_array t -> mark t opt meth
    end

  (** Common signature between fragment and node *)
  and _element =
    object ('this)
      method childCount : int readonly_prop
      (** The number of children that the node has. *)

      method child : int -> node t meth
      (**  Get the child node at the given index. Raise an error when the index
         is out of range. *)

      method maybeChild : int -> node t opt meth
      (** Get the child node at the given index, if it exists. *)

      method eq : 'this t -> bool t meth
      (** Compare this element to another one. *)

      method cut : int -> int opt -> 'this t meth
      (** Cut out the element between the two given positions. *)

      method toString : unit -> Jstr.t meth
      (** Return a debugging string that describes this element. *)

      method descendants :
        (node t -> pos:int -> node t -> bool t) callback -> unit meth

      method forEach :
        (node t -> offset:int -> index:int -> unit) callback -> unit meth
      (** Call [f] for every child node, passing the node, its offset into
          this parent node, and its index. *)
    end

  and fragment =
    object ('this)
      inherit _element

      method size : int readonly_prop
      (** The size of the fragment, which is the total of the size of its
        content nodes. *)

      method append : 'this t -> 'this t meth

      method lastChild : node t opt readonly_prop

      method firstChild : node t opt readonly_prop

      method findDiffStart : 'this t -> int opt meth

      method findDiffEnd : 'this t -> < a : int prop ; b : int prop > t opt meth
    end

  (** https://prosemirror.net/docs/ref/#model.Node *)
  and node =
    object ('this)
      inherit _element

      inherit _node_props

      method _type : node_type t readonly_prop

      method attrs : < .. > t prop

      method content : fragment t prop

      method copy : fragment t -> 'this t meth

      method slice : from:int -> to_:int opt -> slice t meth

      method resolve : int -> resolved_pos t meth

      method nodeAt : int -> 'this t opt meth

      method marks : mark t js_array t readonly_prop

      method sameMarkup : node t -> bool t meth

      method text : Jstr.t opt prop
    end

  (** View *)

  and editor_props =
    object ('this)
      method editable : (editor_state t -> bool t) callback prop

      method handleDOMEvents :
        (editor_view t -> Jv.t -> bool t) callback TypedObject.t prop

      method handleClickOn :
        (   editor_view t
         -> int t
         -> node t
         -> int
         -> Brr.Ev.Mouse.t Brr.Ev.type'
         -> bool t
         -> bool t )
        callback
        prop

      method nodeViews :
        (node t -> editor_view t -> (unit -> int) -> < .. > t) TypedObject.t
        prop
    end

  and direct_editor_props =
    object ('this)
      inherit editor_props

      method state : editor_state t writeonly_prop

      method dispatchTransaction :
        (editor_view t, transaction t -> unit) meth_callback writeonly_prop
      (** The call back is called with this = instance of editor_view *)
    end

  and editor_view =
    object ('this)
      method state : editor_state t readonly_prop

      method dom : Brr.El.t readonly_prop prop

      method editable : bool t readonly_prop

      method props : direct_editor_props t readonly_prop

      method update : direct_editor_props t -> unit meth

      method setProps : direct_editor_props t -> unit meth

      method updateState : editor_state t -> unit meth

      method hasFocus : unit -> bool t meth

      method focus : unit -> unit meth

      method posAtCoords :
           < left : float prop ; top : float prop > t
        -> < pos : int prop ; inside : int prop > t meth

      method coordsAtPos :
           int
        -> int opt
        -> < left : float prop
           ; right : float prop
           ; top : float prop
           ; bottom : float prop >
           t
           meth

      method domAtPos :
           pos:int
        -> side:int opt
        -> < node : Brr.El.t t prop ; offset : int prop > t meth

      method destroy : unit meth

      method dispatch : transaction t -> unit meth
    end

  (** State *)

  and plugin =
    object ('this)
      method props : editor_props t opt prop

      method view : (editor_view t -> < .. > t) callback opt prop

      method filterTransaction :
        (transaction t -> editor_state t -> bool t) opt prop
    end

  and selection =
    object ('this)
      method from : int readonly_prop

      method _to : int readonly_prop

      method empty : bool t readonly_prop

      method eq : 'this t -> bool t meth

      method content : unit -> slice t meth

      method replace : transaction t -> slice t -> unit meth

      method replaceWith : transaction t -> node t -> unit meth
    end

  and text_selection =
    object ('this)
      inherit selection
    end

  and node_selection =
    object ('this)
      inherit selection
    end

  (* Transform *)
  and mappable = object ('this) end

  and step_map =
    object ('this)
      inherit mappable
    end

  and step =
    object ('this)
      method map : mappable t -> 'this t meth
    end

  and transform =
    object ('this)
      method doc : node t readonly_prop

      method steps : step t js_array t readonly_prop

      method docs : node t js_array t readonly_prop

      method step : step t -> 'this t meth

      method docChanged : bool t prop

      method addMark : from:int -> to_:int -> mark t -> 'this t meth

      method removeMark : from:int -> to_:int -> mark t -> 'this t meth

      method replace : from:int -> to_:int -> slice t opt -> 'this t meth

      method delete : from:int -> to_:int -> 'this t meth

      method insert : pos:int -> node t -> 'this t meth

      method replaceRangeWith : from:int -> to_:int -> node t -> 'this t meth

      method setBlockType :
        from:int -> to_:int -> node_type t -> < .. > t -> 'this t meth
    end

  and transaction =
    object ('this)
      inherit transform

      method time : int readonly_prop

      method setTime : int -> 'this t meth

      method storedMarks : mark t js_array t opt readonly_prop

      method setStoredMarks : mark t js_array t opt -> 'this t meth

      method addStoredMark : mark t -> 'this t meth

      method removeStoredMark_mark : mark t -> 'this t meth

      method removeStoredMark_marktype : mark_type t -> 'this t meth

      method ensureMarks : mark t js_array t -> 'this t meth

      method storedMarksSet : bool readonly_prop

      method selection : selection t readonly_prop

      method setSelection : selection t -> 'this t meth

      method deleteSelection : 'this t meth

      method replaceSelection : slice t -> 'this t meth

      method replaceSelectionWith : node t -> bool t opt -> 'this t meth

      method selectionSet : bool readonly_prop

      method before : node t readonly_prop

      method insertText : Jstr.t -> from:int opt -> to_:int opt -> 'this t meth

      method setMeta : 'a meta_data t -> 'a -> 'this t meth

      method getMeta : 'a meta_data t -> 'a optdef meth

      method scrollIntoView : unit -> 'this t meth
    end

  and configuration_prop =
    object ('this)
      method schema : schema t opt prop

      method plugins : plugin t js_array t opt prop
    end

  and creation_prop =
    object ('this)
      inherit configuration_prop

      method doc : node t opt prop

      method selection : selection t opt prop

      method storedMarks : mark t js_array t opt prop
    end

  and editor_state =
    object ('this)
      method doc : node t readonly_prop

      method selection : selection t readonly_prop

      method storedMarks : mark t js_array t opt readonly_prop

      method schema : schema t readonly_prop

      method plugins : plugin t js_array t readonly_prop

      method apply : transaction t -> 'this t meth

      method applyTransaction :
           transaction t
        -> < state : 'this t prop
           ; transactions : transaction t js_array t prop >
           t
           meth

      method tr : transaction t readonly_prop

      method reconfigure : configuration_prop t meth

      method toJSON : unit -> Brr.Json.t meth
    end
end

module Model = struct
  type parse_rule = Classes.parse_rule

  type domOutputSpec = Classes.domOutputSpec

  type depth = Classes.depth

  class type mark = Classes.mark

  class type fragment = Classes.fragment

  class type node_spec = Classes.node_spec

  class type resolved_pos = Classes.resolved_pos

  class type mark_spec = Classes.mark_spec

  class type schema_spec = Classes.schema_spec

  class type schema = Classes.schema

  class type node_type = Classes.node_type

  class type mark_type = Classes.mark_type

  class type node = Classes.node
end

module Transform = struct
  type step_result

  class type step_map = Classes.step_map

  class type step = Classes.step

  class type replace_step =
    object ('this)
      inherit step
    end

  class type replace_around_step =
    object ('this)
      inherit step
    end

  class type add_mark_step =
    object ('this)
      inherit step
    end

  class type transform = Classes.transform
end

module State = struct
  type 'a meta_data = 'a Classes.meta_data

  class type plugin = Classes.plugin

  class type selection = Classes.selection

  class type text_selection = Classes.text_selection

  class type node_selection = Classes.node_selection

  class type transaction = Classes.transaction

  class type configuration_prop = Classes.configuration_prop

  class type creation_prop = Classes.creation_prop

  class type editor_state = Classes.editor_state

  type dispatch = Classes.transaction t -> unit
end

module View = struct
  class type editor_props = Classes.editor_props

  class type direct_editor_props = Classes.direct_editor_props

  class type editor_view = Classes.editor_view
end

module History = struct
  class type history_prop =
    object ('this)
      method depth : int opt prop

      method newGroupDelay : int opt prop
    end
end

module SchemaBasic = struct
  class type nodes =
    object ('this)
      method doc : Model.node_spec t prop

      method paragraph : Model.node_spec t prop

      method blockquote : Model.node_spec t prop

      method horizontal_rule : Model.node_spec t prop

      method heading : Model.node_spec t prop

      method code_block : Model.node_spec t prop

      method text : Model.node_spec t prop

      method image : Model.node_spec t prop

      method hard_break : Model.node_spec t prop
    end

  class type marks =
    object ('this)
      method link : Model.mark_spec t prop

      method em : Model.mark_spec t prop

      method strong : Model.mark_spec t prop

      method code : Model.mark_spec t prop
    end
end

module Menu = struct
  class type menuElement = object ('this) end

  class type menuItemSpec =
    object ('this)
      method title : Jstr.t opt prop

      method label : Jstr.t opt prop

      method select :
        (menuItem t, State.editor_state t -> bool t) meth_callback prop

      method run :
        ( menuItem t
        ,    State.editor_state t
          -> (State.transaction t -> unit)
          -> View.editor_view t
          -> 'a Brr.Ev.t
          -> unit )
        meth_callback
        prop
    end

  and menuItem =
    object ('this)
      inherit menuElement
    end

  class type dropdown =
    object ('this)
      inherit menuElement

      method content : menuItem t js_array t prop
    end
end

module Example = struct
  class type menuItems =
    object ('this)
      method insertMenu : Menu.dropdown t prop

      method fullMenu : Menu.menuElement t js_array t prop
    end

  class type options =
    object ('this)
      method schema : Model.schema t prop

      method menuBar : bool t opt prop

      method floatingMenu : bool t opt prop

      method history : bool t opt prop

      method menuContent : Menu.menuElement t js_array t prop
    end
end