Skip to content

Commit

Permalink
Merge pull request #483 from bemoody/admin-textarea-height
Browse files Browse the repository at this point in the history
Fix ugly HTML source editor when TinyMCE is used inside Django admin
  • Loading branch information
bemoody authored Feb 6, 2025
2 parents ff015be + f365409 commit ceceeb6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"toolbar": "undo redo | blocks | "
"bold italic backcolor | alignleft aligncenter "
"alignright alignjustify | bullist numlist outdent indent | "
"removeformat | help",
"removeformat | code | help",
}

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
11 changes: 6 additions & 5 deletions tests/testapp/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.contrib.flatpages.models import FlatPage
from django.urls import reverse

from tests.testapp.models import TestInline, TestPage
from tinymce.widgets import TinyMCE
from tests.testapp.models import TestInline, TestModel, TestPage
from tinymce.widgets import AdminTinyMCE


class TinyMCETestInlineAdmin(admin.StackedInline):
Expand All @@ -14,7 +14,7 @@ class TinyMCETestInlineAdmin(admin.StackedInline):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name in ("content1", "content2"):
return db_field.formfield(
widget=TinyMCE(
widget=AdminTinyMCE(
attrs={"cols": 80, "rows": 30},
mce_attrs={"external_link_list_url": reverse("tinymce-linklist")},
)
Expand All @@ -26,7 +26,7 @@ class TinyMCEFlatPageAdmin(FlatPageAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == "content":
return db_field.formfield(
widget=TinyMCE(
widget=AdminTinyMCE(
attrs={"cols": 80, "rows": 30},
mce_attrs={"external_link_list_url": reverse("tinymce-linklist")},
)
Expand All @@ -40,7 +40,7 @@ class TinyMCETestPageAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name in ("content1", "content2"):
return db_field.formfield(
widget=TinyMCE(
widget=AdminTinyMCE(
attrs={"cols": 80, "rows": 30},
mce_attrs={"external_link_list_url": reverse("tinymce-linklist")},
)
Expand All @@ -51,3 +51,4 @@ def formfield_for_dbfield(self, db_field, **kwargs):
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, TinyMCEFlatPageAdmin)
admin.site.register(TestPage, TinyMCETestPageAdmin)
admin.site.register(TestModel)
15 changes: 15 additions & 0 deletions tinymce/static/django_tinymce/admin_tinymce.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Workaround for the following rule defined in
django/contrib/admin/static/admin/css/responsive.css (which is
unsuitable for textareas used by TinyMCE internally):
@media (max-width: 1024px) {
textarea {
max-width: 100%;
max-height: 120px;
}
}
*/
.tox .tox-textarea-wrap .tox-textarea {
max-height: none;
}
8 changes: 7 additions & 1 deletion tinymce/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ def _media(self):


class AdminTinyMCE(TinyMCE, admin_widgets.AdminTextareaWidget):
pass
def _media(self):
css = {
"all": ["django_tinymce/admin_tinymce.css"],
}
return forms.Media(css=css) + super().media

media = property(_media)


def get_language_from_django():
Expand Down

0 comments on commit ceceeb6

Please sign in to comment.