0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-25 05:02:57 +01:00

Adopt entity chooserUrls for Draftail richtext links

Avoid the need to load window.chooserUrls globally in the shared _editor_js.html include.

Instead pass in the lazy URLs to the link chooser entity options.
This commit is contained in:
elhussein almasri 2024-02-21 06:29:28 +00:00 committed by LB (Ben Johnston)
parent 6a532d8fe0
commit da88ec81f1
4 changed files with 122 additions and 12 deletions

View File

@ -16,6 +16,9 @@ window.Draftail = Draftail;
// Expose module as a global.
window.draftail = draftail;
/** @deprecated RemovedInWagtail70 - Ensure that any third party packages that use global.chooserUrls can still append to this global object until support is removed. */
window.chooserUrls = global.chooserUrls || {};
// Plugins for the built-in entities.
const entityTypes = [
{

View File

@ -4,16 +4,6 @@
JavaScript declarations to be included on the 'create page' and 'edit page' views
{% endcomment %}
<script>
window.chooserUrls = {
'pageChooser': '{% url "wagtailadmin_choose_page" %}',
'externalLinkChooser': '{% url "wagtailadmin_choose_page_external_link" %}',
'emailLinkChooser': '{% url "wagtailadmin_choose_page_email_link" %}',
'phoneLinkChooser': '{% url "wagtailadmin_choose_page_phone_link" %}',
'anchorLinkChooser': '{% url "wagtailadmin_choose_page_anchor_link" %}',
};
</script>
<script src="{% versioned_static 'wagtailadmin/js/comments.js' %}"></script>
<script src="{% versioned_static 'wagtailadmin/js/vendor/rangy-core.js' %}"></script>
<script src="{% versioned_static 'wagtailadmin/js/vendor/mousetrap.min.js' %}"></script>

View File

@ -3,7 +3,8 @@ import unittest
from django.conf import settings
from django.test import SimpleTestCase, TestCase
from django.test.utils import override_settings
from django.urls import reverse
from django.urls import reverse, reverse_lazy
from django.utils.html import format_html
from wagtail.admin.rich_text import DraftailRichTextArea, get_rich_text_editor_widget
from wagtail.admin.rich_text.converters.editor_html import PageLinkHandler
@ -11,6 +12,7 @@ from wagtail.admin.rich_text.editors.draftail.features import Feature
from wagtail.blocks import RichTextBlock
from wagtail.models import Page, get_page_models
from wagtail.rich_text import RichText
from wagtail.rich_text.feature_registry import FeatureRegistry
from wagtail.test.testapp.models import SingleEventPage
from wagtail.test.testapp.rich_text import CustomRichTextArea, LegacyRichTextArea
from wagtail.test.utils import WagtailTestUtils
@ -491,3 +493,103 @@ class TestDraftailFeature(SimpleTestCase):
media_html = str(feature.media)
self.assertRegex(media_html, r"feature.js\?v=(\w+)")
self.assertRegex(media_html, r"feature.css\?v=(\w+)")
class TestRichTextChooserUrls(WagtailTestUtils, BaseRichTextEditHandlerTestCase):
def setUp(self):
super().setUp()
# Find root page
self.root_page = Page.objects.get(id=2)
self.login()
@override_settings(
WAGTAILADMIN_RICH_TEXT_EDITORS={
"default": {
"WIDGET": "wagtail.admin.rich_text.DraftailRichTextArea",
},
}
)
def test_chooser_urls_exist(self):
features = FeatureRegistry()
link = features.get_editor_plugin("draftail", "link")
self.assertIsNotNone(link.data.get("chooserUrls"))
self.assertEqual(
link.data["chooserUrls"]["pageChooser"],
reverse_lazy("wagtailadmin_choose_page"),
)
self.assertEqual(
link.data["chooserUrls"]["externalLinkChooser"],
reverse_lazy("wagtailadmin_choose_page_external_link"),
)
self.assertEqual(
link.data["chooserUrls"]["emailLinkChooser"],
reverse_lazy("wagtailadmin_choose_page_email_link"),
)
self.assertEqual(
link.data["chooserUrls"]["phoneLinkChooser"],
reverse_lazy("wagtailadmin_choose_page_phone_link"),
)
self.assertEqual(
link.data["chooserUrls"]["anchorLinkChooser"],
reverse_lazy("wagtailadmin_choose_page_anchor_link"),
)
def test_lazy_chooser_urls_resolved_correctly(self):
response = self.client.get(
reverse(
"wagtailadmin_pages:add",
args=("tests", "defaultrichtextfieldpage", self.root_page.id),
)
)
self.assertContains(
response, '"chooserUrls": {"imageChooser": "/admin/images/chooser/"}'
)
self.assertContains(
response, '"chooserUrls": {"embedsChooser": "/admin/embeds/chooser/"}'
)
self.assertContains(
response, '"chooserUrls": {"documentChooser": "/admin/documents/chooser/"}'
)
self.assertContains(
response,
'"chooserUrls": {"pageChooser": "/admin/choose-page/", "externalLinkChooser": "/admin/choose-external-link/", "emailLinkChooser": "/admin/choose-email-link/", "phoneLinkChooser": "/admin/choose-phone-link/", "anchorLinkChooser": "/admin/choose-anchor-link/"}',
)
def test_lazy_urls_resolution(self):
widget = DraftailRichTextArea()
pageChooser = format_html(
"<p>this link_Chooser link : <a href={page_chooser}>{page_chooser}</a></p>",
page_chooser=reverse_lazy("wagtailadmin_choose_page"),
)
imageChooser = format_html(
"<p>this image_chooser link : <a href={image_chooser}>{image_chooser}</a></p>",
image_chooser=reverse_lazy("wagtailimages_chooser:choose"),
)
embedsChooser = format_html(
"<p>this embeds_chooser link: <a href={embeds_chooser}>{embeds_chooser}</a></p>",
embeds_chooser=reverse_lazy("wagtailembeds:chooser"),
)
documentChooser = format_html(
"<p>this document_chooser link: <a href={document_chooser}>{document_chooser}</a></p>",
document_chooser=reverse_lazy("wagtaildocs_chooser:choose"),
)
rendered_html_link = widget.render("test_chooserUrls", pageChooser, {})
rendered_html_image = widget.render("test_chooserUrls", imageChooser, {})
rendered_html_embeds = widget.render("test_chooserUrls", embedsChooser, {})
rendered_html_document = widget.render("test_chooserUrls", documentChooser, {})
# Assert that the lazy URLs has been resolved correctly
self.assertIn("/admin/choose-page/", rendered_html_link)
self.assertIn("/admin/images/chooser/", rendered_html_image)
self.assertIn("/admin/embeds/chooser/", rendered_html_embeds)
self.assertIn("/admin/documents/chooser/", rendered_html_document)

View File

@ -1,6 +1,6 @@
from django.conf import settings
from django.contrib.auth.models import Permission
from django.urls import reverse
from django.urls import reverse, reverse_lazy
from django.utils.functional import cached_property
from django.utils.http import urlencode
from django.utils.translation import gettext
@ -743,6 +743,21 @@ def register_core_features(features):
# Keep pasted links with http/https protocol, and not-pasted links (href = undefined).
"href": "^(http:|https:|undefined$)",
},
"chooserUrls": {
"pageChooser": reverse_lazy("wagtailadmin_choose_page"),
"externalLinkChooser": reverse_lazy(
"wagtailadmin_choose_page_external_link"
),
"emailLinkChooser": reverse_lazy(
"wagtailadmin_choose_page_email_link"
),
"phoneLinkChooser": reverse_lazy(
"wagtailadmin_choose_page_phone_link"
),
"anchorLinkChooser": reverse_lazy(
"wagtailadmin_choose_page_anchor_link"
),
},
},
js=[
"wagtailadmin/js/page-chooser-modal.js",