From e1995c075f1014126cb156b7a306d7109964e6fd Mon Sep 17 00:00:00 2001 From: Matt Furden Date: Wed, 30 Nov 2022 00:12:29 -0800 Subject: [PATCH] Prevent double save dialog boxes by throttling save click As it takes about half a second for the save dialog box to appear, its easy to click the save button twice and have a second save dialog box appear after you save the image. Throttle the save button to only be pressed once a second to prevent this from happening. --- webview/src/index.js | 6 +++--- webview/src/util.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/webview/src/index.js b/webview/src/index.js index 49107ba..e967253 100644 --- a/webview/src/index.js +++ b/webview/src/index.js @@ -1,4 +1,4 @@ -import { $, $$, setVar } from './util.js'; +import { $, $$, setVar, throttle } from './util.js'; import { pasteCode } from './code.js'; import { takeSnap, cameraFlashAnimation } from './snap.js'; @@ -74,8 +74,8 @@ window.addEventListener('message', ({ data: { type, ...cfg } }) => { btnCopy.textContent = "Save As..." } - btnSave.addEventListener('click', actions[0]) - btnCopy.addEventListener('click', actions[1]) + btnSave.addEventListener('click', throttle(actions[0], 1000)) + btnCopy.addEventListener('click', throttle(actions[1], 1000)) if(!showLineNumbers) { document.getElementById('showLineNumBtn').children[0].children[0].classList.toggle('opacity-0'); diff --git a/webview/src/util.js b/webview/src/util.js index c305510..a4b8871 100644 --- a/webview/src/util.js +++ b/webview/src/util.js @@ -18,3 +18,13 @@ export const calcTextWidth = (text) => { div.remove(); return width + 1 + 'px'; }; + +export const throttle = (func, limit) => { + let lastRan; + return (...args) => { + if (!lastRan || Date.now() - lastRan >= limit) { + func.apply(null, ...args); + lastRan = Date.now(); + } + }; +}; \ No newline at end of file