NDOJ/judge/widgets/pagedown.py

129 lines
4.7 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
from django.contrib.admin import widgets as admin_widgets
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
2022-07-15 06:00:34 +00:00
from django.conf import settings
2020-01-21 06:35:58 +00:00
from judge.widgets.mixins import CompressorWidgetMixin
2022-05-14 17:57:27 +00:00
__all__ = [
"PagedownWidget",
"AdminPagedownWidget",
2024-02-05 23:02:49 +00:00
"KatexPagedownWidget",
"KatexAdminPagedownWidget",
2022-05-14 17:57:27 +00:00
"HeavyPreviewPageDownWidget",
"HeavyPreviewAdminPageDownWidget",
]
2020-01-21 06:35:58 +00:00
try:
from pagedown.widgets import PagedownWidget as OldPagedownWidget
except ImportError:
PagedownWidget = None
AdminPagedownWidget = None
2024-02-05 23:02:49 +00:00
KatexPagedownWidget = None
KatexAdminPagedownWidget = None
2020-01-21 06:35:58 +00:00
HeavyPreviewPageDownWidget = None
HeavyPreviewAdminPageDownWidget = None
else:
2022-05-14 17:57:27 +00:00
2020-01-21 06:35:58 +00:00
class PagedownWidget(CompressorWidgetMixin, OldPagedownWidget):
# The goal here is to compress all the pagedown JS into one file.
# We do not want any further compress down the chain, because
# 1. we'll creating multiple large JS files to download.
# 2. this is not a problem here because all the pagedown JS files will be used together.
compress_js = True
def __init__(self, *args, **kwargs):
super(PagedownWidget, self).__init__(*args, **kwargs)
2022-07-15 06:00:34 +00:00
class Media:
2023-01-12 04:07:35 +00:00
extend = False
js = [
"pagedown/Markdown.Converter.js",
"pagedown-extra/pagedown/Markdown.Converter.js",
"pagedown/Markdown.Sanitizer.js",
"pagedown/Markdown.Editor.js",
"pagedown-extra/Markdown.Extra.js",
"pagedown_init.js",
]
2022-07-15 06:00:34 +00:00
2020-01-21 06:35:58 +00:00
class AdminPagedownWidget(PagedownWidget, admin_widgets.AdminTextareaWidget):
class Media:
2022-05-14 17:57:27 +00:00
css = {
"all": [
2022-12-18 09:31:31 +00:00
"pagedown_widget.css",
2022-05-14 17:57:27 +00:00
"content-description.css",
"admin/css/pagedown.css",
2023-01-13 05:21:29 +00:00
"pagedown.css",
2023-10-15 23:19:56 +00:00
"https://fonts.googleapis.com/css2?family=Fira+Code&family=Noto+Sans&display=swap",
2022-05-14 17:57:27 +00:00
]
}
js = ["admin/js/pagedown.js"]
2020-01-21 06:35:58 +00:00
2024-02-05 23:02:49 +00:00
class KatexPagedownWidget(PagedownWidget):
2020-01-21 06:35:58 +00:00
class Media:
2024-02-05 23:02:49 +00:00
css = {
"all": ["https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css"]
}
2020-01-21 06:35:58 +00:00
js = [
2024-02-05 23:02:49 +00:00
"katex_config.js",
"https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js",
"https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js",
2022-05-14 17:57:27 +00:00
"pagedown_math.js",
2020-01-21 06:35:58 +00:00
]
2024-02-05 23:02:49 +00:00
class KatexAdminPagedownWidget(AdminPagedownWidget, KatexPagedownWidget):
2020-01-21 06:35:58 +00:00
pass
class HeavyPreviewPageDownWidget(PagedownWidget):
def __init__(self, *args, **kwargs):
2022-07-15 06:00:34 +00:00
self.template = "pagedown.html"
self.id = kwargs.pop("id", None)
2022-05-14 17:57:27 +00:00
self.preview_url = kwargs.pop("preview")
self.preview_timeout = kwargs.pop("preview_timeout", None)
self.hide_preview_button = kwargs.pop("hide_preview_button", False)
2020-01-21 06:35:58 +00:00
super(HeavyPreviewPageDownWidget, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None, renderer=None):
if value is None:
2022-05-14 17:57:27 +00:00
value = ""
final_attrs = self.build_attrs(attrs, {"name": name})
if "class" not in final_attrs:
final_attrs["class"] = ""
final_attrs["class"] += " wmd-input"
2022-07-15 06:00:34 +00:00
if self.id:
final_attrs["id"] = self.id
2022-05-14 17:57:27 +00:00
return get_template(self.template).render(
self.get_template_context(final_attrs, value)
)
2020-01-21 06:35:58 +00:00
def get_template_context(self, attrs, value):
return {
2022-07-15 06:00:34 +00:00
"image_upload_enabled": getattr(
settings, "PAGEDOWN_IMAGE_UPLOAD_ENABLED", False
),
2022-05-14 17:57:27 +00:00
"attrs": flatatt(attrs),
"body": conditional_escape(force_text(value)),
2022-07-15 06:00:34 +00:00
"postfix": attrs["id"],
"show_preview": True,
2022-05-14 17:57:27 +00:00
"preview_url": self.preview_url,
"preview_timeout": self.preview_timeout,
"extra_classes": "dmmd-no-button" if self.hide_preview_button else None,
2020-01-21 06:35:58 +00:00
}
class Media:
2022-05-14 17:57:27 +00:00
js = ["dmmd-preview.js"]
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
class HeavyPreviewAdminPageDownWidget(
2024-02-05 23:02:49 +00:00
KatexPagedownWidget, AdminPagedownWidget, HeavyPreviewPageDownWidget
2022-05-14 17:57:27 +00:00
):
2020-01-21 06:35:58 +00:00
class Media:
2022-05-14 17:57:27 +00:00
css = {
"all": [
"table.css",
"ranks.css",
2022-12-18 09:31:31 +00:00
"dmmd-preview.css",
2022-05-14 17:57:27 +00:00
]
}