Skip to content

Instantly share code, notes, and snippets.

@joelburget
Created April 20, 2020 01:46
Show Gist options
  • Save joelburget/f1d026ee07ffc1686844389570265c08 to your computer and use it in GitHub Desktop.
Save joelburget/f1d026ee07ffc1686844389570265c08 to your computer and use it in GitHub Desktop.
open Bonsai_web
open Core_kernel
open Js_of_ocaml
module Change = struct
type position =
{ ch: int
; line: int
}
type t =
{ from: position
; to_: position
; text: string array
; removed: string
(* ; origin *)
}
(*
type js =
{
}
*)
end
class type codemirror_type = object
method on : string -> (Change.t -> unit) Js.callback -> unit Js.meth
end
let codemirror : Vdom.Node.t
= let widget_id = Type_equal.Id.create ~name:"codemirror widget" (fun _ -> Sexp.Atom "<codemirror>") in
Vdom.Node.widget
~id:widget_id
~init:(fun () ->
let wrapper = Dom_html.document##createElement (Js.string "div") in
let textarea = Dom_html.document##createElement (Js.string "textarea") in
Dom.appendChild wrapper textarea;
let codemirror_obj = Js.Unsafe.get Dom_html.window (Js.string "CodeMirror") in
let create_fn = Js.Unsafe.get codemirror_obj (Js.string "fromTextArea") in
let editor : codemirror_type Js.t
= Js.Unsafe.fun_call create_fn [| Js.Unsafe.inject textarea |]
in
(* Here is where I get an error:
VM366:1 Uncaught TypeError: args.slice is not a function
at eval (eval at caml_call_gen (stdlib.js:61), <anonymous>:1:6)
at caml_call_gen (stdlib.js:61)
at jslib_js_of_ocaml.js:115
at codemirror.js:2080
at fireCallbacksForOps (codemirror.js:2037)
at finishOperation (codemirror.js:2051)
at endOperation (codemirror.js:3791)
at HTMLTextAreaElement.<anonymous> (codemirror.js:3928)
*)
editor##on "change" (Js.wrap_callback (fun _change_obj -> Printf.printf "change"));
(), wrapper)
()
@joelburget
Copy link
Author

You're right, I've now switched to Js.js_string Js.t locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment