Skip to content

Commit

Permalink
feat: allows an admin_css: tuple in RTEConfig for a list of CSS f…
Browse files Browse the repository at this point in the history
…iles only to be loaded into the admin for the editor (#49)
  • Loading branch information
fsbraun authored Jan 4, 2025
1 parent 8426926 commit 182fabf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions djangocms_text/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def get_editor_widget(self, request, plugins, plugin):
action_token=action_token,
revert_on_cancel=settings.TEXT_CHILDREN_ENABLED and rte_config.child_plugin_support,
body_css_classes=self._get_body_css_classes_from_parent_plugins(plugin),
add_admin_css=True,
)
else:
widget = TextEditorWidget(
Expand Down
16 changes: 10 additions & 6 deletions djangocms_text/editors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Optional
from __future__ import annotations

from collections.abc import Iterable

from django.conf import settings
Expand Down Expand Up @@ -436,8 +437,9 @@ def __init__(
self,
name: str,
config: str,
js: Iterable[str] = None,
css: dict = None,
js: Iterable[str] | None = None,
css: dict | None = None,
admin_css: dict | None = None,
inline_editing: bool = False,
child_plugin_support: bool = False,
):
Expand All @@ -446,6 +448,7 @@ def __init__(
self.config = config
self.js = js or []
self.css = css or {}
self.admin_css = admin_css or ()
self.inline_editing = inline_editing
self.child_plugin_support = child_plugin_support

Expand Down Expand Up @@ -474,7 +477,7 @@ def register(editor: RTEConfig):
configuration[editor.name] = editor


def get_editor_config(editor: Optional[str] = None) -> RTEConfig:
def get_editor_config(editor: str | None = None) -> RTEConfig:
"""
Returns the editor configuration.
Expand All @@ -494,7 +497,7 @@ def get_editor_config(editor: Optional[str] = None) -> RTEConfig:
return configuration[config_name]


def get_editor_base_config(editor: Optional[str] = None) -> dict:
def get_editor_base_config(editor: str | None = None) -> dict:
"""
Returns the base configuration for the editor.
Expand All @@ -510,7 +513,8 @@ def get_editor_base_config(editor: Optional[str] = None) -> dict:
name="tiptap",
config="TIPTAP",
js=("djangocms_text/bundles/bundle.tiptap.min.js",),
css={"all": ("djangocms_text/css/bundle.tiptap.min.css", "djangocms_text/css/tiptap.admin.css")},
css={"all": ("djangocms_text/css/bundle.tiptap.min.css",)},
admin_css=("djangocms_text/css/tiptap.admin.css",),
inline_editing=True,
child_plugin_support=True,
)
Expand Down
2 changes: 1 addition & 1 deletion djangocms_text/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def formfield(self, **kwargs):
# override the admin widget
if defaults["widget"] == admin_widgets.AdminTextareaWidget:
# In the admin the URL endpoint is available
defaults["widget"] = TextEditorWidget(configuration=self.configuration)
defaults["widget"] = TextEditorWidget(configuration=self.configuration, add_admin_css=True)
return super().formfield(**defaults)

def clean(self, value, model_instance):
Expand Down
7 changes: 6 additions & 1 deletion djangocms_text/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,16 @@ class TextEditorWidget(forms.Textarea):
@property
def media(self):
rte_config = get_editor_config()
rte_css = rte_config.css.get("all", ())
if self.add_admin_css:
rte_css += rte_config.admin_css
return forms.Media(
css={
**rte_config.css,
"all": (
"djangocms_text/css/cms.text.css",
"djangocms_text/css/cms.normalize.css",
*rte_config.css.get("all", ()),
*rte_css,
),
},
js=(
Expand All @@ -120,6 +123,7 @@ def __init__(
action_token: str = None,
revert_on_cancel: bool = False,
body_css_classes: str = "",
add_admin_css: bool = False,
):
"""
Create a widget for editing text + plugins.
Expand Down Expand Up @@ -156,6 +160,7 @@ def __init__(
self.action_token = action_token # specific
self.revert_on_cancel = revert_on_cancel
self.body_css_classes = body_css_classes if body_css_classes else self.configuration.get("bodyClass", "")
self.add_admin_css = add_admin_css

def render_textarea(self, name, value, attrs=None, renderer=None):
return super().render(name, value, attrs, renderer)
Expand Down

0 comments on commit 182fabf

Please sign in to comment.