forked from pycontw/pycon.tw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.py
46 lines (37 loc) · 1.46 KB
/
widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django import forms
from django.forms.utils import flatatt
from django.utils.encoding import force_str
from django.utils.html import format_html
from .utils import split_css_class
class CharacterCountedTextarea(forms.Textarea):
class Media:
js = ['js/vendors/eastasianwidth.js', 'js/tools/character-counter.js']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
css_classes = split_css_class(self.attrs.get('class', ''))
css_classes.add('character-counted')
self.attrs['class'] = ' '.join(css_classes)
class SimpleMDEWidget(forms.Textarea):
class Media:
css = {
'all': [
'css/vendors/simplemde.min.css',
'css/simplemde-setup.css',
],
}
js = ['js/vendors/simplemde.min.js', 'js/tools/simplemde-setup.js']
def render(self, name, value, attrs=None, renderer=None):
attrs = self.build_attrs(attrs, {'name': name})
if attrs.get('disabled', False):
return format_html(
'<div class="editor-readonly">'
'<div class="editor-preview editor-preview-active">'
'{content}</div></div>',
content=value,
)
else:
attrs['data-simplemde'] = True
return format_html(
'<textarea{attrs}>\r\n{content}</textarea>',
attrs=flatatt(attrs), content=force_str(value),
)