2018-12-14 15:24:11 +00:00
|
|
|
<div>
|
2019-05-06 18:42:35 +00:00
|
|
|
{# Depends on this package https://github.com/lonaru/easy-markdown-editor #}
|
2018-12-18 13:13:15 +00:00
|
|
|
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
|
2018-12-14 15:24:11 +00:00
|
|
|
|
2019-05-06 18:42:35 +00:00
|
|
|
{# The easymde script can be included twice, it's safe in the code #}
|
2018-12-14 15:24:11 +00:00
|
|
|
<script src="{{ statics.js }}"> </script>
|
|
|
|
<script type="text/javascript">
|
2019-10-16 10:18:23 +00:00
|
|
|
$(function() {
|
|
|
|
const css = "{{ statics.css }}";
|
|
|
|
let lastAPICall;
|
2018-12-14 15:24:11 +00:00
|
|
|
|
2019-10-16 10:18:23 +00:00
|
|
|
// Only import the css once
|
|
|
|
if (!document.head.innerHTML.includes(css)) {
|
|
|
|
document.head.innerHTML += '<link rel="stylesheet" href="' + css + '">';
|
|
|
|
}
|
2018-12-14 15:24:11 +00:00
|
|
|
|
2019-10-16 10:18:23 +00:00
|
|
|
// Custom markdown parser
|
|
|
|
function customMarkdownParser(plainText, cb) {
|
|
|
|
$.ajax({
|
|
|
|
url: "{{ markdown_api_url }}",
|
|
|
|
method: "POST",
|
|
|
|
data: { text: plainText, csrfmiddlewaretoken: getCSRFToken() },
|
|
|
|
}).done(cb);
|
|
|
|
}
|
2018-12-14 15:24:11 +00:00
|
|
|
|
2019-10-16 10:18:23 +00:00
|
|
|
// Pretty markdown input
|
|
|
|
const easymde = new EasyMDE({
|
|
|
|
element: document.getElementById("{{ widget.attrs.id }}"),
|
|
|
|
spellChecker: false,
|
|
|
|
autoDownloadFontAwesome: false,
|
|
|
|
previewRender: function(plainText, preview) { // Async method
|
|
|
|
clearTimeout(lastAPICall);
|
|
|
|
lastAPICall = setTimeout(() => {
|
|
|
|
customMarkdownParser(plainText, (msg) => preview.innerHTML = msg);
|
|
|
|
}, 300);
|
|
|
|
return preview.innerHTML;
|
|
|
|
},
|
|
|
|
forceSync: true, // Avoid validation error on generic create view
|
|
|
|
toolbar: [
|
|
|
|
{
|
|
|
|
name: "heading-smaller",
|
|
|
|
action: EasyMDE.toggleHeadingSmaller,
|
|
|
|
className: "fa fa-header",
|
|
|
|
title: "{{ translations.heading_smaller }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "italic",
|
|
|
|
action: EasyMDE.toggleItalic,
|
|
|
|
className: "fa fa-italic",
|
|
|
|
title: "{{ translations.italic }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bold",
|
|
|
|
action: EasyMDE.toggleBold,
|
|
|
|
className: "fa fa-bold",
|
|
|
|
title: "{{ translations.bold }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "strikethrough",
|
|
|
|
action: EasyMDE.toggleStrikethrough,
|
|
|
|
className: "fa fa-strikethrough",
|
|
|
|
title: "{{ translations.strikethrough }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "underline",
|
|
|
|
action: function customFunction(editor){
|
|
|
|
let cm = editor.codemirror;
|
|
|
|
cm.replaceSelection('__' + cm.getSelection() + '__');
|
|
|
|
},
|
|
|
|
className: "fa fa-underline",
|
|
|
|
title: "{{ translations.underline }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "superscript",
|
|
|
|
action: function customFunction(editor){
|
|
|
|
let cm = editor.codemirror;
|
|
|
|
cm.replaceSelection('<sup>' + cm.getSelection() + '</sup>');
|
|
|
|
},
|
|
|
|
className: "fa fa-superscript",
|
|
|
|
title: "{{ translations.superscript }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "subscript",
|
|
|
|
action: function customFunction(editor){
|
|
|
|
let cm = editor.codemirror;
|
|
|
|
cm.replaceSelection('<sub>' + cm.getSelection() + '</sub>');
|
|
|
|
},
|
|
|
|
className: "fa fa-subscript",
|
|
|
|
title: "{{ translations.subscript }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "code",
|
|
|
|
action: EasyMDE.toggleCodeBlock,
|
|
|
|
className: "fa fa-code",
|
|
|
|
title: "{{ translations.code }}"
|
|
|
|
},
|
|
|
|
"|",
|
|
|
|
{
|
|
|
|
name: "quote",
|
|
|
|
action: EasyMDE.toggleBlockquote,
|
|
|
|
className: "fa fa-quote-left",
|
|
|
|
title: "{{ translations.quote }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unordered-list",
|
|
|
|
action: EasyMDE.toggleUnorderedList,
|
|
|
|
className: "fa fa-list-ul",
|
|
|
|
title: "{{ translations.unordered_list }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ordered-list",
|
|
|
|
action: EasyMDE.toggleOrderedList,
|
|
|
|
className: "fa fa-list-ol",
|
|
|
|
title: "{{ translations.ordered_list }}"
|
|
|
|
},
|
|
|
|
"|",
|
|
|
|
{
|
|
|
|
name: "link",
|
|
|
|
action: EasyMDE.drawLink,
|
|
|
|
className: "fa fa-link",
|
|
|
|
title: "{{ translations.link }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "image",
|
|
|
|
action: EasyMDE.drawImage,
|
|
|
|
className: "fa fa-picture-o",
|
|
|
|
title: "{{ translations.image }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "table",
|
|
|
|
action: EasyMDE.drawTable,
|
|
|
|
className: "fa fa-table",
|
|
|
|
title: "{{ translations.table }}"
|
|
|
|
},
|
|
|
|
"|",
|
|
|
|
{
|
|
|
|
name: "clean-block",
|
|
|
|
action: EasyMDE.cleanBlock,
|
|
|
|
className: "fa fa-eraser fa-clean-block",
|
|
|
|
title: "{{ translations.clean_block }}"
|
|
|
|
},
|
|
|
|
"|",
|
|
|
|
{
|
|
|
|
name: "preview",
|
|
|
|
action: EasyMDE.togglePreview,
|
|
|
|
className: "fa fa-eye no-disable",
|
|
|
|
title: "{{ translations.preview }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "side-by-side",
|
|
|
|
action: EasyMDE.toggleSideBySide,
|
|
|
|
className: "fa fa-columns no-disable no-mobile",
|
|
|
|
title: "{{ translations.side_by_side }}"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "fullscreen",
|
|
|
|
action: EasyMDE.toggleFullScreen,
|
|
|
|
className: "fa fa-arrows-alt no-disable no-mobile",
|
|
|
|
title: "{{ translations.fullscreen }}"
|
|
|
|
},
|
|
|
|
"|",
|
|
|
|
{
|
|
|
|
name: "guide",
|
|
|
|
action: "/page/Aide_sur_la_syntaxe",
|
|
|
|
className: "fa fa-question-circle",
|
|
|
|
title: "{{ translations.guide }}"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
});
|
2019-10-15 07:54:10 +00:00
|
|
|
|
2019-10-16 10:18:23 +00:00
|
|
|
const textarea = document.getElementById('{{ widget.attrs.id }}');
|
|
|
|
const submits = textarea
|
2019-10-15 07:54:10 +00:00
|
|
|
.closest('form')
|
2019-10-16 10:18:23 +00:00
|
|
|
.querySelectorAll('input[type="submit"]');
|
|
|
|
const parentDiv = textarea.parentElement;
|
|
|
|
let submitPressed = false;
|
2019-10-15 07:54:10 +00:00
|
|
|
|
2019-10-15 08:41:10 +00:00
|
|
|
function checkMarkdownInput(e) {
|
2019-10-15 07:54:10 +00:00
|
|
|
// an attribute is null if it does not exist, else a string
|
2019-10-16 10:18:23 +00:00
|
|
|
const required = textarea.getAttribute('required') != null;
|
|
|
|
const length = textarea.value.trim().length;
|
2019-10-15 07:54:10 +00:00
|
|
|
|
|
|
|
if (required && length == 0) {
|
2019-10-16 10:18:23 +00:00
|
|
|
parentDiv.style.boxShadow = 'red 0px 0px 1.5px 1px';
|
2019-10-15 07:54:10 +00:00
|
|
|
} else {
|
2019-10-16 10:18:23 +00:00
|
|
|
parentDiv.style.boxShadow = '';
|
2019-10-15 07:54:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 10:18:23 +00:00
|
|
|
function onSubmitClick(e) {
|
2019-10-15 08:23:15 +00:00
|
|
|
if (!submitPressed) {
|
2019-10-16 10:18:23 +00:00
|
|
|
easymde.codemirror.on('change', checkMarkdownInput);
|
2019-10-15 08:23:15 +00:00
|
|
|
}
|
2019-10-16 10:18:23 +00:00
|
|
|
submitPressed = true;
|
|
|
|
checkMarkdownInput(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
submits.forEach((submit) => {
|
|
|
|
submit.addEventListener('click', onSubmitClick);
|
2019-10-15 07:54:10 +00:00
|
|
|
})
|
|
|
|
})
|
2018-12-14 15:24:11 +00:00
|
|
|
</script>
|
2019-10-15 07:53:44 +00:00
|
|
|
</div>
|