Sith/core/static/webpack/easymde-index.js

187 lines
5.1 KiB
JavaScript
Raw Normal View History

2024-10-08 15:14:22 +00:00
// biome-ignore lint/correctness/noUndeclaredDependencies: shipped by easymde
2024-10-07 23:33:21 +00:00
import "codemirror/lib/codemirror.css";
import "easymde/src/css/easymde.css";
2024-10-08 15:14:22 +00:00
import easyMde from "easymde";
// This scripts dependens on Alpine but it should be loaded on every page
/**
* Create a new easymde based textarea
* @param {HTMLTextAreaElement} textarea to use
* @param {string} link to the markdown api
**/
window.easymdeFactory = (textarea, markdownApiUrl) => {
2024-10-08 15:14:22 +00:00
const easymde = new easyMde({
2024-10-08 11:54:44 +00:00
element: textarea,
spellChecker: false,
autoDownloadFontAwesome: false,
previewRender: Alpine.debounce(async (plainText, preview) => {
2024-10-08 15:14:22 +00:00
const res = await fetch(markdownApiUrl, {
2024-10-08 11:54:44 +00:00
method: "POST",
body: JSON.stringify({ text: plainText }),
});
preview.innerHTML = await res.text();
return null;
}, 300),
forceSync: true, // Avoid validation error on generic create view
toolbar: [
{
name: "heading-smaller",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleHeadingSmaller,
2024-10-08 11:54:44 +00:00
className: "fa fa-header",
title: gettext("Heading"),
},
{
name: "italic",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleItalic,
2024-10-08 11:54:44 +00:00
className: "fa fa-italic",
title: gettext("Italic"),
},
{
name: "bold",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleBold,
2024-10-08 11:54:44 +00:00
className: "fa fa-bold",
title: gettext("Bold"),
},
{
name: "strikethrough",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleStrikethrough,
2024-10-08 11:54:44 +00:00
className: "fa fa-strikethrough",
title: gettext("Strikethrough"),
},
{
name: "underline",
action: function customFunction(editor) {
const cm = editor.codemirror;
cm.replaceSelection(`__${cm.getSelection()}__`);
},
className: "fa fa-underline",
title: gettext("Underline"),
},
{
name: "superscript",
action: function customFunction(editor) {
const cm = editor.codemirror;
cm.replaceSelection(`^${cm.getSelection()}^`);
},
className: "fa fa-superscript",
title: gettext("Superscript"),
},
{
name: "subscript",
action: function customFunction(editor) {
const cm = editor.codemirror;
cm.replaceSelection(`~${cm.getSelection()}~`);
},
className: "fa fa-subscript",
title: gettext("Subscript"),
},
{
name: "code",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleCodeBlock,
2024-10-08 11:54:44 +00:00
className: "fa fa-code",
title: gettext("Code"),
},
"|",
{
name: "quote",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleBlockquote,
2024-10-08 11:54:44 +00:00
className: "fa fa-quote-left",
title: gettext("Quote"),
},
{
name: "unordered-list",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleUnorderedList,
2024-10-08 11:54:44 +00:00
className: "fa fa-list-ul",
title: gettext("Unordered list"),
},
{
name: "ordered-list",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleOrderedList,
2024-10-08 11:54:44 +00:00
className: "fa fa-list-ol",
title: gettext("Ordered list"),
},
"|",
{
name: "link",
2024-10-08 15:14:22 +00:00
action: easyMde.drawLink,
2024-10-08 11:54:44 +00:00
className: "fa fa-link",
title: gettext("Insert link"),
},
{
name: "image",
2024-10-08 15:14:22 +00:00
action: easyMde.drawImage,
2024-10-08 11:54:44 +00:00
className: "fa-regular fa-image",
title: gettext("Insert image"),
},
{
name: "table",
2024-10-08 15:14:22 +00:00
action: easyMde.drawTable,
2024-10-08 11:54:44 +00:00
className: "fa fa-table",
title: gettext("Insert table"),
},
"|",
{
name: "clean-block",
2024-10-08 15:14:22 +00:00
action: easyMde.cleanBlock,
2024-10-08 11:54:44 +00:00
className: "fa fa-eraser fa-clean-block",
title: gettext("Clean block"),
},
"|",
{
name: "preview",
2024-10-08 15:14:22 +00:00
action: easyMde.togglePreview,
2024-10-08 11:54:44 +00:00
className: "fa fa-eye no-disable",
title: gettext("Toggle preview"),
},
{
name: "side-by-side",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleSideBySide,
2024-10-08 11:54:44 +00:00
className: "fa fa-columns no-disable no-mobile",
title: gettext("Toggle side by side"),
},
{
name: "fullscreen",
2024-10-08 15:14:22 +00:00
action: easyMde.toggleFullScreen,
2024-10-08 11:54:44 +00:00
className: "fa fa-expand no-mobile",
title: gettext("Toggle fullscreen"),
},
"|",
{
name: "guide",
action: "/page/Aide_sur_la_syntaxe",
className: "fa fa-question-circle",
title: gettext("Markdown guide"),
},
],
});
2024-10-08 11:54:44 +00:00
const submits = textarea.closest("form").querySelectorAll('input[type="submit"]');
const parentDiv = textarea.parentElement;
let submitPressed = false;
2024-10-08 15:14:22 +00:00
function checkMarkdownInput() {
2024-10-08 11:54:44 +00:00
// an attribute is null if it does not exist, else a string
const required = textarea.getAttribute("required") != null;
const length = textarea.value.trim().length;
2024-10-08 11:54:44 +00:00
if (required && length === 0) {
parentDiv.style.boxShadow = "red 0px 0px 1.5px 1px";
} else {
parentDiv.style.boxShadow = "";
}
}
2024-10-08 11:54:44 +00:00
function onSubmitClick(e) {
if (!submitPressed) {
easymde.codemirror.on("change", checkMarkdownInput);
}
submitPressed = true;
checkMarkdownInput(e);
}
2024-10-08 11:54:44 +00:00
for (const submit of submits) {
submit.addEventListener("click", onSubmitClick);
}
};