"""Custom form widgets."""
from json import dumps
from typing import Any, Dict
from django.forms import Textarea
[docs]class TinyMCE(Textarea):
"""
A textarea :class:`~django.forms.Widget`
for `TinyMCE <https://www.tiny.cloud/>`_.
:param attrs: A dictionary of the widget's attributes.
"""
def __init__(self, attrs: Dict[str, Any] = {}):
if 'class' in attrs: # pragma: no cover
attrs['class'] += ' tinymce'
else:
attrs['class'] = 'tinymce'
# TODO: use dict union (Py3.9+)
attrs.update({'cols': '75', 'rows': '15'})
mce_attrs = {
'selector': '.tinymce',
'theme': 'modern',
'relative_urls': True
}
for key in list(attrs):
if key.startswith('mce_'):
mce_attrs[key[4:]] = attrs.pop(key)
attrs['data-tinymce-config'] = dumps(mce_attrs)
super().__init__(attrs)
__all__ = ['TinyMCE']