From 7c23b96ce5634550341b9554eda9d7c89a79e3c0 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Mon, 7 Feb 2022 16:03:16 +0100 Subject: Update editor --- editor/storage.ml | 80 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 20 deletions(-) (limited to 'editor/storage.ml') diff --git a/editor/storage.ml b/editor/storage.ml index 2dc768a..0d74a05 100755 --- a/editor/storage.ml +++ b/editor/storage.ml @@ -1,6 +1,7 @@ -open Js_of_ocaml 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 @@ -11,7 +12,7 @@ let create_new_state pm mySchema content = let doc = PM.Model.( DOMParser.parse (DOMParser.from_schema pm mySchema) - (Jv.Id.of_jv content)) in + content) in let props = PM.State.creation_prop () in props##.doc := Js.some doc; @@ -30,10 +31,11 @@ let page_id let params = Brr.Uri.Params.of_jstr query in Brr.Uri.Params.find (Jstr.v "page") params -(** Read the state from the local storage, or load the content from the given - element *) +(** [load' pm schema content key] will load the content stored in the local + storage for the [key]. +*) let load' - : PM.t -> PM.Model.schema Js.t -> Jv.t -> Jstr.t -> PM.State.editor_state Js.t + : PM.t -> PM.Model.schema Js.t -> El.t -> Jstr.t -> PM.State.editor_state Js.t = fun pm schema content key -> let opt_data = Brr_io.Storage.get_item storage key in @@ -49,18 +51,6 @@ let load' obj##.schema := Js.some schema; PM.State.fromJSON pm obj json -let load - : PM.t -> PM.Model.schema Js.t -> Jv.t -> PM.State.editor_state Js.t - = fun pm schema content -> - match page_id () with - | None -> load' pm schema content storage_key - | Some value -> - let key = Jstr.concat - ~sep:(Jstr.v "_") - [storage_key ; value] in - load' pm schema content key - - (** Save the view *) let save' : PM.View.editor_view Js.t -> Jstr.t -> unit @@ -74,13 +64,63 @@ let save' |> Console.log_if_error ~use:() +(** [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 -> + match f () with + | None -> load' pm schema content storage_key + | Some value -> + let key = Jstr.concat + ~sep:(Jstr.v "_") + [storage_key ; value] in + load' pm schema content key + let save - : PM.View.editor_view Js.t -> unit - = fun view -> - match page_id () with + : PM.View.editor_view Js.t -> (unit -> Jstr.t option) -> unit + = fun view f -> + match f () with | None -> save' view storage_key | Some value -> let key = Jstr.concat ~sep:(Jstr.v "_") [storage_key ; value] in save' view key + +let delete + : (unit -> Jstr.t option) -> unit + = fun f -> + match f () with + | None -> () + | Some key -> + let storage = Brr_io.Storage.local G.window in + let () = Brr_io.Storage.remove_item storage key in + (* Reload the page *) + Brr.Window.reload G.window + +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 + | 0 -> 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 -- cgit v1.2.3