WS: Command -> Result

by NesterOne

HTML

<script src="http://underscorejs.org/underscore.js"></script>
<script src="https://rawgit.com/sporritt/jsPlumb/1.5.5/dist/js/jquery.jsPlumb-1.5.5.js"></script>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>

CSS

.workspace-layer-command {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1030;
    background-color: #000;
    opacity: .5;
    filter: alpha(opacity=50);
}
.workspace-marker {
    position:absolute;
    z-index: 1031;
    background-color: white;
    border: solid 1px green;
}
.workspace-marker-inplace {
    position: relative;
    z-index: 1031;
    background-color: white;
    /*border: solid 2px green; */
}
.workspace-target {
    position:absolute;
    z-index: 1031;
    background-color: white;
    border: solid 1px green;
}
._jsPlumb_connector {
    z-index:1032;
}
._jsPlumb_endpoint {
    z-index:1033;
}

JavaScript

(function (root, inplaceMarkerTemplate, freeMarkerTemplate, commandInputTemplate) {

    function getSelectedText() {
        var text = "";
        if (window.getSelection) {
            text = window.getSelection().toString();
        } else if (document.selection && document.selection.type != "Control") {
            text = document.selection.createRange().text;
        }
        return text;
    }

    function getSelectionParentElement() {
        var parentEl = null,
            sel;
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.rangeCount) {
                parentEl = sel.getRangeAt(0).commonAncestorContainer;
                if (parentEl.nodeType != 1) {
                    parentEl = parentEl.parentNode;
                }
            }
        } else if ((sel = document.selection) && sel.type != "Control") {
            parentEl = sel.createRange().parentElement();
        }
        return parentEl;
    }

    function getSel() {
        var sel = null;
        if (
        typeof document.selection != "undefined" && document.selection && document.selection.type == "Text") {
            sel = document.selection;
        } else if (
        window.getSelection && window.getSelection().rangeCount > 0) {
            sel = window.getSelection();
        }
        return sel;
    }

    var getSelectionStart = (function () {

        function createRangeFromSel(sel) {
            var rng = null;
            if (sel.createRange) {
                rng = sel.createRange();
            } else if (sel.getRangeAt) {
                rng = sel.getRangeAt(0);
                if (rng.toString() == "") rng = null;
            }
            return rng;
        }

        return function (el) {
            var sel = getSel(),
                rng, r2, i = -1;
            if (sel) {
                rng = createRangeFromSel(sel);
                if (rng) {

                    if (rng.parentElement) {
                       ...