Alternatives to: document.execCommand

by Imri Paloja

HTML

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>
<div class="container">

    <h1>
        WORK IN PROGRESS
    </h1>

    <button onclick="Bold()">B</button>
<!--    <button onclick="Italic()">I</button>-->
<!--    <button onclick="Underline()">U</button>-->
<!--    <button onclick="myFunction()">myFunction</button>-->
<!--    <button onclick="embolden()">embolden</button>-->
<!--    <a href="#" class="embolden">Bold</a>-->

    <p>
        Select text, and click one of the buttons above!
    </p>

    <div class="editor" id="editor" contenteditable="true">
        <p>
            Lorem ipsum <strong>dolor</strong> sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </p>
    </div>

    <ul>
        <li><a href="https://medium.com/swlh/reimplementing-document-execcommand-6ffc33a80f02" rel="noreferrer"><code>reimplementing-document-execcommand</code></a></li>

        <li><a href="https://dev.to/_moehab/documentexeccommand-alternative-5a55" rel="noreferrer"><code>documentexeccommand-alternative</code></a></li>

    </ul>

    <h2>
        Node.insertBefore()
    </h2>

    <div id="parentElement">
        <span id="childElement">foo...

CSS

html,
body {
  color: #454545;
}

.container {
  margin-top: 5%;
}

.editor {
  background: #EEEEEE;
  border: 1px solid #CCCCCC;
  margin: 25px auto;
  padding: 20px 30px;
}

.highlight {
  animation: highlight linear 1s;
}

@keyframes highlight {
  from {
    outline: 1px solid #f00f;
  }

  to {
    outline: 1px solid #f000;
  }
}

JavaScript

function stripHtml(html) {
    let tmp = document.createElement("DIV");
    tmp.innerHTML = html;
    return tmp.textContent || tmp.innerText || "";
}

function getSelectedHtml() {

    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}

function isset(variable) {
    return typeof variable !== 'undefined';
}

function Bold() {

    // Get the content(s) of everything selected
    const userSelection = window.getSelection();

    // Loop through every selected string
    for (var i = 0; i < userSelection.rangeCount; i++) {

        // Get the string of the selected text
        var selectedTextRange = userSelection.getRangeAt(i);
        // text only output from selected string.

        // Selected text as an object
        const parentElement = selectedTextRange.commonAncestorContainer;
        // object: parentElement: [object Text]
        // object: parentElement: [object HTMLParagraphElement]

        // Get the HTML content.
        var HTMLContent = getSelectedHtml();
        // <strong>adipisicing</strong>
        // If the selected text has html, display its content,
        // If not display the text only.

        // Check if we have a html tag
        // if not it is undefined.
        if(isset(parentElement.tagName)) {
            // We have a HTML tag name.

            var tagName = parentElement.tagName;
            // output: P

            var innerHTML = parentElement.innerHTML;
            //...