2022-02-09 20:11:31 +01:00
|
|
|
// @author rubenwardy
|
|
|
|
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
|
|
|
|
|
2023-08-26 13:34:55 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
document.querySelectorAll("textarea.markdown").forEach((element) => {
|
2020-01-23 00:10:02 +01:00
|
|
|
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;
|
2023-08-26 13:34:55 +02:00
|
|
|
element.easy_mde = new EasyMDE({
|
|
|
|
element: element,
|
2020-01-23 00:10:02 +01:00
|
|
|
hideIcons: ["image"],
|
2021-02-05 15:19:29 +01:00
|
|
|
showIcons: ["code", "table"],
|
2020-01-23 00:10:02 +01:00
|
|
|
forceSync: true,
|
2022-01-20 02:18:10 +01:00
|
|
|
toolbar: [
|
|
|
|
"bold",
|
|
|
|
"italic",
|
|
|
|
"heading",
|
|
|
|
"|",
|
|
|
|
"code",
|
|
|
|
"quote",
|
|
|
|
"unordered-list",
|
|
|
|
"ordered-list",
|
|
|
|
"|",
|
|
|
|
"link",
|
|
|
|
"table",
|
|
|
|
"|",
|
|
|
|
"preview",
|
|
|
|
"side-by-side",
|
|
|
|
"fullscreen",
|
|
|
|
"|",
|
|
|
|
"guide",
|
|
|
|
],
|
2020-01-23 00:10:02 +01:00
|
|
|
previewRender: (plainText, preview) => {
|
|
|
|
if (timeout_id) {
|
|
|
|
clearTimeout(timeout_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout_id = setTimeout(() => {
|
2023-08-26 13:34:55 +02:00
|
|
|
render(plainText, preview).catch(console.error);
|
2020-01-23 00:10:02 +01:00
|
|
|
timeout_id = null;
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
return preview.innerHTML;
|
|
|
|
}
|
|
|
|
});
|
2023-08-26 14:38:34 +02:00
|
|
|
|
|
|
|
// Ctrl+enter to submit
|
|
|
|
if (element.getAttribute("data-enter-submit")) {
|
|
|
|
element.easy_mde.codemirror.on("keyup", (mirror, e) => {
|
|
|
|
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
|
|
|
element.form?.submit();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-01-23 00:10:02 +01:00
|
|
|
})
|