moderntribe / square-one

Modern Tribe's legacy WordPress framework for the classic editor.
GNU General Public License v2.0
89 stars 20 forks source link

[SQONE-708]: Fix for TinyMCE floating toolbar repositioning infinite loop. #1027

Closed bjcooper closed 2 years ago

bjcooper commented 2 years ago

What does this do/fix?

Fixes an issue where the TinyMCE floating/inline toolbar (usually when editing a link in the WYSIWYG) "flickers" endlessly. This is especially likely to happen when the TinyMCE editor is inside of a repeater (as in the Accordion block, for example) due the restricted horizontal space.

The issue is ticketed here: https://core.trac.wordpress.org/ticket/44911 and everyone pretty much agrees that it exists, but no one has fixed it in ~3 years.

The issue is caused by some poor positioning logic in wp-includes/js/tinymce/plugins/wordpress/plugin.js, which sometimes places the floating toolbar slightly off-screen when positioning it next to the inline element being edited. Overflowing the body this way causes horizontal scrollbars to appear and triggers a scrollwindow event. The same plugin hides the toolbar during scroll events to be repositioned over the inline element being edited once the scrolling finishes. Hiding the toolbar causes the scrollbars to disappear and the plugin then shows the toolbar again, triggering the scrollbars, triggering the hiding of the toolbar, etc, etc.

The fix is rather simple, but difficult to apply. It would essentially be a one-liner in the core TinyMCE Wordpress plugin, were that a normal library you could patch or replace. 😢 Instead, I've created a custom TinyMCE plugin to observe the toolbar and fix the positioning in the trouble cases. TinyMCE plugins are meant to be independent and "closed", so there's no way to fix the errant positioning function directly. Instead we quickly recalculate and reposition the element ourselves after the bad positioning has been done.

QA

The ticket for this PR: https://github.com/moderntribe/turner-industries/pull/78 Original fix forMilitary Affairs: https://github.com/Microsoft-Stories/military-affairs/pull/221 Subsequent fix for Turner Industries: https://github.com/moderntribe/turner-industries/pull/78

Screenshots/video: image

Important note: This can potentially affect any inline WYSIWYG toolbar, so the potential effects of some bad code here are substantial. Also, there's already some existing "weirdness" with the TinyMCE editor toolbar positioning in some scenarios that isn't related to this fix—this fix should just make the toolbar (at-least) usable in all cases.

Tests

Does this have tests?