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/app.ml | 91 ++--------------------------------------------------------- 1 file changed, 3 insertions(+), 88 deletions(-) (limited to 'editor/app.ml') diff --git a/editor/app.ml b/editor/app.ml index 4559044..9edc947 100755 --- a/editor/app.ml +++ b/editor/app.ml @@ -1,92 +1,7 @@ -open Brr -module Js = Js_of_ocaml.Js - -type events = - | StoreEvent - | LoadEvent of Jstr.t option - | ClosePopup of Forms.Events.event option - | Generic of Actions.Event.t - -let key_of_title - : Jstr.t -> Jstr.t - = fun title -> - title - (** [update] is the event loop. The function take a new event, and apply it to the current state. *) - let update - : (events, State.t) Application.t - = fun event state -> - match event with - - | Generic (E (t, (module EventHandler))) -> - EventHandler.apply t state - - - | ClosePopup result -> - (* First close the last popin. *) - let state = match state.window with - | [] -> { state with window = [] } - | el::tl -> El.remove el - ; { state with window = tl } in - (* Call the handler associated with the event *) - begin match result with - | None -> state - | Some (Event (t, (module Handler))) -> - Handler.on_close t state - end - - | StoreEvent -> - - 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 - - | LoadEvent page_id -> - let json = State.Storage.load page_id in - State.load_page page_id state json - - + : (Actions.Event.t, State.t) Application.t + = fun (E (t, (module EventHandler))) state -> + EventHandler.apply t state -- cgit v1.2.3