Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/haz3lcore/zipper/action/Action.re
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type rel =
[@deriving (show({with_path: false}), sexp, yojson, eq)]
type select =
| All
| PointToPoint((Point.t, Point.t))
| Resize(move)
| Smart(int)
| Tile(rel)
Expand Down Expand Up @@ -224,6 +225,7 @@ let should_animate: t => bool =
switch (s) {
| Resize(_) => false
| All
| PointToPoint(_)
| Smart(_)
| Tile(_)
| Term(_)
Expand Down
7 changes: 7 additions & 0 deletions src/haz3lcore/zipper/action/Perform.re
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ let go =
|> return(Cant_select)
| Select(Resize(Goal(_))) => failwith("Select not implemented for goals")
| Select(All) => Ok(Select.all(z))
| Select(PointToPoint((p1, p2))) =>
z
|> Move.to_point(~measured=syntax.measured, ~goal=p1)
|> OptUtil.and_then(z =>
Select.to_point(~measured=syntax.measured, ~goal=p2, z)
)
|> return(Cant_select)
| Select(Term(Current)) =>
Select.current_term(
syntax.term_data,
Expand Down
7 changes: 7 additions & 0 deletions src/util/JsUtil.re
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ let timestamp = () => date_now()##valueOf;

let precise_timestamp = () => Js.Unsafe.global##.performance##now()##valueOf;

let print_timestamp = (ts: float): string => {
let date =
Js.Unsafe.new_obj(Js.date_fromTimeValue, [|Js.Unsafe.inject(ts)|]);
let date_str = date##toLocaleString(Js.undefined, Js.undefined);
date_str;
};

let download_string_file =
(~filename: string, ~content_type: string, ~contents: string) => {
let blob = File.blob_from_string(~contentType=content_type, contents);
Expand Down
18 changes: 18 additions & 0 deletions src/web/Export.re
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,21 @@ let import_all =
);
import_log(all.log);
};

let import_just_log = (data: string) => {
let all =
try(data |> Yojson.Safe.from_string |> all_of_yojson) {
| _ =>
let all_public = data |> Yojson.Safe.from_string |> all_public_of_yojson;
{
settings: all_public.settings,
scratch: all_public.scratch,
documentation: "",
exercise: all_public.exercise,
tutorial: all_public.tutorial,
log: all_public.log,
explainThisModel: "",
};
};
all.log;
};
76 changes: 59 additions & 17 deletions src/web/Main.re
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ let restart_caret_animation = () =>

let apply =
(
model: History.Model.t,
action: History.Update.t,
model: Logged.Model.t,
action: Logged.Update.t,
~schedule_action,
~schedule_autosave,
)
: History.Model.t => {
: Logged.Model.t => {
restart_caret_animation();

/* This function is split into two phases, update and calculate.
The intention is that eventually, the calculate phase will be
done automatically by incremental calculation. */
// ---------- UPDATE PHASE ----------
let updated: Updated.t(History.Model.t) =
let updated: Updated.t(Logged.Model.t) =
try(
History.Update.update(
Logged.Update.update(
~import_log=Log.import,
~get_log_and=Log.get_and,
~schedule_action,
Expand All @@ -56,12 +56,24 @@ let apply =
};
// ---------- CALCULATE PHASE ----------
let model' =
updated.model
|> History.Update.calculate(
~schedule_action,
~is_edited=updated.is_edit,
~dynamics=true,
);
try(
updated.model
|> Logged.Update.calculate(
~schedule_action,
~is_edited=updated.is_edit,
~dynamics=true,
)
) {
| exc =>
Printf.printf(
"ERROR: Exception during calculate: %s\n",
Printexc.to_string(exc),
);
{
...model,
replay_toggle: false,
};
};

if (updated.is_edit) {
schedule_autosave(
Expand All @@ -86,8 +98,8 @@ let start = {
let%sub save_scheduler = BonsaiUtil.Alarm.alarm;
let%sub (app_model, app_inject) =
Bonsai.state_machine1(
(module History.Model),
(module History.Update),
(module Logged.Model),
(module Logged.Update),
~apply_action=
(~inject, ~schedule_event, input) => {
let schedule_action = x => schedule_event(inject(x));
Expand All @@ -100,8 +112,8 @@ let start = {
apply(~schedule_action, ~schedule_autosave);
},
~default_model=
History.Model.init()
|> History.Update.calculate(
Logged.Model.load()
|> Logged.Update.calculate(
~schedule_action=_ => (),
~is_edited=true,
~dynamics=false,
Expand All @@ -114,6 +126,22 @@ let start = {
Bonsai.Value.map(~f=g => g(Page.Update.Save), app_inject);
let%sub () = BonsaiUtil.Alarm.listen(save_scheduler, ~event=save_effect);

let replay_effect = {
let%map app_inject = app_inject
and model = app_model;
Ui_effect.Many(
model.replay_toggle
? [app_inject(Page.Update.Globals(Log(NextLog)))] : [],
);
};

let%sub () =
Bonsai.Clock.every(
~when_to_start_next_effect=`Wait_period_after_previous_effect_finishes_blocking,
Core.Time_ns.Span.of_sec(0.1),
replay_effect,
);

// Update font metrics on resize
let%sub size =
BonsaiUtil.SizeObserver.observer(
Expand Down Expand Up @@ -155,6 +183,8 @@ let start = {
JsUtil.focus_clipboard_shim();
/* Setup scroll listener for floating elements (backpack) */
FloatingElement.setup_scroll_listener();
// Sync log count from database
Log.sync_count();
schedule_action(
Assistant(AssistantUpdate.ChatAction(FilterLoadingMessages)),
);
Expand Down Expand Up @@ -190,7 +220,8 @@ let start = {
let _ = Haz3lcore.ProbePerform.FocusEffect.execute();
/* Update floating elements (backpack) to viewport coordinates */
FloatingElement.update_all();
model.current.globals.settings.core.statics ? Animation.go() : ();
model.current.current.globals.settings.core.statics
? Animation.go() : ();
},
(),
);
Expand All @@ -200,7 +231,18 @@ let start = {
// View function
let%arr app_model = app_model
and app_inject = app_inject;
History.View.view(app_model, ~inject=app_inject, ~get_log_and=Log.get_and);
try(
Logged.View.view(app_model, ~inject=app_inject, ~get_log_and=Log.get_and)
) {
| exc =>
print_endline(
"ERROR: Exception during view: " ++ Printexc.to_string(exc),
);
WebUtil.Node.div(
~attrs=[WebUtil.Attr.id("page")],
[WebUtil.Node.text("An error occurred.")],
);
};
};

switch (JsUtil.Fragment.get_current()) {
Expand Down
8 changes: 8 additions & 0 deletions src/web/Settings.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Model = {
context_inspector: bool,
instructor_mode: bool,
benchmark: bool,
show_log_panel: bool,
explainThis: ExplainThisModel.Settings.t,
assistant: AssistantSettings.t,
sidebar: SidebarModel.Settings.t,
Expand Down Expand Up @@ -43,6 +44,7 @@ module Model = {
context_inspector: false,
instructor_mode: false,
benchmark: false,
show_log_panel: false,
explainThis: {
show: true,
show_feedback: false,
Expand Down Expand Up @@ -113,6 +115,7 @@ module Update = {
| Benchmark
| ContextInspector
| InstructorMode
| ShowLogPanel
| Evaluation(evaluation)
| Sidebar(SidebarModel.Settings.action)
| ExplainThis(ExplainThisModel.Settings.action)
Expand Down Expand Up @@ -312,6 +315,11 @@ module Update = {
},
}
}
| ShowLogPanel => {
...settings,
show_log_panel:
!settings.show_log_panel && ExerciseSettings.show_instructor,
}
| Benchmark => {
...settings,
benchmark: !settings.benchmark,
Expand Down
6 changes: 3 additions & 3 deletions src/web/Store.re
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ module F =
let save = (data: t): unit =>
JsUtil.set_localstore(key_to_string(key), serialize(data));

let init = () => {
let reset = () => {
JsUtil.set_localstore(key_to_string(key), serialize(default()));
default();
};

let load = (): t =>
switch (JsUtil.get_localstore(key_to_string(key))) {
| None => init()
| None => reset()
| Some(data) => deserialize(data, default())
};

let rec export = () =>
switch (JsUtil.get_localstore(key_to_string(key))) {
| None =>
let _ = init();
let _ = reset();
export();
| Some(data) => data
};
Expand Down
6 changes: 6 additions & 0 deletions src/web/Updated.re
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ let return_quiet =
historic,
};
};

exception InvalidAction;

let raise_invalid_action = _ => {
raise(InvalidAction);
};
13 changes: 9 additions & 4 deletions src/web/app/History.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ module Model = {

let equal = (===);

let init = () => {
let load = () => {
current: Page.Store.load(),
undo_stack: [],
redo_stack: [],
};

let reset = (~font_metrics=?, ()) => {
current: Page.Model.reset(~font_metrics?, ()),
undo_stack: [],
redo_stack: [],
};
};

module Update = {
Expand All @@ -41,7 +47,7 @@ module Update = {
switch (model.undo_stack) {
| [] =>
print_endline("Cannot undo");
model |> return_quiet;
model |> Updated.raise_invalid_action;
| [x, ...rest] => {
...x,
model: {
Expand All @@ -61,7 +67,7 @@ module Update = {
switch (model.redo_stack) {
| [] =>
print_endline("Cannot redo");
model |> return_quiet;
model |> Updated.raise_invalid_action;
| [x, ...rest] => {
...x,
model: {
Expand All @@ -86,7 +92,6 @@ module Update = {
action,
model.current,
);
let _ = Log.update(action, current);
if (Page.Update.can_undo(action)) {
{
...current,
Expand Down
Loading