gorilla-repl
gorilla-repl copied to clipboard
Autosave Option
Opening an issue instead of issuing a PR directly because introducing options of any kind begs the question of: what plans do you have for configuration, either via the lein gorilla
subcommand or via the UI itself during a session?
If you indicate which direction you plan/would like to go, if either, I'll be happy to put together a PR for an option to auto-save worksheets. Nothing like accidentally refreshing your tab...
Thanks for your wonderful work.
Thanks, and thanks for the kind words too! This would be great :-) Just this afternoon I had the sequence of events: I wonder if "search and replace" works in a textarea; press command+r; sad face as the browser refreshed!
I think you're right, configuration needs to be pinned down first. I have a feeling there's a way to add configuration options to your lein config, at either the project or global level, but I must confess to not understanding it. But I think that would be the way to do it, in general. I have a preference for keeping the client UI as light as is possible, consistent with still being usable.
I'm a bit snowed under at the minute with other stuff (catch up!) so I might not be able to give this as much attention as I'd like over the next few days. So apologies in advance if I ignore you at some point :-)
If you're game for having options come through lein gorilla
, then that's enough of a starting point for me. Thanks for the timely response.
@richcole mentions in #78 that it would be useful to have an autosave that triggers on every evaluation. Merging that issue into this one.
Following the discussion on #103, if we implement a worksheet-reload functionality, we might also consider adding an auto-reload option to watch a file for changes.
+1
@JonyEpsilon re autosave and (auto-)reload: +1, that would be really great. Personally, I like having emacs and gorilla open side-by-side, and whilst reloading the worksheet in Gorilla is a tiny bit cumbersome, it's nice to be able to hop back and forth. FWIW autosave and autoreload would obviously make my workflow better!
This simple diff below will autosave the worksheet into a backup (~ appended) at most once per minute if there might have been a change to the worksheet. I am hesitant about issuing a PR because this asks for configuration parameters, but since I added this my gorilla-repl experience is significantly less frustrating.
diff --git a/resources/public/js/main.js b/resources/public/js/main.js
index cd96322..f9c8c85 100644
--- a/resources/public/js/main.js
+++ b/resources/public/js/main.js
@@ -33,6 +33,12 @@ var app = function () {
self.filename(newFilename);
self.worksheet(newWorksheet);
newWorksheet.addEventHandlers();
+
+ // If the worksheet is named, a backup is saved automatically on autosave triggers.
+ eventBus.on("worksheet:leaveBack worksheet:leaveForward worksheet:segment-clicked",
+ function (e) {
+ eventBus.trigger("app:autosave");
+ });
};
// This starts the application. First of all we ask the server for configuration information, and then prepare the
@@ -135,6 +141,29 @@ var app = function () {
});
};
+ // Support for asynchronous autosave
+ var autosave_interval = 60000;
+ var async_autosave = undefined;
+
+ var do_autosave = function (filename) {
+ var filename_as = filename + "~";
+ $.post("/save", {
+ "worksheet-filename": filename_as,
+ "worksheet-data": self.worksheet().toClojure()
+ }).fail(function () {
+ self.flashStatusMessage(
+ "Failed to autosave worksheet: " + filename
+ + " as: " + filename_as, 2000);
+ });
+ clearTimeout(async_autosave);
+ async_autosave = undefined;
+ };
+ var autosave = function(filename) {
+ if(!async_autosave)
+ async_autosave = setTimeout(function () {do_autosave(filename);},
+ autosave_interval);
+ }
+
var loadFromFile = function (filename) {
// ask the backend to load the data from disk
$.get("/load", {"worksheet-filename": filename})
@@ -204,6 +233,12 @@ var app = function () {
} else self.saveDialog.show();
});
+ eventBus.on("app:autosave", function () {
+ var fname = self.filename();
+ if (fname !== "")
+ autosave(fname);
+ });
+
eventBus.on("app:saveas", function () {
var fname = self.filename();
self.saveDialog.show(fname);
Thanks for this, I would definitely like to incorporate something like this into the next release.
Just in case this helps someone, I've been quite happy with this poor man's version of autosave :)
window.setInterval(function() { eventBus.trigger("app:save"); }, 1000 * 60);
@zb0th lovely hack. Running in incognito window to solve for browser keybinding conflicts resulted in lost work.
Is there any progress on this? This has bitten me repeatedly, which is a petty because I otherwise really love working with gorilla-repl. I would also be happy with a You have unsaved changes. Want to save all changed files?
prompt when Ctrl + C
ing.