Repository: wagtail/wagtail
File: wagtail/admin/rich_text/editors/draftail/__init__.py (Python)

Code before:
import json
import warnings

from django.core.serializers.json import DjangoJSONEncoder
from django.forms import Media, widgets
from django.utils.encoding import force_str
from django.utils.functional import Promise, cached_property

from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter
from wagtail.admin.staticfiles import versioned_static
from wagtail.rich_text import features as feature_registry
from wagtail.telepath import register
from wagtail.widget_adapters import WidgetAdapter


class LazyEncoder(DjangoJSONEncoder):
    def default(self, obj):
        if isinstance(obj, Promise):
            return force_str(obj)
        return super().default(obj)


class DraftailRichTextArea(widgets.HiddenInput):
    template_name = "wagtailadmin/widgets/draftail_rich_text_area.html"
    is_hidden = False

    # this class's constructor accepts a 'features' kwarg
    accepts_features = True

    # Draftail has its own commenting
    show_add_comment_button = False

    def __init__(self, *args, **kwargs):
        # note: this constructor will receive an 'options' kwarg taken from the WAGTAILADMIN_RICH_TEXT_EDITORS setting,
        # but we don't currently recognise any options from there (other than 'features', which is passed here as a separate kwarg)
        kwargs.pop("options", None)
        self.options = {}
        self.plugins = []

        self.features = kwargs.pop("features", None)
        if self.features is None:
            self.features = feature_registry.get_default_features()

        for feature in self.features:
            plugin = feature_registry.get_editor_plugin("draftail", feature)

Review comment (suggestion):
`DjangoJSONEncoder` already handles lazy strings: https://github.com/django/django/blob/8eca4077f60fa0705ecfd9437c9ceaeef7a3808b/django/core/serializers/json.py#L111-L112

Code after:
import json
import warnings

from django.core.serializers.json import DjangoJSONEncoder
from django.forms import Media, widgets
from django.utils.functional import cached_property

from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter
from wagtail.admin.staticfiles import versioned_static
from wagtail.rich_text import features as feature_registry
from wagtail.telepath import register
from wagtail.widget_adapters import WidgetAdapter


class DraftailRichTextArea(widgets.HiddenInput):
    template_name = "wagtailadmin/widgets/draftail_rich_text_area.html"
    is_hidden = False

    # this class's constructor accepts a 'features' kwarg
    accepts_features = True

    # Draftail has its own commenting
    show_add_comment_button = False

    def __init__(self, *args, **kwargs):
        # note: this constructor will receive an 'options' kwarg taken from the WAGTAILADMIN_RICH_TEXT_EDITORS setting,
        # but we don't currently recognise any options from there (other than 'features', which is passed here as a separate kwarg)
        kwargs.pop("options", None)
        self.options = {}
        self.plugins = []

        self.features = kwargs.pop("features", None)
        if self.features is None:
            self.features = feature_registry.get_default_features()

        for feature in self.features:
            plugin = feature_registry.get_editor_plugin("draftail", feature)
            if plugin is None:
                warnings.warn(
                    f"Draftail received an unknown feature '{feature}'.",
                    category=RuntimeWarning,
                )
            else:
                plugin.construct_options(self.options)
                self.plugins.append(plugin)