From 8bb6631ea1c8f54071d09ced2d62a16d6319e6f6 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Wed, 2 Jun 2021 21:11:20 +0200 Subject: Editor : Dynamically update the sidebar --- editor/editor.ml | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) (limited to 'editor/editor.ml') diff --git a/editor/editor.ml b/editor/editor.ml index 51ff3e4..f27c60c 100755 --- a/editor/editor.ml +++ b/editor/editor.ml @@ -58,20 +58,17 @@ let build_view view, last_backup module Store = struct - type t = unit + type t = El.t let update : 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 + = fun title_element state -> + let title = El.prop (El.Prop.value) 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 title = Js.some title val date = Js.some new_date end in let save = State.Storage.save @@ -119,10 +116,12 @@ end let app id content = + let title_element = Document.find_el_by_id G.document (Jstr.v "title") in (* Check the pre-requisite *) - let events_opt = Actions.populate_menu () in - match (Jv.is_none id), (Jv.is_none content), events_opt with - | false, false, Some btn_events -> + match title_element, (Jv.is_none id), (Jv.is_none content), Blog.Sidebar.get () with + | Some title, false, false, Some sidebar -> + + let () = Blog.Sidebar.clean sidebar in let pm = PM.v () in let editor:El.t = Jv.Id.of_jv id in @@ -132,24 +131,41 @@ let app id content = let init_state = State.init pm view last_backup page_id in + let side_elements = Editor_actions.build () in + let btn_events = Editor_actions.get_event side_elements in let app_state = State.run ~eq:State.eq init_state (Note.E.select - [ Brr_note.Evr.on_el Ev.focusout (fun _ -> - (State.E - ( () - , (module Store:State.Event with type t = Store.t)))) editor + [ Brr_note.Evr.on_el Ev.focusout + (fun _ -> + State.E + ( title + , (module Store:State.Event with type t = Store.t))) + editor + ; Brr_note.Evr.on_el Ev.focusout + (fun _ -> + State.E + ( title + , (module Store:State.Event with type t = Store.t))) + title ; btn_events ]) in - let () = - Note.S.log app_state (fun _ -> ()) - |> Note.Logr.hold in + let change_event = + Note.S.changes app_state in + + let childs = Editor_actions.complete side_elements change_event in + let () = El.append_children sidebar childs in + let _ = + Note.E.log change_event + (fun _ -> ()) + |> Option.iter Note.Logr.hold + in () - | _, _, _ -> + | _ -> Console.(error [str "No element with id '%s' '%s' found"; id ; content]) let () = -- cgit v1.2.3