open Js_of_ocaml open Brr type t = Jv.t let v : unit -> t = fun () -> Jv.get Jv.global "PM" module Model = struct include Bindings.Model module DOMParser = struct type parser = Jv.t let from_schema : t -> schema Js.t -> parser = fun t schema -> let model = Jv.get t "model" in let parser = Jv.get model "DOMParser" in Jv.call (Jv.Id.to_jv parser) "fromSchema" [|Jv.Id.to_jv schema|] let parse : parser -> 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 schema_spec: node_spec Bindings.ordered_map Js.t -> mark_spec Bindings.ordered_map Js.t option -> string option -> schema_spec Js.t = fun nodes marks_opt topNode_opt -> let marks = Jv.of_option ~none:Jv.null Jv.Id.to_jv marks_opt and topNode = Jv.of_option ~none:Jv.null Jv.of_string topNode_opt in Jv.obj [| "nodes", (Jv.Id.to_jv nodes) ; "marks", marks ; "topNode", topNode |] |> Jv.Id.of_jv let schema : t -> schema_spec Js.t -> schema Js.t = fun t spec -> let model = Jv.get t "model" in Jv.new' (Jv.get model "Schema") [| Jv.Id.to_jv spec |] |> Jv.Id.of_jv 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 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 : t -> creation_prop Js.t -> editor_state Js.t = fun t props -> let state = Jv.get t "state" in let editor_state = Jv.get state "EditorState" in Jv.call editor_state "create" [|Jv.Id.to_jv props|] |> Jv.Id.of_jv let fromJSON : t -> configuration_prop Js_of_ocaml.Js.t -> Brr.Json.t -> editor_state Js.t = fun t config json -> let state = Jv.get t "state" in 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 : t -> El.t -> direct_editor_props Js.t -> editor_view Js.t = fun t node props -> let view = Jv.get t "view" in Jv.new' (Jv.get view "EditorView") [|Jv.Id.to_jv node ; Jv.Id.to_jv props|] |> Jv.Id.of_jv end module SchemaList = struct let add_list_nodes : t -> Model.node_spec Bindings.ordered_map Js.t -> Jstr.t -> Jstr.t option -> Model.node_spec Bindings.ordered_map Js.t = fun t nodes item_content list_group_opt -> let schema_list = Jv.get t "schema_list" in let list_group = Jv.of_option ~none:Jv.null Jv.of_jstr list_group_opt in Jv.call schema_list "addListNodes" [|Jv.Id.to_jv nodes ; Jv.of_jstr item_content ; list_group |] |> Jv.Id.of_jv end module SchemaBasic = struct include Bindings.SchemaBasic let schema : t -> Model.schema Js.t = fun t -> Jv.get (Jv.get t "schema_basic") "schema" |> Jv.Id.of_jv 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