I did a package which renders the Ace editor as a HTML widget:
So the user has a second editor in RStudio
I would like to warn the user when he/she closes the viewer pane without having saved the code. I tried this JavaScript code:
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = '';
});
This seems to work fine at first glance:
But there's an issue: look at what happens to the viewer pane when the user presses "Cancel". The toolbar (the icons) of the viewer pane disappear, e.g. the "close" button is not there anymore.
There's nothing you can do here, unfortunately; RStudio assumes that once it has asked the viewer pane to clear that it has done so. In order for this to work, code in RStudio would need to change to account for the possibility that the user cancelled the action.
You could file an issue for this on the RStudio github repo, and/or submit a pull request addressing it.