0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Made rich text editors configurable

This commit makes it possible to swap out the hallo editor with a custom one
This commit is contained in:
Karl Hobley 2016-03-29 21:05:33 +01:00 committed by Matt Westcott
parent 11f663e0c1
commit e4b75b4c41
3 changed files with 28 additions and 7 deletions

View File

@ -2,7 +2,9 @@ from __future__ import absolute_import, unicode_literals
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 @@ class HalloRichTextArea(WidgetWithScript, widgets.Textarea):
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'])()

View File

@ -303,8 +303,9 @@ class ChoiceBlock(FieldBlock):
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 @@ class RichTextBlock(FieldBlock):
@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):

View File

@ -10,9 +10,13 @@ from wagtail.wagtailcore.blocks import Block, BlockField, StreamBlock, StreamVal
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)