From 733bd84c6a5b3466823b7a9cc2da009d44702c49 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Mon, 30 Dec 2024 12:29:56 +0100 Subject: [PATCH] fix: Proper registration of third-party RTE (CKEditor4 or 5, or other) (#47) --- CHANGELOG.rst | 5 +++++ djangocms_text/__init__.py | 2 +- djangocms_text/cms_plugins.py | 4 ++-- djangocms_text/editors.py | 9 +++++---- djangocms_text/models.py | 6 +++++- private/css/cms.tiptap.css | 1 + tests/integration/test_ckeditor4.py | 16 ++++++++++++++++ 7 files changed, 35 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 1adaa014..4c5d2406 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,11 @@ Changelog ========= +0.5.1 (30-12-2024) +================== + +* fix: Proper registration of third-party RTE (CKEditor4 or 5, or other) by @fsbraun in https://github.com/django-cms/djangocms-text/pull/47 + 0.5.0 (28-12-2024) ================== diff --git a/djangocms_text/__init__.py b/djangocms_text/__init__.py index 18bcd208..1934205c 100644 --- a/djangocms_text/__init__.py +++ b/djangocms_text/__init__.py @@ -16,4 +16,4 @@ 10. Github actions will publish the new package to pypi """ -__version__ = "0.5.0" +__version__ = "0.5.1" diff --git a/djangocms_text/cms_plugins.py b/djangocms_text/cms_plugins.py index 4d2e71e0..543d5ca7 100644 --- a/djangocms_text/cms_plugins.py +++ b/djangocms_text/cms_plugins.py @@ -49,7 +49,7 @@ from . import settings from .forms import ActionTokenValidationForm, RenderPluginForm, TextForm from .html import render_dynamic_attributes -from .models import Text +from .models import Text, _MAX_RTE_LENGTH from .utils import ( OBJ_ADMIN_WITH_CONTENT_RE_PATTERN, _plugin_tags_to_html, @@ -712,7 +712,7 @@ def save_model(self, request, obj, form, change): # subclassing cms_plugin_instance (one to one relation) value = getattr(self.cms_plugin_instance, field.name) setattr(obj, field.name, value) - obj.rte = rte_config.name + obj.rte = rte_config.name[:_MAX_RTE_LENGTH] super().save_model(request, obj, form, change) # This must come after calling save # If `clean_plugins()` deletes child plugins, django-treebeard will call diff --git a/djangocms_text/editors.py b/djangocms_text/editors.py index 2d0e8572..9452f8ad 100644 --- a/djangocms_text/editors.py +++ b/djangocms_text/editors.py @@ -485,14 +485,15 @@ def get_editor_config(editor: Optional[str] = None) -> RTEConfig: """ config = editor or getattr(settings, "TEXT_EDITOR", "tiptap") - if "." in config and config not in configuration: + config_name = config.rsplit(".", 1)[-1] + if config_name not in configuration: # Load the configuration from the module module = __import__(config.rsplit(".", 1)[0], fromlist=[""]) - rte_config_instance = getattr(module, config.rsplit(".", 1)[-1]) + rte_config_instance = getattr(module, config_name) # Cache editor configuration - rte_config_instance.name = config + rte_config_instance.name = config_name register(rte_config_instance) - return configuration[config] + return configuration[config_name] def get_editor_base_config(editor: Optional[str] = None) -> dict: diff --git a/djangocms_text/models.py b/djangocms_text/models.py index f1980615..d54676a9 100644 --- a/djangocms_text/models.py +++ b/djangocms_text/models.py @@ -7,6 +7,10 @@ from django.utils.text import Truncator from django.utils.translation import gettext_lazy as _ + +_MAX_RTE_LENGTH = 16 + + if apps.is_installed("cms"): from cms.models import CMSPlugin @@ -52,7 +56,7 @@ class AbstractText(CMSPlugin): rte = models.CharField( default="", blank=True, - max_length=16, + max_length=_MAX_RTE_LENGTH, help_text="The rich text editor used to create this text. JSON formats vary between editors.", ) diff --git a/private/css/cms.tiptap.css b/private/css/cms.tiptap.css index 7c631ca2..36fd4f8b 100644 --- a/private/css/cms.tiptap.css +++ b/private/css/cms.tiptap.css @@ -12,6 +12,7 @@ max-width: 100%; /* for djangocms-admin-style */ .ProseMirror { + height: 100%; overflow-y: scroll; padding: 0 0.5rem; diff --git a/tests/integration/test_ckeditor4.py b/tests/integration/test_ckeditor4.py index 3f247b54..63299a25 100644 --- a/tests/integration/test_ckeditor4.py +++ b/tests/integration/test_ckeditor4.py @@ -37,3 +37,19 @@ def handle_console_message(msg): expect(page.locator(".cke_button.cke_button__bold")).to_be_visible() # a button in the menu bar assert not console_errors, f"Console errors found: {console_errors}" + + +@pytest.mark.django_db +@pytest.mark.skipif(not DJANGO_CMS4, reason="Integration tests only work on Django CMS 4") +def test_editor_saves(live_server, page, text_plugin, superuser, use_ckeditor4): + """Test that tiptap editor loads and initializes properly""" + # Navigate to the text plugin add view + login(live_server, page, superuser) + + page.goto(f"{live_server.url}{admin_reverse('cms_placeholder_edit_plugin', args=(text_plugin.pk,))}") + + save_button = page.locator('input[type="submit"]') + save_button.click() + + messagelist = page.locator("div.messagelist") + assert '
' in messagelist.inner_html().strip()