-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editor - Prompt warning when overwriting a file that is modified on disk #2783
Editor - Prompt warning when overwriting a file that is modified on disk #2783
Conversation
Hi @minrk, @takluyver, can one of you look over this pull request? Suggestions, and criticism welcome. The left over personal tasks for me are to remove some of the personal comments, and set up tests for this. |
Thanks! We're just about to release notebook 5.1, and most of the team are currently at Jupytercon, so it might be a week or two before we can look at this properly. I can probably have a quick look today, but I'd like others to have a look too. |
notebook/static/edit/js/editor.js
Outdated
|
||
var _save = function () { | ||
// What does this event do? Does this always need to happen, | ||
// even if the file can't be saved? What is dependent on it? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that nothing in our code currently listens for it, but it parallels an event notebook_saving.Notebook
which is used to show a 'Saving notebook' notification. It's possible that we'll want something similar in the future, and I don't think it hurts to have an event.
There should probably be a corresponding file_save_failed.Editor
event to fire on failure, but that needn't be in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, I'll hold off adding in functionality for this. @takluyver, is there a backlog where we can place tasks and items, or does the issues tab function as this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, it's the issues. Thanks!
notebook/static/edit/js/editor.js
Outdated
// record change generation for isClean | ||
// (I don't know what this implies for the editor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 'generation' is a number Codemirror uses to tell whether the editor is 'clean' - whether the current text matches what is saved. This gets the current generation when we save the file. Elsewhere in the code, we pass the number to isClean
to ask Codemirror if the file has changed since the last save.
This means that if you save, type something, and Ctrl-z to undo, we know that you're back at the saved state.
So this should probably only happen when we're actually about to save. If the users cancels the dialog, this.generation
shouldn't have changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant to add this link to the Codemirror docs: http://codemirror.net/doc/manual.html#changeGeneration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, fascinating! Thanks for the explanation and link. I'll also pull the changeGeneration method within the promise 'then' statement that validates the save.
We are about to release 5.2, so I'm going to mark this for 5.3. Let's try to get this merged soon! 👍 |
@gnestor, this should be all set for a merge. I've reverted the changes I've made to the test suite that were impacting the build. I was trying to create a test for this patch but came under time constraints from other duties. Let me know if you have any suggestions for my PR, or if I could be of anymore assistance for this bug. |
@unnamedplay-r I checked this out and tested it and it appears to be working but not as I would expect. Master:
This PR:
|
@gnestor, when I perform the steps you provided, I don't experience any of the behavior you're describing. This has been tested on Mac's common browsers: Safari, Chrome, Firefox with cache cleared, and also done in private/incognito modes. When I save a modified file, then re-save it, I'm not presented the overwrite modal. The modal appears only until I modify the second file in the other tab and save. Two questions:
|
I tested this in Firefox and saw the same behaviour that @gnestor described, and I noticed an error message in the JS console about " @unnamedplay-r I'm not sure why it was working for you but not for us. As I could see the error, I hope you don't mind me fixing it directly! |
cc @gnestor - if you're happy with this, I think it's ready to merge. |
Thanks for digging into the error and fixing it @takluyver. It's highly appreciated! |
// We want to check last_modified (disk) > that.last_modified (our last save) | ||
// In some cases the filesystem reports an inconsistent time, | ||
// so we allow 0.5 seconds difference before complaining. | ||
if ((last_modified.getTime() - that.last_modified.getTime()) > 500) { // 500 ms |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We recently merged a pull request to make this 0.5s difference configurable in the notebook - #3273 . Could you have a look at that and copy the functionality here? Thanks!
@takluyver I should've caught that console error 😳 @unnamedplay-r It's working great! Thank you!! 🙏 |
Will this change apply also to jupyterlab or should I file a similar issues there as well? |
This behavior already exists in JupyterLab, so no further action is necessary. |
I'm reasonable sure that I am not seeing this behavior. e.g I have an open editor and do a git checkout but don't see a warning etc |
Ah okay, it doesn't notify until you try to save which is a little annoying. Might be nice for it to poll / check on some basis or events. |
Closes #2726.
Adds functionality to issue the user a warning and prompts them to overwrite the file currently modified on disk.
For any reviewer, I have a question:
TODO
Personal Notes
Some notes I need to ponder about next time I take this on:
{Editor}.events.trigger('file_renamed.Editor', model)
, what assumptions do these events come with? Should we be modifying the{Editor}.last_modified
property after, or before these events?last modified
variable._clean_state
function do, and what is thecodemirror
?_last_modified
in savewidget.js for ensuring the renaming of the file, but isn't found in the main editor class? Can we use this instead of creating another variable within the Editor class? Also, why is it used to display information about the last save event to the user in base/js/notebook.js (using this file to model the functionality)?