Ionaru / easy-markdown-editor

EasyMDE: A simple, beautiful, and embeddable JavaScript Markdown editor. Delightful editing for beginners and experts alike. Features built-in autosaving and spell checking.
https://stackblitz.com/edit/easymde
MIT License
2.39k stars 316 forks source link

Leave editor with shortcut #291

Closed Offerel closed 3 years ago

Offerel commented 3 years ago

I have setup easyMDE to toggle the Editor to Preview mode (from Edit mode) by pressing ESC key with:

document.onkeyup = function(e) {
    if (e.which == 27) {
        mde.togglePreview();
    }
};

Is there a way to hide the toolbar with the same key? Meaning, i will switch to a preview only mode with this. Is that possible?

Offerel commented 3 years ago

I have found a workaround. I'm using now the following code:

document.addEventListener("keyup", event => {
    if(event.key == 'Escape') {
        if(document.getElementById('estate').value == 'e') {
            mde.togglePreview();
            document.querySelector('.editor-toolbar').style.display = 'none';
            document.getElementById('estate').value = 's';
        }
    }
});

I'm using a hidden input field with id = estate, to track, if the editor is edit mode or preview mode. Not ideal, but its ok for me.