diff --git a/client/src/entrypoints/admin/comments.js b/client/src/entrypoints/admin/comments.js index 817f605469..e7da4a82f5 100644 --- a/client/src/entrypoints/admin/comments.js +++ b/client/src/entrypoints/admin/comments.js @@ -338,6 +338,17 @@ window.comments = (() => { updateCommentCount(); } + /** Add support for initializing comments via event dispatching. */ + document.addEventListener( + 'w-comments:init', + ({ target }) => { + setTimeout(() => { + initCommentsInterface(target); + }); + }, + { once: true }, + ); + return { commentApp, getContentPath, diff --git a/wagtail/admin/templates/wagtailadmin/pages/create.html b/wagtail/admin/templates/wagtailadmin/pages/create.html index 8788e73c5f..144425d96b 100644 --- a/wagtail/admin/templates/wagtailadmin/pages/create.html +++ b/wagtail/admin/templates/wagtailadmin/pages/create.html @@ -4,10 +4,11 @@ {% block bodyclass %}editor-view{% endblock %} {% block content %} + {% get_comments_enabled as comments_enabled %} {% include 'wagtailadmin/shared/headers/page_create_header.html' with title=header_title %} -
+ {% csrf_token %} @@ -81,12 +82,6 @@ commentApp: window.comments.commentApp } ); - - {% get_comments_enabled as comments_enabled %} - {% if comments_enabled %} - // Initialise comments UI - window.comments.initCommentsInterface(document.getElementById('page-edit-form')); - {% endif %} }); {% endblock %} diff --git a/wagtail/admin/templates/wagtailadmin/pages/edit.html b/wagtail/admin/templates/wagtailadmin/pages/edit.html index a628ce5313..37b3e88b48 100644 --- a/wagtail/admin/templates/wagtailadmin/pages/edit.html +++ b/wagtail/admin/templates/wagtailadmin/pages/edit.html @@ -5,13 +5,14 @@ {% block bodyclass %}editor-view {% if page.live %}page-is-live{% endif %} {% if page_locked %}content-locked{% endif %}{% endblock %} {% block content %} + {% get_comments_enabled as comments_enabled %} {% page_permissions page as page_perms %} {% include 'wagtailadmin/shared/headers/page_edit_header.html' with title=header_title %} {% block form %} - + {% csrf_token %} @@ -81,12 +82,6 @@ commentApp: window.comments.commentApp } ); - - {% get_comments_enabled as comments_enabled %} - {% if comments_enabled %} - // Initialise comments UI - window.comments.initCommentsInterface(document.getElementById('page-edit-form')); - {% endif %} }); diff --git a/wagtail/admin/tests/pages/test_create_page.py b/wagtail/admin/tests/pages/test_create_page.py index e51048360c..e118d06c49 100644 --- a/wagtail/admin/tests/pages/test_create_page.py +++ b/wagtail/admin/tests/pages/test_create_page.py @@ -1874,3 +1874,46 @@ class TestPageSubscriptionSettings(WagtailTestUtils, TestCase): self.assertEqual(subscription.user, self.user) self.assertFalse(subscription.comment_notifications) + + +class TestCommenting(WagtailTestUtils, TestCase): + """ + Tests the commenting related logic of the create page view. + """ + + def setUp(self): + # Find root page + self.root_page = Page.objects.get(id=2) + + # Login + self.user = self.login() + + def test_commments_enabled_by_default(self): + response = self.client.get( + reverse( + "wagtailadmin_pages:add", + args=["tests", "simplepage", self.root_page.id], + ) + ) + + self.assertEqual(response.status_code, 200) + self.assertContains( + response, + 'data-edit-form data-controller="w-init" data-w-init-event-value="w-comments:init"', + ) + + @override_settings(WAGTAILADMIN_COMMENTS_ENABLED=False) + def test_commments_disabled(self): + response = self.client.get( + reverse( + "wagtailadmin_pages:add", + args=["tests", "simplepage", self.root_page.id], + ) + ) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, "data-edit-form") + self.assertNotContains( + response, + 'data-controller="w-init" data-w-init-event-value="w-comments:init"', + ) diff --git a/wagtail/admin/tests/pages/test_edit_page.py b/wagtail/admin/tests/pages/test_edit_page.py index 67f38c29c2..44df6ddb70 100644 --- a/wagtail/admin/tests/pages/test_edit_page.py +++ b/wagtail/admin/tests/pages/test_edit_page.py @@ -3215,6 +3215,31 @@ class TestCommenting(WagtailTestUtils, TestCase): [to for email in mail.outbox for to in email.to], ) + def test_commments_enabled_by_default(self): + response = self.client.get( + reverse("wagtailadmin_pages:edit", args=[self.child_page.id]) + ) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, "data-edit-form") + self.assertContains( + response, + 'data-edit-form data-controller="w-init" data-w-init-event-value="w-comments:init"', + ) + + @override_settings(WAGTAILADMIN_COMMENTS_ENABLED=False) + def test_commments_disabled(self): + response = self.client.get( + reverse("wagtailadmin_pages:edit", args=[self.child_page.id]) + ) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, "data-edit-form") + self.assertNotContains( + response, + 'data-controller="w-init" data-w-init-event-value="w-comments:init"', + ) + def test_new_comment(self): post_data = { "title": "I've been edited!",