From 896bfb14fa05cf07bb57216fdccd678e027bc4f9 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Fri, 9 Apr 2021 11:10:57 +0200 Subject: Update editor modules --- editor/link_editor.ml | 134 +++++++++++++++++--------------------------------- 1 file changed, 45 insertions(+), 89 deletions(-) (limited to 'editor/link_editor.ml') diff --git a/editor/link_editor.ml b/editor/link_editor.ml index 454dacd..cd7f477 100755 --- a/editor/link_editor.ml +++ b/editor/link_editor.ml @@ -3,63 +3,6 @@ open Brr module PM = Prosemirror - -type binded_field = - { field: El.t - ; button: El.t - } - - -(** Build a button which allow to activate or desactivate the given Element. - - The function f is called when the user validate the input. - -*) -let build_field - : El.t -> (Jstr.t -> bool) -> binded_field - = fun field f -> - - let button_content = - [ El.i - ~at:At.[ class' (Jstr.v "fas") - ; class' (Jstr.v "fa-pen") ] - [] - ] in - - let button = El.button - button_content - in - - Ev.listen Ev.click - (fun _ -> - match El.at (Jstr.v "contenteditable") field with - | Some value when (Jstr.equal value (Jstr.v "true")) -> - let new_value = El.prop - (El.Prop.jstr (Jstr.v "textContent")) - field in - begin match f new_value with - | true -> - El.set_at (Jstr.v "contenteditable") None field; - El.set_children button button_content - | false -> () - end - | _ -> - El.set_at (Jstr.v "contenteditable") - (Some (Jstr.v "true")) field; - El.set_children button - [ El.i - ~at:At.[ class' (Jstr.v "fas") - ; class' (Jstr.v "fa-check") ] - [] - ] - ) - (El.as_target button); - - { field - ; button = button - } - - let link_edit : PM.View.editor_view Js.t -> < .. > Js.t = fun view -> @@ -82,14 +25,8 @@ let link_edit : PM.View.editor_view Js.t -> PM.State.editor_state Js. t Js.opt -> unit = fun view _state_opt -> - (* Compare the previous and actual state. If the stored marks are the - same, just return *) let state = view##.state in - - (* Get the cursor position *) - - let root = state##.doc in - Js.Opt.case (root##nodeAt (view##.state##.selection##._to)) + Js.Opt.case (state##.doc##nodeAt (view##.state##.selection##._to)) (fun () -> hide ()) (fun node -> (* Check if we are editing a link *) @@ -101,14 +38,13 @@ let link_edit is_present (fun () -> hide ()) (fun mark -> - (* We are on a link we can edit the popsin *) - - (* Get the node's bounding position *) - let position = root##resolve (view##.state##.selection##._to) in + (* Get the node's bounding position and display the popin *) + let position = state##.doc##resolve + (view##.state##.selection##._to) in let start = position##start Js.null and end' = position##_end Js.null in - Tooltip.set_position + Popin.set_position ~start ~end' view popin; @@ -116,30 +52,50 @@ let link_edit (* Extract the value from the attribute *) let attrs = mark##.attrs in let href_opt = PM.O.get attrs "href" in - let href' = Option.value href_opt ~default:Jstr.empty in + let href_value = Option.value + ~default:Jstr.empty + href_opt + in + (* Create the popin content *) let a = El.a - ~at:At.[ href href' ] - [ El.txt href' ] in + ~at:At.[ href href_value ] + [ El.txt href_value ] in - let entry = build_field a + let entry = Popin.build_field a (fun new_value -> - PM.O.set attrs "href" new_value; - - let mark' = state##.schema##mark_fromType link_type (Js.some attrs) in - (* Create a transaction which update the - mark with the new value *) - view##dispatch - state - ##.tr - ##(removeMark - ~from:start - ~to_:end' - mark) - ##(addMark - ~from:start - ~to_:end' - mark'); + (* The function is called when the user validate + the change in the popi. We create a new + transaction in the document by replacing the + mark with the new one. *) + if not (Jstr.equal new_value href_value) then ( + + (* Create a new attribute object for the mark in + order to keep history safe *) + let attrs' = PM.O.init + [| "href", new_value |] in + + Option.iter + (fun v -> PM.O.set attrs' "title" v) + (PM.O.get attrs "title"); + + let mark' = state##.schema##mark_fromType + link_type + (Js.some attrs') in + (* Create a transaction which update the + mark with the new value *) + view##dispatch + state + ##.tr + ##(removeMark + ~from:start + ~to_:end' + mark) + ##(addMark + ~from:start + ~to_:end' + mark') + ); true ) in -- cgit v1.2.3