diff --git a/modules/panopoly/panopoly_magic/js/panopoly-magic-preview.js b/modules/panopoly/panopoly_magic/js/panopoly-magic-preview.js index 144acbef..c836aa89 100644 --- a/modules/panopoly/panopoly_magic/js/panopoly-magic-preview.js +++ b/modules/panopoly/panopoly_magic/js/panopoly-magic-preview.js @@ -1,5 +1,11 @@ (function ($, Drupal) { + // Used to only update preview after changes stop. + let timer = null; + + // Used to preserve focus after preview renders. + let preserveFocusForElement = null; + Drupal.behaviors.panopolyMagicLiveUpdate = { attach: function (context) { // e.keyCode: key @@ -27,13 +33,19 @@ const $form = $(context).find(form_selector).addBack(form_selector); const $preview = $form.find('.panopoly-magic-live-preview'); + // If the layout builder section has been reloaded, and we wish to preserve focus for a particular element, + // then put focus back on it here. + if (context.id === 'layout-builder' && preserveFocusForElement !== null) { + setTimeout(function () { + preserveFocusForElement.focus(); + preserveFocusForElement = null; + }, 0); + } + if ($form.length === 0 || $preview.length === 0) { return; } - // Used to only update preview after changes stop. - let timer; - function triggerSubmit() { if (timer) { clearTimeout(timer); @@ -49,6 +61,9 @@ // Disable the preview button. $preview.prop('disabled', true); + // Store the element that currently has focus, so we can preserve it after the preview reloads. + preserveFocusForElement = document.activeElement; + // Note: .click does not work here. // See https://drupal.stackexchange.com/questions/11638/how-to-programmatically-trigger-a-click-on-an-ajax-enabled-form-submit-button $preview.mousedown(); diff --git a/modules/panopoly/panopoly_magic/src/Form/PanopolyMagicLayoutBuilderUpdateBlockForm.php b/modules/panopoly/panopoly_magic/src/Form/PanopolyMagicLayoutBuilderUpdateBlockForm.php index ae1466c2..ce3be185 100644 --- a/modules/panopoly/panopoly_magic/src/Form/PanopolyMagicLayoutBuilderUpdateBlockForm.php +++ b/modules/panopoly/panopoly_magic/src/Form/PanopolyMagicLayoutBuilderUpdateBlockForm.php @@ -29,7 +29,8 @@ class PanopolyMagicLayoutBuilderUpdateBlockForm extends UpdateBlockForm { ], ], '#ajax' => [ - 'callback' => '::ajaxSubmit' + 'callback' => '::ajaxSubmit', + 'disable-refocus' => TRUE, ], ]; $form['actions']['cancel'] = [