mirror of
https://github.com/Hopiu/django-markdownx.git
synced 2026-05-11 14:53:09 +00:00
Merge pull request #52 from aaugustin/v.2+docs
Fix #50 -- Native template-based widget rendering.
This commit is contained in:
commit
1b90db9247
2 changed files with 58 additions and 11 deletions
4
markdownx/templates/markdownx/widget2.html
Normal file
4
markdownx/templates/markdownx/widget2.html
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<div class="markdownx">
|
||||
{% include 'django/forms/widgets/textarea.html' %}
|
||||
<div class="markdownx-preview"></div>
|
||||
</div>
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
from django import VERSION as DJANGO_VERSION
|
||||
from django import forms
|
||||
from django.template.loader import get_template
|
||||
from django.contrib.admin import widgets
|
||||
|
|
@ -18,6 +19,31 @@ class MarkdownxWidget(forms.Textarea):
|
|||
"""
|
||||
|
||||
"""
|
||||
|
||||
if DJANGO_VERSION[:2] >= (1, 11):
|
||||
template_name = 'markdownx/widget2.html'
|
||||
|
||||
def get_context(self, name, value, attrs=None):
|
||||
"""
|
||||
|
||||
:param name:
|
||||
:type name:
|
||||
:param value:
|
||||
:type value:
|
||||
:param attrs:
|
||||
:type attrs:
|
||||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
if not DJANGO_VERSION[:2] >= (1, 11):
|
||||
return super(MarkdownxWidget, self).get_context(name, value, attrs)
|
||||
|
||||
if attrs is None:
|
||||
attrs = {}
|
||||
|
||||
attrs.update(self.add_markdownx_attrs(attrs))
|
||||
|
||||
return super(MarkdownxWidget, self).get_context(name, value, attrs)
|
||||
|
||||
def render(self, name, value, attrs=None, renderer=None):
|
||||
"""
|
||||
|
|
@ -33,8 +59,29 @@ class MarkdownxWidget(forms.Textarea):
|
|||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
if not DJANGO_VERSION[:2] < (1, 11):
|
||||
return super(MarkdownxWidget, self).render(name, value, attrs, renderer)
|
||||
|
||||
attrs = self.build_attrs(attrs, name=name)
|
||||
attrs.update(self.add_markdownx_attrs(attrs))
|
||||
|
||||
widget = super(MarkdownxWidget, self).render(name, value, attrs)
|
||||
|
||||
template = get_template('markdownx/widget.html')
|
||||
|
||||
return template.render({
|
||||
'markdownx_editor': widget,
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def add_markdownx_attrs(attrs):
|
||||
"""
|
||||
|
||||
:param attrs:
|
||||
:type attrs:
|
||||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
if 'class' in attrs:
|
||||
attrs['class'] += ' markdownx-editor'
|
||||
else:
|
||||
|
|
@ -42,19 +89,15 @@ class MarkdownxWidget(forms.Textarea):
|
|||
'class': 'markdownx-editor'
|
||||
})
|
||||
|
||||
attrs['data-markdownx-editor-resizable'] = MARKDOWNX_EDITOR_RESIZABLE
|
||||
attrs['data-markdownx-urls-path'] = MARKDOWNX_URLS_PATH
|
||||
attrs['data-markdownx-upload-urls-path'] = MARKDOWNX_UPLOAD_URLS_PATH
|
||||
attrs['data-markdownx-latency'] = MARKDOWNX_SERVER_CALL_LATENCY
|
||||
|
||||
widget = super(MarkdownxWidget, self).render(name, value, attrs, renderer)
|
||||
|
||||
template = get_template('markdownx/widget.html')
|
||||
|
||||
return template.render({
|
||||
'markdownx_editor': widget,
|
||||
attrs.update({
|
||||
'data-markdownx-editor-resizable': MARKDOWNX_EDITOR_RESIZABLE,
|
||||
'data-markdownx-urls-path': MARKDOWNX_URLS_PATH,
|
||||
'data-markdownx-upload-urls-path': MARKDOWNX_UPLOAD_URLS_PATH,
|
||||
'data-markdownx-latency': MARKDOWNX_SERVER_CALL_LATENCY
|
||||
})
|
||||
|
||||
return attrs
|
||||
|
||||
class Media:
|
||||
"""
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue