From 8ecb501a9a9c71855bb0b8e192b5f1f87df08bcb Mon Sep 17 00:00:00 2001 From: Mikhail Chekan Date: Sun, 22 Aug 2021 13:29:39 +0800 Subject: [PATCH] Make toolbar actions lift the 'text changed' flag --- static/editor.js | 8 ++++---- static/toolbar.js | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/static/editor.js b/static/editor.js index 64c9d2d..79f8973 100644 --- a/static/editor.js +++ b/static/editor.js @@ -1,20 +1,20 @@ (function () { - let changed = false; + window.hyphaChanged = false; let textarea = document.querySelector('.edit-form__textarea'); let form = document.querySelector('.edit-form'); let warnBeforeClosing = function (ev) { - if (!changed) return; + if (!window.hyphaChanged) return; ev.preventDefault(); return ev.returnValue = 'Are you sure you want to exit? You have unsaved changes.'; }; textarea.addEventListener('input', function () { - changed = true; + window.hyphaChanged = true; }); form.addEventListener('submit', function () { - changed = false; + window.hyphaChanged = false; }); window.addEventListener('beforeunload', warnBeforeClosing); diff --git a/static/toolbar.js b/static/toolbar.js index e8c46f3..491493e 100644 --- a/static/toolbar.js +++ b/static/toolbar.js @@ -13,6 +13,7 @@ function getSelectedText(el = editTextarea) { function textInserter(text, cursorPosition = null, el = editTextarea) { return function() { + window.hyphaChanged = true; const [start, end] = [el.selectionStart, el.selectionEnd] el.setRangeText(text, start, end, 'select') el.focus() @@ -26,6 +27,7 @@ function textInserter(text, cursorPosition = null, el = editTextarea) { function selectionWrapper(cursorPosition, prefix, postfix = null, el = editTextarea) { return function() { + window.hyphaChanged = true; const [start, end] = [el.selectionStart, el.selectionEnd] if (postfix == null) { postfix = prefix