2019-12-03 20:32:01 +01:00
|
|
|
{% macro text_editor(field, label=True) %}
|
2020-09-17 22:36:52 +02:00
|
|
|
{{ field.label if label }}
|
|
|
|
{{ field() }}
|
2020-09-18 11:53:50 +02:00
|
|
|
<script>
|
|
|
|
window.addEventListener("load", function(){
|
2020-09-23 23:58:25 +02:00
|
|
|
var simplemde = new SimpleMDE({
|
|
|
|
element: document.getElementById("{{field.name}}"),
|
|
|
|
autofocus: true,
|
|
|
|
autosave: {
|
|
|
|
enabled: true,
|
|
|
|
uniqueId: "{{ request.path }}+{{ field.name }}",
|
|
|
|
delay: 1000,
|
|
|
|
},
|
|
|
|
hideIcons: ["guide", "side-by-side", "fullscreen", "heading"],
|
|
|
|
showIcons: ["code", "table", "horizontal-rule", "ordered-list",
|
|
|
|
"unordered-list", "heading-1", "heading-2", "heading-3",
|
|
|
|
"strikethrough"
|
|
|
|
],
|
|
|
|
insertTexts: {
|
|
|
|
image: ["![](https://", ")"],
|
|
|
|
link: ["[", "](https://)"],
|
|
|
|
},
|
|
|
|
previewRender: function(plainText, preview) { // Async method
|
|
|
|
data = {text: plainText};
|
|
|
|
fetch('{{ url_for("api_markdown") }}', {
|
|
|
|
method: "POST",
|
2020-10-07 21:47:21 +02:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-CSRFToken': '{{ csrf_token() }}'
|
|
|
|
},
|
2020-09-23 23:58:25 +02:00
|
|
|
body: JSON.stringify(data)
|
|
|
|
})
|
|
|
|
.then(response => response.text())
|
|
|
|
.then(data => preview.innerHTML = data);
|
|
|
|
return "Chargement…";
|
|
|
|
},
|
|
|
|
spellChecker: false, // Uses CDN jsdelivr.net
|
|
|
|
forceSync: true,
|
|
|
|
tabSize: 4,
|
|
|
|
shortcuts: {
|
|
|
|
toggleFullScreen: null,
|
|
|
|
},
|
2020-09-24 22:43:39 +02:00
|
|
|
status: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ctrl+Enter submits form
|
|
|
|
ta = document.querySelector("div.CodeMirror");
|
|
|
|
ta.addEventListener('keydown', function(e) {
|
|
|
|
var keyCode = e.keyCode || e.which;
|
|
|
|
if (e.ctrlKey && keyCode == 13) {
|
|
|
|
var e = e.target;
|
|
|
|
while(! (e instanceof HTMLFormElement)) {
|
|
|
|
e = e.parentNode;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
e.submit();
|
|
|
|
} catch(exception) {
|
|
|
|
e.submit.click();
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 23:58:25 +02:00
|
|
|
});
|
2020-09-18 11:53:50 +02:00
|
|
|
});
|
2020-09-24 22:43:39 +02:00
|
|
|
|
2020-09-18 11:53:50 +02:00
|
|
|
</script>
|
2020-09-17 22:36:52 +02:00
|
|
|
{% for error in field.errors %}
|
|
|
|
<span class="msgerror">{{ error }}</span>
|
|
|
|
{% endfor %}
|
2019-09-08 14:34:51 +02:00
|
|
|
{% endmacro %}
|