Skip to content

Commit

Permalink
feat(SLB-209): debounce-based autosave
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Apr 14, 2024
1 parent 7dd097f commit 004a708
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/cms/config/sync/silverback_autosave.settings.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 13 additions & 12 deletions packages/drupal/silverback_autosave/js/silverback_autosave.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
Expand All @@ -100,7 +100,7 @@
!Drupal.autosaveForm.autosaveFormRunning &&
Drupal.autosaveForm.timer
) {
clearInterval(Drupal.autosaveForm.timer);
clearTimeout(Drupal.autosaveForm.timer);
Drupal.autosaveForm.timer = null;
} else {
return;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
}
}

Expand Down

0 comments on commit 004a708

Please sign in to comment.