summaryrefslogtreecommitdiff
path: root/editor/storage.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2022-02-07 16:14:09 +0100
committerSébastien Dailly <sebastien@dailly.me>2022-02-07 16:43:33 +0100
commitbf94695abeda0d7bb296ae4cd0f9a53782587d4a (patch)
tree06dac432cfaa57dc6ad428b116332fdf777c84d8 /editor/storage.ml
parent4d35508a76676a548ac45e0bff2d63eafaf014e2 (diff)
Update editor organisation
Diffstat (limited to 'editor/storage.ml')
-rwxr-xr-xeditor/storage.ml137
1 files changed, 0 insertions, 137 deletions
diff --git a/editor/storage.ml b/editor/storage.ml
deleted file mode 100755
index f893c2d..0000000
--- a/editor/storage.ml
+++ /dev/null
@@ -1,137 +0,0 @@
-open Brr
-module Js = Js_of_ocaml.Js
-
-let storage_key = (Jstr.v "editor")
-
-let storage = Brr_io.Storage.local G.window
-
-class type content = object
-
- method title
- : Jstr.t Js.opt Js.readonly_prop
-
- method content
- : Jv.t Js.opt Js.readonly_prop
-
- method date
- : float Js.opt Js.readonly_prop
-
-end
-
-let page_id
- : unit -> Jstr.t option
- = fun () ->
- let uri = Brr.Window.location Brr.G.window in
- let query = Brr.Uri.query uri in
- let params = Brr.Uri.Params.of_jstr query in
- Brr.Uri.Params.find (Jstr.v "page") params
-
-(** [load' pm schema content key] will load the content stored in the local
- storage for the [key].
-*)
-let load'
- : Jstr.t -> content Js.t
- = fun key ->
-
- let opt_data = Brr_io.Storage.get_item storage key in
- match opt_data with
- | 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 _ ->
- object%js
- val title = Js.null
- val content = Js.null
- val date = Js.null
- end
-
- | Ok json ->
- Jv.Id.of_jv json
-
-(** Save the view *)
-let save'
- : 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
- : Jstr.t option -> content Js.t
- = fun key ->
- match key with
- | None -> load' storage_key
- | Some value ->
- let key = Jstr.concat
- ~sep:(Jstr.v "_")
- [storage_key ; value] in
- load' key
-
-let save
- : check:(content Js.t -> bool) -> content Js.t -> Jstr.t option -> (bool, Jv.Error.t) result
- = fun ~check object_content key ->
- match key with
- | None ->
- save' ~check object_content storage_key
- | Some value ->
- let key = Jstr.concat
- ~sep:(Jstr.v "_")
- [storage_key ; value] in
- save' ~check object_content key
-
-let delete
- : (unit -> Jstr.t option) -> unit
- = fun f ->
- match f () with
- | None -> ()
- | Some value ->
- let key = Jstr.concat
- ~sep:(Jstr.v "_")
- [storage_key ; value] in
- let storage = Brr_io.Storage.local G.window in
- Brr_io.Storage.remove_item storage key
-
-let get_ids
- : unit -> Jstr.t list
- = fun () ->
- let open Brr_io in
- let storage = Storage.local G.window in
- let items = Storage.length storage in
-
- let sub = Jstr.( storage_key + (v "_") ) in
- let start = Jstr.length sub in
-
- let rec add_element acc = function
- | -1 -> acc
- | nb ->
- begin match Storage.key storage nb with
- | Some key when (Jstr.starts_with ~sub key) ->
-
- let key_name = Jstr.sub key
- ~start in
- add_element (key_name::acc) (nb -1)
- | _ ->
- add_element acc (nb -1)
- end
-
- in
- add_element [] items