contentdb/app/public/static/js/markdowntextarea.js

69 lines
1.4 KiB
JavaScript
Raw Normal View History

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
"use strict";
document.querySelectorAll("textarea.markdown").forEach((element) => {
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;
element.easy_mde = new EasyMDE({
element: element,
hideIcons: ["image"],
2021-02-05 15:19:29 +01:00
showIcons: ["code", "table"],
forceSync: true,
toolbar: [
"bold",
"italic",
"heading",
"|",
"code",
"quote",
"unordered-list",
"ordered-list",
"|",
"link",
"table",
"|",
"preview",
"side-by-side",
"fullscreen",
"|",
"guide",
],
previewRender: (plainText, preview) => {
if (timeout_id) {
clearTimeout(timeout_id);
}
timeout_id = setTimeout(() => {
render(plainText, preview).catch(console.error);
timeout_id = null;
}, 500);
return preview.innerHTML;
}
});
// 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();
}
});
}
})