Permalink
Browse files

Made rich text editors configurable

This commit makes it possible to swap out the hallo editor with a custom one
  • Loading branch information...
kaedroho authored and gasman committed Mar 29, 2016
1 parent 11f663e commit e4b75b4c415e26ab53196858701aa9b1ce9ef05b
Showing with 28 additions and 7 deletions.
  1. +16 −0 wagtail/wagtailadmin/rich_text.py
  2. +6 −5 wagtail/wagtailcore/blocks/field_block.py
  3. +6 −2 wagtail/wagtailcore/fields.py
@@ -2,7 +2,9 @@
import json
from django.conf import settings
from django.forms import widgets
from django.utils.module_loading import import_string
from wagtail.utils.widgets import WidgetWithScript
from wagtail.wagtailadmin.edit_handlers import RichTextFieldPanel
@@ -28,3 +30,17 @@ def value_from_datadict(self, data, files, name):
if original_value is None:
return None
return DbWhitelister.clean(original_value)
DEFAULT_RICH_TEXT_EDITORS = {
'default': {
'WIDGET': 'wagtail.wagtailadmin.rich_text.HalloRichTextArea'
}
}
def get_rich_text_editor(name='default'):
editor_settings = getattr(settings, 'WAGTAILADMIN_RICH_TEXT_EDITORS', DEFAULT_RICH_TEXT_EDITORS)
editor = editor_settings[name]
return import_string(editor['WIDGET'])()
@@ -303,8 +303,9 @@ class Meta:
class RichTextBlock(FieldBlock):
def __init__(self, required=True, help_text=None, **kwargs):
def __init__(self, required=True, help_text=None, editor='default', **kwargs):
self.field_options = {'required': required, 'help_text': help_text}
self.editor = editor
super(RichTextBlock, self).__init__(**kwargs)
def get_default(self):
@@ -325,16 +326,16 @@ def get_prep_value(self, value):
@cached_property
def field(self):
from wagtail.wagtailadmin.rich_text import HalloRichTextArea
return forms.CharField(widget=HalloRichTextArea, **self.field_options)
from wagtail.wagtailadmin.rich_text import get_rich_text_editor
return forms.CharField(widget=get_rich_text_editor(self.editor), **self.field_options)
def value_for_form(self, value):
# HalloRichTextArea takes the source-HTML string as input (and takes care
# Rich text editors take the source-HTML string as input (and takes care
# of expanding it for the purposes of the editor)
return value.source
def value_from_form(self, value):
# HalloRichTextArea returns a source-HTML string; concert to a RichText object
# Rich text editors return a source-HTML string; convert to a RichText object
return RichText(value)
def get_searchable_content(self, value):
@@ -10,9 +10,13 @@
class RichTextField(models.TextField):
def __init__(self, *args, **kwargs):
self.editor = kwargs.pop('editor', 'default')
super(RichTextField, self).__init__(*args, **kwargs)
def formfield(self, **kwargs):
from wagtail.wagtailadmin.rich_text import HalloRichTextArea
defaults = {'widget': HalloRichTextArea}
from wagtail.wagtailadmin.rich_text import get_rich_text_editor
defaults = {'widget': get_rich_text_editor(self.editor)}
defaults.update(kwargs)
return super(RichTextField, self).formfield(**defaults)

0 comments on commit e4b75b4

Please sign in to comment.