summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2022-02-07 15:49:48 +0100
committerSébastien Dailly <sebastien@dailly.me>2022-02-07 16:43:33 +0100
commit54f6e68c051afec0d20c349352feee5356e11b35 (patch)
treeb9cf8c7c64538728c9f6e864896c00d32641b834
parentfe2cced55e1b44dbae57e55fe0f459c85e7369cb (diff)
Update editor code
-rwxr-xr-xeditor/actions/actions.ml14
-rwxr-xr-xeditor/actions/add_page.ml10
-rwxr-xr-xeditor/actions/delete_page.ml10
-rwxr-xr-xeditor/actions/event.ml9
-rwxr-xr-xeditor/actions/load_page.ml2
-rwxr-xr-xeditor/app.ml7
-rwxr-xr-xeditor/editor.ml9
-rwxr-xr-xeditor/forms/ui.ml79
-rwxr-xr-xeditor/state/dune1
-rwxr-xr-xeditor/state/state.ml3
-rwxr-xr-xeditor/state/state.mli16
11 files changed, 41 insertions, 119 deletions
diff --git a/editor/actions/actions.ml b/editor/actions/actions.ml
index b150279..f35beef 100755
--- a/editor/actions/actions.ml
+++ b/editor/actions/actions.ml
@@ -3,12 +3,10 @@ open Js_of_ocaml
open Brr
open Brr_note
-module Event = Event
-
type button_actions =
- { delete : Event.t Note.event
- ; redirect : Event.t Note.event
- ; add: Event.t Note.event
+ { delete : State.event Note.event
+ ; redirect : State.event Note.event
+ ; add: State.event Note.event
}
let populate_menu
@@ -87,19 +85,19 @@ let populate_menu
- the list for all the pages presents in the sidebar *)
let redirect_handler =
- (module Load_page.M : Event.Handler with type t = Load_page.M.t ) in
+ (module Load_page.M : State.Event with type t = Load_page.M.t ) in
let redirect_event = Note.E.select
(( Evr.on_el
Ev.click
- (fun _ -> Event.E (None, redirect_handler))
+ (fun _ -> State.E (None, redirect_handler))
home_button
) :: (
List.map2 stored_pages pages
~f:(fun name el ->
Evr.on_el
Ev.click
- (fun _ -> Event.E ((Some name), redirect_handler))
+ (fun _ -> State.E ((Some name), redirect_handler))
el ))) in
let childs =
diff --git a/editor/actions/add_page.ml b/editor/actions/add_page.ml
index 3ada726..4a3fcf3 100755
--- a/editor/actions/add_page.ml
+++ b/editor/actions/add_page.ml
@@ -9,7 +9,7 @@ module M = struct
= fun title ->
title
- let apply
+ let update
: t -> State.t -> State.t
= fun {title} state ->
let page_id = key_of_title title in
@@ -24,17 +24,17 @@ end
(** Create a new element *)
let create
- : unit -> Event.t Note.event
+ : unit -> State.event Note.event
= fun () ->
let title = Jstr.v "Nouvelle page" in
- let ev = Forms.Ui.popup
+ let ev = Elements.Popup.create
~title
~form:(Some (Forms.Add_page.create ()))
in
Note.E.map
- (fun v -> Event.E
+ (fun v -> State.E
(v
- , (module M : Event.Handler with type t = M.t )))
+ , (module M : State.Event with type t = M.t )))
(* Option.on_some trigger the event only when the pop up is validated.
Closing the popup doesn't do anything.
*)
diff --git a/editor/actions/delete_page.ml b/editor/actions/delete_page.ml
index 5c625bd..9086fc3 100755
--- a/editor/actions/delete_page.ml
+++ b/editor/actions/delete_page.ml
@@ -2,7 +2,7 @@ module M = struct
type t = unit
- let apply
+ let update
: t -> State.t -> State.t
= fun () state ->
match state.page_id with
@@ -15,15 +15,15 @@ module M = struct
end
let create
- : unit -> Event.t Note.event
+ : unit -> State.event Note.event
= fun () ->
let title = Jstr.v "Confirmation" in
- let ev = Forms.Ui.popup
+ let ev = Elements.Popup.create
~title
~form:(Some (Forms.Delete_page.create () ))
in
Note.E.map
- (fun v -> Event.E
+ (fun v -> State.E
( v
- , (module M : Event.Handler with type t = M.t )))
+ , (module M : State.Event with type t = M.t )))
(Note.E.Option.on_some ev)
diff --git a/editor/actions/event.ml b/editor/actions/event.ml
deleted file mode 100755
index 21e3d3a..0000000
--- a/editor/actions/event.ml
+++ /dev/null
@@ -1,9 +0,0 @@
-module type Handler = sig
-
- type t
-
- val apply: t -> State.t -> State.t
-
-end
-
-type t = E : 'a * (module Handler with type t = 'a) -> t
diff --git a/editor/actions/load_page.ml b/editor/actions/load_page.ml
index eb4afac..ceee038 100755
--- a/editor/actions/load_page.ml
+++ b/editor/actions/load_page.ml
@@ -2,7 +2,7 @@ module M = struct
type t = Jstr.t option
- let apply
+ let update
: t -> State.t -> State.t
= fun page_id state ->
let json = State.Storage.load page_id in
diff --git a/editor/app.ml b/editor/app.ml
deleted file mode 100755
index 9edc947..0000000
--- a/editor/app.ml
+++ /dev/null
@@ -1,7 +0,0 @@
-(** [update] is the event loop.
-
- The function take a new event, and apply it to the current state. *)
-let update
- : (Actions.Event.t, State.t) Application.t
- = fun (E (t, (module EventHandler))) state ->
- EventHandler.apply t state
diff --git a/editor/editor.ml b/editor/editor.ml
index a991b25..79ad54a 100755
--- a/editor/editor.ml
+++ b/editor/editor.ml
@@ -58,7 +58,7 @@ let build_view
module Store = struct
type t = unit
- let apply
+ let update
: t -> State.t -> State.t
= fun () state ->
let title_element = Document.find_el_by_id G.document (Jstr.v "title") in
@@ -123,15 +123,14 @@ let app id content =
let init_state = State.init pm view last_backup page_id in
- let app_state = Application.run
+ let app_state = State.run
~eq:State.eq
- (App.update )
init_state
(Note.E.select
[ Brr_note.Evr.on_el Ev.focusout (fun _ ->
- (Actions.Event.E
+ (State.E
( ()
- , (module Store:Actions.Event.Handler with type t = Store.t)))) editor
+ , (module Store:State.Event 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
diff --git a/editor/forms/ui.ml b/editor/forms/ui.ml
deleted file mode 100755
index 53039c4..0000000
--- a/editor/forms/ui.ml
+++ /dev/null
@@ -1,79 +0,0 @@
-open Brr
-open Brr_note
-module Js = Js_of_ocaml.Js
-
-let popup
- : ?form:('a Note.signal * El.t) option -> title:Jstr.t -> 'a option Note.event
- = fun ?(form = None) ~title ->
-
- (* Ensure we keep a track for the signal event.
-
- This looks a bit like the old memory magment in C, as it require to
- destroy the logger each time the popup is removed. *)
- let log_opt = Option.map
- (fun (values, _) -> Note.S.log values (fun _ -> ()))
- form in
-
- let close_btn =
- El.span
- ~at:At.[class' (Jstr.v "modal-close")]
- [ El.txt' "×"]
-
- and submit_btn = El.input ()
- ~at:At.[type' (Jstr.v "submit")] in
-
- let container = match form with
- | None -> El.div
- | Some _ -> El.form
-
- and body = match form with
- | None -> El.div []
- | Some (_, content) -> content
-
- and footer = match form with
- | None -> El.txt Jstr.empty
- | Some _ ->
-
- El.div [ submit_btn ]
- ~at:At.[class' (Jstr.v "row")] in
-
- (* HTML Element creation *)
- let el = El.div
- ~at:At.[class' (Jstr.v "modal")]
- [ container
- ~at:At.[class' (Jstr.v "modal-content")]
- [ El.div
- ~at:At.[class' (Jstr.v "modal-header")]
- [ close_btn
- ; El.h3
- [ El.txt title ]]
- ; El.div
- ~at:At.[class' (Jstr.v "modal-body")]
- [ body ]
- ; El.div
- ~at:At.[class' (Jstr.v "modal-footer")]
- [ footer ]]] in
-
- let () = El.append_children (Document.body G.document)
- [ el ] in
-
- (* Event handler *)
- let close_event = Evr.on_el
- Ev.click
- (fun _ ->
- El.remove el;
- Option.iter Note.Logr.destroy log_opt;
- None)
- close_btn
-
- and submit_event = Evr.on_el
- Ev.click
- (fun _ ->
- El.remove el;
- Option.iter Note.Logr.destroy log_opt;
- Option.map (fun v -> Note.S.value (fst v)) form)
- submit_btn in
-
- Note.E.select
- [ close_event
- ; submit_event ]
diff --git a/editor/state/dune b/editor/state/dune
index dd405a1..b61174d 100755
--- a/editor/state/dune
+++ b/editor/state/dune
@@ -2,6 +2,7 @@
(name state)
(libraries
brr
+ application
prosemirror
plugins
)
diff --git a/editor/state/state.ml b/editor/state/state.ml
index 569f26c..649473c 100755
--- a/editor/state/state.ml
+++ b/editor/state/state.ml
@@ -14,6 +14,7 @@ type t =
; window : Brr.El.t list
; pm : PM.t
}
+type state = t
(** Compare two states together.
@@ -86,3 +87,5 @@ let init
; pm
}
+
+include Application.Make(struct type t = state end)
diff --git a/editor/state/state.mli b/editor/state/state.mli
index 6984067..20d5288 100755
--- a/editor/state/state.mli
+++ b/editor/state/state.mli
@@ -11,6 +11,7 @@ type t =
; pm : Prosemirror.t
}
+
val eq: t -> t -> bool
(** Update the title element according to the page. *)
@@ -26,3 +27,18 @@ val load_page
(** Initialise a new state *)
val init
: Prosemirror.t -> Prosemirror.View.editor_view Js.t -> float -> Jstr.t option -> t
+
+type state = t
+module type Event = sig
+
+ type t
+
+ val update: t -> state -> state
+
+end
+
+type event = E : 'a * (module Event with type t = 'a) -> event
+
+(** Simple helper for the main event loop *)
+val run
+ : ?eq:(t -> t -> bool) -> t -> event Note.E.t -> state Note.S.t