From 2925cde8ab4de536f502e656a27347caa4d169f8 Mon Sep 17 00:00:00 2001 From: Skia Date: Sat, 10 Jun 2017 19:39:40 +0200 Subject: [PATCH] Add a MarkdownInput widget, and make use of it Signed-off-by: Skia --- core/static/core/style.scss | 12 ++++++++++++ core/templates/core/base.jinja | 4 +--- core/views/forms.py | 9 ++++++++- core/views/page.py | 6 +++--- forum/templates/forum/reply.jinja | 8 +++----- forum/views.py | 13 +++++++++++-- 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/core/static/core/style.scss b/core/static/core/style.scss index 4880f5a1..ddc9fded 100644 --- a/core/static/core/style.scss +++ b/core/static/core/style.scss @@ -515,6 +515,12 @@ textarea { margin: 1px; font-size: smaller; } + a { + color: $black-color; + } + a:hover { + text-decoration: underline; + } } .tools { @@ -551,6 +557,12 @@ textarea { margin: 1px; font-size: smaller; } + a { + color: $black-color; + } + a:hover { + text-decoration: underline; + } } .category { diff --git a/core/templates/core/base.jinja b/core/templates/core/base.jinja index 87199d6c..0de6e1f1 100644 --- a/core/templates/core/base.jinja +++ b/core/templates/core/base.jinja @@ -248,8 +248,7 @@ function add_syntax(e, choice) { } $(document).ready(function() { - textarea = $('.markdown_editor textarea'); - editor = textarea.parent(); + editor = $('.markdown_editor'); editor.prepend('{% trans %}Image{% endtrans %}'); editor.prepend('{% trans %}Link{% endtrans %}'); editor.prepend('{% trans %}sup{% endtrans %}'); @@ -258,7 +257,6 @@ $(document).ready(function() { editor.prepend('{% trans %}U{% endtrans %}'); editor.prepend('{% trans %}I{% endtrans %}'); editor.prepend('{% trans %}B{% endtrans %}'); - console.log(textarea.parent()); }); {% endblock %} diff --git a/core/views/forms.py b/core/views/forms.py index 38c5f4bb..a3c78b4e 100644 --- a/core/views/forms.py +++ b/core/views/forms.py @@ -27,7 +27,7 @@ from django import forms from django.db import transaction from django.core.exceptions import ValidationError from django.contrib.auth import logout, login, authenticate -from django.forms import CheckboxSelectMultiple, Select, DateInput, TextInput, DateTimeInput +from django.forms import CheckboxSelectMultiple, Select, DateInput, TextInput, DateTimeInput, Textarea from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext from phonenumber_field.widgets import PhoneNumberInternationalFallbackWidget @@ -73,6 +73,13 @@ class SelectDate(DateInput): attrs = {'class': "select_date"} return super(SelectDate, self).render(name, value, attrs) +class MarkdownInput(Textarea): + def render(self, name, value, attrs=None): + output = '
%(content)s
' % { + 'content': super(MarkdownInput, self).render(name, value, attrs), + } + return output + class SelectFile(TextInput): def render(self, name, value, attrs=None): if attrs: diff --git a/core/views/page.py b/core/views/page.py index dcceb616..999f6482 100644 --- a/core/views/page.py +++ b/core/views/page.py @@ -30,10 +30,10 @@ from django.views.generic.edit import UpdateView, CreateView, DeleteView from django.contrib.auth.decorators import login_required, permission_required from django.utils.decorators import method_decorator from django.forms.models import modelform_factory -from django.forms import CheckboxSelectMultiple +from django.forms import CheckboxSelectMultiple, modelform_factory from core.models import Page, PageRev, LockError -from core.views.forms import PagePropForm +from core.views.forms import PagePropForm, MarkdownInput from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin class PageListView(CanViewMixin, ListView): @@ -147,7 +147,7 @@ class PagePropView(CanEditPropMixin, UpdateView): class PageEditView(CanEditMixin, UpdateView): model = PageRev - fields = ['title', 'content',] + form_class = modelform_factory(model=PageRev, fields=['title', 'content',], widgets={'content': MarkdownInput}) template_name = 'core/pagerev_edit.jinja' def get_object(self): diff --git a/forum/templates/forum/reply.jinja b/forum/templates/forum/reply.jinja index fd41766b..82f847c6 100644 --- a/forum/templates/forum/reply.jinja +++ b/forum/templates/forum/reply.jinja @@ -22,18 +22,16 @@

{{ topic.title }}

{% trans %}Reply{% endtrans %}

- {% else %} +{% else %} +

{% trans %}New topic{% endtrans %}

- {% endif %} +{% endif %}
{% csrf_token %}

{{ form.title.errors }} {{ form.title }}

{{ form.message.errors }}

{% trans %}Help on the syntax{% endtrans %} -

{{ form.message }} -
-

diff --git a/forum/views.py b/forum/views.py index 21dcb5f6..86377185 100644 --- a/forum/views.py +++ b/forum/views.py @@ -38,6 +38,7 @@ from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from ajax_select import make_ajax_form, make_ajax_field from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin, TabedViewMixin +from core.views.forms import MarkdownInput from core.models import Page from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta @@ -142,6 +143,9 @@ class TopicForm(forms.ModelForm): class Meta: model = ForumMessage fields = ['title', 'message'] + widgets = { + 'message': MarkdownInput, + } title = forms.CharField(required=True, label=_("Title")) class ForumTopicCreateView(CanCreateMixin, CreateView): @@ -162,6 +166,11 @@ class ForumTopicCreateView(CanCreateMixin, CreateView): form.instance.author = self.request.user return super(ForumTopicCreateView, self).form_valid(form) + def get_context_data(self, **kwargs): + kwargs = super(ForumTopicCreateView, self).get_context_data(**kwargs) + kwargs['syntax_help_page'] = Page.get_page_by_full_name(settings.SITH_CORE_PAGE_SYNTAX) + return kwargs + class ForumTopicEditView(CanEditMixin, UpdateView): model = ForumTopic fields = ['forum'] @@ -205,7 +214,7 @@ class ForumMessageView(SingleObjectMixin, RedirectView): class ForumMessageEditView(CanEditMixin, UpdateView): model = ForumMessage - fields = ['title', 'message'] + form_class = forms.modelform_factory(model=ForumMessage, fields=['title', 'message',], widgets={'message': MarkdownInput}) template_name = "forum/reply.jinja" pk_url_kwarg = "message_id" @@ -243,7 +252,7 @@ class ForumMessageUndeleteView(SingleObjectMixin, RedirectView): class ForumMessageCreateView(CanCreateMixin, CreateView): model = ForumMessage - fields = ['title', 'message'] + form_class = forms.modelform_factory(model=ForumMessage, fields=['title', 'message',], widgets={'message': MarkdownInput}) template_name = "forum/reply.jinja" def dispatch(self, request, *args, **kwargs):