Thank you! God helped me to go with option 1. And it works :partying_face:

I use a hmtl element (ID = htmlElement) that displays the html text saved to the database from the rich text editor.

This is the java code that Chat GPT made for me (after a lot of back and forth) to copy and process it so it can be pasted in any text editor:

function bbcodeToHtml(text) {
    text = text.replace(/\[li\s+[^\]]*\](.*?)\[\/li\]/g, "<li>$1</li>");

    text = text.replace(/\[b\](.*?)\[\/b\]/g, "<strong>$1</strong>");
    text = text.replace(/\[i\](.*?)\[\/i\]/g, "<em>$1</em>");
    text = text.replace(/\[u\](.*?)\[\/u\]/g, "<u>$1</u>");
    text = text.replace(/\[s\](.*?)\[\/s\]/g, "<s>$1</s>");
    text = text.replace(/\[url=(.*?)\](.*?)\[\/url\]/g, "<a href='$1' target='_blank'>$2</a>");

    // Überschriften ohne zusätzliche Leerzeile
    text = text.replace(/\[h1\](.*?)\[\/h1\]/g, "<h1>$1</h1>");
    text = text.replace(/\[h2\](.*?)\[\/h2\]/g, "<h2>$1</h2>");
    text = text.replace(/\[h3\](.*?)\[\/h3\]/g, "<h3>$1</h3>");
    text = text.replace(/\[h4\](.*?)\[\/h4\]/g, "<h4>$1</h4>");
    text = text.replace(/\[h5\](.*?)\[\/h5\]/g, "<h5>$1</h5>");
    text = text.replace(/\[h6\](.*?)\[\/h6\]/g, "<h6>$1</h6>");

    text = text.replace(/\[ul\](.*?)\[\/ul\]/gs, function(match, content) {
        let listContent = content.replace(/\[\*\](.*?)\n/g, "<li>$1</li>");
        return "<ul>" + listContent + "</ul>";
    });

    // Zentrierten Text und rechtsausgerichteten Text konvertieren
    text = text.replace(/\[center\](.*?)\[\/center\]/g, "<div style='text-align: center;'>$1</div>");
    text = text.replace(/\[right\](.*?)\[\/right\]/g, "<div style='text-align: right;'>$1</div>");
    
    text = text.replace(/\n/g, "<br>");

    return text;
}

function copyRichText() {
    var bbcodeText = document.getElementById("htmlElement").innerText;  

    bbcodeText = bbcodeText.replace(/\[ml\]/g, "").replace(/\[\/ml\]/g, "");

    var htmlText = bbcodeToHtml(bbcodeText);

    htmlText = `<div style="font-family: Arial, sans-serif; font-size: 14px; white-space: pre-wrap;">${htmlText}</div>`;

    navigator.clipboard.write([
        new ClipboardItem({
            "text/html": new Blob([htmlText], { type: "text/html" }),
            "text/plain": new Blob([bbcodeText], { type: "text/plain" }) 
        })
    ]).catch(err => {
        console.error("Fehler beim Kopieren:", err);
    });
}

copyRichText();
1 Like