2018-12-14 15:24:11 +00:00
|
|
|
<div>
|
|
|
|
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
|
|
|
|
{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
|
|
|
|
|
|
|
|
{# The simplemde script can be included twice, it's safe in the code #}
|
|
|
|
<script src="{{ statics.js }}"> </script>
|
|
|
|
<script type="text/javascript">
|
|
|
|
var css = "{{ statics.css }}";
|
|
|
|
|
|
|
|
// Only import the css once
|
|
|
|
if (!document.head.innerHTML.includes(css)){
|
|
|
|
document.head.innerHTML += '<link rel="stylesheet" href="' + css + '">';
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can't get the csrf token from the template in a widget
|
|
|
|
// We get it from a cookie as a workaround, see this link
|
|
|
|
// https://docs.djangoproject.com/en/2.0/ref/csrf/#ajax
|
2018-12-14 16:53:25 +00:00
|
|
|
function getCookie(cname) {
|
|
|
|
var name = cname + "=";
|
|
|
|
var decodedCookie = decodeURIComponent(document.cookie);
|
|
|
|
var ca = decodedCookie.split(';');
|
|
|
|
for(var i = 0; i <ca.length; i++) {
|
|
|
|
var c = ca[i];
|
|
|
|
while (c.charAt(0) == ' ') {
|
|
|
|
c = c.substring(1);
|
|
|
|
}
|
|
|
|
if (c.indexOf(name) == 0) {
|
|
|
|
return c.substring(name.length, c.length);
|
2018-12-14 15:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-14 16:53:25 +00:00
|
|
|
return "";
|
2018-12-14 15:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Custom markdown parser
|
|
|
|
function customMarkdownParser(plainText, preview) {
|
2018-12-18 12:54:53 +00:00
|
|
|
$.ajax({
|
|
|
|
url: "{{ markdown_api_url }}",
|
|
|
|
method: "POST",
|
|
|
|
data: { text: plainText, csrfmiddlewaretoken: getCookie('csrftoken') },
|
|
|
|
}).done(function (msg) {
|
|
|
|
preview.innerHTML = msg;
|
|
|
|
});
|
2018-12-14 15:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pretty markdown input
|
|
|
|
var simplemde = new SimpleMDE({
|
|
|
|
element: document.getElementById("{{ widget.attrs.id }}"),
|
|
|
|
spellChecker: false,
|
|
|
|
previewRender: function(plainText, preview){ // Async method
|
|
|
|
customMarkdownParser(plainText, preview);
|
2018-12-14 17:02:01 +00:00
|
|
|
return preview.innerHTML;
|
2018-12-14 15:24:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</div>
|