aboutsummaryrefslogtreecommitdiff
path: root/editor/state
diff options
context:
space:
mode:
Diffstat (limited to 'editor/state')
-rwxr-xr-xeditor/state/state.ml26
-rwxr-xr-xeditor/state/state.mli5
-rwxr-xr-xeditor/state/storage.ml24
-rwxr-xr-xeditor/state/storage.mli3
4 files changed, 55 insertions, 3 deletions
diff --git a/editor/state/state.ml b/editor/state/state.ml
index 649473c..2662a48 100755
--- a/editor/state/state.ml
+++ b/editor/state/state.ml
@@ -60,8 +60,9 @@ let state_of_storage
PM.State.fromJSON pm obj page_content)
let load_page
- : Jstr.t option -> t -> Storage.content Js.t -> t
- = fun page_id state json ->
+ : Jstr.t option -> t -> t
+ = fun page_id state ->
+ let json = Storage.load page_id in
let editor_state = state_of_storage state.pm json state.view##.state##.schema in
let () = state.view##updateState editor_state
and () = set_title json in
@@ -74,6 +75,27 @@ let load_page
{ state with page_id
; last_backup }
+let new_page
+ : Jstr.t option -> title:Jstr.t -> t -> t
+ = fun page_id ~title state ->
+ let new_date = (new%js Js.date_now)##getTime in
+ let content_obj = object%js
+ val content = Js.null
+ val title = Js.some title
+ val date = Js.some new_date
+ end in
+ let editor_state = state_of_storage state.pm content_obj state.view##.state##.schema in
+ let () = state.view##updateState editor_state
+ and () = set_title content_obj in
+
+ let last_backup =
+ Js.Opt.case content_obj##.date
+ (fun () -> state.last_backup )
+ (fun v -> v) in
+
+ { state with page_id
+ ; last_backup }
+
let init
: PM.t -> PM.View.editor_view Js.t -> float -> Jstr.t option -> t
diff --git a/editor/state/state.mli b/editor/state/state.mli
index 20d5288..2f3e9c4 100755
--- a/editor/state/state.mli
+++ b/editor/state/state.mli
@@ -22,7 +22,10 @@ val state_of_storage
: Prosemirror.t -> Storage.content Js.t -> Prosemirror.Model.schema Js.t -> Prosemirror.State.editor_state Js.t
val load_page
- : Jstr.t option -> t -> Storage.content Js.t -> t
+ : Jstr.t option -> t -> t
+
+val new_page
+ : Jstr.t option -> title:Jstr.t -> t -> t
(** Initialise a new state *)
val init
diff --git a/editor/state/storage.ml b/editor/state/storage.ml
index a790a9d..1bb8b81 100755
--- a/editor/state/storage.ml
+++ b/editor/state/storage.ml
@@ -157,3 +157,27 @@ let to_json
(* Also add the home page *)
let pages = Array.of_list @@ (save_for_id None)::pages in
Brr.Json.encode (Jv.Id.to_jv pages)
+
+let of_json
+ : Jstr.t -> (unit, Jv.Error.t) result
+ = fun json ->
+ let result = Json.decode json in
+ Result.map
+ (fun v ->
+ Array.iter (Jv.Id.of_jv v)
+ ~f:(fun element ->
+ let key = element##.id
+ and content = object%js
+ val title = element##.title
+ val content = element##.content
+ val date = element##.date
+ end in
+ ignore @@
+ save
+ ~check:(fun _ -> true)
+ content
+ key
+ ))
+ result
+
+
diff --git a/editor/state/storage.mli b/editor/state/storage.mli
index 50e164e..4d022d1 100755
--- a/editor/state/storage.mli
+++ b/editor/state/storage.mli
@@ -37,3 +37,6 @@ val get_ids
val to_json
: unit -> Jstr.t
+
+val of_json
+ : Jstr.t -> (unit, Jv.Error.t) result