markdown_textarea.jinja 5.61 KB
Newer Older
Sli's avatar
Sli committed
1
<div>
2
	{# Depends on this package https://github.com/lonaru/easy-markdown-editor #}
3
	<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
Sli's avatar
Sli committed
4

5
	{# The easymde script can be included twice, it's safe in the code #}
Sli's avatar
Sli committed
6
7
	<script src="{{ statics.js }}"> </script>
	<script type="text/javascript">
8
9
10
		$(function() {
			const css = "{{ statics.css }}";
			let lastAPICall;
Sli's avatar
Sli committed
11

12
13
14
15
			// Only import the css once
			if (!document.head.innerHTML.includes(css)) {
				document.head.innerHTML += '<link rel="stylesheet" href="' + css + '">';
			}
Sli's avatar
Sli committed
16

17
18
19
20
21
22
23
24
			// Custom markdown parser
			function customMarkdownParser(plainText, cb) {
				$.ajax({
					url: "{{ markdown_api_url }}",
					method: "POST",
					data: { text: plainText, csrfmiddlewaretoken: getCSRFToken() },
				}).done(cb);
			}
Sli's avatar
Sli committed
25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
			// 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 }}"
					},
				]
			});
170

171
172
			const textarea = document.getElementById('{{ widget.attrs.id }}');
			const submits = textarea
173
				.closest('form')
174
175
176
				.querySelectorAll('input[type="submit"]');
			const parentDiv = textarea.parentElement;
			let submitPressed = false;
177

178
			function checkMarkdownInput(e) {
179
				// an attribute is null if it does not exist, else a string
180
181
				const required = textarea.getAttribute('required') != null;
				const length = textarea.value.trim().length;
182
183

				if (required && length == 0) {
184
					parentDiv.style.boxShadow = 'red 0px 0px 1.5px 1px';
185
				} else {
186
					parentDiv.style.boxShadow = '';
187
188
189
				}
			}

190
			function onSubmitClick(e) {
191
				if (!submitPressed) {
192
					easymde.codemirror.on('change', checkMarkdownInput);
193
				}
194
195
196
197
198
199
				submitPressed = true;
				checkMarkdownInput(e);
			}

			submits.forEach((submit) => {
				submit.addEventListener('click', onSubmitClick);
200
201
			})
		})
Sli's avatar
Sli committed
202
	</script>
tleb's avatar
tleb committed
203
</div>