Closed (fixed)
Project:
Panopoly
Version:
8.x-2.x-dev
Component:
Magic
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
5 Aug 2019 at 21:19 UTC
Updated:
10 Mar 2020 at 21:29 UTC
Jump to comment: Most recent, Most recent file

Comments
Comment #2
dsnopekMy comment from #3055472-8: How to do (live) block preview in Layout Builder? that related to this patch was:
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:
Comment #3
dsnopekComment #5
dsnopekOh, and the actual patch that @arshadcn wrote!
Comment #6
dsnopekHere'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.
Comment #7
dsnopekSo, 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!
Comment #8
dsnopekHere'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
LayoutBuilderChooseBlockControllerandLayoutBuilderControllerwhich 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.Comment #9
dsnopekAlso, 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.Comment #10
dsnopekA couple more notes:
Comment #11
dsnopekSo, 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.
Comment #12
dsnopekFound 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.
Comment #13
dsnopekFinally! Here's a patch that makes it so clicking the "X" cancels the changes and removes the preview.
Comment #14
dsnopekI'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...
Comment #15
dsnopekOk, 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...
Comment #16
dsnopekI 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!
Comment #17
dsnopekFinally, 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.
Comment #18
dsnopekHere'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!
Comment #19
dsnopekHm, I messed up that patch. Here's one with some bytes :-)
Comment #20
dsnopekI'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
Comment #21
dsnopekOh, and here's a new version of the patch that disables the live preview for reusable widgets, so at least nothing dangerous will happen.
Comment #23
dsnopekCommitted :-)