From 7d83ee3912582d3435d5a7c5fe4cb8a21617425b Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 8 Apr 2021 20:27:36 +0200 Subject: Added custom link popin in text editor --- editor/tooltip.ml | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'editor/tooltip.ml') diff --git a/editor/tooltip.ml b/editor/tooltip.ml index 43c345f..adb37f1 100755 --- a/editor/tooltip.ml +++ b/editor/tooltip.ml @@ -9,17 +9,22 @@ module PM = Prosemirror (** Set the element position just above the selection *) let set_position - : PM.View.editor_view Js.t -> El.t -> unit - = fun view el -> + : start:int -> end':int -> PM.View.editor_view Js.t -> El.t -> unit + = fun ~start ~end' view el -> El.set_inline_style El.Style.display (Jstr.v "") el; - let start = view##coordsAtPos (view##.state##.selection##.from) Js.null - and end' = view##coordsAtPos (view##.state##.selection##._to) Js.null in + + (* These are in screen coordinates *) + let start = view##coordsAtPos start Js.null + and end' = view##coordsAtPos end' Js.null in let offsetParent = Jv.(Id.of_jv @@ get (Jv.Id.to_jv el) "offsetParent") in + (* The box in which the tooltip is positioned, to use as base *) let box = Jv.(Id.of_jv @@ call (Jv.Id.to_jv offsetParent) "getBoundingClientRect" [||]) in let box_left = Jv.(Id.of_jv @@ get (Jv.Id.to_jv box) "left") in let box_bottom = Jv.(Id.of_jv @@ get (Jv.Id.to_jv box) "bottom") in + (* Find a center-ish x position from the selection endpoints (when + crossing lines, end may be more to the left) *) let left = (start##.left +. end'##.left) /. 2. in El.set_inline_style (Jstr.v "left") @@ -34,7 +39,8 @@ let boldtip = fun view -> (* Create the element which will be displayed over the editor *) let tooltip = El.div [] - ~at:At.([class' (Jstr.v "tooltip")]) in + ~at:At.([ class' (Jstr.v "tooltip") + ]) in El.set_inline_style El.Style.display (Jstr.v "none") tooltip; let parent = Jv.(Id.of_jv @@ get (Jv.Id.to_jv view##.dom) "parentNode") in @@ -51,6 +57,7 @@ let boldtip Js.Opt.bind state_opt (fun state -> state##.storedMarks) |> Js.Opt.to_option and current_stored_marks = state##.storedMarks in + let same = match previous_stored_marks, Js.Opt.to_option current_stored_marks with | Some arr1, Some arr2 -> Js_lib.Array.compare arr1 arr2 ~f:(fun v1 v2 -> v1##eq v2) @@ -69,10 +76,10 @@ let boldtip (fun _ -> Some (Jstr.v "gras"))) in let is_em = Option.bind (PM.O.get state##.schema##.marks "em") (fun mark_type -> - let is_strong = + let is_em = Js.Opt.bind current_stored_marks (fun t -> mark_type##isInSet t) in - Js.Opt.case is_strong + Js.Opt.case is_em (fun () -> None) (fun _ -> Some (Jstr.(v "emphase")))) in @@ -81,7 +88,9 @@ let boldtip | [] -> El.set_inline_style El.Style.display (Jstr.v "none") tooltip | _ -> (* The mark is present, add in the content *) - set_position view tooltip; + let start = view##.state##.selection##.from + and end' = view##.state##.selection##._to in + set_position ~start ~end' view tooltip; El.set_prop (El.Prop.jstr (Jstr.v "textContent")) (Jstr.concat marks ~sep:(Jstr.v ", ")) -- cgit v1.2.3