Make Draftail the default editor

This commit is contained in:
Matt Westcott 2017-12-14 20:25:29 +00:00 committed by Thibaud Colas
parent 8551ee9c2d
commit 8280c3e56e
8 changed files with 82 additions and 20 deletions

View file

@ -1,14 +1,15 @@
from django.conf import settings
from django.utils.module_loading import import_string
from wagtail.admin.rich_text.editors.hallo import (
from wagtail.admin.rich_text.editors.hallo import ( # NOQA
HalloFormatPlugin, HalloHeadingPlugin, HalloListPlugin, HalloPlugin, HalloRichTextArea
) # NOQA
)
from wagtail.admin.rich_text.editors.draftail import DraftailRichTextArea # NOQA
DEFAULT_RICH_TEXT_EDITORS = {
'default': {
'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea'
'WIDGET': 'wagtail.admin.rich_text.DraftailRichTextArea'
}
}

View file

@ -14,7 +14,7 @@ from wagtail.admin.edit_handlers import (
FieldPanel, FieldRowPanel, InlinePanel, ObjectList, PageChooserPanel, RichTextFieldPanel,
TabbedInterface, extract_panel_definitions_from_model_class, get_form_for_model)
from wagtail.admin.forms import WagtailAdminModelForm, WagtailAdminPageForm
from wagtail.admin.rich_text import HalloRichTextArea
from wagtail.admin.rich_text import DraftailRichTextArea
from wagtail.admin.widgets import AdminAutoHeightTextInput, AdminDateInput, AdminPageChooser
from wagtail.core.models import Page, Site
from wagtail.images.edit_handlers import ImageChooserPanel
@ -62,7 +62,7 @@ class TestGetFormForModel(TestCase):
# RichTextField - they should retain their default widgets
EventPageForm = get_form_for_model(EventPage, form_class=WagtailAdminPageForm)
event_form = EventPageForm()
self.assertEqual(type(event_form.fields['body'].widget), HalloRichTextArea)
self.assertEqual(type(event_form.fields['body'].widget), DraftailRichTextArea)
def test_get_form_for_model_with_specific_fields(self):
EventPageForm = get_form_for_model(

View file

@ -4231,15 +4231,15 @@ class TestInlinePanelMedia(TestCase, WagtailTestUtils):
homepage = Page.objects.get(id=2)
self.login()
# simplepage does not need hallo...
# simplepage does not need draftail...
response = self.client.get(reverse('wagtailadmin_pages:add', args=('tests', 'simplepage', homepage.id)))
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, 'wagtailadmin/js/hallo-bootstrap.js')
self.assertNotContains(response, 'wagtailadmin/js/draftail.js')
# but sectionedrichtextpage does
response = self.client.get(reverse('wagtailadmin_pages:add', args=('tests', 'sectionedrichtextpage', homepage.id)))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'wagtailadmin/js/hallo-bootstrap.js')
self.assertContains(response, 'wagtailadmin/js/draftail.js')
class TestInlineStreamField(TestCase, WagtailTestUtils):
@ -4737,7 +4737,9 @@ class TestPreview(TestCase, WagtailTestUtils):
self.post_data = {
'title': "Beach party",
'slug': 'beach-party',
'body': "party on wayne",
'body': '''{"entityMap": {},"blocks": [
{"inlineStyleRanges": [], "text": "party on wayne", "depth": 0, "type": "unstyled", "key": "00000", "entityRanges": []}
]}''',
'date_from': '2017-08-01',
'audience': 'public',
'location': 'the beach',

View file

@ -8,7 +8,7 @@ from django.urls import reverse
from wagtail.tests.testapp.models import SingleEventPage
from wagtail.tests.testapp.rich_text import CustomRichTextArea
from wagtail.tests.utils import WagtailTestUtils
from wagtail.admin.rich_text import HalloRichTextArea, get_rich_text_editor_widget
from wagtail.admin.rich_text import DraftailRichTextArea, HalloRichTextArea, get_rich_text_editor_widget
from wagtail.core.blocks import RichTextBlock
from wagtail.core.models import Page, get_page_models
from wagtail.core.rich_text import RichText
@ -48,7 +48,7 @@ class TestGetRichTextEditorWidget(TestCase):
if hasattr(settings, 'WAGTAILADMIN_RICH_TEXT_EDITORS'):
del settings.WAGTAILADMIN_RICH_TEXT_EDITORS
self.assertIsInstance(get_rich_text_editor_widget(), HalloRichTextArea)
self.assertIsInstance(get_rich_text_editor_widget(), DraftailRichTextArea)
@override_settings(WAGTAILADMIN_RICH_TEXT_EDITORS={
'default': {
@ -79,7 +79,6 @@ class TestGetRichTextEditorWidget(TestCase):
self.assertIsInstance(get_rich_text_editor_widget('custom'), CustomRichTextArea)
@override_settings()
class TestDefaultRichText(BaseRichTextEditHandlerTestCase, WagtailTestUtils):
def setUp(self):
@ -89,10 +88,59 @@ class TestDefaultRichText(BaseRichTextEditHandlerTestCase, WagtailTestUtils):
self.login()
@override_settings()
def test_default_editor_in_rich_text_field(self):
# Simulate the absence of a setting
if hasattr(settings, 'WAGTAILADMIN_RICH_TEXT_EDITORS'):
del settings.WAGTAILADMIN_RICH_TEXT_EDITORS
response = self.client.get(reverse(
'wagtailadmin_pages:add', args=('tests', 'defaultrichtextfieldpage', self.root_page.id)
))
# Check status code
self.assertEqual(response.status_code, 200)
# Check that draftail (default editor) initialisation is applied
self.assertContains(response, "window.draftail.initEditor('body',")
# check that media for draftail is being imported
self.assertContains(response, 'wagtailadmin/js/draftail.js')
@override_settings()
def test_default_editor_in_rich_text_block(self):
# Simulate the absence of a setting
if hasattr(settings, 'WAGTAILADMIN_RICH_TEXT_EDITORS'):
del settings.WAGTAILADMIN_RICH_TEXT_EDITORS
response = self.client.get(reverse(
'wagtailadmin_pages:add', args=('tests', 'defaultrichblockfieldpage', self.root_page.id)
))
# Check status code
self.assertEqual(response.status_code, 200)
# Check that draftail (default editor) initialisation is applied
self.assertContains(response, "window.draftail.initEditor('__PREFIX__-value',")
# check that media for draftail is being imported
self.assertContains(response, 'wagtailadmin/js/draftail.js')
@override_settings(WAGTAILADMIN_RICH_TEXT_EDITORS={
'default': {
'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea'
},
})
class TestHalloRichText(BaseRichTextEditHandlerTestCase, WagtailTestUtils):
def setUp(self):
super().setUp()
# Find root page
self.root_page = Page.objects.get(id=2)
self.login()
def test_default_editor_in_rich_text_field(self):
response = self.client.get(reverse(
'wagtailadmin_pages:add', args=('tests', 'defaultrichtextfieldpage', self.root_page.id)
@ -101,7 +149,7 @@ class TestDefaultRichText(BaseRichTextEditHandlerTestCase, WagtailTestUtils):
# Check status code
self.assertEqual(response.status_code, 200)
# Check that hallo (default editor by now)
# Check that hallo (default editor now) initialisation is applied
self.assertContains(response, 'makeHalloRichTextEditable("id_body",')
# check that media for the default hallo features (but not others) is being imported
@ -116,7 +164,7 @@ class TestDefaultRichText(BaseRichTextEditHandlerTestCase, WagtailTestUtils):
# Check status code
self.assertEqual(response.status_code, 200)
# Check that hallo (default editor by now)
# Check that hallo (default editor now) initialisation is applied
self.assertContains(response, 'makeHalloRichTextEditable("__PREFIX__-value",')
# check that media for the default hallo features (but not others) is being imported
@ -219,7 +267,6 @@ class TestRichTextValue(TestCase):
)
self.root_page.add_child(instance=self.single_event_page)
def test_render(self):
text = '<p>To the <a linktype="page" id="{}">moon</a>!</p>'.format(
self.single_event_page.id
@ -275,6 +322,11 @@ class TestHalloJsWithCustomPluginOptions(BaseRichTextEditHandlerTestCase, Wagtai
self.assertIn('makeHalloRichTextEditable("body", {"halloheadings": {"formatBlocks": ["p", "h2"]}});', form_html)
@override_settings(WAGTAILADMIN_RICH_TEXT_EDITORS={
'default': {
'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea'
},
})
class TestHalloJsWithFeaturesKwarg(BaseRichTextEditHandlerTestCase, WagtailTestUtils):
def setUp(self):

View file

@ -446,7 +446,7 @@ class TestRichTextBlock(TestCase):
render_form should produce the editor-specific rendition of the rich text value
(which includes e.g. 'data-linktype' attributes on <a> elements)
"""
block = blocks.RichTextBlock()
block = blocks.RichTextBlock(editor='hallo')
value = RichText('<p>Merry <a linktype="page" id="4">Christmas</a>!</p>')
result = block.render_form(value, prefix='richtext')
self.assertIn(

View file

@ -79,11 +79,13 @@ class TestWagtailPageTests(WagtailPageTests):
self.assertCanNotCreateAt(EventIndex, EventPage)
def test_assert_can_create(self):
self.assertFalse(EventIndex.objects.exists())
self.assertCanCreate(self.root, EventIndex, {
'title': 'Event Index',
'intro': '<p>Event intro</p>'})
'intro': '''{"entityMap": {},"blocks": [
{"inlineStyleRanges": [], "text": "Event intro", "depth": 0, "type": "unstyled", "key": "00000", "entityRanges": []}
]}'''
})
self.assertTrue(EventIndex.objects.exists())
self.assertCanCreate(self.root, StreamPage, {
@ -93,7 +95,9 @@ class TestWagtailPageTests(WagtailPageTests):
'body-0-order': '0',
'body-0-deleted': '',
'body-1-type': 'rich_text',
'body-1-value': '<p>Dit is onze mooie text in een ferrari</p>',
'body-1-value': '''{"entityMap": {},"blocks": [
{"inlineStyleRanges": [], "text": "Dit is onze mooie text in een ferrari", "depth": 0, "type": "unstyled", "key": "00000", "entityRanges": []}
]}''',
'body-1-order': '1',
'body-1-deleted': '',
'body-count': '2'

View file

@ -716,7 +716,7 @@ class TestInlinePanelMedia(TestCase, WagtailTestUtils):
response = self.client.get(reverse('wagtailsnippets:add', args=('snippetstests', 'multisectionrichtextsnippet')))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'wagtailadmin/js/hallo-bootstrap.js')
self.assertContains(response, 'wagtailadmin/js/draftail.js')
class TestSnippetChooserBlock(TestCase):

View file

@ -203,6 +203,9 @@ WAGTAIL_USER_CUSTOM_FIELDS = ['country', 'attachment']
WAGTAILADMIN_RICH_TEXT_EDITORS = {
'default': {
'WIDGET': 'wagtail.admin.rich_text.DraftailRichTextArea'
},
'hallo': {
'WIDGET': 'wagtail.admin.rich_text.HalloRichTextArea'
},
'custom': {