From fe2cced55e1b44dbae57e55fe0f459c85e7369cb Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Mon, 7 Feb 2022 16:21:26 +0100 Subject: Application unification --- editor/editor.ml | 70 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 10 deletions(-) (limited to 'editor/editor.ml') diff --git a/editor/editor.ml b/editor/editor.ml index bca8fb2..a991b25 100755 --- a/editor/editor.ml +++ b/editor/editor.ml @@ -55,15 +55,63 @@ let build_view props in view, last_backup +module Store = struct + type t = unit + + let apply + : t -> State.t -> State.t + = fun () state -> + let title_element = Document.find_el_by_id G.document (Jstr.v "title") in + let content = Option.map + (fun el -> El.prop (El.Prop.value) el) + title_element in + + let new_date = (new%js Js.date_now)##getTime in + let content_obj = object%js + val content = Js.some @@ Jv.Id.to_jv (state.view##.state##toJSON ()) + val title = Js.Opt.option content + val date = Js.some new_date + end in + let save = State.Storage.save + content_obj + state.page_id + ~check:(fun previous_state -> + Js.Opt.case previous_state##.date + (fun () -> true) + (fun date -> + (* I do not figure how the previous date could be older + than the last backup. It could be either : + + - equal (if we are the only one to update it) + - more recent (if the content has been updated elsewhere) + + but older shoud be a bug. *) + let is_ok = date <= state.last_backup in + if (not is_ok) then ( + let open Console in + log + [ Jstr.v "Last backup date is " + ; new%js Js.date_fromTimeValue state.last_backup + ; Jstr.v " but date is " + ; new%js Js.date_fromTimeValue date] ); + is_ok)) in + begin match save with + | Ok true -> { state with last_backup = new_date } + | Ok false -> + Console.(log [Jstr.v "Didn't save"]); + state + | Error other -> + (* TODO In case of error, notify the user *) + Console.(log [Jstr.v "Couldn't save" ; other]); + state + end +end -let app id content = - (* This event is used in the pop process. The sender is given to the - subroutine in order to track the window closing *) - let (event: Forms.Events.event option Note.event), sender = Note.E.create () in +let app id content = (* Check the pre-requisite *) - let events_opt = Actions.populate_menu sender in + let events_opt = Actions.populate_menu () in match (Jv.is_none id), (Jv.is_none content), events_opt with | false, false, Some btn_events -> @@ -80,11 +128,13 @@ let app id content = (App.update ) init_state (Note.E.select - [ Brr_note.Evr.on_el Ev.focusout (fun _ -> App.StoreEvent) editor - ; Note.E.map (fun ev -> App.Generic ev) btn_events.Actions.delete - ; Note.E.map (fun ev -> App.Generic ev) btn_events.Actions.add - ; Note.E.map (fun v -> App.LoadEvent v) btn_events.Actions.redirect - ; Note.E.map (fun v -> App.ClosePopup v) event + [ Brr_note.Evr.on_el Ev.focusout (fun _ -> + (Actions.Event.E + ( () + , (module Store:Actions.Event.Handler with type t = Store.t)))) editor + ; Note.E.map (fun ev -> ev) btn_events.Actions.delete + ; Note.E.map (fun ev -> ev) btn_events.Actions.add + ; Note.E.map (fun v -> v) btn_events.Actions.redirect ]) in let () = -- cgit v1.2.3