Spinning this out based on @arshadcn's work on #3055472-6: How to do (live) block preview in Layout Builder?.

I've been looking into this for a while now. I've tested a few core patches and tried some other contrib solutions. I like the direction that @tim.plunkett implemented in #2999580: Make blocks appear in the layout builder without saving formatter settings.

Attached is a proof-of-concept patch [...snip...] for the panopoly_magic module.

This patch contains suggestions from @tedbow in #6.

Here's how it works:

  1. We take over the layout_builder.add_block form and replace with a custom controller.
  2. When you click on a block to add to the layout builder, instead of showing the configure form, we programmatically insert the block into the layout builder first, then opens the configuration form.
  3. For live preview, the layout_builder_update_block form is enhanced with a preview button and a live preview library.
  4. On preview, the configuration for the block is updated, but not the layout temp storage. This allows previewing without updating the actual layout until the user clicks update.
  5. This allows block to be added, configured and previewed without affecting the layout like we had in Panopoly 1.x
  6. Config validation on previewing still works

Here's a quick gif:

Preview

Let's test 👍(Note: this does not work with content fields for now)

TODO

  • Rename the added classes to match existing classes (namely, remove "PanopolyMagic*" prefix
  • Prevent focus getting moved away when preview refreshes
  • Cancel changes when user presses the "X" in the off canvas tray
  • Don't make changes to reusable blocks until user actually presses "Save" (ie. preview shouldn't update the content, just the display)

Comments

dsnopek created an issue. See original summary.

dsnopek’s picture

My comment from #3055472-8: How to do (live) block preview in Layout Builder? that related to this patch was:

The only issue I noticed is that edits in the WYSIWYG don't seem to trigger the preview to update. We had support for that in Panopoly 1.x, but it was specific to TinyMCE - we'll need to implement something similar to for CKEditor.

Played with it a little more, I had another thought:

If you close the system tray, the changes that you last made will remain in the preview, even though they weren't actually saved. This could give users the impression that they don't need to save to keep their changes in the tray, and lead to big surprises when they actually save the layout.

We should probably do two things when the close button is hit:

  1. Revert the preview so it reloads from the tempstore
  2. Show a confirmation of some kind letting the user know they are discarding changes
dsnopek’s picture

dsnopek credited arshadcn.

dsnopek’s picture

Status: Active » Needs review
StatusFileSize
new10.56 KB

Oh, and the actual patch that @arshadcn wrote!

dsnopek’s picture

Status: Needs review » Needs work
StatusFileSize
new11.09 KB
new2.23 KB

Here's an updated patch that adds support for WYSIWYG editors and fixes a bug where pressing SHIFT was causing the preview to update (even though it should have been ignored).

I'm still trying to figure out how to "cancel" the edits that are previewed but not saved.

dsnopek’s picture

So, on further testing (and I don't know if this was always the case and I didn't notice, or if this is new), it appears that the preview is causing the fields to get saved on a content block, even if you don't save it. Although, that could have to do with another Panopoly feature or patch, because those might not be editable in core... Anyway, that'll need to be fixed for Panopoly!

dsnopek’s picture

StatusFileSize
new10.7 KB

Here's the patch re-rolled after #3073658: Implement preview when choosing a block to add in layout_builder was merged.

There's some naming descrepencies I'd like address before merge, namely that the added controller and form use PanopolyMagic as a prefix and I'm not crazy about that as we are essentially overriding stuff from layout builder and we already have panopoly_magic in the namespace (so it's redundant). However, then we end up with a LayoutBuilderChooseBlockController and LayoutBuilderController which is also a little weird. I couldn't come up with a better name for that 2nd one so I punted on the whole renaming issue for now.

dsnopek’s picture

Also, I'm not entirely sure why we even need PanopolyMagicLayoutBuilderController::addBlock(). Couldn't/shouldn't we make an add form class that does the work it's doing there, just like how layout_builder is doing it? It may be necessary for some reason, but it isn't obvious from the code... we should add a comment explaining if it is necessary.

dsnopek’s picture

A couple more notes:

  • It looks like the beginnings of an attempt at cancelling block edits (see #2) snuck into my re-roll above. Sorry!
  • My issues in #7 appear to only happen with reusable blocks! Preview changes don't automatically get saved for normal block plugins or inline blocks. Whew! Still needs to be solved, but it's a less serious problem for sure.
dsnopek’s picture

StatusFileSize
new12.52 KB
new2.93 KB

So, I've been attempting to make it so that clicking the 'X' button to close the off canvas dialog would click the "Cancel" button to cancel out the changes. However, everything I've tried so far has failed to work, and I'm super frustrated. :-( Here's a patch with the non-working code I've been messing with so far.

dsnopek’s picture

Issue summary: View changes

Found an additional issue: the focus is getting moved away from the current control when preview renders. Since the preview will render just be stopping typing for a second, this can make editing very annoying.

Since there's quite a few issues going now, I've made a TODO section in the issue summary.

dsnopek’s picture

Issue summary: View changes
StatusFileSize
new11.67 KB
new2.23 KB

Finally! Here's a patch that makes it so clicking the "X" cancels the changes and removes the preview.

dsnopek’s picture

StatusFileSize
new12.34 KB
new2.58 KB

I've been working on preserving the focus when the preview reloads, and struggling to find a good way to make it work.

I tried adding some code to save which element was focused and then restoring it, which sort of works for anything except WYSIWYG, but in the case of text inputs it doesn't put the caret back where it was, which makes typing in the middle hard. There is also this sort of "flash" where the input loses focus and then gets it back, which could possibly mess up someone who was actively typing. In any case, I've attached the patch that does that for reference.

The best solution here is going to be preventing the focus from being lost in the first place! I'm not totally sure what's causing it, though. The Drupal AJAX system has some "refocus" logic, but even with that disabled focus is lost. We're doing 'mousedown' on the button, which will steal focus, but we should be able to implement a 'mousedown' handler that prevents the default which should stop that, but it isn't working either in my testing (it does work when testing with a minimal HTML page).

Still searching...

dsnopek’s picture

Ok, it looks like core/misc/dialog/dialog.ajax.es6.js is causing this. Whenever the AJAX is run on an element in a dialog, it's behavior attach will force the dialog to be focused.

See https://git.drupalcode.org/project/drupal/blob/8.7.x/core/misc/dialog/di...

Commenting out that line, along with my other changes, will leave focus alone.

One possible way to avoid this could be using a link rather than a button, because I think that wouldn't try to re-attach behaviors on the form. However, I think the code in dialog.ajax.es6.js might be wrong generally. What if you have a button in a dialog that triggers some AJAX, this would (I think) move the focus away from the button to the dialog as a whole, which isn't right. I think I might try to make a minimal example that does that, and then assuming it's also broken, open a core issue.

Although, with all things core, who knows if/when that would be fixed... I wonder if there's a way to make the Drupal AJAX call manually in such a way as to disable some of this behavior?

Continuing to think about this...

dsnopek’s picture

I had a really great idea, that turned out not to work after much testing and research.

The idea: we could bind to 'focus', 'focusin' or 'focusout' (one of the focus events) and prevent the focus from moving via event.preventDefault().

Unfortunately, it turns out that none of those events are cancelable. :-( This is the best info I've found about this:

https://stackoverflow.com/questions/14613804/is-there-a-way-to-use-event...

Back to the drawing board!

dsnopek’s picture

Issue summary: View changes
StatusFileSize
new12.82 KB
new2.7 KB

Finally, I've got something that works! It's using monkey patching to add a "kill switch" variable to disable the code that's messing us up, which is a pretty extreme solution, but I'm out of ideas. Anyway, the patch is attached!

For informational purposes: I also looked at doing an AJAX link rather than a button, but it seems that AJAX links are basically limited to opening dialogs, which isn't what we want to do here.

dsnopek’s picture

Issue summary: View changes
StatusFileSize
new0 bytes

Here's a patch that renames the classes. I'm not including an interdiff because it's just renaming files/classes, and that would be one big messy interdiff.

All that's left is solving the problem with reusable content blocks saving on preview!

dsnopek’s picture

StatusFileSize
new12.71 KB

Hm, I messed up that patch. Here's one with some bytes :-)

dsnopek’s picture

I've been digging into it, and handling preview of reusable blocks is going to be super tricky. And since they really don't make up most blocks, I've decided to spin that off into it's own issue so we can get this one closed out. Here's the issue:

#3116002: Live preview while editing REUSABLE blocks

dsnopek’s picture

StatusFileSize
new13.5 KB
new3.27 KB

Oh, and here's a new version of the patch that disables the live preview for reusable widgets, so at least nothing dangerous will happen.

  • dsnopek committed 24b9a8c on 8.x-2.x
    Issue #3072796 by dsnopek, arshadcn: Implement live preview while...
dsnopek’s picture

Status: Needs work » Fixed

Committed :-)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.