aboutsummaryrefslogtreecommitdiff
path: root/editor/storage.ml
diff options
context:
space:
mode:
Diffstat (limited to 'editor/storage.ml')
-rwxr-xr-xeditor/storage.ml85
1 files changed, 47 insertions, 38 deletions
diff --git a/editor/storage.ml b/editor/storage.ml
index 0d74a05..5dbaab9 100755
--- a/editor/storage.ml
+++ b/editor/storage.ml
@@ -1,27 +1,22 @@
open Brr
-module PM = Prosemirror
module Js = Js_of_ocaml.Js
let storage_key = (Jstr.v "editor")
let storage = Brr_io.Storage.local G.window
-let create_new_state pm mySchema content =
- let module PM = Prosemirror in
+class type content = object
- let doc = PM.Model.(
- DOMParser.parse
- (DOMParser.from_schema pm mySchema)
- content) in
+ method title
+ : Jstr.t Js.opt Js.readonly_prop
- let props = PM.State.creation_prop () in
- props##.doc := Js.some doc;
- props##.plugins := Plugins.default pm mySchema;
+ method content
+ : Jv.t Js.opt Js.readonly_prop
- PM.State.create
- pm
- props
+ method date
+ : float Js.opt Js.readonly_prop
+end
let page_id
: unit -> Jstr.t option
@@ -35,59 +30,73 @@ let page_id
storage for the [key].
*)
let load'
- : PM.t -> PM.Model.schema Js.t -> El.t -> Jstr.t -> PM.State.editor_state Js.t
- = fun pm schema content key ->
+ : Jstr.t -> content Js.t
+ = fun key ->
let opt_data = Brr_io.Storage.get_item storage key in
match opt_data with
- | None -> create_new_state pm schema content
+ | None ->
+ object%js
+ val title = Js.null
+ val content = Js.null
+ val date = Js.null
+ end
| Some contents ->
+
(* Try to load from the storage *)
match Json.decode contents with
- | Error _ -> create_new_state pm schema content
+ | Error _ ->
+ object%js
+ val title = Js.null
+ val content = Js.null
+ val date = Js.null
+ end
+
| Ok json ->
- let obj = PM.State.configuration_prop () in
- obj##.plugins := Plugins.default pm schema;
- obj##.schema := Js.some schema;
- PM.State.fromJSON pm obj json
+ Jv.Id.of_jv json
(** Save the view *)
let save'
- : PM.View.editor_view Js.t -> Jstr.t -> unit
- = fun view key ->
- let contents = view##.state##toJSON () in
- let storage = Brr_io.Storage.local G.window in
- Brr_io.Storage.set_item
- storage
- key
- (Json.encode @@ contents)
- |> Console.log_if_error ~use:()
+ : check:(content Js.t -> bool) -> content Js.t -> Jstr.t -> (bool, Jv.Error.t) result
+ = fun ~check object_content key ->
+
+ (* First load the content from the storage *)
+ match check (load' key) with
+ | false -> Ok false
+ | true ->
+ let storage = Brr_io.Storage.local G.window in
+ let operation = Brr_io.Storage.set_item
+ storage
+ key
+ (Json.encode @@ Jv.Id.to_jv @@ object_content) in
+ Result.map (fun () -> true) operation
(** [load pm schema content f] will try load the content stored in the local
storage. The right key is given by the result of the function [f]
*)
let load
- : PM.t -> PM.Model.schema Js.t -> El.t -> (unit -> Jstr.t option) -> PM.State.editor_state Js.t
- = fun pm schema content f ->
+ : (unit -> Jstr.t option) -> content Js.t
+ = fun f ->
match f () with
- | None -> load' pm schema content storage_key
+ | None -> load' storage_key
| Some value ->
let key = Jstr.concat
~sep:(Jstr.v "_")
[storage_key ; value] in
- load' pm schema content key
+ load' key
let save
- : PM.View.editor_view Js.t -> (unit -> Jstr.t option) -> unit
- = fun view f ->
+ : check:(content Js.t -> bool) -> content Js.t -> (unit -> Jstr.t option) -> (bool, Jv.Error.t) result
+ = fun ~check object_content f ->
match f () with
- | None -> save' view storage_key
+ | None ->
+ save' ~check object_content storage_key
| Some value ->
let key = Jstr.concat
~sep:(Jstr.v "_")
[storage_key ; value] in
- save' view key
+ save' ~check object_content key
let delete
: (unit -> Jstr.t option) -> unit