diff options
| author | Sébastien Dailly <sebastien@chimrod.com> | 2021-01-31 04:21:01 +0100 | 
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2022-02-07 16:43:33 +0100 | 
| commit | d17d17261faccb3eb42e91f88ca035e5b1730c66 (patch) | |
| tree | 28424d286bda347aee77528ece79907026b2e35b /editor/prosemirror/prosemirror.ml | |
| parent | 1961a9779b482cf9cbdb3365137c2e74423067c6 (diff) | |
Bindings to prosemirror
Diffstat (limited to 'editor/prosemirror/prosemirror.ml')
| -rwxr-xr-x | editor/prosemirror/prosemirror.ml | 206 | 
1 files changed, 206 insertions, 0 deletions
diff --git a/editor/prosemirror/prosemirror.ml b/editor/prosemirror/prosemirror.ml new file mode 100755 index 0000000..bf72227 --- /dev/null +++ b/editor/prosemirror/prosemirror.ml @@ -0,0 +1,206 @@ +open Js_of_ocaml +open Brr + +type t = Jv.t + +let v +  : unit -> t +  = fun () -> +    Jv.get Jv.global "PM" + +type pm_schema + +type pm_state = Jv.t + +type pm_view = Jv.t + + +let state +  : (t, pm_state) J.prop +  = J.prop "state" + +let view +  : (t, pm_view) J.prop +  = J.prop "view" + +type schema + +let schema_basic +  : (t, Jv.t) J.prop +  = J.prop "schema_basic" + +(* Model *) + +type pm_model = Jv.t + +let model +  : (t, pm_model) J.prop +  = J.prop "model" + +module Model = struct + +  include Bindings.Model + +  module DOMParser = struct + +    type t = Jv.t + +    let from_schema +      : pm_model -> schema Js.t -> t +      = fun model schema -> +        let parser = Jv.get model "DOMParser" in +        Jv.call (Jv.Id.to_jv parser) "fromSchema" [|Jv.Id.to_jv schema|] + +    let parse +      : t -> El.t -> node Js.t +      = fun dom_parser el -> +        Jv.call dom_parser "parse" [|Jv.Id.to_jv el|] +        |> Jv.Id.of_jv + +  end + +  let empty_fragment +    : t -> fragment Js.t +    = fun t -> +      let model = Jv.get t "model" in +      let fragment = Jv.get model "Fragment" in +      Jv.get fragment "empty" +      |> Jv.Id.of_jv + +end + +type pm_transform = Jv.t + +let transform +  : (t, pm_transform) J.prop +  = J.prop "prosemirror-transform" + + +module State = struct + +  include Bindings.State + +  let configuration_prop +    : unit -> configuration_prop Js_of_ocaml.Js.t +    = fun () -> Js_of_ocaml.Js.Unsafe.obj [||] + +  let creation_prop +    : unit -> creation_prop Js.t +    = fun () -> Js_of_ocaml.Js.Unsafe.obj [||] + +  let create +    : pm_state -> creation_prop Js.t -> editor_state Js.t +    = fun state props -> +      let editor_state = Jv.get state "EditorState" in +      Jv.call editor_state "create" [|Jv.Id.to_jv props|] +      |> Jv.Id.of_jv + +  let fromJSON +    : pm_state -> configuration_prop Js_of_ocaml.Js.t -> Brr.Json.t -> editor_state Js.t +    = fun state config json -> +      let editor_state = Jv.get state "EditorState" in +      Jv.call editor_state "fromJSON" [|Jv.Id.to_jv config ; json |] +      |> Jv.Id.of_jv +end + +(* Editor view *) + +module View = struct + +  module EditorProps = struct +    type t = Jv.t +  end + +  include Bindings.View +  let direct_editor_props +    : unit -> direct_editor_props Js.t +    = fun () -> Js_of_ocaml.Js.Unsafe.obj [||] + +  let editor_view +    : pm_view -> El.t -> direct_editor_props Js.t -> editor_view Js.t +    = fun view node props -> +      Jv.new' (Jv.get view "EditorView") [|Jv.Id.to_jv node ; Jv.Id.to_jv props|] +      |> Jv.Id.of_jv +end + +(* Schema list *) + +type schema_list = Jv.t + +let schema_list +  : (t, schema_list) J.prop +  = J.prop "schema_list" + +module SchemaList = struct + +  let js f = Jv.of_jstr @@ Jstr.v f + +  let js_opt = Jv.of_option +      ~none:Jv.null +      js + +  let add_list_nodes +    : schema_list -> ?listGroup:string -> node:Model.node Js.t -> itemContent:string -> unit +    = fun s ?listGroup ~node ~itemContent -> +      Jv.call (Jv.Id.to_jv s) "addListNodes" [|Jv.Id.to_jv node; js itemContent ; js_opt listGroup|] +      |> ignore + +end + +module History = struct + +  include Bindings.History + +  let history_prop +    : unit -> history_prop Js.t +    = fun () -> Js_of_ocaml.Js.Unsafe.obj [||] + +  let history +    : t -> history_prop Js.t -> State.plugin Js.t +    = fun t props -> +      Jv.call (Jv.get t "history") "history" [|Jv.Id.to_jv props|] +      |> Jv.Id.of_jv + +  let undo +    : t -> State.editor_state Js.t -> (State.transaction -> unit) -> bool +    = fun t state fn -> +      Jv.call (Jv.get t "history") "undo" [|Jv.Id.to_jv state; Jv.repr fn|] +      |> Jv.Id.of_jv + +  let redo +    : t -> State.editor_state Js.t -> (State.transaction -> unit) -> bool +    = fun t state fn -> +      Jv.call (Jv.get t "history") "redo" [|Jv.Id.to_jv state; Jv.repr fn|] +      |> Jv.Id.of_jv +end + +module Keymap = struct + +  let keymap +    : t -> (string * (State.editor_state Js.t -> (State.transaction -> unit) -> bool)) array -> State.plugin Js.t +    = fun t props -> +      let props = Jv.obj @@ Array.map (fun (id, f) -> (id, Jv.repr f)) props in +      Jv.call (Jv.get t "keymap") "keymap" [|props|] +      |> Jv.Id.of_jv + +end + +module Commands = struct + +  let baseKeymap +    : t -> (string * (State.editor_state Js.t -> (State.transaction -> unit) -> bool)) array +    = fun t -> +      Jv.get (Jv.get t "commands") "baseKeymap" +      |> Jv.Id.of_jv + +end + +(* Example Setup *) + +let example_setup +  : t -> Model.schema Js.t -> State.plugin Js.t Js.js_array Js.t +  = fun t schema -> +    let setup = Jv.get t "example_setup" in +    let props = Jv.obj [|("schema", Jv.Id.to_jv schema)|] in +    Jv.call setup "exampleSetup" [|props|] +    |> Jv.Id.of_jv  | 
