Editor Code

by ed4becky

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script>
<div ng-app='myApp'>
    <div ng-controller="ctrl">
    <div>
        <div class='editor evedit' contenteditable='true' ng-model="data.source"  ng-bind-html-unsafe='data.source'></div>
    </div>
    <div id='editTools' class="evedit" style='display:none' tabindex="-1">
        <button class='toolButton' type='button' tabindex="-1" ng-click='bold();'> <span style='font-weight:bold;'>B</span>

        </button>
        <button class='toolButton' type='button' tabindex="-1" ng-click='italic();'> <span style='font-style:italic;'>I</span>

        </button>
        <button class='toolButton' type='button' tabindex="-1" ng-click='underline();'> <span style='text-decoration:underline;'>U</span>

        </button>
        <button class='toolButton' type='button' tabindex="-1"  ng-click='strike();'> <span style='text-decoration:line-through;'>S</span>

        </button>
    </div>
</div>
</div>

CSS

body{
    font-size: 14px;
    font-family: Tahoma;
    background-color:lightgrey;    
    overflow:auto;
    -webkit-user-select: text;
}
div {
    margin: 1em;
}
.editor {
    text-align:left;
    resize:vertical;
    border-radius: 10px;
    overflow:auto;
    border:2px green solid;
    padding:6px;
    background-color:whitesmoke;
    margin-top:5em;
    height:3em;
}
#editTools {
    width: auto;
    height: auto;
    background-color:lightgrey;
    background-image: none;
    padding:4px;
    border-radius:4px;
    border: solid black 1px;
    position:absolute;
    z-index:98;
}

JavaScript

var myApp = angular.module('myApp', []);
myApp.controller('ctrl', ['$scope', '$timeout', Ctrl]);

function Ctrl($scope, $timeout) {
    $scope.data = {};
    $scope.data.source = 'Hello cruel editable world!';

    $scope.bold = function () { 
        if (window.getSelection().toString().length > 0) {
            document.execCommand("bold", false, null);
            window.getSelection().empty();
        }
    };

    $scope.italic = function () {
        if (window.getSelection().toString().length > 0) {
            document.execCommand("italic");
        }
    };

    $scope.underline = function () {
        if (window.getSelection().toString().length > 0) {
            document.execCommand("underline");
        }
    };

    $scope.strike = function () {
        if (window.getSelection().toString().length > 0) {
            document.execCommand("strikethrough");
        }
    };
    $scope.save = function () {
        alert("Calling save();");
    }
}

myApp.directive('contenteditable',function ($rootScope, $compile, $timeout) {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {

            var needTools = (attrs['class'].indexOf('evedit') >= 0);

            if (needTools) {

                elm.on('focus', function (e) {
                    var t = $(e.target);
                    $timeout(function () {
                        var o1 = $(elm).offset();
                        var tools = $('#editTools');
                        tools.css({
                            left: o1.left + 8,
                            top: o1.top - 50
                        });
                        if (attrs.evMode === '2') {
                            $('#citeButton').show();
                        } else {
                            $('#citeButton').hide();
                        }
                        tools.show();
                    }, 200);

                });
                var document = window.document;

           ...