aboutsummaryrefslogtreecommitdiff
path: root/app/public/static/markdowntextarea.js
blob: c2ba34582b831b49299b6b23c0c4be60d662543f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$("textarea.markdown").each(function() {
	async function render(plainText, preview) {
		const response = await fetch(new Request("/api/markdown/", {
			method: "POST",
			credentials: "same-origin",
			body: plainText,
			headers: {
				"Accept": "text/html; charset=UTF-8",
			},
		}));

		preview.innerHTML = await response.text();
	}

	let timeout_id = null;

	new EasyMDE({
		element: this,
		hideIcons: ["image"],
		forceSync: true,
		previewRender: (plainText, preview) => {
			if (timeout_id) {
				clearTimeout(timeout_id);
			}

			timeout_id = setTimeout(() => {
				render(plainText, preview);
				timeout_id = null;
			}, 500);

			return preview.innerHTML;
		}
	});
})