From 004a708e933e95313e6d48ba5a18bea9ebeedd16 Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Sun, 14 Apr 2024 08:52:28 +0200 Subject: [PATCH] feat(SLB-209): debounce-based autosave --- .../sync/silverback_autosave.settings.yml | 2 +- .../js/silverback_autosave.js | 25 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/apps/cms/config/sync/silverback_autosave.settings.yml b/apps/cms/config/sync/silverback_autosave.settings.yml index 9fc203c20..c67f05571 100644 --- a/apps/cms/config/sync/silverback_autosave.settings.yml +++ b/apps/cms/config/sync/silverback_autosave.settings.yml @@ -1,6 +1,6 @@ _core: default_config_hash: HaTNbtQe6_A0WF-ZElHNWcv0VK_aqOj5lBEFhAgY85s -interval: 3000 +interval: 2000 only_on_form_change: false active_on: content_entity_forms: true diff --git a/packages/drupal/silverback_autosave/js/silverback_autosave.js b/packages/drupal/silverback_autosave/js/silverback_autosave.js index cfebc5704..c4501987f 100644 --- a/packages/drupal/silverback_autosave/js/silverback_autosave.js +++ b/packages/drupal/silverback_autosave/js/silverback_autosave.js @@ -84,7 +84,7 @@ Drupal.autosaveForm.form.submit(function () { if (Drupal.autosaveForm.autosaveFormRunning) { Drupal.autosaveForm.autosaveFormRunning = false; - clearInterval(Drupal.autosaveForm.timer); + clearTimeout(Drupal.autosaveForm.timer); Drupal.autosaveForm.timer = null; } }); @@ -100,7 +100,7 @@ !Drupal.autosaveForm.autosaveFormRunning && Drupal.autosaveForm.timer ) { - clearInterval(Drupal.autosaveForm.timer); + clearTimeout(Drupal.autosaveForm.timer); Drupal.autosaveForm.timer = null; } else { return; @@ -237,7 +237,7 @@ ) { if (xmlhttprequest.status === 0 || xmlhttprequest.status >= 400) { Drupal.autosaveForm.autosaveFormRunning = false; - clearInterval(Drupal.autosaveForm.timer); + clearTimeout(Drupal.autosaveForm.timer); Drupal.autosaveForm.timer = null; if (!Drupal.autosaveForm.beforeUnloadCalled) { @@ -307,16 +307,17 @@ ); }, 500); } - // TODO: Implement a mechanism that takes user interaction into account. - // E.g. autosave after the user did not interact for 2 seconds. - Drupal.autosaveForm.timer = setInterval(function () { - if (!Drupal.ajax.instances.some(isAjaxing)) { - triggerAjaxSubmitWithoutProgressIndication( - Drupal.autosaveForm.autosave_submit_class, - ); - } - }, Drupal.autosaveForm.interval); + $('body').on('click keyup', () => { + clearTimeout(Drupal.autosaveForm.timer); + Drupal.autosaveForm.timer = setTimeout(function () { + if (!Drupal.ajax.instances.some(isAjaxing)) { + triggerAjaxSubmitWithoutProgressIndication( + Drupal.autosaveForm.autosave_submit_class, + ); + } + }, Drupal.autosaveForm.interval); + }); } }