From 1ef68e0fdb33df80a01fb604e9e06f007c6e5613 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Fri, 15 Jul 2022 13:00:34 +0700 Subject: [PATCH] Upgrade pagedown --- dmoj/urls.py | 4 +++- judge/views/comment.py | 12 ++++++----- judge/widgets/pagedown.py | 21 ++++++++++++++++---- requirements.txt | 2 +- resources/pagedown_widget.css | 3 +++ templates/comments/edit-ajax.html | 7 ++----- templates/comments/media-js.html | 4 +--- templates/pagedown.html | 33 +++++++++++++++++++++++++------ 8 files changed, 61 insertions(+), 25 deletions(-) diff --git a/dmoj/urls.py b/dmoj/urls.py index e184e0d..c136ea9 100644 --- a/dmoj/urls.py +++ b/dmoj/urls.py @@ -12,6 +12,7 @@ from django.utils.functional import lazystr from django.utils.translation import ugettext_lazy as _ from django.views.generic import RedirectView from django.contrib.auth.decorators import login_required +from django.conf.urls.static import static as url_static from judge.feed import ( @@ -201,6 +202,7 @@ def paged_list_view(view, name, **kwargs): urlpatterns = [ + url("", include("pagedown.urls")), url( r"^$", blog.PostList.as_view(template_name="home.html", title=_("Home")), @@ -1086,7 +1088,7 @@ urlpatterns = [ ] ), ), -] +] + url_static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) favicon_paths = [ "apple-touch-icon-180x180.png", diff --git a/judge/views/comment.py b/judge/views/comment.py index edba7f3..20d1d5b 100644 --- a/judge/views/comment.py +++ b/judge/views/comment.py @@ -14,13 +14,14 @@ from django.shortcuts import get_object_or_404 from django.utils.translation import gettext as _ from django.views.decorators.http import require_POST from django.views.generic import DetailView, UpdateView +from django.urls import reverse_lazy from reversion import revisions from reversion.models import Version from judge.dblock import LockModel from judge.models import Comment, CommentVote, Notification from judge.utils.views import TitleMixin -from judge.widgets import MathJaxPagedownWidget +from judge.widgets import MathJaxPagedownWidget, HeavyPreviewPageDownWidget from judge.comments import add_mention_notifications, del_mention_notifications __all__ = [ @@ -133,10 +134,11 @@ class CommentEditForm(ModelForm): class Meta: model = Comment fields = ["body"] - if MathJaxPagedownWidget is not None: - widgets = { - "body": MathJaxPagedownWidget(attrs={"id": "id-edit-comment-body"}) - } + widgets = { + "body": HeavyPreviewPageDownWidget( + id="id-edit-comment-body", preview=reverse_lazy("comment_preview") + ), + } class CommentEditAjax(LoginRequiredMixin, CommentMixin, UpdateView): diff --git a/judge/widgets/pagedown.py b/judge/widgets/pagedown.py index 3225dc7..27c72e7 100644 --- a/judge/widgets/pagedown.py +++ b/judge/widgets/pagedown.py @@ -3,6 +3,7 @@ from django.forms.utils import flatatt from django.template.loader import get_template from django.utils.encoding import force_text from django.utils.html import conditional_escape +from django.conf import settings from judge.widgets.mixins import CompressorWidgetMixin @@ -34,9 +35,15 @@ else: compress_js = True def __init__(self, *args, **kwargs): - kwargs.setdefault("css", ("pagedown_widget.css",)) super(PagedownWidget, self).__init__(*args, **kwargs) + class Media: + css = { + "all": [ + "pagedown_widget.css", + ] + } + class AdminPagedownWidget(PagedownWidget, admin_widgets.AdminTextareaWidget): class Media: css = { @@ -60,7 +67,8 @@ else: class HeavyPreviewPageDownWidget(PagedownWidget): def __init__(self, *args, **kwargs): - kwargs.setdefault("template", "pagedown.html") + self.template = "pagedown.html" + self.id = kwargs.pop("id", None) self.preview_url = kwargs.pop("preview") self.preview_timeout = kwargs.pop("preview_timeout", None) self.hide_preview_button = kwargs.pop("hide_preview_button", False) @@ -73,16 +81,21 @@ else: if "class" not in final_attrs: final_attrs["class"] = "" final_attrs["class"] += " wmd-input" + if self.id: + final_attrs["id"] = self.id return get_template(self.template).render( self.get_template_context(final_attrs, value) ) def get_template_context(self, attrs, value): return { + "image_upload_enabled": getattr( + settings, "PAGEDOWN_IMAGE_UPLOAD_ENABLED", False + ), "attrs": flatatt(attrs), "body": conditional_escape(force_text(value)), - "id": attrs["id"], - "show_preview": self.show_preview, + "postfix": attrs["id"], + "show_preview": True, "preview_url": self.preview_url, "preview_timeout": self.preview_timeout, "extra_classes": "dmmd-no-button" if self.hide_preview_button else None, diff --git a/requirements.txt b/requirements.txt index dae6d6e..f43e4c3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ Django>=2.2,<3 django_compressor django-mptt -django-pagedown<2 +django-pagedown django-registration-redux django-reversion django-reversion-compare diff --git a/resources/pagedown_widget.css b/resources/pagedown_widget.css index 3d2326a..fbdd83e 100644 --- a/resources/pagedown_widget.css +++ b/resources/pagedown_widget.css @@ -59,14 +59,17 @@ .wmd-spacer1 { left: 50px; + display: None; } .wmd-spacer2 { left: 175px; + display: None; } .wmd-spacer3 { left: 300px; + display: None; } .wmd-prompt-background { diff --git a/templates/comments/edit-ajax.html b/templates/comments/edit-ajax.html index c854133..77357e3 100644 --- a/templates/comments/edit-ajax.html +++ b/templates/comments/edit-ajax.html @@ -3,11 +3,8 @@ {% csrf_token %} -
- {{ form.non_field_errors() }} - {{ form.body.errors }} - -
+ {{ form.non_field_errors() }} + {{ form.body.errors }}
{{ form.body }}
diff --git a/templates/comments/media-js.html b/templates/comments/media-js.html index df9f711..8789e95 100644 --- a/templates/comments/media-js.html +++ b/templates/comments/media-js.html @@ -161,15 +161,13 @@ $comments.find('a.edit-link').featherlight({ afterOpen: function () { + register_dmmd_preview($('#id-edit-comment-body-preview')); if ('DjangoPagedown' in window) { var $wmd = $('.featherlight .wmd-input'); if ($wmd.length) { window.DjangoPagedown.createEditor($wmd.get(0)); if ('MathJax' in window) { var preview = $('.featherlight div.wmd-preview')[0]; - window.editors[$wmd.attr('id')].hooks.chain('onPreviewRefresh', function () { - MathJax.Hub.Queue(['Typeset', MathJax.Hub, preview]); - }); MathJax.Hub.Queue(['Typeset', MathJax.Hub, preview]); } } diff --git a/templates/pagedown.html b/templates/pagedown.html index 74a038d..03b1cc3 100644 --- a/templates/pagedown.html +++ b/templates/pagedown.html @@ -1,14 +1,35 @@ -
+
-
- {{ body }} +
+
- {% if show_preview %} -
Update Preview
{% endif %} -
+ {% if image_upload_enabled %} +
+

Insert Image

+
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+ {% endif %} +
\ No newline at end of file