Prevent closing the viewer pane

Hello,

I did a package which renders the Ace editor as a HTML widget:

So the user has a second editor in RStudio :slight_smile:

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.

Do you have any suggestion? Thank you.

Impressive work!

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. :wink:

Ok, thanks!
Perhaps I will trigger the Save in the beforeunload event. Will see...

@jonathan

Do you know whether it's possible to close the viewer pane with JavaScript? I tried window.close() but this has no effect.

Nope, you can't close it with JavaScript.

Ok, thank you. I will fill an issue. I also started a package which renders the Monaco editor as a HTML widget.

@jonathan

FYI https://laustep.github.io/stlahblog/posts/AceMonaco.html :wink: