Skip to content

Commit

Permalink
load defaults: shift-F1 to F9
Browse files Browse the repository at this point in the history
  • Loading branch information
disconcision committed Nov 4, 2024
1 parent 90aa88f commit 5c8c235
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
52 changes: 35 additions & 17 deletions src/web/Store.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
open Tylr_core;

let insert: (Zipper.t, string) => Zipper.t =
(z, str) => {
switch (Edit.perform(Insert(str), z)) {
| None =>
print_endline("WARNING: Store.insert failed");
z;
| Some(r) => r
};
};

let parse: string => Zipper.t = insert(Zipper.empty);

let serialize = z => z |> Zipper.sexp_of_t |> Sexplib.Sexp.to_string;

let deserialize = (str: string): Zipper.t =>
Expand All @@ -13,30 +25,36 @@ let save_syntax_key: int => string =
let save_syntax = (save_idx: int, z: Zipper.t) =>
LocalStorage.set(save_syntax_key(save_idx), z |> serialize);

let editor_defaults = [serialize(Zipper.empty)];
let editor_defaults = [
serialize(Zipper.empty),
serialize(parse("2")),
serialize(parse("3 + 3")),
serialize(parse("(4)")),
serialize(parse("(5 + 5) * 5")),
serialize(parse("6")),
serialize(parse("7")),
serialize(parse("8")),
serialize(parse("9")),
serialize(parse("0")),
//serialize(parse("let x = 3 in x")),
//serialize(parse("let f = fun x -> 4 in f(4)")),
//serialize(parse("case 5 | x => 5")),
];

let load_syntax: int => Zipper.t =
let load_default_syntax: int => Zipper.t =
save_idx =>
switch (LocalStorage.get(save_syntax_key(save_idx))) {
| None =>
switch (List.nth_opt(editor_defaults, save_idx)) {
| Some(str) => deserialize(str)
| None => Zipper.empty
}
switch (List.nth_opt(editor_defaults, save_idx)) {
| None => Zipper.empty
| Some(str) => deserialize(str)
};

let insert: (Zipper.t, string) => Zipper.t =
(z, str) => {
switch (Edit.perform(Insert(str), z)) {
| None =>
print_endline("WARNING: Store.insert failed");
z;
| Some(r) => r
let load_syntax: int => Zipper.t =
save_idx =>
switch (LocalStorage.get(save_syntax_key(save_idx))) {
| None => load_default_syntax(save_idx)
| Some(str) => deserialize(str)
};
};

let parse: string => Zipper.t = insert(Zipper.empty);
//let unparse: Zipper.t => string = z => z |> Zipper.zip |> Cell.tokens;

let _editor_defaults = [
Expand Down
18 changes: 10 additions & 8 deletions src/web/update/Update.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type t =
// | FailedInput(FailedInput.reason) //TODO(andrew): refactor as failure?
| Undo
| Redo
| Reset;
| Reset(int);

let is_f_key = s => Re.Str.(string_match(regexp("^F[0-9][0-9]*$"), s, 0));

Expand All @@ -44,12 +44,9 @@ let handle_key_event = (k: Util.Key.t, ~model as _: Model.t): list(t) => {
// }
| {key: D(key), sys: _, shift: Down, meta: Up, ctrl: Up, alt: Up}
when is_f_key(key) =>
switch (key) {
| "F1" =>
print_endline("F1: Resetting Model");
now_save_u(Reset);
| _ => []
}
let index = int_of_string(String.sub(key, 1, 1)) - 1;
print_endline("F key pressed: index: " ++ string_of_int(index));
now_save_u(Reset(index));
| {key: D(key), sys: _, shift, meta: Up, ctrl: Up, alt: Up} =>
switch (shift, key) {
| (Up, "ArrowLeft") => now(Move(Step(H(L))))
Expand Down Expand Up @@ -230,7 +227,12 @@ let apply =
| None => Error(CantRedo)
| Some((zipper, history)) => Ok({...model, zipper, history})
}
| Reset => Ok({...model, zipper: Zipper.empty, history: History.empty})
| Reset(n) =>
Ok({
...model,
zipper: Store.load_default_syntax(n),
history: History.empty,
})
// | Set(s_action) =>
// Ok({...model, settings: update_settings(s_action, model.settings)})
// | LoadInit =>
Expand Down

0 comments on commit 5c8c235

Please sign in to comment.