arduino / lab-micropython-editor

Arduino Lab for MicroPython is an Integrated Development Environment (IDE) for MicroPython.
GNU Affero General Public License v3.0
224 stars 28 forks source link

Alert user before closing tab with unsaved changes #108

Closed murilopolese closed 7 months ago

murilopolese commented 7 months ago

Summary:

Prompt user with a native confirmation dialog when they are closing a file that has unsaved changes.

Solution:

We had a CodeMirror plugin already in place to update the content on a class property so the place to send a signal about code change was already in place.

Since we instantiate the CodeMirrorEditor instance through choo's cache, we can't pass a callback through parameters so we need to do a method overwrite. I thought it would be natural to call it onChange and be called without arguments.

On the store.js file, we instantiate a new file when we start the app and when we open a file. I overwrote the default onChange method by a local function that sets the file object property hasChange to true and calls for a screen re-render.

The hasChanges property won't compare previous and current content of the tab but if the file has received any new input, it will trigger there are changes if you write and erase what you wrote.