Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ugly HTML source editor when TinyMCE is used inside Django admin #483

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

bemoody
Copy link

@bemoody bemoody commented Jan 3, 2025

The standard CSS for the django.contrib.admin app contains this:

@media (max-width: 1024px) {
    textarea {
        max-width: 100%;
        max-height: 120px;
    }
}

(see https://github.com/django/django/blob/main/django/contrib/admin/static/admin/css/responsive.css)

This limits the height of the textarea used by TinyMCE's HTML source editor (the "Source Code" button), when using TinyMCE inside the Django admin app, in a 1024-pixel-or-smaller window. Combined with TinyMCE's styling, the result is unattractive and unintuitive.

(It's likely that the same problem affects any other textareas used by TinyMCE or third-party plugins, but the "Source Code" feature is the only one I know about.)

Of course it's possible for a site to override this using custom stylesheets (though it's not totally obvious how to do so in a clean way.) But since the admin app is meant to work out-of-the-box without customization, it's better for this to be handled by the widget.

Benjamin Moody added 3 commits January 3, 2025 12:43
The Django admin app's default stylesheet includes a blanket rule that
limits the height of all textarea elements, if the browser window is
less than 1024 pixels wide.

This interferes with TinyMCE's use of a textarea element in the
"Source Code" dialog.  In that dialog, the textarea (.tox-textarea) is
placed inside a div wrapper (.tox-textarea-wrap); due to TinyMCE's
stylesheet, the div has a visible border while the textarea itself has
none.  So the overall effect is that if you click the "Source Code"
button, a large dialog box appears (covering most of the browser
window), but only the top 120 pixels of that box are usable.  This is
visually confusing and makes poor use of screen space.

Fix this by adding a small additional stylesheet to the AdminTinyMCE
widget.
This allows testing of TinyMCE's source code editor feature.
The AdminTinyMCE widget is a slightly more opinionated version of the
TinyMCE widget, intended to look and behave slightly better when used
inside Django admin pages.

Additionally, add an admin interface for editing TestModel objects, to
allow testing the functionality of the HTMLField class (in the absence
of any custom ModelAdmin class.)
@bemoody
Copy link
Author

bemoody commented Jan 3, 2025

As a side note, I found (when trying to use the "testapp" example) that TINYMCE_COMPRESSOR = True doesn't work out of the box. Firefox gives the error Uncaught ReferenceError: tinyMCE is not defined; I haven't investigated why that is.

So in order to test this functionality, I also needed to edit tests/settings.py and set TINYMCE_COMPRESSOR = False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants