Skip to content

Commit a764aa3

Browse files
committed
chore: update MathJax widget in Wagtail admin
1 parent b1d6cd3 commit a764aa3

3 files changed

Lines changed: 75 additions & 7 deletions

File tree

codeanon/templates/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
{% endblock %}
5959
{# Global javascript #}
6060
<script type="text/javascript" src="{% static 'js/codeanon.js' %}"></script>
61+
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"></script>
62+
<script src="{% static "wagtailmath/js/wagtailmath.public.js" %}"></script>
6163
{% block extra_js %}
6264
{# Override this in templates to add extra javascript #}
6365
{% endblock %}

contrib/wagtailmath/blocks.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import logging
22

3-
from django.forms import Widget, CharField, forms
4-
from django.template.loader import render_to_string
3+
from django.forms import Widget, CharField
54
from django.templatetags.static import static
65
from django.utils.functional import lazy
7-
from django.utils.safestring import mark_safe
86
from wagtail.core.blocks import FieldBlock
7+
from wagtail.core.telepath import register
8+
from wagtail.core.widget_adapters import WidgetAdapter
99

1010
static_lazy = lazy(static, str)
1111

@@ -16,7 +16,6 @@ class MathJaxWidget(Widget):
1616
class Media:
1717
js = (
1818
"https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js",
19-
static_lazy("wagtailmath/js/wagtailmath.js")
2019
)
2120

2221
template_name = "wagtailmath/mathjax_widget.html"
@@ -33,9 +32,18 @@ def get_context(self, name, value, attrs):
3332
}
3433
}
3534

36-
def render(self, name, value, attrs=None, renderer=None):
37-
context = self.get_context(name, value, attrs)
38-
return mark_safe(render_to_string(self.template_name, context))
35+
36+
class MathJaxAdapter(WidgetAdapter):
37+
js_constructor = "contrib.wagtailmath.blocks.MathJaxWidget"
38+
39+
def js_args(self, _: MathJaxWidget):
40+
return []
41+
42+
class Media:
43+
js = [static_lazy("wagtailmath/js/wagtailmath.js")]
44+
45+
46+
register(MathJaxAdapter(), MathJaxWidget)
3947

4048

4149
class MathjaxBlock(FieldBlock):

contrib/wagtailmath/static/wagtailmath/js/wagtailmath.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,61 @@ class Preview {
1919
}, 150);
2020
}
2121
}
22+
23+
/** @typedef {{idForLabel: string, getValue(): string, getState(): unknown, setState(state: unknown): void, focus(soft: bool): void}} BoundWidget */
24+
25+
class MathJaxWidget {
26+
/**
27+
* @param {string} idForLabel
28+
* @param {string} initialValue
29+
*/
30+
constructor(idForLabel, initialValue) {
31+
this.idForLabel = idForLabel;
32+
this.initialValue = initialValue;
33+
}
34+
35+
/**
36+
* @param {HTMLElement} container
37+
* @param {string} name
38+
* @param {string} id
39+
* @param {unknown} initialState
40+
* @returns {BoundWidget}
41+
*/
42+
render(container, name, id, initialState) {
43+
const inputEl = document.createElement("input");
44+
const displayEl = document.createElement("div");
45+
const update = () => displayEl.innerHTML = MathJax.tex2svg(inputEl.value).outerHTML;
46+
47+
if(this.initialValue)
48+
inputEl.value = this.initialValue;
49+
inputEl.id = this.idForLabel;
50+
inputEl.name = name;
51+
displayEl.attributes.cols = "50";
52+
displayEl.attributes.rows = "10";
53+
displayEl.classList.add("richtext");
54+
displayEl.id = "mathjax-preview-"+id;
55+
56+
update();
57+
58+
inputEl.addEventListener("input", update);
59+
60+
container.append(inputEl, displayEl);
61+
62+
return {
63+
idForLabel: this.idForLabel,
64+
getValue() {
65+
return inputEl.value;
66+
},
67+
getState() {
68+
return initialState;
69+
},
70+
setState() {
71+
},
72+
focus() {
73+
inputEl.focus();
74+
}
75+
}
76+
}
77+
}
78+
79+
window.telepath.register("contrib.wagtailmath.blocks.MathJaxWidget", MathJaxWidget);

0 commit comments

Comments
 (0)